| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 |
|
| 8 | @Environment(EnvType.CLIENT)
|
| 9 | public class EndFlashState {
|
| 10 | public static final int SOUND_DELAY_IN_TICKS = 30;
|
| 11 | private static final int FLASH_INTERVAL_IN_TICKS = 600;
|
| 12 | private static final int MAX_FLASH_OFFSET_IN_TICKS = 200;
|
| 13 | private static final int MIN_FLASH_DURATION_IN_TICKS = 100;
|
| 14 | private static final int MAX_FLASH_DURATION_IN_TICKS = 380;
|
| 15 | private long flashSeed;
|
| 16 | private int offset;
|
| 17 | private int duration;
|
| 18 | private float intensity;
|
| 19 | private float oldIntensity;
|
| 20 | private float xAngle;
|
| 21 | private float yAngle;
|
| 22 |
|
| 23 | public void tick(final long clockTime) {
|
| 24 | this.calculateFlashParameters(clockTime);
|
| 25 | this.oldIntensity = this.intensity;
|
| 26 | this.intensity = this.calculateIntensity(clockTime);
|
| 27 | }
|
| 28 |
|
| 29 | private void calculateFlashParameters(final long clockTime) {
|
| 30 | long newSeed = clockTime / 600L;
|
| 31 | if (newSeed != this.flashSeed) {
|
| 32 | RandomSource randomSource = RandomSource.createThreadLocalInstance(newSeed);
|
| 33 | randomSource.nextFloat();
|
| 34 | this.offset = Mth.randomBetweenInclusive(randomSource, 0, 200);
|
| 35 | this.duration = Mth.randomBetweenInclusive(randomSource, 100, Math.min(380, 600 - this.offset));
|
| 36 | this.xAngle = Mth.randomBetween(randomSource, -60.0F, 10.0F);
|
| 37 | this.yAngle = Mth.randomBetween(randomSource, -180.0F, 180.0F);
|
| 38 | this.flashSeed = newSeed;
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | private float calculateIntensity(final long clockTime) {
|
| 43 | long clockTimeWithinInterval = clockTime % 600L;
|
| 44 | return clockTimeWithinInterval >= this.offset && clockTimeWithinInterval <= this.offset + this.duration
|
| 45 | ? Mth.sin((float)(clockTimeWithinInterval - this.offset) * (float) Math.PI / this.duration)
|
| 46 | : 0.0F;
|
| 47 | }
|
| 48 |
|
| 49 | public float getXAngle() {
|
| 50 | return this.xAngle;
|
| 51 | }
|
| 52 |
|
| 53 | public float getYAngle() {
|
| 54 | return this.yAngle;
|
| 55 | }
|
| 56 |
|
| 57 | public float getIntensity(final float partialTicks) {
|
| 58 | return Mth.lerp(partialTicks, this.oldIntensity, this.intensity);
|
| 59 | }
|
| 60 |
|
| 61 | public boolean flashStartedThisTick() {
|
| 62 | return this.intensity > 0.0F && this.oldIntensity <= 0.0F;
|
| 63 | }
|
| 64 | }
|
| 65 |
|