From 22a2d6aebc57f6b3e7bafa2cfa20c0793978361e Mon Sep 17 00:00:00 2001 From: Atsuihsio <842960157@qq.com> Date: Sat, 28 Dec 2024 22:13:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=A8=E8=A1=8C=E8=BD=BD?= =?UTF-8?q?=E5=85=B7=E5=92=8C=E8=BD=AE=E6=A4=85=E5=90=B8=E9=99=84=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/MobileVehicleEntity.java | 34 +++++++-- .../superbwarfare/entity/SpeedboatEntity.java | 8 +- .../superbwarfare/entity/VehicleEntity.java | 10 +++ .../entity/WheelChairEntity.java | 76 +++++++++++++------ 4 files changed, 94 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/MobileVehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/MobileVehicleEntity.java index 15c079e5e..6be7f1db1 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/MobileVehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/MobileVehicleEntity.java @@ -4,10 +4,15 @@ import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity; import com.atsuishio.superbwarfare.init.ModDamageTypes; import com.atsuishio.superbwarfare.init.ModSounds; import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.syncher.EntityDataAccessor; +import net.minecraft.network.syncher.EntityDataSerializers; +import net.minecraft.network.syncher.SynchedEntityData; +import net.minecraft.sounds.SoundEvent; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.MoverType; import net.minecraft.world.entity.item.ItemEntity; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.projectile.Projectile; import net.minecraft.world.level.Level; import net.minecraft.world.level.entity.EntityTypeTest; @@ -15,7 +20,7 @@ import net.minecraft.world.phys.Vec3; import org.joml.Math; public class MobileVehicleEntity extends EnergyVehicleEntity { - public float power; + public static final EntityDataAccessor POWER = SynchedEntityData.defineId(MobileVehicleEntity.class, EntityDataSerializers.FLOAT); public boolean leftInputDown; public boolean rightInputDown; public boolean forwardInputDown; @@ -28,11 +33,22 @@ public class MobileVehicleEntity extends EnergyVehicleEntity { super(pEntityType, pLevel); } + @Override + public void playerTouch(Player pPlayer) { + if (pPlayer.isCrouching() && !this.level().isClientSide) { + double entitySize = pPlayer.getBbWidth() * pPlayer.getBbHeight(); + double thisSize = this.getBbWidth() * this.getBbHeight(); + double f = Math.min(entitySize / thisSize, 2); + double f1 = Math.min(thisSize / entitySize, 4); + this.setDeltaMovement(this.getDeltaMovement().add(new Vec3(pPlayer.position().vectorTo(this.position()).toVector3f()).scale(0.15 * f * pPlayer.getDeltaMovement().length()))); + pPlayer.setDeltaMovement(pPlayer.getDeltaMovement().add(new Vec3(this.position().vectorTo(pPlayer.position()).toVector3f()).scale(0.1 * f1 * pPlayer.getDeltaMovement().length()))); + } + } + @Override public void baseTick() { super.baseTick(); crushEntities(this.getDeltaMovement()); - this.move(MoverType.SELF, this.getDeltaMovement()); this.refreshDimensions(); } @@ -42,8 +58,9 @@ public class MobileVehicleEntity extends EnergyVehicleEntity { * @param velocity 动量 */ public void crushEntities(Vec3 velocity) { + if (velocity.horizontalDistance() < 0.1) return; var frontBox = getBoundingBox().move(velocity.scale(0.5)); - var velAdd = velocity.add(0, 0, 0).scale(1.3); + var velAdd = velocity.add(0, 0, 0).scale(0.9); for (var entity : level().getEntities(EntityTypeTest.forClass(Entity.class), frontBox, entity -> entity != this && entity != getFirstPassenger() && entity.getVehicle() == null)) { double entitySize = entity.getBbWidth() * entity.getBbHeight(); @@ -51,8 +68,8 @@ public class MobileVehicleEntity extends EnergyVehicleEntity { double f = Math.min(entitySize / thisSize, 2); double f1 = Math.min(thisSize / entitySize, 4); - if (entity.isAlive() && entity.getVehicle() == null && !(entity instanceof ItemEntity || entity instanceof Projectile || entity instanceof ProjectileEntity)) { - if (velocity.horizontalDistance() > 0.5) { + if (entity.isAlive() && !(entity instanceof ItemEntity || entity instanceof Projectile || entity instanceof ProjectileEntity)) { + if (velocity.horizontalDistance() > 0.4) { if (!this.level().isClientSide) { this.level().playSound(null, this, ModSounds.VEHICLE_STRIKE.get(), this.getSoundSource(), 1, 1); } @@ -60,7 +77,7 @@ public class MobileVehicleEntity extends EnergyVehicleEntity { this.push(-f * velAdd.x, -f * velAdd.y, -f * velAdd.z); } entity.push(f1 * velAdd.x, f1 * velAdd.y, f1 * velAdd.z); - entity.hurt(ModDamageTypes.causeVehicleStrikeDamage(this.level().registryAccess(), this, this.getFirstPassenger() == null ? this : this.getFirstPassenger()), (float) (thisSize * 60 * (velocity.horizontalDistance() - 0.5))); + entity.hurt(ModDamageTypes.causeVehicleStrikeDamage(this.level().registryAccess(), this, this.getFirstPassenger() == null ? this : this.getFirstPassenger()), (float) (thisSize * 40 * (velocity.horizontalDistance() - 0.4))); } else { entity.push(0.2 * f1 * velAdd.x, 0.2 * f1 * velAdd.y, 0.2 * f1 * velAdd.z); } @@ -68,9 +85,14 @@ public class MobileVehicleEntity extends EnergyVehicleEntity { } } + public SoundEvent getEngineSound() { + return null; + } + @Override protected void defineSynchedData() { super.defineSynchedData(); + this.entityData.define(POWER, 0f); } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/SpeedboatEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/SpeedboatEntity.java index 49b9646df..0899c3340 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/SpeedboatEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/SpeedboatEntity.java @@ -80,7 +80,6 @@ import java.util.List; import static com.atsuishio.superbwarfare.tools.ParticleTool.sendParticle; public class SpeedboatEntity extends MobileVehicleEntity implements GeoEntity, IChargeEntity, IVehicleEntity, HasCustomInventoryScreen, ContainerEntity { - public static final EntityDataAccessor POWER = SynchedEntityData.defineId(SpeedboatEntity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor FIRE_ANIM = SynchedEntityData.defineId(SpeedboatEntity.class, EntityDataSerializers.INT); public static final EntityDataAccessor DELTA_ROT = SynchedEntityData.defineId(SpeedboatEntity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor HEAT = SynchedEntityData.defineId(SpeedboatEntity.class, EntityDataSerializers.INT); @@ -116,7 +115,6 @@ public class SpeedboatEntity extends MobileVehicleEntity implements GeoEntity, I @Override protected void defineSynchedData() { super.defineSynchedData(); - this.entityData.define(POWER, 0f); this.entityData.define(AMMO, 0); this.entityData.define(FIRE_ANIM, 0); this.entityData.define(DELTA_ROT, 0f); @@ -237,7 +235,7 @@ public class SpeedboatEntity extends MobileVehicleEntity implements GeoEntity, I if (this.level() instanceof ServerLevel serverLevel && this.isInWater() && this.getDeltaMovement().length() > 0.1) { sendParticle(serverLevel, ParticleTypes.CLOUD, this.getX() + 0.5 * this.getDeltaMovement().x, this.getY() + getSubmergedHeight(this) - 0.2, this.getZ() + 0.5 * this.getDeltaMovement().z, (int)(2 + 4 * this.getDeltaMovement().length()), 0.65, 0, 0.65, 0, true); sendParticle(serverLevel, ParticleTypes.BUBBLE_COLUMN_UP, this.getX() + 0.5 * this.getDeltaMovement().x, this.getY() + getSubmergedHeight(this) - 0.2, this.getZ() + 0.5 * this.getDeltaMovement().z, (int)(2 + 10 * this.getDeltaMovement().length()), 0.65, 0, 0.65, 0, true); - sendParticle(serverLevel, ParticleTypes.BUBBLE_COLUMN_UP, this.getX() - 4.5 * this.getLookAngle().x, this.getY() - 0.25, this.getZ() - 4.5 * this.getLookAngle().z, (int)(40 * Mth.abs(power)), 0.15, 0.15, 0.15, 0.02, true); + sendParticle(serverLevel, ParticleTypes.BUBBLE_COLUMN_UP, this.getX() - 4.5 * this.getLookAngle().x, this.getY() - 0.25, this.getZ() - 4.5 * this.getLookAngle().z, (int)(40 * Mth.abs(this.entityData.get(POWER))), 0.15, 0.15, 0.15, 0.02, true); } collideBlock(); @@ -444,8 +442,8 @@ public class SpeedboatEntity extends MobileVehicleEntity implements GeoEntity, I public void setRudderRot(float pRudderRot) { this.rudderRot = pRudderRot; } - - protected SoundEvent getEngineSound() { + @Override + public SoundEvent getEngineSound() { return ModSounds.BOAT_ENGINE.get(); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/VehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/VehicleEntity.java index 70b304686..172390bdb 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/VehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/VehicleEntity.java @@ -80,6 +80,11 @@ public class VehicleEntity extends Entity { player.setXRot(this.getXRot()); player.setYRot(this.getYRot()); return player.startRiding(this) ? InteractionResult.CONSUME : InteractionResult.PASS; + } else if (!(this.getFirstPassenger() instanceof Player)) { + this.getFirstPassenger().stopRiding(); + player.setXRot(this.getXRot()); + player.setYRot(this.getYRot()); + return player.startRiding(this) ? InteractionResult.CONSUME : InteractionResult.PASS; } } } else if (!this.level().isClientSide) { @@ -87,6 +92,11 @@ public class VehicleEntity extends Entity { player.setXRot(this.getXRot()); player.setYRot(this.getYRot()); return player.startRiding(this) ? InteractionResult.CONSUME : InteractionResult.PASS; + } else if (!(this.getFirstPassenger() instanceof Player)) { + this.getFirstPassenger().stopRiding(); + player.setXRot(this.getXRot()); + player.setYRot(this.getYRot()); + return player.startRiding(this) ? InteractionResult.CONSUME : InteractionResult.PASS; } } return InteractionResult.PASS; diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/WheelChairEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/WheelChairEntity.java index f385657b3..58ef14e68 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/WheelChairEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/WheelChairEntity.java @@ -13,17 +13,17 @@ import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.sounds.SoundEvent; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.util.Mth; import net.minecraft.world.damagesource.DamageSource; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.EntityDimensions; -import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.Pose; +import net.minecraft.world.entity.*; +import net.minecraft.world.entity.animal.WaterAnimal; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Explosion; import net.minecraft.world.level.Level; +import net.minecraft.world.phys.Vec3; import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.PlayMessages; import org.joml.Math; @@ -32,6 +32,8 @@ import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache import software.bernie.geckolib.core.animation.AnimatableManager; import software.bernie.geckolib.util.GeckoLibUtil; +import java.util.List; + public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity, IVehicleEntity { private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); public static final float MAX_HEALTH = 50; @@ -52,6 +54,20 @@ public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity, this.setMaxUpStep(1.1f); } + @Override + public void playerTouch(Player pPlayer) { + if (this.position().distanceTo(pPlayer.position()) > 1.25) return; + if (!this.level().isClientSide) { + double entitySize = pPlayer.getBbWidth() * pPlayer.getBbHeight(); + double thisSize = this.getBbWidth() * this.getBbHeight(); + double f = Math.min(entitySize / thisSize, 2); + double f1 = Math.min(thisSize / entitySize, 4); + this.setDeltaMovement(this.getDeltaMovement().add(new Vec3(pPlayer.position().vectorTo(this.position()).toVector3f()).scale(0.3 * f * pPlayer.getDeltaMovement().length()))); + this.setYRot(pPlayer.getYHeadRot()); + pPlayer.setDeltaMovement(pPlayer.getDeltaMovement().add(new Vec3(this.position().vectorTo(pPlayer.position()).toVector3f()).scale(0.01 * f1 * pPlayer.getDeltaMovement().length()))); + } + } + @Override protected void defineSynchedData() { super.defineSynchedData(); @@ -77,11 +93,6 @@ public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity, return NetworkHooks.getEntitySpawningPacket(this); } - @Override - public double getPassengersRidingOffset() { - return super.getPassengersRidingOffset() - 0.05; - } - @Override public boolean hurt(DamageSource source, float amount) { super.hurt(source, amount); @@ -112,16 +123,28 @@ public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity, } else { this.setDeltaMovement(this.getDeltaMovement().multiply(0.99, 0.95, 0.99)); } - - if (level().isClientSide && this.getEnergy() > 0) { - level().playLocalSound(this.getX(), this.getY() + this.getBbHeight() * 0.5, this.getZ(), ModSounds.WHEEL_CHAIR_ENGINE.get(), this.getSoundSource(), (float) (0.2 * this.getDeltaMovement().length()), (random.nextFloat() * 0.1f + 0.7f), false); - } - this.setSprinting(this.getDeltaMovement().horizontalDistance() > 0.15); - + attractEntity(); this.refreshDimensions(); } + public boolean hasEnoughSpaceFor(Entity pEntity) { + return pEntity.getBbWidth() < this.getBbWidth(); + } + + public void attractEntity() { + List list = this.level().getEntities(this, this.getBoundingBox().inflate(0.2F, -0.01F, 0.2F)); + if (!list.isEmpty()) { + boolean flag = !this.level().isClientSide && !(this.getControllingPassenger() instanceof Player); + + for (Entity entity : list) { + if (!entity.hasPassenger(this) && flag && !entity.isPassenger() && this.hasEnoughSpaceFor(entity) && (entity instanceof LivingEntity || entity instanceof MortarEntity) && !(entity instanceof WaterAnimal) && !(entity instanceof Player)) { + entity.startRiding(this); + } + } + } + } + @Override public void travel() { Entity passenger = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0); @@ -133,20 +156,23 @@ public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity, this.rightInputDown = false; this.forwardInputDown = false; this.backInputDown = false; - } else { + } else if (passenger instanceof Player) { + if (level().isClientSide) { + level().playLocalSound(this.getX(), this.getY() + this.getBbHeight() * 0.5, this.getZ(), this.getEngineSound(), this.getSoundSource(), Math.min((this.forwardInputDown || this.backInputDown ? 7.5f : 5f) * 2 * Mth.abs(this.entityData.get(POWER)), 0.25f), (random.nextFloat() * 0.1f + 1f), false); + } diffY = Math.clamp(-90f, 90f, Mth.wrapDegrees(passenger.getYHeadRot() - this.getYRot())); this.setYRot(this.getYRot() + Mth.clamp(0.4f * diffY, -5f, 5f)); } if (this.forwardInputDown) { - power += 0.01f; + this.entityData.set(POWER, this.entityData.get(POWER) + 0.01f); if (this.getEnergy() <= 0 && passenger instanceof Player player) { moveWithOutPower(player, true); } } if (this.backInputDown) { - power -= 0.01f; + this.entityData.set(POWER, this.entityData.get(POWER) - 0.01f); if (this.getEnergy() <= 0 && passenger instanceof Player player) { moveWithOutPower(player, false); } @@ -158,7 +184,7 @@ public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity, } this.extraEnergy(400); this.setDeltaMovement(this.getDeltaMovement().add(0, 0.48, 0)); - jumpCoolDown = 5; + jumpCoolDown = 3; } if (this.forwardInputDown || this.backInputDown) { @@ -172,7 +198,7 @@ public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity, } } - power *= 0.87f; + this.entityData.set(POWER, this.entityData.get(POWER) * 0.87f); float angle = (float) calculateAngle(this.getDeltaMovement(), this.getViewVector(1)); double s0; @@ -190,12 +216,11 @@ public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity, // player.displayClientMessage(Component.literal("Angle:" + new java.text.DecimalFormat("##.##").format(this.getRightWheelRot())), true); // } - this.setDeltaMovement(this.getDeltaMovement().add(Mth.sin(-this.getYRot() * 0.017453292F) * (this.onGround() ? 1 : 0.1) * power, 0.0, Mth.cos(this.getYRot() * 0.017453292F) * (this.onGround() ? 1 : 0.1) * power)); - + this.setDeltaMovement(this.getDeltaMovement().add(Mth.sin(-this.getYRot() * 0.017453292F) * (this.onGround() ? 1 : 0.1) * this.entityData.get(POWER), 0.0, Mth.cos(this.getYRot() * 0.017453292F) * (this.onGround() ? 1 : 0.1) * this.entityData.get(POWER))); } public void moveWithOutPower(Player player, boolean forward) { - power += (forward ? 0.015f : -0.015f); + this.entityData.set(POWER, this.entityData.get(POWER) + (forward ? 0.015f : -0.015f)); if (player instanceof ServerPlayer serverPlayer) { serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.BOAT_PADDLE_LAND, SoundSource.PLAYERS, 1, 1); } @@ -206,6 +231,11 @@ public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity, this.backInputDown = false; } + @Override + public SoundEvent getEngineSound() { + return ModSounds.WHEEL_CHAIR_ENGINE.get(); + } + public float getLeftWheelRot() { return this.leftWheelRot; }