| 1 | package net.minecraft.client.resources.sounds;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.sounds.SoundSource;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
|
| 9 | import net.minecraft.world.entity.vehicle.minecart.NewMinecartBehavior;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class MinecartSoundInstance extends AbstractTickableSoundInstance {
|
| 13 | private static final float VOLUME_MIN = 0.0F;
|
| 14 | private static final float VOLUME_MAX = 0.7F;
|
| 15 | private static final float PITCH_MIN = 0.0F;
|
| 16 | private static final float PITCH_MAX = 1.0F;
|
| 17 | private static final float PITCH_DELTA = 0.0025F;
|
| 18 | private final AbstractMinecart minecart;
|
| 19 | private float pitch = 0.0F;
|
| 20 |
|
| 21 | public MinecartSoundInstance(final AbstractMinecart minecart) {
|
| 22 | super(SoundEvents.MINECART_RIDING, SoundSource.NEUTRAL, SoundInstance.createUnseededRandom());
|
| 23 | this.minecart = minecart;
|
| 24 | this.looping = true;
|
| 25 | this.delay = 0;
|
| 26 | this.volume = 0.0F;
|
| 27 | this.x = (float)minecart.getX();
|
| 28 | this.y = (float)minecart.getY();
|
| 29 | this.z = (float)minecart.getZ();
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public boolean canPlaySound() {
|
| 34 | return !this.minecart.isSilent();
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public boolean canStartSilent() {
|
| 39 | return true;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public void tick() {
|
| 44 | if (this.minecart.isRemoved()) {
|
| 45 | this.stop();
|
| 46 | } else {
|
| 47 | this.x = (float)this.minecart.getX();
|
| 48 | this.y = (float)this.minecart.getY();
|
| 49 | this.z = (float)this.minecart.getZ();
|
| 50 | float speed = (float)this.minecart.getDeltaMovement().horizontalDistance();
|
| 51 | boolean offRail = !this.minecart.isOnRails() && this.minecart.getBehavior() instanceof NewMinecartBehavior;
|
| 52 | if (speed >= 0.01F && this.minecart.level().tickRateManager().runsNormally() && !offRail) {
|
| 53 | this.pitch = Mth.clamp(this.pitch + 0.0025F, 0.0F, 1.0F);
|
| 54 | this.volume = Mth.lerp(Mth.clamp(speed, 0.0F, 0.5F), 0.0F, 0.7F);
|
| 55 | } else {
|
| 56 | this.pitch = 0.0F;
|
| 57 | this.volume = 0.0F;
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 | }
|
| 62 |
|