diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/Ah6Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/Ah6Entity.java index 16da0506d..a5793ec7b 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/Ah6Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/Ah6Entity.java @@ -138,34 +138,34 @@ public class Ah6Entity extends MobileVehicleEntity implements GeoEntity, IHelico this.setYRot(this.getYRot() + Mth.clamp((this.onGround() ? 0.1f : 0.7f) * diffY * this.entityData.get(POWER) + 0.5f * this.entityData.get(DELTA_ROT), -5f, 5f)); this.setXRot(Mth.clamp(this.getXRot() + (this.onGround() ? 0 : 0.6f) * diffX * this.entityData.get(POWER), -80, 80)); this.setZRot(Mth.clamp(this.getRoll() - this.entityData.get(DELTA_ROT) + (this.onGround() ? 0 : 0.2f) * diffY * this.entityData.get(POWER), -50, 50)); + } - if (this.level() instanceof ServerLevel) { - boolean up = this.upInputDown || this.forwardInputDown; - boolean down = this.downInputDown || this.backInputDown; + if (this.level() instanceof ServerLevel) { + boolean up = this.upInputDown || this.forwardInputDown; + boolean down = this.downInputDown || this.backInputDown; - if (!engineStart && up) { - engineStart = true; - this.level().playSound(null, this, ModSounds.HELICOPTER_ENGINE_START.get(), this.getSoundSource(), 3, 1); - } + if (!engineStart && up) { + engineStart = true; + this.level().playSound(null, this, ModSounds.HELICOPTER_ENGINE_START.get(), this.getSoundSource(), 3, 1); + } - if (up && engineStartOver) { - this.entityData.set(POWER, Math.min(this.entityData.get(POWER) + 0.002f, 0.12f)); - } + if (up && engineStartOver) { + this.entityData.set(POWER, Math.min(this.entityData.get(POWER) + 0.002f, 0.12f)); + } - if (down && engineStartOver) { - this.entityData.set(POWER, Math.max(this.entityData.get(POWER) - 0.0015f, this.onGround() ? 0 : 0.0375f)); - } + if (down && engineStartOver) { + this.entityData.set(POWER, Math.max(this.entityData.get(POWER) - 0.0015f, this.onGround() ? 0 : 0.0375f)); + } - if (engineStart && !engineStartOver) { - this.entityData.set(POWER, Math.min(this.entityData.get(POWER) + 0.0012f, 0.045f)); - } + if (engineStart && !engineStartOver) { + this.entityData.set(POWER, Math.min(this.entityData.get(POWER) + 0.0012f, 0.045f)); + } - if (!(up || down) && engineStartOver) { - if (this.getDeltaMovement().y() + 0.06 < 0) { - this.entityData.set(POWER, Math.min(this.entityData.get(POWER) + 0.0002f, 0.12f)); - } else { - this.entityData.set(POWER, Math.max(this.entityData.get(POWER) - (this.onGround() ? 0.00005f : 0.0006f), 0)); - } + if (!(up || down) && engineStartOver) { + if (this.getDeltaMovement().y() + 0.06 < 0) { + this.entityData.set(POWER, Math.min(this.entityData.get(POWER) + 0.0002f, 0.12f)); + } else { + this.entityData.set(POWER, Math.max(this.entityData.get(POWER) - (this.onGround() ? 0.00005f : 0.0006f), 0)); } } } @@ -173,6 +173,7 @@ public class Ah6Entity extends MobileVehicleEntity implements GeoEntity, IHelico this.entityData.set(DELTA_ROT, this.entityData.get(DELTA_ROT) * 0.95f); this.entityData.set(PROPELLER_ROT, Mth.lerp(0.0001f, this.entityData.get(POWER), this.entityData.get(POWER))); this.setPropellerRot(this.getPropellerRot() + 30 * this.entityData.get(PROPELLER_ROT)); + this.entityData.set(PROPELLER_ROT, this.entityData.get(PROPELLER_ROT) * 0.9995f); setDeltaMovement(getDeltaMovement().add(0.0f, Math.min(Math.sin((90 - this.getXRot()) * Mth.DEG_TO_RAD), Math.sin((90 + this.getRoll()) * Mth.DEG_TO_RAD)) * this.entityData.get(POWER), 0.0f)); @@ -191,9 +192,9 @@ public class Ah6Entity extends MobileVehicleEntity implements GeoEntity, IHelico engineStartOver = false; } - if (level().isClientSide) { - level().playLocalSound(this.getX() + this.getDeltaMovement().x, this.getY() + this.getBbHeight() + this.getDeltaMovement().y + 0.06, this.getZ() + this.getDeltaMovement().z, this.getEngineSound(), this.getSoundSource(), Math.max((this.upInputDown ? 10f : 3f) * 6 * this.entityData.get(POWER) - 0.038f, 0), (random.nextFloat() * 0.1f + 1.0f), false); - } +// if (level().isClientSide) { +// level().playLocalSound(this.getX() + this.getDeltaMovement().x, this.getY() + this.getBbHeight() + this.getDeltaMovement().y + 0.06, this.getZ() + this.getDeltaMovement().z, this.getEngineSound(), this.getSoundSource(), Math.max((this.upInputDown ? 10f : 3f) * 6 * this.entityData.get(POWER) - 0.038f, 0), (random.nextFloat() * 0.1f + 1.0f), false); +// } } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/event/ClientSoundHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/ClientSoundHandler.java new file mode 100644 index 000000000..d457475b1 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/event/ClientSoundHandler.java @@ -0,0 +1,54 @@ +package com.atsuishio.superbwarfare.event; + +import com.atsuishio.superbwarfare.entity.Ah6Entity; +import com.atsuishio.superbwarfare.entity.MobileVehicleEntity; +import com.atsuishio.superbwarfare.tools.SeekTool; +import net.minecraft.client.Minecraft; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.core.BlockPos; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.phys.Vec3; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +import org.joml.Math; + +import java.util.List; + +import static com.atsuishio.superbwarfare.entity.MobileVehicleEntity.POWER; + +@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT) +public class ClientSoundHandler { + + @SubscribeEvent + public static void handleClientTick(TickEvent.ClientTickEvent event) { + LocalPlayer player = Minecraft.getInstance().player; + var options = Minecraft.getInstance().options; + if (player == null) { + return; + } + + List engineVehicle = SeekTool.getVehicleWithinRange(player, player.level(), 196); + + for (var e : engineVehicle) { + if (e instanceof MobileVehicleEntity mobileVehicle){ + + Vec3 listener = player.getEyePosition(); + Vec3 engineRealPos = e.getEyePosition(); + Vec3 toVec = listener.vectorTo(engineRealPos).normalize(); + double distance = listener.distanceTo(engineRealPos); + + var engineSoundPos = new Vec3(listener.x + toVec.x, listener.y + toVec.y, listener.z + toVec.z); + SoundEvent engineSound = mobileVehicle.getEngineSound(); + float distanceReduce; + if (e instanceof Ah6Entity ah6Entity) { + distanceReduce = (float) (1 - distance / 128); + player.level().playLocalSound(BlockPos.containing(engineSoundPos), engineSound, mobileVehicle.getSoundSource(), 20 * (mobileVehicle.getEntityData().get(POWER) - 0.012f) * distanceReduce * distanceReduce, (float) ((2 * Math.random() - 1) * 0.1f + 1.0f), false); +// player.displayClientMessage(Component.literal("Angle:" + engineSoundPos), true); + } + } + } + } +} diff --git a/src/main/java/com/atsuishio/superbwarfare/tools/SeekTool.java b/src/main/java/com/atsuishio/superbwarfare/tools/SeekTool.java index cd118f6d6..3c0b16d7b 100644 --- a/src/main/java/com/atsuishio/superbwarfare/tools/SeekTool.java +++ b/src/main/java/com/atsuishio/superbwarfare/tools/SeekTool.java @@ -1,6 +1,7 @@ package com.atsuishio.superbwarfare.tools; import com.atsuishio.superbwarfare.entity.ClaymoreEntity; +import com.atsuishio.superbwarfare.entity.MobileVehicleEntity; import com.atsuishio.superbwarfare.entity.VehicleEntity; import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity; import net.minecraft.core.BlockPos; @@ -24,6 +25,13 @@ import java.util.stream.StreamSupport; public class SeekTool { + public static List getVehicleWithinRange(Player player, Level level, double range) { + return StreamSupport.stream(EntityFindUtil.getEntities(level).getAll().spliterator(), false) + .filter(e -> e.position().distanceTo(player.getEyePosition()) <= range + && e instanceof MobileVehicleEntity) + .toList(); + } + public static Entity seekEntity(Entity entity, Level level, double seekRange, double seekAngle) { return StreamSupport.stream(EntityFindUtil.getEntities(level).getAll().spliterator(), false) .filter(e -> {