| 1 | package net.minecraft.client.particle;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.Camera;
|
| 7 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 8 | import net.minecraft.client.renderer.state.level.QuadParticleRenderState;
|
| 9 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 10 | import net.minecraft.core.particles.VibrationParticleOption;
|
| 11 | import net.minecraft.util.LightCoordsUtil;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.world.level.gameevent.PositionSource;
|
| 15 | import net.minecraft.world.phys.Vec3;
|
| 16 | import org.joml.Quaternionf;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class VibrationSignalParticle extends SingleQuadParticle {
|
| 20 | private final PositionSource target;
|
| 21 | private float rot;
|
| 22 | private float rotO;
|
| 23 | private float pitch;
|
| 24 | private float pitchO;
|
| 25 |
|
| 26 | private VibrationSignalParticle(
|
| 27 | final ClientLevel level,
|
| 28 | final double x,
|
| 29 | final double y,
|
| 30 | final double z,
|
| 31 | final PositionSource target,
|
| 32 | final int arrivalInTicks,
|
| 33 | final TextureAtlasSprite sprite
|
| 34 | ) {
|
| 35 | super(level, x, y, z, 0.0, 0.0, 0.0, sprite);
|
| 36 | this.quadSize = 0.3F;
|
| 37 | this.target = target;
|
| 38 | this.lifetime = arrivalInTicks;
|
| 39 | Optional<Vec3> position = target.getPosition(level);
|
| 40 | if (position.isPresent()) {
|
| 41 | Vec3 destination = position.get();
|
| 42 | double dx = x - destination.x();
|
| 43 | double dy = y - destination.y();
|
| 44 | double dz = z - destination.z();
|
| 45 | this.rotO = this.rot = (float)Mth.atan2(dx, dz);
|
| 46 | this.pitchO = this.pitch = (float)Mth.atan2(dy, Math.sqrt(dx * dx + dz * dz));
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void extract(final QuadParticleRenderState particleTypeRenderState, final Camera camera, final float partialTickTime) {
|
| 52 | float randomSway = Mth.sin((this.age + partialTickTime - (float) (Math.PI * 2)) * 0.05F) * 2.0F;
|
| 53 | float lerpedRotation = Mth.lerp(partialTickTime, this.rotO, this.rot);
|
| 54 | float lerpedPitch = Mth.lerp(partialTickTime, this.pitchO, this.pitch) + (float) (Math.PI / 2);
|
| 55 | Quaternionf rotation = new Quaternionf();
|
| 56 | rotation.rotationY(lerpedRotation).rotateX(-lerpedPitch).rotateY(randomSway);
|
| 57 | this.extractRotatedQuad(particleTypeRenderState, camera, rotation, partialTickTime);
|
| 58 | rotation.rotationY((float) -Math.PI + lerpedRotation).rotateX(lerpedPitch).rotateY(randomSway);
|
| 59 | this.extractRotatedQuad(particleTypeRenderState, camera, rotation, partialTickTime);
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public int getLightCoords(final float a) {
|
| 64 | return LightCoordsUtil.withBlock(super.getLightCoords(a), 15);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public SingleQuadParticle.Layer getLayer() {
|
| 69 | return SingleQuadParticle.Layer.TRANSLUCENT;
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public void tick() {
|
| 74 | this.xo = this.x;
|
| 75 | this.yo = this.y;
|
| 76 | this.zo = this.z;
|
| 77 | if (this.age++ >= this.lifetime) {
|
| 78 | this.remove();
|
| 79 | } else {
|
| 80 | Optional<Vec3> position = this.target.getPosition(this.level);
|
| 81 | if (position.isEmpty()) {
|
| 82 | this.remove();
|
| 83 | } else {
|
| 84 | int ticksRemaining = this.lifetime - this.age;
|
| 85 | double alpha = 1.0 / ticksRemaining;
|
| 86 | Vec3 destination = position.get();
|
| 87 | this.x = Mth.lerp(alpha, this.x, destination.x());
|
| 88 | this.y = Mth.lerp(alpha, this.y, destination.y());
|
| 89 | this.z = Mth.lerp(alpha, this.z, destination.z());
|
| 90 | double dx = this.x - destination.x();
|
| 91 | double dy = this.y - destination.y();
|
| 92 | double dz = this.z - destination.z();
|
| 93 | this.rotO = this.rot;
|
| 94 | this.rot = (float)Mth.atan2(dx, dz);
|
| 95 | this.pitchO = this.pitch;
|
| 96 | this.pitch = (float)Mth.atan2(dy, Math.sqrt(dx * dx + dz * dz));
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | @Environment(EnvType.CLIENT)
|
| 102 | public static class Provider implements ParticleProvider<VibrationParticleOption> {
|
| 103 | private final SpriteSet sprite;
|
| 104 |
|
| 105 | public Provider(final SpriteSet sprite) {
|
| 106 | this.sprite = sprite;
|
| 107 | }
|
| 108 |
|
| 109 | public Particle createParticle(
|
| 110 | final VibrationParticleOption options,
|
| 111 | final ClientLevel level,
|
| 112 | final double x,
|
| 113 | final double y,
|
| 114 | final double z,
|
| 115 | final double xAux,
|
| 116 | final double yAux,
|
| 117 | final double zAux,
|
| 118 | final RandomSource random
|
| 119 | ) {
|
| 120 | VibrationSignalParticle particle = new VibrationSignalParticle(
|
| 121 | level, x, y, z, options.getDestination(), options.getArrivalInTicks(), this.sprite.get(random)
|
| 122 | );
|
| 123 | particle.setAlpha(1.0F);
|
| 124 | return particle;
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|