initial commit

This commit is contained in:
Baipyrus 2022-05-15 16:47:46 +02:00
commit 7978e521c4
2 changed files with 175 additions and 0 deletions

101
ObjectDump.pde Normal file
View File

@ -0,0 +1,101 @@
// 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);
}
}

74
newestFirework.pde Normal file
View File

@ -0,0 +1,74 @@
/*
Idea: Just a Firework "animation"
- Create an Object that flies upward from the bottom of the screen
- Make said Object explode after reaching some turning point
- Maybe make that explosion out of even more Objects that fly outward from their origin
- Create an Explosion on the current Mouse Position when Mouse is Pressed?
### Ignore the filename, it's because I have done this before but it's been years and I don't want to delete the old code. ###
*/
// Section for constant variables:
final int AMOUNT_OF_ROCKETS = 20;
final float ROCKET_DIAMETER = 10f;
final int MAX_FLARE_LIFETIME = 130;
final int AMOUNT_OF_PARTICLES = 75;
final float PARTICLE_DIAMETER = 7.5;
final float MAX_FLARE_VELOCITY = 2.125;
final float MAX_ROCKET_VELOCITY = 10.35;
final float DECELERATION_MAGNITUDE = 0.05;
// Section for global variables:
ArrayList<Rocket> firework = new ArrayList<Rocket>();
ArrayList<Rocket> userInputs = new ArrayList<Rocket>();
// Function called when program is started
void setup() {
// Initialize full-screen window
fullScreen();
colorMode(HSB);
textAlign(LEFT, TOP);
textSize(24);
for (int i = 0; i < AMOUNT_OF_ROCKETS; i++) {
firework.add(new Rocket(firework, new PVector(random(width), height)));
}
}
// Function called every Frame at <frameRate>
void draw() {
// Set black background
background(0);
// Display Framerate, fixed in the top left corner in white
noStroke();
fill(255);
text("FPS: "+round(frameRate), 0, 0);
// Loop backwards through all Rockets, update their position, remove if out of bounds, then show them
for (int i = firework.size()-1; i >= 0; i--) {
Rocket r = firework.get(i);
r.showNext();
}
for (int i = userInputs.size()-1; i >= 0; i--) {
Rocket r = userInputs.get(i);
r.showNext();
}
// If any Rockets got deleted from main Array 'firework', add new random ones
while (firework.size() < AMOUNT_OF_ROCKETS) {
firework.add(new Rocket(firework, new PVector(random(width), height)));
}
}
// Function called when a user presses any mouse button inside of the window
void mousePressed() {
// Create Rocket at current mouse position, make it explode instantly and add it to special array
Rocket r = new Rocket(userInputs, new PVector(mouseX, mouseY));
r.vel.y = 0;
userInputs.add(r);
}