-
Notifications
You must be signed in to change notification settings - Fork 1
/
FireWorks.pde
98 lines (92 loc) · 2.17 KB
/
FireWorks.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Particle{
private PVector startPos;
private PVector endPos;
private PVector currPos;
color co;
Particle(){
startPos = new PVector();
endPos = new PVector();
currPos = new PVector();
co=color(random(0,255),random(0,255),random(0,255));
}
void resetParticle(float x, float y){
this.startPos.set(x,y,0);
this.currPos.set(x,y,0);
}
void setStartPos(float x, float y){
startPos.set(x,y,0);
currPos.set(x,y,0);
}
void setEndPos(float x, float y){
endPos.set(x,y,0);
}
void setColour(float r,float g, float b, float a){
co=color(r,g,b,a);
}
void setColour(color c){
co=c;
}
void update(float normalizedValue){
currPos.x = currPos.x + (endPos.x * normalizedValue);
currPos.y = currPos.y + (endPos.y * normalizedValue);
}
void draw(){
strokeWeight(3);
stroke(co);
point(currPos.x, currPos.y);
}
}
class FireWorks{
ArrayList points;
float lastUpdate;
PVector position;
float explosionDuration;
float lastExplosion;
color co;
boolean isRandomColour;
FireWorks(){
points=new ArrayList();
for(float i = 0; i < PI * 2; i += 0.05){
Particle p = new Particle();
points.add(p);
}
explosionDuration=random(5000,8000);
isRandomColour=true;
reset();
}
void reset(){
float x=random(100, width-100);
float y=random(100, height-100);
float j=0;
if(isRandomColour)
co=color(random(0,255),random(0,255),random(0,255));
for(int i=0;i<points.size();i++){
Particle p=(Particle)points.get(i);
p.setColour(co);
p.setStartPos(x + cos(j) * 5.0f, y + sin(j) * 5.0f);
float v=min(width,height)/2;
v=random(20,v);
p.setEndPos(cos(j)*v,sin(j)*v);
j+=0.05;
}
lastUpdate=millis();
lastExplosion=millis();
}
void setColour(float r,float g, float b){
co=color(r,g,b);
isRandomColour=false;
}
void draw(){
float now=millis();
float dv=lastUpdate-now;
lastUpdate=now;
for(int i = 0; i < points.size(); i++){
Particle p = (Particle)points.get(i);
p.update(dv/1000);
p.draw();
}
if(now-lastExplosion > explosionDuration){
reset();
}
}
}