-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexplosion.js
86 lines (76 loc) · 2.02 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
"use strict";
var Explosion = function Explosion(pos) {
this.position = pos;
this.age = 0;
this.rate = 50;
this.mirrors = {};
}
Explosion.prototype.update = function(dt) {
if(this.invalid) return;
this.age += dt;
Object.forEach(this.mirrors, function(mirror) {
mirror.update(dt);
});
}
Explosion.prototype.clone = function() {
var e = new Explosion(this.position.clone());
e.age = this.age;
e.rate = this.rate;
return e;
}
Explosion.prototype.bounceWithin = function(width, height) {
if(this.invalid) return;
if(this.position.x - this.radius < 0) {
var m = this.clone();
m.position.x = -m.position.x
this.mirrors.left = m;
}
if(this.position.y - this.radius < 0) {
var m = this.clone();
m.position.y = -m.position.y
this.mirrors.top = m;
}
if(this.position.x + this.radius > width) {
var m = this.clone();
m.position.x = 2*width - m.position.x
this.mirrors.right = m;
}
if(this.position.y + this.radius > height) {
var m = this.clone();
m.position.y = 2*height - m.position.y
this.mirrors.bottom = m;
}
}
Object.defineProperty(Explosion.prototype, 'brightness', {
get: function() {
if(this.invalid) return 0;
var b = 0.5*Math.exp(-this.age);
if(b < 0.01)
this.invalid = true;
return b;
}
});
Object.defineProperty(Explosion.prototype, 'radius', {
get: function() {
return this.age*this.rate
}
});
Explosion.prototype.drawTo = function(ctx) {
if(this.invalid) return;
ctx.save();
ctx.fillStyle = 'rgba(255,255,255,'+ this.brightness + ')';
ctx.beginPath();
ctx.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2, false);
ctx.fill();
ctx.beginPath();
ctx.arc(this.position.x, this.position.y, 3 * this.radius / 5, 0, Math.PI * 2, false);
ctx.fill();
ctx.beginPath();
ctx.arc(this.position.x, this.position.y, this.radius / 5, 0, Math.PI * 2, false);
ctx.fill();
ctx.restore();
Object.forEach(this.mirrors, function(mirror) {
mirror.drawTo(ctx);
});
}
module.exports = Explosion;