-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticlePool.pde
48 lines (42 loc) · 1.21 KB
/
ParticlePool.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class ParticlePool {
public ArrayList<Particle> particles;
public ArrayList<Particle> recycler;
public PShape shape;
public int maxParticleNumber = 100;
public ParticlePool(PShape shape) {
particles = new ArrayList<Particle>();
recycler = new ArrayList<Particle>();
this.shape = shape;
}
public void update() {
pushMatrix();
for (Particle p : particles) {
p.update();
p.display();
}
popMatrix();
if (frameCount%12 == 0 && particles.size() < maxParticleNumber) {
float xOryBased = random(100);
if (xOryBased <= 50) {
particles.add(getParticle(width+100, random(0, height)));
} else {
particles.add(getParticle(random(0, width), -100));
}
}
for (int i = 0; i < particles.size(); i++) {
if (particles.get(i).needRecycle) {
recycler.add(particles.get(i));
particles.remove(i);
}
}
}
public Particle getParticle(float x, float y) {
if (recycler.size() == 0) {
return new Particle(x, y);
}
Particle recycledParticle = recycler.get(0);
recycler.remove(0);
recycledParticle.reset(x, y);
return recycledParticle;
}
}