mirror of
https://github.com/processing/processing4.git
synced 2026-01-30 11:51:54 +01:00
50 lines
1.4 KiB
Plaintext
50 lines
1.4 KiB
Plaintext
// An ArrayList is used to manage the list of Particles
|
|
// An Iterator is used to remove "dead" particles while iterating over the list
|
|
import java.util.Iterator;
|
|
|
|
class ParticleSystem {
|
|
|
|
ArrayList<Particle> particles; // An arraylist for all the particles
|
|
PVector origin; // An origin point for where particles are birthed
|
|
|
|
ParticleSystem(int num, PVector v) {
|
|
particles = new ArrayList<Particle>(); // Initialize the arraylist
|
|
origin = v.get(); // Store the origin point
|
|
for (int i = 0; i < num; i++) {
|
|
particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist
|
|
}
|
|
}
|
|
|
|
void run() {
|
|
// Cycle through the ArrayList using Iterator, because we are deleting while iterating
|
|
Iterator<Particle> it = particles.iterator();
|
|
while (it.hasNext()) {
|
|
Particle p = it.next();
|
|
p.run();
|
|
if (p.isDead()) {
|
|
it.remove();
|
|
}
|
|
}
|
|
}
|
|
|
|
void addParticle() {
|
|
Particle p;
|
|
// Add either a Particle or CrazyParticle to the system
|
|
if (int(random(0, 2)) == 0) {
|
|
p = new Particle(origin);
|
|
} else {
|
|
p = new CrazyParticle(origin);
|
|
}
|
|
particles.add(p);
|
|
}
|
|
|
|
void addParticle(Particle p) {
|
|
particles.add(p);
|
|
}
|
|
|
|
// A method to test if the particle system still has particles
|
|
boolean dead() {
|
|
return particles.isEmpty();
|
|
}
|
|
|
|
} |