forked from CodingTrain/website-archive
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ParticleSystem.pde
63 lines (53 loc) · 1.4 KB
/
ParticleSystem.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for:
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class Firework {
ArrayList<Particle> particles; // An arraylist for all the particles
Particle firework;
float hu;
Firework() {
hu = random(255);
firework = new Particle(random(-width/2, width/2), height/2, random(-800, 800), hu);
particles = new ArrayList<Particle>(); // Initialize the arraylist
}
boolean done() {
if (firework == null && particles.isEmpty()) {
return true;
} else {
return false;
}
}
void run() {
if (firework != null) {
fill(hu,255,255);
firework.applyForce(gravity);
firework.update();
firework.display();
if (firework.explode()) {
for (int i = 0; i < 750; i++) {
particles.add(new Particle(firework.location, hu)); // Add "num" amount of particles to the arraylist
}
firework = null;
}
}
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.applyForce(gravity);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
// A method to test if the particle system still has particles
boolean dead() {
if (particles.isEmpty()) {
return true;
} else {
return false;
}
}
}