-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirework.js
More file actions
54 lines (46 loc) · 1.33 KB
/
firework.js
File metadata and controls
54 lines (46 loc) · 1.33 KB
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
class Firework {
constructor(x, vel) {
this.color = random(colors)
this.firework = new Particle(x, height, true, this.color, vel);
this.state = false; //this.state = false means firework has not exploded yet.
this.particles = [];
}
update() {
if (!this.state) {
this.firework.applyForce(gravity);
this.firework.update();
if (this.firework.vel.y >=random(0, 2)) {
this.state = true;
this.explode();
}
}
for (var i=0; i<this.particles.length; i++) {
this.particles[i].applyForce(gravity);
this.particles[i].update();
if (this.particles[i].done()) {
this.particles.splice(i, 1);
}
}
}
explode() {
for (var i=0; i<30; i++) {
var particle = new Particle(this.firework.pos.x, this.firework.pos.y, false, this.color, random(40, -40));
this.particles.push(particle);
}
}
done() {
if(this.state && this.particles.length == 0) {
return true;
} else {
return false;
}
}
show() {
if (!this.state) {
this.firework.show();
}
for (var i=0; i<this.particles.length; i++) {
this.particles[i].show();
}
}
}