更新校准

This commit is contained in:
Atsuihsio 2024-12-07 02:41:14 +08:00
parent 4af253bb40
commit be612527f4

View file

@ -8,8 +8,8 @@ import com.atsuishio.superbwarfare.item.ContainerBlockItem;
import com.atsuishio.superbwarfare.network.message.ShakeClientMessage;
import com.atsuishio.superbwarfare.tools.CustomExplosion;
import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.SeekTool;
import com.atsuishio.superbwarfare.tools.SoundTool;
import com.atsuishio.superbwarfare.tools.TraceTool;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleTypes;
@ -72,8 +72,6 @@ public class AnnihilatorEntity extends Entity implements GeoEntity, ICannonEntit
protected double serverYRot;
protected double serverXRot;
public static double lookDistance = 0;
public AnnihilatorEntity(PlayMessages.SpawnEntity packet, Level world) {
this(ModEntities.ANNIHILATOR.get(), world);
}
@ -438,6 +436,43 @@ public class AnnihilatorEntity extends Entity implements GeoEntity, ICannonEntit
if (!(passenger instanceof LivingEntity entity)) return;
if (this.entityData.get(ENERGY) <= 0) return;
float yRot = this.getYRot();
if (yRot < 0) {
yRot += 360;
}
yRot = yRot + 90 % 360;
var BarrelRoot = new Vector3d(4.95, 2.25, 0);
BarrelRoot.rotateY(-yRot * Mth.DEG_TO_RAD);
Vec3 BarrelRootPos = new Vec3(this.getX() + BarrelRoot.x, this.getY() + BarrelRoot.y, this.getZ() + BarrelRoot.z);
double range;
Entity lookingEntity = TraceTool.findLookingEntity(entity,512);
if (lookingEntity != null) {
range = Math.max(eyeDistanceTo(entity ,lookingEntity), 5);
} else {
range = Math.max(new Vec3(entity.getX(),entity.getEyeY(), entity.getZ()).distanceTo((Vec3.atLowerCornerOf(passenger.level().clip(
new ClipContext(passenger.getEyePosition(), passenger.getEyePosition().add(passenger.getLookAngle().scale(512)),
ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, passenger)).getBlockPos()))), 5);
}
double l1 = new Vec3(passenger.getX() - BarrelRootPos.x , passenger.getEyeY() - BarrelRootPos.y, passenger.getZ() - BarrelRootPos.z).length();
double l4 = new Vec3(passenger.getX() - BarrelRootPos.x , passenger.getEyeY() - BarrelRootPos.y, passenger.getZ() - BarrelRootPos.z).horizontalDistance();
double l5 = Mth.abs((float) (passenger.getEyeY() - this.getY() + 2.25));
double a1 = -entity.getXRot();
double a2 = (Math.atan(l5 / l4)) * Mth.RAD_TO_DEG;
double a0 = a1 + a2;
double l2 = (l1 * Math.cos(a0));
double l3 = (l1 * Math.sin(a0));
double angle = Math.atan(Mth.abs((float) l3) / (range + l2)) * Mth.RAD_TO_DEG;
this.entityData.set(OFFSET_ANGLE , (float)(angle - (0.3f * (1 - 0.02 * a1))));
float passengerY = entity.getYHeadRot();
if (passengerY > 180.0f) {
@ -446,27 +481,6 @@ public class AnnihilatorEntity extends Entity implements GeoEntity, ICannonEntit
passengerY += 360.0f;
}
// 玩家瞄准坐标
var lookingBlock = Vec3.atLowerCornerOf(entity.level().clip(
new ClipContext(new Vec3(entity.getX(), entity.getEyeY(), entity.getZ()), new Vec3(entity.getX(), entity.getEyeY() + 1, entity.getZ()).add(entity.getLookAngle().scale(512)),
ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, entity)).getBlockPos()
);
var lookingEntity = SeekTool.seekEntity(entity, entity.level(), 520, 5);
var lookingAt = lookingEntity != null ? lookingEntity.position() : lookingBlock;
var barrelRoot = new Vector3d(4.95, 2.25, 0);
barrelRoot.rotateY(-this.getYRot() * Mth.DEG_TO_RAD);
// 中间炮管transform origin世界坐标
var barrelRootPos = new Vec3(this.getX() + barrelRoot.x, this.getY() + barrelRoot.y, this.getZ() + barrelRoot.z);
// 看向的目标相对炮管原点的位置
var diffVec = lookingAt.subtract(barrelRootPos).add(0, 1, 0);
// 修正后的目标垂直方向角度差距
var targetXDegree = Math.atan(diffVec.y / diffVec.horizontalDistance()) * Mth.RAD_TO_DEG;
this.entityData.set(OFFSET_ANGLE, (float)targetXDegree);
float diffY = passengerY - this.getYRot();
float diffX = entity.getXRot() - this.entityData.get(OFFSET_ANGLE) - this.getXRot();
if (diffY > 180.0f) {
@ -482,6 +496,13 @@ public class AnnihilatorEntity extends Entity implements GeoEntity, ICannonEntit
this.setRot(this.getYRot(), this.getXRot());
}
public float eyeDistanceTo(Entity entity ,Entity target) {
float f = (float)(entity.getX() - target.getX());
float f1 = (float)(entity.getEyeY() - target.getY());
float f2 = (float)(entity.getZ() - target.getZ());
return Mth.sqrt(f * f + f1 * f1 + f2 * f2);
}
protected void clampRotation(Entity entity) {
float f = Mth.wrapDegrees(entity.getXRot());
float f1 = Mth.clamp(f, -45.0F, 5f + this.entityData.get(OFFSET_ANGLE));