import processing.core.PApplet;
public class Fireworks extends PApplet
{
Firework [] fs = new Firework[10];
boolean once;
PApplet parent;
public void setup(){
size(400,400);
smooth();
for (int i = 0; i < fs.length; i++){
fs[i] = new Firework();
}
}
public void draw()
{
noStroke();
fill(50,0,40,20);
rect(0,0,width,height);
for (int i = 0; i < fs.length; i++){
fs[i].draw();
}
}
public void mouseReleased(){
once = false;
for (int i = 0; i < fs.length; i++){
if((fs[i].hidden)&&(!once)){
fs[i].launch();
once = true;
}
}
}
public class Firework<color>{
float x, y, oldX,oldY, ySpeed, targetX, targetY, explodeTimer, flareWeight, flareAngle;
int flareAmount, duration;
boolean launched,exploded,hidden;
int flare;
Firework(){
launched = false;
exploded = false;
hidden = true;
}
public void draw(){
if((launched)&&(!exploded)&&(!hidden)){
launchMaths();
strokeWeight(1);
stroke(255);
line(x,y,oldX,oldY);
}
if((!launched)&&(exploded)&&(!hidden)){
explodeMaths();
noStroke();
strokeWeight(flareWeight);
stroke((Integer) flare);
for(int i = 0; i < flareAmount + 1; i++){
pushMatrix();
translate(x,y);
point(sin(radians(i*flareAngle))*explodeTimer,cos(radians(i*flareAngle))*explodeTimer);
popMatrix();
}
}
if((!launched)&&(!exploded)&&(hidden)){
//do nothing
}
}
public void launch(){
x = oldX = mouseX;
y = oldY = height;
//mouseX = left mouse button
//mouseY = right mouse button
targetX = mouseX;
targetY = mouseY;
ySpeed =2;
flare = color(random(3)*50 + 105,random(3)*50 + 105,random(3)*50 + 105);
flareAmount = ceil(random(30)) + 20;
flareWeight = ceil(random(3));
duration = ceil(random(4))*20 + 30;
//makes explosion a full circle
flareAngle = 360/flareAmount;
launched = true;
exploded = false;
hidden = false;
}
public void launchMaths(){
oldX = x;
oldY = y;
if(dist(x,y,targetX,targetY) > 6){
x += (targetX - x)/2;
y += -ySpeed;
}else{
explode();
}
}
public void explode(){
explodeTimer = 3;
launched = false;
exploded = true;
hidden = false;
}
public void explodeMaths() {
if(explodeTimer < duration){
explodeTimer+= 2;
}else
{
hide();
}
}
public void hide()
{
launched = false;
exploded = false;
hidden = true;
}
}
}
1