| 1 | package net.minecraft.client.particle;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Camera;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 10 | import net.minecraft.client.renderer.state.level.QuadParticleRenderState;
|
| 11 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 12 | import net.minecraft.core.particles.ColorParticleOption;
|
| 13 | import net.minecraft.core.particles.ParticleTypes;
|
| 14 | import net.minecraft.core.particles.SimpleParticleType;
|
| 15 | import net.minecraft.sounds.SoundEvent;
|
| 16 | import net.minecraft.sounds.SoundEvents;
|
| 17 | import net.minecraft.sounds.SoundSource;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.util.RandomSource;
|
| 20 | import net.minecraft.util.Util;
|
| 21 | import net.minecraft.world.item.DyeColor;
|
| 22 | import net.minecraft.world.item.component.FireworkExplosion;
|
| 23 | import net.minecraft.world.item.component.FireworkExplosion.Shape;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class FireworkParticles {
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public static class FlashProvider implements ParticleProvider<ColorParticleOption> {
|
| 29 | private final SpriteSet sprite;
|
| 30 |
|
| 31 | public FlashProvider(final SpriteSet sprite) {
|
| 32 | this.sprite = sprite;
|
| 33 | }
|
| 34 |
|
| 35 | public Particle createParticle(
|
| 36 | final ColorParticleOption options,
|
| 37 | final ClientLevel level,
|
| 38 | final double x,
|
| 39 | final double y,
|
| 40 | final double z,
|
| 41 | final double xAux,
|
| 42 | final double yAux,
|
| 43 | final double zAux,
|
| 44 | final RandomSource random
|
| 45 | ) {
|
| 46 | FireworkParticles.OverlayParticle particle = new FireworkParticles.OverlayParticle(level, x, y, z, this.sprite.get(random));
|
| 47 | particle.setColor(options.getRed(), options.getGreen(), options.getBlue());
|
| 48 | particle.setAlpha(options.getAlpha());
|
| 49 | return particle;
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | @Environment(EnvType.CLIENT)
|
| 54 | public static class OverlayParticle extends SingleQuadParticle {
|
| 55 | private OverlayParticle(final ClientLevel level, final double x, final double y, final double z, final TextureAtlasSprite sprite) {
|
| 56 | super(level, x, y, z, sprite);
|
| 57 | this.lifetime = 4;
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public SingleQuadParticle.Layer getLayer() {
|
| 62 | return SingleQuadParticle.Layer.TRANSLUCENT;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void extract(final QuadParticleRenderState particleTypeRenderState, final Camera camera, final float partialTickTime) {
|
| 67 | this.setAlpha(0.6F - (this.age + partialTickTime - 1.0F) * 0.25F * 0.5F);
|
| 68 | super.extract(particleTypeRenderState, camera, partialTickTime);
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public float getQuadSize(final float a) {
|
| 73 | return 7.1F * Mth.sin((this.age + a - 1.0F) * 0.25F * (float) Math.PI);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | @Environment(EnvType.CLIENT)
|
| 78 | private static class SparkParticle extends SimpleAnimatedParticle {
|
| 79 | private boolean trail;
|
| 80 | private boolean twinkle;
|
| 81 | private final ParticleEngine engine;
|
| 82 | private float fadeR;
|
| 83 | private float fadeG;
|
| 84 | private float fadeB;
|
| 85 | private boolean hasFade;
|
| 86 |
|
| 87 | private SparkParticle(
|
| 88 | final ClientLevel level,
|
| 89 | final double x,
|
| 90 | final double y,
|
| 91 | final double z,
|
| 92 | final double xa,
|
| 93 | final double ya,
|
| 94 | final double za,
|
| 95 | final ParticleEngine engine,
|
| 96 | final SpriteSet sprites
|
| 97 | ) {
|
| 98 | super(level, x, y, z, sprites, 0.1F);
|
| 99 | this.xd = xa;
|
| 100 | this.yd = ya;
|
| 101 | this.zd = za;
|
| 102 | this.engine = engine;
|
| 103 | this.quadSize *= 0.75F;
|
| 104 | this.lifetime = 48 + this.random.nextInt(12);
|
| 105 | this.setSpriteFromAge(sprites);
|
| 106 | }
|
| 107 |
|
| 108 | public void setTrail(final boolean trail) {
|
| 109 | this.trail = trail;
|
| 110 | }
|
| 111 |
|
| 112 | public void setTwinkle(final boolean twinkle) {
|
| 113 | this.twinkle = twinkle;
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public void extract(final QuadParticleRenderState particleTypeRenderState, final Camera camera, final float partialTickTime) {
|
| 118 | if (!this.twinkle || this.age < this.lifetime / 3 || (this.age + this.lifetime) / 3 % 2 == 0) {
|
| 119 | super.extract(particleTypeRenderState, camera, partialTickTime);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public void tick() {
|
| 125 | super.tick();
|
| 126 | if (this.trail && this.age < this.lifetime / 2 && (this.age + this.lifetime) % 2 == 0) {
|
| 127 | FireworkParticles.SparkParticle sparkParticle = new FireworkParticles.SparkParticle(
|
| 128 | this.level, this.x, this.y, this.z, 0.0, 0.0, 0.0, this.engine, this.sprites
|
| 129 | );
|
| 130 | sparkParticle.setAlpha(0.99F);
|
| 131 | sparkParticle.setColor(this.rCol, this.gCol, this.bCol);
|
| 132 | sparkParticle.age = sparkParticle.lifetime / 2;
|
| 133 | if (this.hasFade) {
|
| 134 | sparkParticle.hasFade = true;
|
| 135 | sparkParticle.fadeR = this.fadeR;
|
| 136 | sparkParticle.fadeG = this.fadeG;
|
| 137 | sparkParticle.fadeB = this.fadeB;
|
| 138 | }
|
| 139 |
|
| 140 | sparkParticle.twinkle = this.twinkle;
|
| 141 | this.engine.add(sparkParticle);
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | @Environment(EnvType.CLIENT)
|
| 147 | public static class SparkProvider implements ParticleProvider<SimpleParticleType> {
|
| 148 | private final SpriteSet sprites;
|
| 149 |
|
| 150 | public SparkProvider(final SpriteSet sprites) {
|
| 151 | this.sprites = sprites;
|
| 152 | }
|
| 153 |
|
| 154 | public Particle createParticle(
|
| 155 | final SimpleParticleType options,
|
| 156 | final ClientLevel level,
|
| 157 | final double x,
|
| 158 | final double y,
|
| 159 | final double z,
|
| 160 | final double xAux,
|
| 161 | final double yAux,
|
| 162 | final double zAux,
|
| 163 | final RandomSource random
|
| 164 | ) {
|
| 165 | FireworkParticles.SparkParticle particle = new FireworkParticles.SparkParticle(
|
| 166 | level, x, y, z, xAux, yAux, zAux, Minecraft.getInstance().particleEngine, this.sprites
|
| 167 | );
|
| 168 | particle.setAlpha(0.99F);
|
| 169 | return particle;
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | @Environment(EnvType.CLIENT)
|
| 174 | public static class Starter extends NoRenderParticle {
|
| 175 | private static final double[][] CREEPER_PARTICLE_COORDS = new double[][]{
|
| 176 | {0.0, 0.2}, {0.2, 0.2}, {0.2, 0.6}, {0.6, 0.6}, {0.6, 0.2}, {0.2, 0.2}, {0.2, 0.0}, {0.4, 0.0}, {0.4, -0.6}, {0.2, -0.6}, {0.2, -0.4}, {0.0, -0.4}
|
| 177 | };
|
| 178 | private static final double[][] STAR_PARTICLE_COORDS = new double[][]{
|
| 179 | {0.0, 1.0},
|
| 180 | {0.3455, 0.309},
|
| 181 | {0.9511, 0.309},
|
| 182 | {0.3795918367346939, -0.12653061224489795},
|
| 183 | {0.6122448979591837, -0.8040816326530612},
|
| 184 | {0.0, -0.35918367346938773}
|
| 185 | };
|
| 186 | private final boolean playSound;
|
| 187 | private int life;
|
| 188 | private final ParticleEngine engine;
|
| 189 | private final List<FireworkExplosion> explosions;
|
| 190 | private boolean twinkleDelay;
|
| 191 |
|
| 192 | public Starter(
|
| 193 | final ClientLevel level,
|
| 194 | final double x,
|
| 195 | final double y,
|
| 196 | final double z,
|
| 197 | final double xd,
|
| 198 | final double yd,
|
| 199 | final double zd,
|
| 200 | final ParticleEngine engine,
|
| 201 | final List<FireworkExplosion> explosions,
|
| 202 | final boolean playSound
|
| 203 | ) {
|
| 204 | super(level, x, y, z);
|
| 205 | this.xd = xd;
|
| 206 | this.yd = yd;
|
| 207 | this.zd = zd;
|
| 208 | this.engine = engine;
|
| 209 | this.playSound = playSound;
|
| 210 | if (explosions.isEmpty()) {
|
| 211 | throw new IllegalArgumentException("Cannot create firework starter with no explosions");
|
| 212 | }
|
| 213 |
|
| 214 | this.explosions = explosions;
|
| 215 | this.lifetime = explosions.size() * 2 - 1;
|
| 216 |
|
| 217 | for (FireworkExplosion explosion : explosions) {
|
| 218 | if (explosion.hasTwinkle()) {
|
| 219 | this.twinkleDelay = true;
|
| 220 | this.lifetime += 15;
|
| 221 | break;
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | @Override
|
| 227 | public void tick() {
|
| 228 | if (this.life == 0 && this.playSound) {
|
| 229 | boolean farEffect = this.isFarAwayFromCamera();
|
| 230 | boolean largeExplosion = false;
|
| 231 | if (this.explosions.size() >= 3) {
|
| 232 | largeExplosion = true;
|
| 233 | } else {
|
| 234 | for (FireworkExplosion explosion : this.explosions) {
|
| 235 | if (explosion.shape() == Shape.LARGE_BALL) {
|
| 236 | largeExplosion = true;
|
| 237 | break;
|
| 238 | }
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 | SoundEvent sound;
|
| 243 | if (largeExplosion) {
|
| 244 | sound = farEffect ? SoundEvents.FIREWORK_ROCKET_LARGE_BLAST_FAR : SoundEvents.FIREWORK_ROCKET_LARGE_BLAST;
|
| 245 | } else {
|
| 246 | sound = farEffect ? SoundEvents.FIREWORK_ROCKET_BLAST_FAR : SoundEvents.FIREWORK_ROCKET_BLAST;
|
| 247 | }
|
| 248 |
|
| 249 | this.level.playLocalSound(this.x, this.y, this.z, sound, SoundSource.AMBIENT, 20.0F, 0.95F + this.random.nextFloat() * 0.1F, true);
|
| 250 | }
|
| 251 |
|
| 252 | if (this.life % 2 == 0 && this.life / 2 < this.explosions.size()) {
|
| 253 | int eIndex = this.life / 2;
|
| 254 | FireworkExplosion explosion = this.explosions.get(eIndex);
|
| 255 | boolean trail = explosion.hasTrail();
|
| 256 | boolean twinkle = explosion.hasTwinkle();
|
| 257 | IntList colors = explosion.colors();
|
| 258 | IntList fadeColors = explosion.fadeColors();
|
| 259 | if (colors.isEmpty()) {
|
| 260 | colors = IntList.of(DyeColor.BLACK.getFireworkColor());
|
| 261 | }
|
| 262 |
|
| 263 | switch (explosion.shape()) {
|
| 264 | case SMALL_BALL:
|
| 265 | this.createParticleBall(0.25, 2, colors, fadeColors, trail, twinkle);
|
| 266 | break;
|
| 267 | case LARGE_BALL:
|
| 268 | this.createParticleBall(0.5, 4, colors, fadeColors, trail, twinkle);
|
| 269 | break;
|
| 270 | case STAR:
|
| 271 | this.createParticleShape(0.5, STAR_PARTICLE_COORDS, colors, fadeColors, trail, twinkle, false);
|
| 272 | break;
|
| 273 | case CREEPER:
|
| 274 | this.createParticleShape(0.5, CREEPER_PARTICLE_COORDS, colors, fadeColors, trail, twinkle, true);
|
| 275 | break;
|
| 276 | case BURST:
|
| 277 | this.createParticleBurst(colors, fadeColors, trail, twinkle);
|
| 278 | }
|
| 279 |
|
| 280 | int color = colors.getInt(0);
|
| 281 | this.engine.createParticle(ColorParticleOption.create(ParticleTypes.FLASH, color), this.x, this.y, this.z, 0.0, 0.0, 0.0);
|
| 282 | }
|
| 283 |
|
| 284 | this.life++;
|
| 285 | if (this.life > this.lifetime) {
|
| 286 | if (this.twinkleDelay && this.playSound) {
|
| 287 | boolean farEffect = this.isFarAwayFromCamera();
|
| 288 | SoundEvent sound = farEffect ? SoundEvents.FIREWORK_ROCKET_TWINKLE_FAR : SoundEvents.FIREWORK_ROCKET_TWINKLE;
|
| 289 | this.level.playLocalSound(this.x, this.y, this.z, sound, SoundSource.AMBIENT, 20.0F, 0.9F + this.random.nextFloat() * 0.15F, true);
|
| 290 | }
|
| 291 |
|
| 292 | this.remove();
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | private boolean isFarAwayFromCamera() {
|
| 297 | Minecraft instance = Minecraft.getInstance();
|
| 298 | return instance.gameRenderer.mainCamera().position().distanceToSqr(this.x, this.y, this.z) >= 256.0;
|
| 299 | }
|
| 300 |
|
| 301 | private void createParticle(
|
| 302 | final double x,
|
| 303 | final double y,
|
| 304 | final double z,
|
| 305 | final double xa,
|
| 306 | final double ya,
|
| 307 | final double za,
|
| 308 | final IntList rgbColors,
|
| 309 | final IntList fadeColors,
|
| 310 | final boolean trail,
|
| 311 | final boolean twinkle
|
| 312 | ) {
|
| 313 | FireworkParticles.SparkParticle sparkParticle = (FireworkParticles.SparkParticle)this.engine.createParticle(ParticleTypes.FIREWORK, x, y, z, xa, ya, za);
|
| 314 | sparkParticle.setTrail(trail);
|
| 315 | sparkParticle.setTwinkle(twinkle);
|
| 316 | sparkParticle.setAlpha(0.99F);
|
| 317 | sparkParticle.setColor(Util.getRandom(rgbColors, this.random));
|
| 318 | if (!fadeColors.isEmpty()) {
|
| 319 | sparkParticle.setFadeColor(Util.getRandom(fadeColors, this.random));
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | private void createParticleBall(
|
| 324 | final double baseSpeed, final int steps, final IntList rgbColors, final IntList fadeColors, final boolean trail, final boolean twinkle
|
| 325 | ) {
|
| 326 | double xx = this.x;
|
| 327 | double yy = this.y;
|
| 328 | double zz = this.z;
|
| 329 |
|
| 330 | for (int yStep = -steps; yStep <= steps; yStep++) {
|
| 331 | for (int xStep = -steps; xStep <= steps; xStep++) {
|
| 332 | for (int zStep = -steps; zStep <= steps; zStep++) {
|
| 333 | double xa = xStep + (this.random.nextDouble() - this.random.nextDouble()) * 0.5;
|
| 334 | double ya = yStep + (this.random.nextDouble() - this.random.nextDouble()) * 0.5;
|
| 335 | double za = zStep + (this.random.nextDouble() - this.random.nextDouble()) * 0.5;
|
| 336 | double len = Math.sqrt(xa * xa + ya * ya + za * za) / baseSpeed + this.random.nextGaussian() * 0.05;
|
| 337 | this.createParticle(xx, yy, zz, xa / len, ya / len, za / len, rgbColors, fadeColors, trail, twinkle);
|
| 338 | if (yStep != -steps && yStep != steps && xStep != -steps && xStep != steps) {
|
| 339 | zStep += steps * 2 - 1;
|
| 340 | }
|
| 341 | }
|
| 342 | }
|
| 343 | }
|
| 344 | }
|
| 345 |
|
| 346 | private void createParticleShape(
|
| 347 | final double baseSpeed,
|
| 348 | final double[][] coords,
|
| 349 | final IntList rgbColors,
|
| 350 | final IntList fadeColors,
|
| 351 | final boolean trail,
|
| 352 | final boolean twinkle,
|
| 353 | final boolean flat
|
| 354 | ) {
|
| 355 | double sx = coords[0][0];
|
| 356 | double sy = coords[0][1];
|
| 357 | this.createParticle(this.x, this.y, this.z, sx * baseSpeed, sy * baseSpeed, 0.0, rgbColors, fadeColors, trail, twinkle);
|
| 358 | float baseAngle = this.random.nextFloat() * (float) Math.PI;
|
| 359 | double angleMod = flat ? 0.034 : 0.34;
|
| 360 |
|
| 361 | for (int angleStep = 0; angleStep < 3; angleStep++) {
|
| 362 | double angle = baseAngle + angleStep * (float) Math.PI * angleMod;
|
| 363 | double ox = sx;
|
| 364 | double oy = sy;
|
| 365 |
|
| 366 | for (int c = 1; c < coords.length; c++) {
|
| 367 | double tx = coords[c][0];
|
| 368 | double ty = coords[c][1];
|
| 369 |
|
| 370 | for (double subStep = 0.25; subStep <= 1.0; subStep += 0.25) {
|
| 371 | double xa = Mth.lerp(subStep, ox, tx) * baseSpeed;
|
| 372 | double ya = Mth.lerp(subStep, oy, ty) * baseSpeed;
|
| 373 | double za = xa * Math.sin(angle);
|
| 374 | xa *= Math.cos(angle);
|
| 375 |
|
| 376 | for (double flip = -1.0; flip <= 1.0; flip += 2.0) {
|
| 377 | this.createParticle(this.x, this.y, this.z, xa * flip, ya, za * flip, rgbColors, fadeColors, trail, twinkle);
|
| 378 | }
|
| 379 | }
|
| 380 |
|
| 381 | ox = tx;
|
| 382 | oy = ty;
|
| 383 | }
|
| 384 | }
|
| 385 | }
|
| 386 |
|
| 387 | private void createParticleBurst(final IntList rgbColors, final IntList fadeColors, final boolean trail, final boolean twinkle) {
|
| 388 | double baseOffX = this.random.nextGaussian() * 0.05;
|
| 389 | double baseOffZ = this.random.nextGaussian() * 0.05;
|
| 390 |
|
| 391 | for (int i = 0; i < 70; i++) {
|
| 392 | double xa = this.xd * 0.5 + this.random.nextGaussian() * 0.15 + baseOffX;
|
| 393 | double za = this.zd * 0.5 + this.random.nextGaussian() * 0.15 + baseOffZ;
|
| 394 | double ya = this.yd * 0.5 + this.random.nextDouble() * 0.5;
|
| 395 | this.createParticle(this.x, this.y, this.z, xa, ya, za, rgbColors, fadeColors, trail, twinkle);
|
| 396 | }
|
| 397 | }
|
| 398 | }
|
| 399 | }
|
| 400 |
|