-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles.js
76 lines (60 loc) · 1.67 KB
/
particles.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
"use strict";
class Particle {
constructor(x, y, r, v, color) {
this.radius = r;
this.friction = 0.99;
this.gravity = 0.02;
this.color = color;
this.opacity = 0.9;
this.xVel = v.x;
this.yVel = v.y;
this.position = {
x: x,
y: y
}
this.rocketDamage = 3;
}
draw(ctx) {
ctx.save();
ctx.globalAlpha = this.opacity;
ctx.beginPath();
ctx.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2, false);
ctx.fillStyle = this.color
ctx.fill();
ctx.closePath();
ctx.restore();
}
update() {
this.xVel *= this.friction;
this.yVel *= this.friction;
this.yVel += this.gravity;
this.position.x += this.xVel;
this.position.y += this.yVel;
this.opacity -= 0.004;
}
checkCollision(enemyX, enemyY, enemyRadius) {
let xDist = Math.abs(enemyX - this.position.x);
let yDist = Math.abs(enemyY - this.position.y);
let totalRadii = enemyRadius + this.radius;
if (Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2)) <= totalRadii) return true;
else return false;
}
}
function createParticles(x, y, num, color) {
let rads = (Math.PI * 2) / num;
for (let particleIndex = 0; particleIndex < num; particleIndex++) {
let velocity = {
x: Math.cos(rads * particleIndex) * (Math.random() * particlePower),
y: Math.sin(rads * particleIndex) * (Math.random() * particlePower)
}
particles.push(new Particle(x, y, 4, velocity, color));
}
}
function checkLivingParticles() {
particles.forEach((particle, index) => {
if (particle.opacity > 0) {
particle.draw(ctx);
particle.update();
} else particles.splice(index, 1);
});
}