-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparticle_class.js
83 lines (74 loc) · 1.9 KB
/
particle_class.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
class ParticleClass {
constructor() {
this.particles = [];
for (let i = 0; i < 500; i++) {
this.particles.push(new ParticleObject());
}
}
drawParticles(drawingContext, cameraClass) {
for (let i = 0; i < this.particles.length; i++) {
if (this.particles[i].active === true) {
this.particles[i].draw(
drawingContext,
-cameraClass.getViewboxLeft(),
-cameraClass.getViewboxTop()
);
}
}
}
setParticles(shootInfo) {
var startAngle = ((shootInfo.angle - 180 - 30) * Math.PI) / 180;
const angleRange = (60 * Math.PI) / 180;
var count = 0;
for (let i = 0; i < this.particles.length; i++) {
if (this.particles[i].active === false) {
const angle = startAngle + Math.random() * angleRange;
this.particles[i].build(
shootInfo.target.x,
shootInfo.target.y,
Math.cos(angle) * Math.random() * 10,
Math.sin(angle) * Math.random() * 10
);
if (count++ > 50) {
break;
}
}
}
}
}
class ParticleObject {
constructor() {
this.active = false;
}
build(x, y, vx, vy) {
this.x = x;
this.y = y;
this.r = Math.random() * 2;
if (!vx) {
this.vx = Math.random() * 10 - 5;
} else {
this.vx = vx;
}
if (!vy) {
this.vy = Math.random() * 10 - 5;
} else {
this.vy = vy;
}
this.gravity = 0.9;
this.opacity = Math.random() + 0.5;
this.active = true;
}
draw(dc, offsetX, offsetY) {
dc.beginPath();
dc.arc(this.x + offsetX, this.y + offsetY, this.r, 0, 2 * Math.PI, false);
dc.fillStyle = "yellow";
dc.fill();
this.active = true;
this.x += this.vx;
this.y += this.vy;
this.r = this.r - 0.1;
if (this.r <= 0.05) {
this.active = false;
}
}
}