diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java index 4c77f703c..2bec648db 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java @@ -1,6 +1,7 @@ package com.atsuishio.superbwarfare.entity.vehicle; import com.atsuishio.superbwarfare.ModUtils; +import com.atsuishio.superbwarfare.client.gui.RangeHelper; import com.atsuishio.superbwarfare.config.server.ExplosionDestroyConfig; import com.atsuishio.superbwarfare.config.server.VehicleConfig; import com.atsuishio.superbwarfare.entity.projectile.CannonShellEntity; @@ -11,6 +12,8 @@ import com.atsuishio.superbwarfare.tools.CustomExplosion; import com.atsuishio.superbwarfare.tools.EntityFindUtil; import com.atsuishio.superbwarfare.tools.ParticleTool; import com.atsuishio.superbwarfare.tools.SoundTool; +import net.minecraft.commands.arguments.EntityAnchorArgument; +import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.Packet; @@ -50,6 +53,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, ICannonEntit public static final EntityDataAccessor COOL_DOWN = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.INT); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); public static final float MAX_HEALTH = VehicleConfig.MK42_HP.get(); + public static final EntityDataAccessor PITCH = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT); + public static final EntityDataAccessor YAW = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT); public Mk42Entity(PlayMessages.SpawnEntity packet, Level world) { this(ModEntities.MK_42.get(), world); @@ -63,6 +68,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, ICannonEntit protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(COOL_DOWN, 0); + this.entityData.define(PITCH, 0f); + this.entityData.define(YAW, 0f); } @Override @@ -275,16 +282,54 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, ICannonEntit @Override public void travel() { Entity passenger = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0); + float diffY = 0; + float diffX = 0; - if (!(passenger instanceof LivingEntity entity)) return; + if (passenger instanceof Player player) { + if (player.getOffhandItem().getItem() == ModItems.FIRING_PARAMETERS.get()) { + if (setTarget(player.getOffhandItem())) { + diffY = Math.clamp(-90f, 90f, Mth.wrapDegrees(entityData.get(YAW) - this.getYRot())); + diffX = Mth.wrapDegrees(entityData.get(PITCH) - this.getXRot()); + this.setYRot(this.getYRot() + Mth.clamp(0.5f * diffY, -1.75f, 1.75f)); + this.setXRot(Mth.clamp(this.getXRot() + Mth.clamp(0.95f * diffX, -5, 5), -85, 15)); + } + } else { + diffY = Math.clamp(-90f, 90f, Mth.wrapDegrees(passenger.getYHeadRot() - this.getYRot())); + diffX = passenger.getXRot() - 1.3f - this.getXRot(); + diffX = diffX * 0.15f; + this.setYRot(this.getYRot() + Mth.clamp(0.5f * diffY, -1.75f, 1.75f)); + this.setXRot(Mth.clamp(this.getXRot() + Mth.clamp(diffX, -3f, 3f), -85, 16.3f)); + } + } - float diffY = Math.clamp(-90f, 90f, Mth.wrapDegrees(entity.getYHeadRot() - this.getYRot())); - float diffX = entity.getXRot() - 1.3f - this.getXRot(); - diffX = diffX * 0.15f; + } - this.setYRot(this.getYRot() + Mth.clamp(0.5f * diffY, -1.75f, 1.75f)); - this.setXRot(Mth.clamp(this.getXRot() + Mth.clamp(diffX, -3f, 3f), -85, 16.3f)); + public boolean setTarget(ItemStack stack) { + int targetX = stack.getOrCreateTag().getInt("TargetX"); + int targetY = stack.getOrCreateTag().getInt("TargetY"); + int targetZ = stack.getOrCreateTag().getInt("TargetZ"); + + this.look(EntityAnchorArgument.Anchor.EYES, new Vec3(targetX, targetY, targetZ)); + + double[] angles = new double[2]; + boolean flag = RangeHelper.canReachTarget(15, 0.05, 0.99, + new BlockPos((int) this.getX(), (int) this.getEyeY(), (int) this.getZ()), + new BlockPos(targetX, targetY, targetZ), + angles); + + if (flag) { + this.entityData.set(PITCH, -(float) angles[1]); + } + + return flag; + } + + private void look(EntityAnchorArgument.Anchor pAnchor, Vec3 pTarget) { + Vec3 vec3 = pAnchor.apply(this); + double d0 = (pTarget.x - vec3.x) * 0.2; + double d2 = (pTarget.z - vec3.z) * 0.2; + this.entityData.set(YAW, Mth.wrapDegrees((float) (Mth.atan2(d2, d0) * 57.2957763671875) - 90.0F)); } protected void clampRotation(Entity entity) {