BiomeAmbientSoundsHandler.java

net.minecraft.client.resources.sounds.BiomeAmbientSoundsHandler

信息

  • 全限定名:net.minecraft.client.resources.sounds.BiomeAmbientSoundsHandler
  • 类型:public class
  • 包:net.minecraft.client.resources.sounds
  • 源码路径:src/main/java/net/minecraft/client/resources/sounds/BiomeAmbientSoundsHandler.java
  • 起始行号:L24
  • 实现:AmbientSoundHandler
  • 职责:

    TODO

字段/常量

  • LOOP_SOUND_CROSS_FADE_TIME

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L25
    • 说明:

      TODO

  • SKY_MOOD_RECOVERY_RATE

    • 类型: float
    • 修饰符: private static final
    • 源码定位: L26
    • 说明:

      TODO

  • player

    • 类型: LocalPlayer
    • 修饰符: private final
    • 源码定位: L27
    • 说明:

      TODO

  • soundManager

    • 类型: SoundManager
    • 修饰符: private final
    • 源码定位: L28
    • 说明:

      TODO

  • random

    • 类型: RandomSource
    • 修饰符: private final
    • 源码定位: L29
    • 说明:

      TODO

  • loopSounds

    • 类型: Object2ObjectArrayMap<Holder<SoundEvent>,BiomeAmbientSoundsHandler.LoopSoundInstance>
    • 修饰符: private final
    • 源码定位: L30
    • 说明:

      TODO

  • moodiness

    • 类型: float
    • 修饰符: private
    • 源码定位: L31
    • 说明:

      TODO

  • previousLoopSound

    • 类型: Holder<SoundEvent>
    • 修饰符: private
    • 源码定位: L32
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.resources.sounds.BiomeAmbientSoundsHandler.LoopSoundInstance
    • 类型: class
    • 修饰符: public static
    • 源码定位: L117
    • 说明:

      TODO

构造器

public BiomeAmbientSoundsHandler(LocalPlayer player, SoundManager soundManager) @ L34

  • 构造器名:BiomeAmbientSoundsHandler
  • 源码定位:L34
  • 修饰符:public

参数:

  • player: LocalPlayer
  • soundManager: SoundManager

说明:

TODO

方法

下面的方法块按源码顺序生成。

public float getMoodiness() @ L40

  • 方法名:getMoodiness
  • 源码定位:L40
  • 返回类型:float
  • 修饰符:public

参数:

说明:

TODO

public void tick() @ L44

  • 方法名:tick
  • 源码定位:L44
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class BiomeAmbientSoundsHandler implements AmbientSoundHandler {
    private static final int LOOP_SOUND_CROSS_FADE_TIME = 40;
    private static final float SKY_MOOD_RECOVERY_RATE = 0.001F;
    private final LocalPlayer player;
    private final SoundManager soundManager;
    private final RandomSource random;
    private final Object2ObjectArrayMap<Holder<SoundEvent>, BiomeAmbientSoundsHandler.LoopSoundInstance> loopSounds = new Object2ObjectArrayMap<>();
    private float moodiness;
    private @Nullable Holder<SoundEvent> previousLoopSound;
 
    public BiomeAmbientSoundsHandler(LocalPlayer player, SoundManager soundManager) {
        this.random = player.level().getRandom();
        this.player = player;
        this.soundManager = soundManager;
    }
 
    public float getMoodiness() {
        return this.moodiness;
    }
 
    @Override
    public void tick() {
        this.loopSounds.values().removeIf(AbstractTickableSoundInstance::isStopped);
        Level level = this.player.level();
        EnvironmentAttributeSystem environmentAttributes = level.environmentAttributes();
        AmbientSounds ambientSounds = environmentAttributes.getValue(EnvironmentAttributes.AMBIENT_SOUNDS, this.player.position());
        Holder<SoundEvent> currentLoopSound = ambientSounds.loop().orElse(null);
        if (!Objects.equals(currentLoopSound, this.previousLoopSound)) {
            this.previousLoopSound = currentLoopSound;
            this.loopSounds.values().forEach(BiomeAmbientSoundsHandler.LoopSoundInstance::fadeOut);
            if (currentLoopSound != null) {
                this.loopSounds.compute(currentLoopSound, (biomeKey, soundInstance) -> {
                    if (soundInstance == null) {
                        soundInstance = new BiomeAmbientSoundsHandler.LoopSoundInstance(currentLoopSound.value());
                        this.soundManager.play(soundInstance);
                    }
 
                    soundInstance.fadeIn();
                    return (BiomeAmbientSoundsHandler.LoopSoundInstance)soundInstance;
                });
            }
        }
 
        for (AmbientAdditionsSettings additions : ambientSounds.additions()) {
            if (this.random.nextDouble() < additions.tickChance()) {
                this.soundManager.play(SimpleSoundInstance.forAmbientAddition(additions.soundEvent().value()));
            }
        }
 
        ambientSounds.mood()
            .ifPresent(
                mood -> {
                    int searchSpan = mood.blockSearchExtent() * 2 + 1;
                    BlockPos blockSamplingPos = BlockPos.containing(
                        this.player.getX() + this.random.nextInt(searchSpan) - mood.blockSearchExtent(),
                        this.player.getEyeY() + this.random.nextInt(searchSpan) - mood.blockSearchExtent(),
                        this.player.getZ() + this.random.nextInt(searchSpan) - mood.blockSearchExtent()
                    );
                    int skyBrightness = level.getBrightness(LightLayer.SKY, blockSamplingPos);
                    if (skyBrightness > 0) {
                        this.moodiness -= skyBrightness / 15.0F * 0.001F;
                    } else {
                        this.moodiness = this.moodiness - (float)(level.getBrightness(LightLayer.BLOCK, blockSamplingPos) - 1) / mood.tickDelay();
                    }
 
                    if (this.moodiness >= 1.0F) {
                        double blockSampleX = blockSamplingPos.getX() + 0.5;
                        double blockSampleY = blockSamplingPos.getY() + 0.5;
                        double blockSampleZ = blockSamplingPos.getZ() + 0.5;
                        double blockDirectionX = blockSampleX - this.player.getX();
                        double blockDirectionY = blockSampleY - this.player.getEyeY();
                        double blockDirectionZ = blockSampleZ - this.player.getZ();
                        double blockDistance = Math.sqrt(
                            blockDirectionX * blockDirectionX + blockDirectionY * blockDirectionY + blockDirectionZ * blockDirectionZ
                        );
                        double soundSourceDistance = blockDistance + mood.soundPositionOffset();
                        SimpleSoundInstance moodSoundInstance = SimpleSoundInstance.forAmbientMood(
                            mood.soundEvent().value(),
                            this.random,
                            this.player.getX() + blockDirectionX / blockDistance * soundSourceDistance,
                            this.player.getEyeY() + blockDirectionY / blockDistance * soundSourceDistance,
                            this.player.getZ() + blockDirectionZ / blockDistance * soundSourceDistance
                        );
                        this.soundManager.play(moodSoundInstance);
                        this.moodiness = 0.0F;
                    } else {
                        this.moodiness = Math.max(this.moodiness, 0.0F);
                    }
                }
            );
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class LoopSoundInstance extends AbstractTickableSoundInstance {
        private int fadeDirection;
        private int fade;
 
        public LoopSoundInstance(SoundEvent soundEvent) {
            super(soundEvent, SoundSource.AMBIENT, SoundInstance.createUnseededRandom());
            this.looping = true;
            this.delay = 0;
            this.volume = 1.0F;
            this.relative = true;
        }
 
        @Override
        public void tick() {
            if (this.fade < 0) {
                this.stop();
            }
 
            this.fade = this.fade + this.fadeDirection;
            this.volume = Mth.clamp(this.fade / 40.0F, 0.0F, 1.0F);
        }
 
        public void fadeOut() {
            this.fade = Math.min(this.fade, 40);
            this.fadeDirection = -1;
        }
 
        public void fadeIn() {
            this.fade = Math.max(0, this.fade);
            this.fadeDirection = 1;
        }
    }
}

引用的其他类

  • LocalPlayer

    • 引用位置: 参数/字段
  • AmbientSoundHandler

    • 引用位置: 实现
  • SimpleSoundInstance

    • 引用位置: 方法调用
    • 关联成员: SimpleSoundInstance.forAmbientAddition(), SimpleSoundInstance.forAmbientMood()
  • SoundInstance

    • 引用位置: 方法调用
    • 关联成员: SoundInstance.createUnseededRandom()
  • SoundManager

    • 引用位置: 参数/字段
  • BlockPos

    • 引用位置: 方法调用
    • 关联成员: BlockPos.containing()
  • Holder

    • 引用位置: 字段
  • SoundEvent

    • 引用位置: 字段
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.clamp()
  • RandomSource

    • 引用位置: 字段