Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/effects/trail.js
1036 views
1
/*globals define*/
2
define([
3
'base-object',
4
'utils',
5
'color',
6
'config/settings'
7
], function( BaseObject, Utils, Color, Settings ) {
8
'use strict';
9
10
var PI2 = Utils.PI2;
11
12
var defaults = {
13
shrink: 0.95,
14
radius: 1.5,
15
};
16
17
function Trail( options ) {
18
BaseObject.call( this );
19
20
this.target = null;
21
this.particles = [];
22
this.fill = new Color();
23
this.period = 1 / 4;
24
25
Utils.defaults( this, options, defaults );
26
27
this.time = 0;
28
this.game = null;
29
}
30
31
Trail.prototype = new BaseObject();
32
Trail.prototype.constructor = Trail;
33
34
Trail.prototype.update = function( dt ) {
35
if ( !Settings.trail ) {
36
return;
37
}
38
39
this.time += dt;
40
41
if ( this.time > this.period ) {
42
this.time = 0;
43
44
// If the target is moving, add a trail particle.
45
if ( this.target && ( this.target.vx || this.target.vy ) ) {
46
var x = this.target.x,
47
y = this.target.y;
48
49
this.particles.push({
50
x: x,
51
y: y,
52
radius: this.radius
53
});
54
}
55
}
56
57
var removed = [];
58
this.particles.forEach(function( particle, index ) {
59
particle.radius *= this.shrink;
60
if ( particle.radius < this.minRadius ) {
61
removed.push( index );
62
}
63
}.bind( this ));
64
65
Utils.removeIndices( this.particles, removed );
66
};
67
68
Trail.prototype.draw = function( ctx ) {
69
if ( !this.particles.length || !Settings.trail ) {
70
return;
71
}
72
73
if ( Settings.glow ) {
74
ctx.globalCompositeOperation = 'lighter';
75
}
76
77
ctx.beginPath();
78
79
this.particles.forEach(function( particle ) {
80
ctx.moveTo( particle.x, particle.y );
81
ctx.arc( particle.x, particle.y, particle.radius, 0, PI2 );
82
}.bind( this ));
83
84
ctx.fillStyle = this.fill.rgba();
85
ctx.fill();
86
87
if ( Settings.glow ) {
88
ctx.globalCompositeOperation = 'source-over';
89
}
90
};
91
92
Trail.prototype.aabb = function() {
93
return null;
94
};
95
96
Object.defineProperty( Trail.prototype, 'frequency', {
97
get: function() {
98
return 1 / this.period;
99
},
100
101
set: function( frequency ) {
102
this.period = 1 / frequency;
103
}
104
});
105
106
return Trail;
107
});
108
109