| 1 | package net.minecraft.client.particle;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.particles.ParticleLimit;
|
| 10 | import net.minecraft.util.LightCoordsUtil;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.entity.Entity;
|
| 14 | import net.minecraft.world.phys.AABB;
|
| 15 | import net.minecraft.world.phys.Vec3;
|
| 16 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public abstract class Particle {
|
| 20 | private static final AABB INITIAL_AABB = new AABB(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
|
| 21 | private static final double MAXIMUM_COLLISION_VELOCITY_SQUARED = Mth.square(100.0);
|
| 22 | protected final ClientLevel level;
|
| 23 | protected double xo;
|
| 24 | protected double yo;
|
| 25 | protected double zo;
|
| 26 | protected double x;
|
| 27 | protected double y;
|
| 28 | protected double z;
|
| 29 | protected double xd;
|
| 30 | protected double yd;
|
| 31 | protected double zd;
|
| 32 | private AABB bb = INITIAL_AABB;
|
| 33 | protected boolean onGround;
|
| 34 | protected boolean hasPhysics = true;
|
| 35 | private boolean stoppedByCollision;
|
| 36 | protected boolean removed;
|
| 37 | protected float bbWidth = 0.6F;
|
| 38 | protected float bbHeight = 1.8F;
|
| 39 | protected final RandomSource random = RandomSource.create();
|
| 40 | protected int age;
|
| 41 | protected int lifetime;
|
| 42 | protected float gravity;
|
| 43 | protected float friction = 0.98F;
|
| 44 | protected boolean speedUpWhenYMotionIsBlocked = false;
|
| 45 |
|
| 46 | protected Particle(final ClientLevel level, final double x, final double y, final double z) {
|
| 47 | this.level = level;
|
| 48 | this.setSize(0.2F, 0.2F);
|
| 49 | this.setPos(x, y, z);
|
| 50 | this.xo = x;
|
| 51 | this.yo = y;
|
| 52 | this.zo = z;
|
| 53 | this.lifetime = (int)(4.0F / (this.random.nextFloat() * 0.9F + 0.1F));
|
| 54 | }
|
| 55 |
|
| 56 | public Particle(final ClientLevel level, final double x, final double y, final double z, final double xa, final double ya, final double za) {
|
| 57 | this(level, x, y, z);
|
| 58 | this.xd = xa + (this.random.nextFloat() * 2.0F - 1.0F) * 0.4F;
|
| 59 | this.yd = ya + (this.random.nextFloat() * 2.0F - 1.0F) * 0.4F;
|
| 60 | this.zd = za + (this.random.nextFloat() * 2.0F - 1.0F) * 0.4F;
|
| 61 | double speed = (this.random.nextFloat() + this.random.nextFloat() + 1.0F) * 0.15F;
|
| 62 | double dd = Math.sqrt(this.xd * this.xd + this.yd * this.yd + this.zd * this.zd);
|
| 63 | this.xd = this.xd / dd * speed * 0.4F;
|
| 64 | this.yd = this.yd / dd * speed * 0.4F + 0.1F;
|
| 65 | this.zd = this.zd / dd * speed * 0.4F;
|
| 66 | }
|
| 67 |
|
| 68 | public Particle setPower(final float power) {
|
| 69 | this.xd *= power;
|
| 70 | this.yd = (this.yd - 0.1F) * power + 0.1F;
|
| 71 | this.zd *= power;
|
| 72 | return this;
|
| 73 | }
|
| 74 |
|
| 75 | public void setParticleSpeed(final double xd, final double yd, final double zd) {
|
| 76 | this.xd = xd;
|
| 77 | this.yd = yd;
|
| 78 | this.zd = zd;
|
| 79 | }
|
| 80 |
|
| 81 | public Particle scale(final float scale) {
|
| 82 | this.setSize(0.2F * scale, 0.2F * scale);
|
| 83 | return this;
|
| 84 | }
|
| 85 |
|
| 86 | public void setLifetime(final int lifetime) {
|
| 87 | this.lifetime = lifetime;
|
| 88 | }
|
| 89 |
|
| 90 | public int getLifetime() {
|
| 91 | return this.lifetime;
|
| 92 | }
|
| 93 |
|
| 94 | public void tick() {
|
| 95 | this.xo = this.x;
|
| 96 | this.yo = this.y;
|
| 97 | this.zo = this.z;
|
| 98 | if (this.age++ >= this.lifetime) {
|
| 99 | this.remove();
|
| 100 | } else {
|
| 101 | this.yd = this.yd - 0.04 * this.gravity;
|
| 102 | this.move(this.xd, this.yd, this.zd);
|
| 103 | if (this.speedUpWhenYMotionIsBlocked && this.y == this.yo) {
|
| 104 | this.xd *= 1.1;
|
| 105 | this.zd *= 1.1;
|
| 106 | }
|
| 107 |
|
| 108 | this.xd = this.xd * this.friction;
|
| 109 | this.yd = this.yd * this.friction;
|
| 110 | this.zd = this.zd * this.friction;
|
| 111 | if (this.onGround) {
|
| 112 | this.xd *= 0.7F;
|
| 113 | this.zd *= 0.7F;
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | public abstract ParticleRenderType getGroup();
|
| 119 |
|
| 120 | @Override
|
| 121 | public String toString() {
|
| 122 | return this.getClass().getSimpleName() + ", Pos (" + this.x + "," + this.y + "," + this.z + "), Age " + this.age;
|
| 123 | }
|
| 124 |
|
| 125 | public void remove() {
|
| 126 | this.removed = true;
|
| 127 | }
|
| 128 |
|
| 129 | protected void setSize(final float w, final float h) {
|
| 130 | if (w != this.bbWidth || h != this.bbHeight) {
|
| 131 | this.bbWidth = w;
|
| 132 | this.bbHeight = h;
|
| 133 | AABB aabb = this.getBoundingBox();
|
| 134 | double newMinX = (aabb.minX + aabb.maxX - w) / 2.0;
|
| 135 | double newMinZ = (aabb.minZ + aabb.maxZ - w) / 2.0;
|
| 136 | this.setBoundingBox(new AABB(newMinX, aabb.minY, newMinZ, newMinX + this.bbWidth, aabb.minY + this.bbHeight, newMinZ + this.bbWidth));
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | public void setPos(final double x, final double y, final double z) {
|
| 141 | this.x = x;
|
| 142 | this.y = y;
|
| 143 | this.z = z;
|
| 144 | float w = this.bbWidth / 2.0F;
|
| 145 | float h = this.bbHeight;
|
| 146 | this.setBoundingBox(new AABB(x - w, y, z - w, x + w, y + h, z + w));
|
| 147 | }
|
| 148 |
|
| 149 | public void move(double xa, double ya, double za) {
|
| 150 | if (!this.stoppedByCollision) {
|
| 151 | double originalXa = xa;
|
| 152 | double originalYa = ya;
|
| 153 | double originalZa = za;
|
| 154 | if (this.hasPhysics && (xa != 0.0 || ya != 0.0 || za != 0.0) && xa * xa + ya * ya + za * za < MAXIMUM_COLLISION_VELOCITY_SQUARED) {
|
| 155 | Vec3 movement = Entity.collideBoundingBox(CollisionContext.positionContext(this.y), new Vec3(xa, ya, za), this.getBoundingBox(), this.level, List.of());
|
| 156 | xa = movement.x;
|
| 157 | ya = movement.y;
|
| 158 | za = movement.z;
|
| 159 | }
|
| 160 |
|
| 161 | if (xa != 0.0 || ya != 0.0 || za != 0.0) {
|
| 162 | this.setBoundingBox(this.getBoundingBox().move(xa, ya, za));
|
| 163 | this.setLocationFromBoundingbox();
|
| 164 | }
|
| 165 |
|
| 166 | if (Math.abs(originalYa) >= 1.0E-5F && Math.abs(ya) < 1.0E-5F) {
|
| 167 | this.stoppedByCollision = true;
|
| 168 | }
|
| 169 |
|
| 170 | this.onGround = originalYa != ya && originalYa < 0.0;
|
| 171 | if (originalXa != xa) {
|
| 172 | this.xd = 0.0;
|
| 173 | }
|
| 174 |
|
| 175 | if (originalZa != za) {
|
| 176 | this.zd = 0.0;
|
| 177 | }
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | protected void setLocationFromBoundingbox() {
|
| 182 | AABB aabb = this.getBoundingBox();
|
| 183 | this.x = (aabb.minX + aabb.maxX) / 2.0;
|
| 184 | this.y = aabb.minY;
|
| 185 | this.z = (aabb.minZ + aabb.maxZ) / 2.0;
|
| 186 | }
|
| 187 |
|
| 188 | protected int getLightCoords(final float a) {
|
| 189 | BlockPos pos = BlockPos.containing(this.x, this.y, this.z);
|
| 190 | return this.level.hasChunkAt(pos) ? LightCoordsUtil.getLightCoords(this.level, pos) : 15728640;
|
| 191 | }
|
| 192 |
|
| 193 | public boolean isAlive() {
|
| 194 | return !this.removed;
|
| 195 | }
|
| 196 |
|
| 197 | public AABB getBoundingBox() {
|
| 198 | return this.bb;
|
| 199 | }
|
| 200 |
|
| 201 | public void setBoundingBox(final AABB bb) {
|
| 202 | this.bb = bb;
|
| 203 | }
|
| 204 |
|
| 205 | public Optional<ParticleLimit> getParticleLimit() {
|
| 206 | return Optional.empty();
|
| 207 | }
|
| 208 |
|
| 209 | @Environment(EnvType.CLIENT)
|
| 210 | public record LifetimeAlpha(float startAlpha, float endAlpha, float startAtNormalizedAge, float endAtNormalizedAge) {
|
| 211 | public static final Particle.LifetimeAlpha ALWAYS_OPAQUE = new Particle.LifetimeAlpha(1.0F, 1.0F, 0.0F, 1.0F);
|
| 212 |
|
| 213 | public boolean isOpaque() {
|
| 214 | return this.startAlpha >= 1.0F && this.endAlpha >= 1.0F;
|
| 215 | }
|
| 216 |
|
| 217 | public float currentAlphaForAge(final int age, final int lifetime, final float partialTickTime) {
|
| 218 | if (Mth.equal(this.startAlpha, this.endAlpha)) {
|
| 219 | return this.startAlpha;
|
| 220 | }
|
| 221 |
|
| 222 | float timeNormalized = Mth.inverseLerp((age + partialTickTime) / lifetime, this.startAtNormalizedAge, this.endAtNormalizedAge);
|
| 223 | return Mth.clampedLerp(timeNormalized, this.startAlpha, this.endAlpha);
|
| 224 | }
|
| 225 | }
|
| 226 | }
|
| 227 |
|