From 5f554adc7c103e4dda95e9514d99c3da78ffe9f5 Mon Sep 17 00:00:00 2001 From: Atsuishio <842960157@qq.com> Date: Tue, 6 May 2025 00:02:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=84=E9=A3=9E=E8=BD=A6=E5=A5=87=E5=8C=A0?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E6=98=AF=E6=B2=A1=E6=88=90=E5=8A=9F=EF=BC=88?= =?UTF-8?q?=E6=81=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../atsuishio/superbwarfare/ModClient.java | 19 ++++ .../client/ClientVehicleSoundsInstance.java | 46 ---------- .../client/VehicleSoundInstance.java | 89 +++++++++++++++++++ .../entity/vehicle/Tom6Entity.java | 2 +- .../vehicle/base/MobileVehicleEntity.java | 4 + .../event/ClientSoundHandler.java | 8 ++ 6 files changed, 121 insertions(+), 47 deletions(-) create mode 100644 src/main/java/com/atsuishio/superbwarfare/ModClient.java delete mode 100644 src/main/java/com/atsuishio/superbwarfare/client/ClientVehicleSoundsInstance.java create mode 100644 src/main/java/com/atsuishio/superbwarfare/client/VehicleSoundInstance.java diff --git a/src/main/java/com/atsuishio/superbwarfare/ModClient.java b/src/main/java/com/atsuishio/superbwarfare/ModClient.java new file mode 100644 index 000000000..b64f47f4b --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/ModClient.java @@ -0,0 +1,19 @@ +package com.atsuishio.superbwarfare; + +import com.atsuishio.superbwarfare.client.VehicleSoundInstance; +import com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntity; +import net.minecraft.client.Minecraft; + + +public class ModClient { + public static void init() { + initEntities(); + } + + public static void initEntities() { + MobileVehicleEntity.engineSound = mobileVehicle -> { + var client = Minecraft.getInstance(); + client.getSoundManager().play(new VehicleSoundInstance.EngineSound(client, mobileVehicle)); + }; + } +} diff --git a/src/main/java/com/atsuishio/superbwarfare/client/ClientVehicleSoundsInstance.java b/src/main/java/com/atsuishio/superbwarfare/client/ClientVehicleSoundsInstance.java deleted file mode 100644 index c74f3db75..000000000 --- a/src/main/java/com/atsuishio/superbwarfare/client/ClientVehicleSoundsInstance.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.atsuishio.superbwarfare.client; - -import com.atsuishio.superbwarfare.entity.vehicle.Tom6Entity; -import net.minecraft.client.resources.sounds.AbstractTickableSoundInstance; -import net.minecraft.client.resources.sounds.SoundInstance; -import net.minecraft.sounds.SoundEvents; -import net.minecraft.sounds.SoundSource; -import net.minecraft.util.Mth; -import net.neoforged.api.distmarker.Dist; -import net.neoforged.api.distmarker.OnlyIn; - -@OnlyIn(Dist.CLIENT) -public class ClientVehicleSoundsInstance extends AbstractTickableSoundInstance { - private final Tom6Entity tom6Entity; - - public ClientVehicleSoundsInstance(Tom6Entity pTom6Entity) { - super(SoundEvents.ELYTRA_FLYING, SoundSource.PLAYERS, SoundInstance.createUnseededRandom()); - this.tom6Entity = pTom6Entity; - this.looping = true; - this.delay = 0; - this.volume = 0.0F; - this.x = pTom6Entity.getX(); - this.y = pTom6Entity.getY(); - this.z = pTom6Entity.getZ(); - } - - public void tick() { - if (this.tom6Entity.isRemoved()) { - this.stop(); - } else { - this.x = tom6Entity.getX(); - this.y = tom6Entity.getY(); - this.z = tom6Entity.getZ(); - float $$0 = (float) tom6Entity.getDeltaMovement().horizontalDistance(); - if ($$0 >= 0.01F) { - this.pitch = Mth.clamp(this.pitch + 0.0025F, 0.0F, 1.0F); - this.volume = Mth.lerp(Mth.clamp($$0, 0.0F, 0.5F), 0.0F, 0.7F); - } else { - this.pitch = 0.0F; - this.volume = 0.0F; - } - - } - } -} - diff --git a/src/main/java/com/atsuishio/superbwarfare/client/VehicleSoundInstance.java b/src/main/java/com/atsuishio/superbwarfare/client/VehicleSoundInstance.java new file mode 100644 index 000000000..afc8864ca --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/client/VehicleSoundInstance.java @@ -0,0 +1,89 @@ +package com.atsuishio.superbwarfare.client; + +import com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntity; +import net.minecraft.client.Minecraft; +import net.minecraft.client.resources.sounds.AbstractTickableSoundInstance; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.sounds.SoundSource; + +public abstract class VehicleSoundInstance extends AbstractTickableSoundInstance { + private final Minecraft client; + private final MobileVehicleEntity mobileVehicle; + + private double lastDistance; + + private int fade = 0; + private boolean die = false; + + public VehicleSoundInstance(SoundEvent sound, Minecraft client, MobileVehicleEntity mobileVehicle) { + super(sound, SoundSource.AMBIENT, mobileVehicle.getCommandSenderWorld().getRandom()); + this.client = client; + this.mobileVehicle = mobileVehicle; + this.looping = true; + this.delay = 0; + } + + protected abstract boolean canPlay(MobileVehicleEntity mobileVehicle); + + protected abstract float getPitch(MobileVehicleEntity mobileVehicle); + + protected abstract float getVolume(MobileVehicleEntity mobileVehicle); + + @Override + public void tick() { + var player = this.client.player; + if (mobileVehicle.isRemoved() || player == null) { + this.stop(); + return; + } else if (!this.canPlay(mobileVehicle)) { + this.die = true; + } + + if (this.die) { + if (this.fade > 0) this.fade--; + else if (this.fade == 0) { + this.stop(); + return; + } + } else if (this.fade < 3) { + this.fade++; + } + this.volume = this.getVolume(this.mobileVehicle) * (float)fade / 3; + + this.x = this.mobileVehicle.getX(); + this.y = this.mobileVehicle.getY(); + this.z = this.mobileVehicle.getZ(); + + this.pitch = this.getPitch(this.mobileVehicle); + + if (player.getVehicle() != this.mobileVehicle) { + double distance = this.mobileVehicle.position().subtract(player.position()).length(); + this.pitch += (float) (0.36 * Math.atan(lastDistance - distance)); + + this.lastDistance = distance; + } else { + this.lastDistance = 0; + } + } + + public static class EngineSound extends VehicleSoundInstance { + public EngineSound(Minecraft client, MobileVehicleEntity mobileVehicle) { + super(mobileVehicle.getEngineSound(), client, mobileVehicle); + } + + @Override + protected boolean canPlay(MobileVehicleEntity mobileVehicle) { + return true; + } + + @Override + protected float getPitch(MobileVehicleEntity mobileVehicle) { + return 1; + } + + @Override + protected float getVolume(MobileVehicleEntity mobileVehicle) { + return 1; + } + } +} diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Tom6Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Tom6Entity.java index 089c1280b..3464a2a26 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Tom6Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Tom6Entity.java @@ -234,7 +234,7 @@ public class Tom6Entity extends MobileVehicleEntity implements GeoEntity { @Override public SoundEvent getEngineSound() { - return ModSounds.WHEEL_CHAIR_ENGINE.get(); + return SoundEvents.ELYTRA_FLYING; } protected void clampRotation(Entity entity) { diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/MobileVehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/MobileVehicleEntity.java index 7d0166994..b222d5c5c 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/MobileVehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/MobileVehicleEntity.java @@ -47,9 +47,11 @@ import org.joml.Vector3f; import org.joml.Vector4f; import java.util.List; +import java.util.function.Consumer; import java.util.stream.StreamSupport; public abstract class MobileVehicleEntity extends EnergyVehicleEntity implements ControllableVehicle { + public static Consumer engineSound = e -> {}; public static final EntityDataAccessor CANNON_RECOIL_TIME = SynchedEntityData.defineId(MobileVehicleEntity.class, EntityDataSerializers.INT); public static final EntityDataAccessor POWER = SynchedEntityData.defineId(MobileVehicleEntity.class, EntityDataSerializers.FLOAT); @@ -192,6 +194,8 @@ public abstract class MobileVehicleEntity extends EnergyVehicleEntity implements super.baseTick(); + engineSound.accept(this); + double direct = (90 - calculateAngle(this.getDeltaMovement(), this.getViewVector(1))) / 90; setVelocity(Mth.lerp(0.4, getVelocity(), getDeltaMovement().horizontalDistance() * direct * 20)); diff --git a/src/main/java/com/atsuishio/superbwarfare/event/ClientSoundHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/ClientSoundHandler.java index 1e368e118..d7dc09f69 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/ClientSoundHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/ClientSoundHandler.java @@ -1,5 +1,6 @@ package com.atsuishio.superbwarfare.event; +import com.atsuishio.superbwarfare.ModClient; import com.atsuishio.superbwarfare.entity.projectile.SwarmDroneEntity; import com.atsuishio.superbwarfare.entity.vehicle.*; import com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntity; @@ -18,6 +19,7 @@ import net.minecraft.world.phys.Vec3; import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import net.neoforged.neoforge.client.event.ClientTickEvent; import org.joml.Math; @@ -29,6 +31,12 @@ import static com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntit @EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME, value = Dist.CLIENT) public class ClientSoundHandler { + + @SubscribeEvent + public static void initClient(FMLClientSetupEvent setup) { + ModClient.init(); + } + @SubscribeEvent public static void handleClientTick(ClientTickEvent.Pre event) { LocalPlayer player = Minecraft.getInstance().player;