-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexplosion.js
88 lines (85 loc) · 1.86 KB
/
explosion.js
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
//explosion particle code for the main game...
/*
author : Vighnesh SK
*/
var particleArray = [];
function Particle() {
this.x_pos = 0;
this.y_pos = 0;
this.x_vel = 0;
this.y_vel = 0;
this.angle = 0;
this.life = 100;
this.size = 2;
this.draw = function draw() {
context.fillStyle = "rgba(255, 255, 255, 0.5)";
context.beginPath();
context.arc(this.x_pos, this.y_pos, this.size, 0, 2*Math.PI, true);
context.closePath();
context.fill();
}
this.update = function update() {
this.draw();
this.x_pos += this.x_vel;
this.y_pos += this.y_vel;
}
}
//explosion particle effect
function Explode(x, y) {
var count = 50;
for (i=0; i<count; i++) {
p = new Particle();
p.x_pos = x;
p.y_pos = y;
p.life = 200*Math.random();
p.angle = (360/count)*i*Math.PI/180;
p.x_vel = (0.2+1*Math.random())*Math.cos(p.angle);
p.y_vel = (0.2+1*Math.random())*Math.sin(p.angle);
particleArray.push(p);
}
this.update = function update() {
for (i=0; i<particleArray.length; i++) {
particle = particleArray[i];
if (particle===undefined) {
continue;
}
particle.update();
if (particleArray[i].life<0) {
delete particleArray[i];
}
else {
particleArray[i].life -= 1;
}
}
}
}
//experimental thrust particle effect
function thrust(x, y, angle) {
var count = 10;
for (i=0; i<count; i++) {
p = new Particle();
p.size = 1;
p.x_pos = x;
p.y_pos = y;
p.angle = angle;
p.life = 10;
p.x_vel = (12+5*Math.random())*Math.cos(p.angle*Math.PI/180);
p.y_vel = (12+5*Math.random())*Math.sin(p.angle*Math.PI/180);
particleArray.push(p);
}
this.update = function update() {
for (i=0; i<particleArray.length; i++) {
particle = particleArray[i];
if (particle===undefined) {
continue;
}
particle.update();
if (particleArray[i].life<0) {
delete particleArray[i];
}
else {
particleArray[i].life -= 1;
}
}
}
}