| 1 | package net.minecraft.client.particle;
|
| 2 |
|
| 3 | import java.util.ArrayDeque;
|
| 4 | import java.util.Iterator;
|
| 5 | import java.util.Queue;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.CrashReport;
|
| 9 | import net.minecraft.CrashReportCategory;
|
| 10 | import net.minecraft.ReportedException;
|
| 11 | import net.minecraft.client.Camera;
|
| 12 | import net.minecraft.client.renderer.culling.Frustum;
|
| 13 | import net.minecraft.client.renderer.state.level.ParticleGroupRenderState;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public abstract class ParticleGroup<P extends Particle> {
|
| 17 | private static final int MAX_PARTICLES = 16384;
|
| 18 | private static final int RESERVOIR_SIZE = 4096;
|
| 19 | private static final int RESERVOIR_START = 12288;
|
| 20 | protected final ParticleEngine engine;
|
| 21 | protected final Queue<P> particles = new ArrayDeque<>(16384);
|
| 22 |
|
| 23 | public ParticleGroup(final ParticleEngine engine) {
|
| 24 | this.engine = engine;
|
| 25 | }
|
| 26 |
|
| 27 | public boolean isEmpty() {
|
| 28 | return this.particles.isEmpty();
|
| 29 | }
|
| 30 |
|
| 31 | public void tickParticles() {
|
| 32 | if (!this.particles.isEmpty()) {
|
| 33 | Iterator<P> iterator = this.particles.iterator();
|
| 34 |
|
| 35 | while (iterator.hasNext()) {
|
| 36 | P particle = iterator.next();
|
| 37 | this.tickParticle(particle);
|
| 38 | if (!particle.isAlive()) {
|
| 39 | particle.getParticleLimit().ifPresent(options -> this.engine.updateCount(options, -1));
|
| 40 | iterator.remove();
|
| 41 | }
|
| 42 | }
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | private void tickParticle(final Particle particle) {
|
| 47 | try {
|
| 48 | particle.tick();
|
| 49 | } catch (Throwable t) {
|
| 50 | CrashReport report = CrashReport.forThrowable(t, "Ticking Particle");
|
| 51 | CrashReportCategory category = report.addCategory("Particle being ticked");
|
| 52 | category.setDetail("Particle", particle::toString);
|
| 53 | category.setDetail("Particle Type", particle.getGroup()::toString);
|
| 54 | throw new ReportedException(report);
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | public boolean add(final Particle particle) {
|
| 59 | int currentSize = this.particles.size();
|
| 60 | if (currentSize >= 16384) {
|
| 61 | return false;
|
| 62 | }
|
| 63 |
|
| 64 | if (currentSize >= 12288) {
|
| 65 | float freeSpace = (16384 - currentSize) / 4096.0F;
|
| 66 | if (this.engine.getRandom().nextFloat() >= freeSpace * freeSpace) {
|
| 67 | return false;
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | this.particles.add((P)particle);
|
| 72 | return true;
|
| 73 | }
|
| 74 |
|
| 75 | public int size() {
|
| 76 | return this.particles.size();
|
| 77 | }
|
| 78 |
|
| 79 | public abstract ParticleGroupRenderState extractRenderState(Frustum frustum, Camera camera, float partialTickTime);
|
| 80 | }
|
| 81 |
|