| 1 | package net.minecraft.client.resources.sounds;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Minecraft;
|
| 6 | import net.minecraft.sounds.SoundEvent;
|
| 7 | import net.minecraft.sounds.SoundSource;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.world.entity.animal.bee.Bee;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public abstract class BeeSoundInstance extends AbstractTickableSoundInstance {
|
| 13 | private static final float VOLUME_MIN = 0.0F;
|
| 14 | private static final float VOLUME_MAX = 1.2F;
|
| 15 | private static final float PITCH_MIN = 0.0F;
|
| 16 | protected final Bee bee;
|
| 17 | private boolean hasSwitched;
|
| 18 |
|
| 19 | public BeeSoundInstance(final Bee bee, final SoundEvent event, final SoundSource source) {
|
| 20 | super(event, source, SoundInstance.createUnseededRandom());
|
| 21 | this.bee = bee;
|
| 22 | this.x = (float)bee.getX();
|
| 23 | this.y = (float)bee.getY();
|
| 24 | this.z = (float)bee.getZ();
|
| 25 | this.looping = true;
|
| 26 | this.delay = 0;
|
| 27 | this.volume = 0.0F;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public void tick() {
|
| 32 | boolean shouldSwitchSounds = this.shouldSwitchSounds();
|
| 33 | if (shouldSwitchSounds && !this.isStopped()) {
|
| 34 | Minecraft.getInstance().getSoundManager().queueTickingSound(this.getAlternativeSoundInstance());
|
| 35 | this.hasSwitched = true;
|
| 36 | }
|
| 37 |
|
| 38 | if (!this.bee.isRemoved() && !this.hasSwitched) {
|
| 39 | this.x = (float)this.bee.getX();
|
| 40 | this.y = (float)this.bee.getY();
|
| 41 | this.z = (float)this.bee.getZ();
|
| 42 | float speed = (float)this.bee.getDeltaMovement().horizontalDistance();
|
| 43 | if (speed >= 0.01F) {
|
| 44 | this.pitch = Mth.lerp(Mth.clamp(speed, this.getMinPitch(), this.getMaxPitch()), this.getMinPitch(), this.getMaxPitch());
|
| 45 | this.volume = Mth.lerp(Mth.clamp(speed, 0.0F, 0.5F), 0.0F, 1.2F);
|
| 46 | } else {
|
| 47 | this.pitch = 0.0F;
|
| 48 | this.volume = 0.0F;
|
| 49 | }
|
| 50 | } else {
|
| 51 | this.stop();
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | private float getMinPitch() {
|
| 56 | return this.bee.isBaby() ? 1.1F : 0.7F;
|
| 57 | }
|
| 58 |
|
| 59 | private float getMaxPitch() {
|
| 60 | return this.bee.isBaby() ? 1.5F : 1.1F;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public boolean canStartSilent() {
|
| 65 | return true;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public boolean canPlaySound() {
|
| 70 | return !this.bee.isSilent();
|
| 71 | }
|
| 72 |
|
| 73 | protected abstract AbstractTickableSoundInstance getAlternativeSoundInstance();
|
| 74 |
|
| 75 | protected abstract boolean shouldSwitchSounds();
|
| 76 | }
|
| 77 |
|