-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfire.js
54 lines (54 loc) · 1.25 KB
/
fire.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
class Fire{
constructor(x, y, r = 50) {
this.x = x;
this.y = y;
this.r = r;
this.vx = 0;
this.vy = 0;
this.speed = 5;
this.brightness = 0;
this.particles = [];
}
move() {
this.vx = random(-this.speed, this.speed);
this.vy = random(-this.speed, this.speed);
return this.updatePos();
}
moveTowards() {
let posX = this.x > mouseX;
let posY = this.y > mouseY;
if(posX) {
this.vx = random(-this.speed,0);
} else {
this.vx = random(0,this.speed);
}
if(posY) {
this.vy = random(-this.speed,0);
} else {
this.vy = random(0,this.speed)
}
return this.updatePos();
}
intersects(other) {
let d = dist(this.x, this.y, other.x, other.y);
return d < (this.r + other.r)/4;
}
changeColor(bright) {
this.brightness = bright;
}
showParticles(){
for (let i = this.particles.length - 1; i >= 0; i--) {
this.particles[i].update();
this.particles[i].show();
this.particles[i].move(this.x, this.y);
if (this.particles[i].extinguished()) {
this.particles[i].reset();
}
}
}
updatePos(){
this.x = this.x + this.vx;
this.y = this.y + this.vy;
return (this.x > windowWidth || this.x < 0 || this.y > windowHeight || this.y < 0);
}
}