添加MK42读取射击诸元的功能

This commit is contained in:
Atsuihsio 2025-01-18 03:39:17 +08:00
parent 50e174c9dc
commit cda8be8b26

View file

@ -1,6 +1,7 @@
package com.atsuishio.superbwarfare.entity.vehicle; package com.atsuishio.superbwarfare.entity.vehicle;
import com.atsuishio.superbwarfare.ModUtils; 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.ExplosionDestroyConfig;
import com.atsuishio.superbwarfare.config.server.VehicleConfig; import com.atsuishio.superbwarfare.config.server.VehicleConfig;
import com.atsuishio.superbwarfare.entity.projectile.CannonShellEntity; 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.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.ParticleTool; import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.SoundTool; 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.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.Packet;
@ -50,6 +53,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, ICannonEntit
public static final EntityDataAccessor<Integer> COOL_DOWN = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.INT); public static final EntityDataAccessor<Integer> COOL_DOWN = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.INT);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public static final float MAX_HEALTH = VehicleConfig.MK42_HP.get(); public static final float MAX_HEALTH = VehicleConfig.MK42_HP.get();
public static final EntityDataAccessor<Float> PITCH = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> YAW = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT);
public Mk42Entity(PlayMessages.SpawnEntity packet, Level world) { public Mk42Entity(PlayMessages.SpawnEntity packet, Level world) {
this(ModEntities.MK_42.get(), world); this(ModEntities.MK_42.get(), world);
@ -63,6 +68,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, ICannonEntit
protected void defineSynchedData() { protected void defineSynchedData() {
super.defineSynchedData(); super.defineSynchedData();
this.entityData.define(COOL_DOWN, 0); this.entityData.define(COOL_DOWN, 0);
this.entityData.define(PITCH, 0f);
this.entityData.define(YAW, 0f);
} }
@Override @Override
@ -275,17 +282,55 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, ICannonEntit
@Override @Override
public void travel() { public void travel() {
Entity passenger = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0); 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()) {
float diffY = Math.clamp(-90f, 90f, Mth.wrapDegrees(entity.getYHeadRot() - this.getYRot())); if (setTarget(player.getOffhandItem())) {
diffY = Math.clamp(-90f, 90f, Mth.wrapDegrees(entityData.get(YAW) - this.getYRot()));
float diffX = entity.getXRot() - 1.3f - this.getXRot(); 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; diffX = diffX * 0.15f;
this.setYRot(this.getYRot() + Mth.clamp(0.5f * diffY, -1.75f, 1.75f)); 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)); 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) { protected void clampRotation(Entity entity) {
float f = Mth.wrapDegrees(entity.getXRot()); float f = Mth.wrapDegrees(entity.getXRot());