| 1 | package net.minecraft.client.particle;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 6 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.particles.SimpleParticleType;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class FireflyParticle extends SingleQuadParticle {
|
| 14 | private static final float PARTICLE_FADE_OUT_LIGHT_TIME = 0.3F;
|
| 15 | private static final float PARTICLE_FADE_IN_LIGHT_TIME = 0.1F;
|
| 16 | private static final float PARTICLE_FADE_OUT_ALPHA_TIME = 0.5F;
|
| 17 | private static final float PARTICLE_FADE_IN_ALPHA_TIME = 0.3F;
|
| 18 | private static final int PARTICLE_MIN_LIFETIME = 200;
|
| 19 | private static final int PARTICLE_MAX_LIFETIME = 300;
|
| 20 |
|
| 21 | private FireflyParticle(
|
| 22 | final ClientLevel level, final double x, final double y, final double z, final double xa, final double ya, final double za, final TextureAtlasSprite sprite
|
| 23 | ) {
|
| 24 | super(level, x, y, z, xa, ya, za, sprite);
|
| 25 | this.speedUpWhenYMotionIsBlocked = true;
|
| 26 | this.friction = 0.96F;
|
| 27 | this.quadSize *= 0.75F;
|
| 28 | this.yd *= 0.8F;
|
| 29 | this.xd *= 0.8F;
|
| 30 | this.zd *= 0.8F;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public SingleQuadParticle.Layer getLayer() {
|
| 35 | return SingleQuadParticle.Layer.TRANSLUCENT;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public int getLightCoords(final float a) {
|
| 40 | return (int)(255.0F * getFadeAmount(this.getLifetimeProgress(this.age + a), 0.1F, 0.3F));
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void tick() {
|
| 45 | super.tick();
|
| 46 | if (!this.level.getBlockState(BlockPos.containing(this.x, this.y, this.z)).isAir()) {
|
| 47 | this.remove();
|
| 48 | } else {
|
| 49 | this.setAlpha(getFadeAmount(this.getLifetimeProgress(this.age), 0.3F, 0.5F));
|
| 50 | if (this.random.nextFloat() > 0.95F || this.age == 1) {
|
| 51 | this.setParticleSpeed(-0.05F + 0.1F * this.random.nextFloat(), -0.05F + 0.1F * this.random.nextFloat(), -0.05F + 0.1F * this.random.nextFloat());
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | private float getLifetimeProgress(final float currentAge) {
|
| 57 | return Mth.clamp(currentAge / this.lifetime, 0.0F, 1.0F);
|
| 58 | }
|
| 59 |
|
| 60 | private static float getFadeAmount(final float lifetimeProgress, final float fadeInTime, final float fadeOutTime) {
|
| 61 | if (lifetimeProgress >= 1.0F - fadeInTime) {
|
| 62 | return (1.0F - lifetimeProgress) / fadeInTime;
|
| 63 | } else {
|
| 64 | return lifetimeProgress <= fadeOutTime ? lifetimeProgress / fadeOutTime : 1.0F;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Environment(EnvType.CLIENT)
|
| 69 | public static class FireflyProvider implements ParticleProvider<SimpleParticleType> {
|
| 70 | private final SpriteSet sprite;
|
| 71 |
|
| 72 | public FireflyProvider(final SpriteSet sprite) {
|
| 73 | this.sprite = sprite;
|
| 74 | }
|
| 75 |
|
| 76 | public Particle createParticle(
|
| 77 | final SimpleParticleType options,
|
| 78 | final ClientLevel level,
|
| 79 | final double x,
|
| 80 | final double y,
|
| 81 | final double z,
|
| 82 | final double xAux,
|
| 83 | final double yAux,
|
| 84 | final double zAux,
|
| 85 | final RandomSource random
|
| 86 | ) {
|
| 87 | FireflyParticle particle = new FireflyParticle(
|
| 88 | level, x, y, z, 0.5 - random.nextDouble(), random.nextBoolean() ? yAux : -yAux, 0.5 - random.nextDouble(), this.sprite.get(random)
|
| 89 | );
|
| 90 | particle.setLifetime(random.nextIntBetweenInclusive(200, 300));
|
| 91 | particle.scale(1.5F);
|
| 92 | particle.setAlpha(0.0F);
|
| 93 | return particle;
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|