Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/entities/explosion.js
1036 views
1
/*globals define*/
2
define([
3
'entities/entity',
4
'utils',
5
'config/settings'
6
], function( Entity, Utils, Settings ) {
7
'use strict';
8
9
var PI2 = Utils.PI2;
10
11
var defaults = {
12
particleCount: 10,
13
14
radius: 1.0,
15
radiusSpread: 0.6,
16
17
minRadius: 0.05,
18
19
drag: 0.925,
20
dragSpread: 0.025,
21
22
force: 10,
23
forceSpread: 5,
24
25
spin: 1.5,
26
spinSpread: 0.5,
27
28
shrink: 0.95
29
};
30
31
function Explosion( x, y, options ) {
32
Entity.call( this, x, y );
33
34
this.area = null;
35
this.particles = [];
36
37
Utils.defaults( this, options, defaults );
38
39
this.initialize();
40
}
41
42
Explosion.prototype = new Entity();
43
Explosion.prototype.constructor = Explosion;
44
45
Explosion.prototype.initialize = function() {
46
var particleCount = this.particleCount;
47
48
var x, y;
49
var point;
50
var angle, force;
51
while ( particleCount-- ) {
52
x = 0;
53
y = 0;
54
55
if ( this.area ) {
56
point = this.area.random();
57
x = point.x;
58
y = point.y;
59
}
60
61
angle = Math.random() * PI2;
62
force = Utils.floatSpread( this.force, this.forceSpread );
63
64
this.particles.push({
65
x: x,
66
y: y,
67
radius: Utils.floatSpread( this.radius, this.radiusSpread ),
68
angle: angle,
69
vx: Math.cos( angle ) * force,
70
vy: Math.sin( angle ) * force,
71
drag: Utils.floatSpread( this.drag, this.dragSpread ),
72
spin: Utils.floatSpread( this.spin, this.spinSpread )
73
});
74
}
75
};
76
77
Explosion.prototype.update = function( dt ) {
78
Entity.prototype.update.call( this, dt );
79
80
var removed = [];
81
this.particles.forEach(function( particle, index ) {
82
particle.x += particle.vx * dt;
83
particle.y += particle.vy * dt;
84
85
particle.vx *= particle.drag;
86
particle.vy *= particle.drag;
87
88
particle.angle += Utils.randomFloat( -0.5, 0.5 ) * particle.spin;
89
90
particle.vx += Math.cos( particle.angle ) * 0.1;
91
particle.vy += Math.sin( particle.angle ) * 0.1;
92
93
particle.radius *= this.shrink;
94
if ( particle.radius < this.minRadius ) {
95
removed.push( index );
96
}
97
}.bind( this ));
98
99
Utils.removeIndices( this.particles, removed );
100
101
if ( !this.particles.length ) {
102
this.game.removed.push( this );
103
}
104
};
105
106
Explosion.prototype.draw = function( ctx ) {
107
ctx.save();
108
109
ctx.translate( this.x, this.y );
110
ctx.rotate( -this.angle );
111
112
if ( Settings.glow ) {
113
ctx.globalCompositeOperation = 'lighter';
114
}
115
116
ctx.fillStyle = this.fill.rgba();
117
this.particles.forEach(function( particle ) {
118
ctx.beginPath();
119
ctx.arc( particle.x, particle.y, particle.radius, 0, PI2 );
120
ctx.fill();
121
});
122
123
if ( Settings.glow ) {
124
ctx.globalCompositeOperation = 'source-over';
125
}
126
127
ctx.restore();
128
};
129
130
Explosion.prototype.aabb = function() {
131
return {
132
xmin: this.x - 8,
133
ymin: this.y - 8,
134
xmax: this.x + 8,
135
ymax: this.y + 8
136
};
137
};
138
139
return Explosion;
140
});
141
142