/* Our particle system is a simple collection of a variable number of Particle objects. The particle system provides a few utility methods to easily run simulations on all of the particles */ class ParticleSystem { // The list of particles ArrayList particles; // The source of the particle system Vector3D origin; ParticleSystem(int num, Vector3D origin_) { // Set the particles array particles = new ArrayList(); // Save the center of the simulation origin = origin_.copy(); // Create all of the starting particles for (int i = 0; i < num; i++) { particles.add(new Particle(origin)); } } void simulate() { /* This simulate method is a bit different than what we're used too. Because particles in our system can die out, we need to remove particles that are 'dead'. To make this efficient we want to iterate over the list of particles once to run the simualtion, removing those that are already 'dead'. To do this, we count backwards through our array, so that when we remove a particle (element) from the array, we don't change the order or position of elements we haven't yet processed. */ for (int i = particles.size() - 1; i >= 0; i--) { // Get our particle; we need to cast the particle into its type Particle p = (Particle)particles.get(i); // Run the simulation p.simulate(); // Remove any dead particles if (p.isDead()) { particles.remove(i); } } } void addParticle() { particles.add(new Particle(origin)); } void addParticle(Particle p) { particles.add(p); } boolean isDead() { if (particles.isEmpty()) { return true; } else { return false; } } }