From 78a67b6c513c288e84af695c978b8e52538e88e7 Mon Sep 17 00:00:00 2001 From: Atsuihsio <842960157@qq.com> Date: Sat, 7 Dec 2024 00:25:38 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E6=A0=A1=E5=87=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/AnnihilatorEntity.java | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/AnnihilatorEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/AnnihilatorEntity.java index 6f15bd615..b7cfb8617 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/AnnihilatorEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/AnnihilatorEntity.java @@ -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,6 +72,8 @@ 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); } @@ -436,6 +438,40 @@ 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 = SeekTool.seekEntity(passenger, passenger.level(), 512, 5); + + if (lookingEntity != null) { + range = Math.max(passenger.distanceTo(lookingEntity), 5); + } else { + range = Math.max(passenger.position().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); + } + + lookDistance = Mth.lerp(0.1, lookDistance, range); + + double angle = 0; + + double roothorizontalDistance = new Vec3(passenger.getX() - BarrelRootPos.x , passenger.getEyeY() - BarrelRootPos.y, passenger.getZ() -BarrelRootPos.z).horizontalDistance(); + + if (lookDistance != 0) { + angle = Math.atan(Mth.abs((float)(passenger.getEyeY() - BarrelRootPos.y)) / (lookDistance + roothorizontalDistance)) * Mth.RAD_TO_DEG; + } + + this.entityData.set(OFFSET_ANGLE , (float)angle); + float passengerY = entity.getYHeadRot(); if (passengerY > 180.0f) { @@ -444,27 +480,8 @@ 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 = TraceTool.findLookingEntity(entity, 512); - - 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; - float diffY = passengerY - this.getYRot(); - float diffX = (float) (targetXDegree - this.getXRot() - this.entityData.get(OFFSET_ANGLE)); + float diffX = entity.getXRot() - this.entityData.get(OFFSET_ANGLE) - this.getXRot(); if (diffY > 180.0f) { diffY -= 360.0f; } else if (diffY < -180.0f) {