Firework/ObjectDump.pde
2022-05-15 16:47:46 +02:00

101 lines
3.4 KiB
Plaintext

// Class to represent a 'Rocket' Object
class Rocket {
// Every Rocket needs a position, velocity and acceleration
ArrayList<Rocket> hA;
boolean exploded = false;
PVector pos, vel;
Flare children;
// Initialize the 'Rocket' Object
Rocket(ArrayList<Rocket> hostArray, PVector position) {
// Copy hostArray and position from argument, start at 'random' velocity
hA = hostArray;
pos = position.copy();
vel = new PVector(0, random((MAX_ROCKET_VELOCITY*5f)/8f, MAX_ROCKET_VELOCITY));
}
// Update Rocket and afterwards display Rocket or its Flare
void showNext() {
if (!exploded) {
// In this case, the Rocket is to be updated and shown
// Update position and accelerate towards 'ground'
vel.y -= DECELERATION_MAGNITUDE;
pos.sub(vel);
// Detect if reached highest point, then 'explode', else show Rocket
if (vel.y <= 0) {
exploded = true;
children = new Flare(this, pos, round(random(255)));
} else {
// Set stroke and fill color for Rockets to white
stroke(255);
fill(255);
ellipse(pos.x, pos.y, ROCKET_DIAMETER, ROCKET_DIAMETER);
}
} else {
// In this case, the Flare is to be updated and shown
children.showNext();
}
}
}
// Class to represent all Particles of a Rockets' Flare
class Flare {
// Every Flare needs to contain each Particles and have a color and a lifespan
ArrayList<Particle> bits = new ArrayList<Particle>();
int lifetime = 0, hc, maxtime = round(random(MAX_FLARE_LIFETIME/2f, 1.5*MAX_FLARE_LIFETIME));
Rocket par;
// Initialize the 'Flare' Object
Flare(Rocket parent, PVector position, int flareHue) {
// Copy parent, color and position from argument to Particles
par = parent;
for (int i = 0; i < AMOUNT_OF_PARTICLES; i++) {
bits.add(new Particle(position));
}
hc = flareHue;
}
// Update Flare and afterwards display its individual Particles
void showNext() {
// Set color of Partciles to Flares' own
int ac = round(map(lifetime, 0, maxtime, 255, 0));
stroke(hc, 255, 255, ac);
fill(hc, 255, 255, ac);
// Keep track of any inbound Particles, if none found, delete Rocket
boolean inBound = false;
// Update and show Particles
for (int i = 0; i < bits.size(); i++) {
Particle b = bits.get(i);
b.showNext();
if (b.pos.y <= height-ROCKET_DIAMETER/2f) {
inBound = true;
}
}
// Delete Rocket from either Array, depending on if it's user-made or not
if (!inBound) {
par.hA.remove(par);
}
lifetime++;
}
}
// Class to represent an individual Particle
class Particle {
PVector pos, vel;
// Initialize the 'Particle' Object
Particle(PVector position) {
// Copy position from argument, start at random 2D velocity with magnitude of MAX_FLARE_VELOCITY
pos = position.copy();
vel = PVector.random2D();
vel.setMag(random(MAX_FLARE_VELOCITY));
}
// Update Particle and display it afterwards
void showNext() {
vel.y += DECELERATION_MAGNITUDE;
pos.add(vel);
ellipse(pos.x, pos.y, PARTICLE_DIAMETER, PARTICLE_DIAMETER);
}
}