Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/entities/trigger.js
1036 views
1
/*jshint camelcase: false*/
2
/*globals define*/
3
define([
4
'entities/physics-entity',
5
'config/colors',
6
'config/material',
7
'config/settings',
8
'utils'
9
], function( PhysicsEntity, Colors, Material, Settings, Utils ) {
10
'use strict';
11
12
var HALF_PI = Utils.HALF_PI;
13
14
var defaults = {
15
duration: 0.4,
16
17
// So we know the actual drawn size.
18
frameRatio: 1.2,
19
20
platformAngle: 0,
21
platformAngularVelocity: 2
22
};
23
24
function Trigger( x, y, radius, material, options ) {
25
this.radius = radius || 0;
26
27
PhysicsEntity.call( this, {
28
shape: 'circle',
29
radius: this.radius,
30
fixture: {
31
isSensor: true,
32
filter: {
33
categoryBits: material
34
}
35
},
36
body: {
37
position: {
38
x: x,
39
y: y
40
}
41
}
42
});
43
44
Utils.defaults( this, options, defaults );
45
46
this.object = null;
47
// If there is an object AND the trigger has caught it.
48
this.active = false;
49
50
this.time = 0;
51
}
52
53
Trigger.prototype = new PhysicsEntity();
54
Trigger.prototype.constructor = Trigger;
55
56
Trigger.prototype.update = function( dt ) {
57
PhysicsEntity.prototype.update.call( this, dt );
58
59
// When the correct trash object enters the trigger,
60
// set its lifeTime to infinite.
61
if ( this.object && !this.active ) {
62
this.object.x = this.x;
63
this.object.y = this.y;
64
this.object.vx = 0;
65
this.object.vy = 0;
66
this.object.fixture.SetSensor( true );
67
if ( typeof this.object.lifeTime !== 'undefined' ) {
68
this.object.lifeTime = Number.POSITIVE_INFINITY;
69
}
70
71
this.active = true;
72
73
// Super hacky last minute audio.
74
var sound = document.getElementById( 'trigger-activate-audio' );
75
if ( sound ) {
76
sound.volume = 0.1;
77
sound.load();
78
sound.play();
79
}
80
}
81
82
// Opening animation.
83
if ( this.active && this.time < this.duration ) {
84
this.time += dt;
85
}
86
87
// Rotating animation.
88
if ( this.active ) {
89
this.platformAngle += this.platformAngularVelocity * dt;
90
}
91
};
92
93
Trigger.prototype.drawPath = function( ctx ) {
94
var radius = this.radius;
95
96
var size = this.frameRatio * ( 2 * radius ),
97
halfSize = 0.5 * size,
98
quarterSize = 0.5 * halfSize;
99
100
// Draw base.
101
ctx.beginPath();
102
ctx.rect( -halfSize, -halfSize, size, size );
103
ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
104
ctx.fill();
105
106
// Draw frame/border.
107
var glowColor = Colors.Glow[ Material.type( this.material ) ];
108
109
if ( glowColor ) {
110
ctx.beginPath();
111
112
// Top left.
113
ctx.moveTo( -quarterSize, -halfSize );
114
ctx.lineTo( -halfSize, -halfSize );
115
ctx.lineTo( -halfSize, -quarterSize );
116
117
// Bottom left.
118
ctx.moveTo( -halfSize, quarterSize );
119
ctx.lineTo( -halfSize, halfSize );
120
ctx.lineTo( -quarterSize, halfSize );
121
122
// Bottom right.
123
ctx.moveTo( quarterSize, halfSize );
124
ctx.lineTo( halfSize, halfSize );
125
ctx.lineTo( halfSize, quarterSize );
126
127
// Top right.
128
ctx.moveTo( halfSize, -quarterSize );
129
ctx.lineTo( halfSize, -halfSize );
130
ctx.lineTo( quarterSize, -halfSize );
131
132
ctx.lineWidth = 0.3 * radius;
133
ctx.strokeStyle = glowColor;
134
ctx.stroke();
135
136
ctx.lineWidth = 0.1 * radius;
137
ctx.strokeStyle = '#fff';
138
ctx.stroke();
139
}
140
141
var halfRadius = 0.5 * radius;
142
143
var t = 1;
144
if ( this.time < this.duration ) {
145
t = this.time / this.duration;
146
}
147
148
t *= 0.5 * halfRadius;
149
150
if ( this.platformAngle ) {
151
ctx.save();
152
ctx.rotate( this.platformAngle );
153
}
154
155
// Draw beams.
156
if ( glowColor && this.time >= this.duration ) {
157
if ( Settings.glow ) {
158
ctx.globalCompositeOperation = 'lighter';
159
}
160
161
// 45 degrees.
162
var diagonal = 0.5 * Math.sqrt( 2 ) * halfRadius;
163
164
ctx.beginPath();
165
// Left diagonal.
166
ctx.moveTo( -t - diagonal, -t - diagonal );
167
ctx.lineTo( t + diagonal, t + diagonal );
168
// Right diagonal.
169
ctx.moveTo( t + diagonal, -t - diagonal );
170
ctx.lineTo( -t - diagonal, t + diagonal );
171
172
ctx.lineWidth = ( 0.3 + Math.random() * 0.2 ) * radius;
173
ctx.strokeStyle = glowColor;
174
ctx.stroke();
175
176
ctx.lineWidth = 0.2 * radius;
177
ctx.strokeStyle = '#fff';
178
ctx.stroke();
179
180
if ( Settings.glow ) {
181
ctx.globalCompositeOperation = 'source-over';
182
}
183
}
184
185
// Draw sensor.
186
ctx.beginPath();
187
188
// Top left.
189
ctx.moveTo( -halfRadius - t, -t );
190
ctx.arc( -t, -t, halfRadius, -Math.PI, -HALF_PI );
191
192
// Top right.
193
ctx.moveTo( t, -halfRadius - t );
194
ctx.arc( t, -t, halfRadius, -HALF_PI, 0 );
195
196
// Bottom right.
197
ctx.moveTo( halfRadius + t, t );
198
ctx.arc( t, t, halfRadius, 0, HALF_PI );
199
200
// Bottom left.
201
ctx.moveTo( -t, halfRadius + t );
202
ctx.arc( -t, t, halfRadius, HALF_PI, Math.PI );
203
204
ctx.lineWidth = 0.5 * radius;
205
ctx.strokeStyle = '#333';
206
ctx.stroke();
207
208
ctx.lineWidth = 0.2 * radius;
209
ctx.strokeStyle = '#fff';
210
ctx.stroke();
211
212
if ( this.platformAngle ) {
213
ctx.restore();
214
}
215
216
PhysicsEntity.prototype.drawPath.call( this, ctx );
217
};
218
219
Trigger.prototype.aabb = function() {
220
var aabb = PhysicsEntity.prototype.aabb.call( this );
221
// Add half lineWidth of frame.
222
var ratio = this.frameRatio + 0.15;
223
return Utils.ratioExpandAABB( aabb, ratio, ratio );
224
};
225
226
return Trigger;
227
});
228
229