Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/effects/trigger-wire.js
1036 views
1
/*globals define*/
2
define([
3
'base-object',
4
'config/colors',
5
'config/material',
6
'config/settings',
7
'utils'
8
], function( BaseObject, Colors, Material, Settings, Utils ) {
9
'use strict';
10
11
var Direction = {
12
TOP: 0,
13
LEFT: 1,
14
BOTTOM: 2,
15
RIGHT: 3
16
};
17
18
var defaults = {
19
sourceDirection: Direction.TOP,
20
targetDirection: Direction.TOP,
21
22
// Relative coordinates.
23
vertices: []
24
};
25
26
function TriggerWire( source, target, options ) {
27
BaseObject.call( this );
28
29
// Trigger.
30
this.source = source;
31
// Door.
32
this.target = target;
33
34
Utils.defaults( this, options, defaults );
35
}
36
37
TriggerWire.prototype = new BaseObject();
38
TriggerWire.prototype.constructor = TriggerWire;
39
40
TriggerWire.prototype.update = function() {};
41
42
TriggerWire.prototype.draw = function( ctx ) {
43
if ( !this.source || !this.target ) {
44
return;
45
}
46
47
var x0 = this.source.x,
48
y0 = this.source.y,
49
r0 = this.source.radius;
50
51
var frameRatio = this.source.frameRatio,
52
frameRadius = frameRatio * r0;
53
54
var x1 = this.target.x,
55
y1 = this.target.y,
56
r1 = this.target.radius;
57
58
// Calculate endpoint locations.
59
var horizontal = this.sourceDirection % 2,
60
sign = this.sourceDirection < 2 ? -1 : 1;
61
62
x0 += horizontal * sign * frameRadius;
63
y0 += !horizontal * sign * frameRadius;
64
65
horizontal = this.targetDirection % 2;
66
sign = this.targetDirection < 2 ? -1 : 1;
67
68
x1 += horizontal * sign * r1;
69
y1 += !horizontal * sign * r1;
70
71
var glowColor = Colors.Glow[ Material.type( this.source.material ) ];
72
73
// Draw wire.
74
var dx = x1 - x0,
75
dy = y1 - y0;
76
77
ctx.beginPath();
78
ctx.moveTo( x0, y0 );
79
80
var xi, yi;
81
for ( var i = 0, il = 0.5 * this.vertices.length; i < il; i++ ) {
82
xi = this.vertices[ 2 * i ];
83
yi = this.vertices[ 2 * i + 1 ];
84
ctx.lineTo( x0 + xi * dx, y0 + yi * dy );
85
}
86
87
ctx.lineTo( x1, y1 );
88
89
if ( this.source.active && glowColor ) {
90
ctx.strokeStyle = glowColor;
91
} else {
92
ctx.strokeStyle = '#333';
93
}
94
95
if ( Settings.glow ) {
96
ctx.globalCompositeOperation = 'lighter';
97
}
98
99
ctx.lineWidth = 0.2 * r0;
100
ctx.stroke();
101
102
if ( Settings.glow ) {
103
ctx.globalCompositeOperation = 'source-over';
104
}
105
106
// Draw endpoints.
107
ctx.beginPath();
108
ctx.rect( -0.2 * r0 + x0, -0.2 * r0 + y0, 0.4 * r0, 0.4 * r0 );
109
ctx.fillStyle = '#fff';
110
ctx.fill();
111
112
if ( this.source.active && glowColor ) {
113
ctx.strokeStyle = glowColor;
114
} else {
115
ctx.strokeStyle = '#333';
116
}
117
118
ctx.lineWidth = 0.1 * r0;
119
ctx.stroke();
120
121
ctx.beginPath();
122
ctx.arc( x1, y1, 0.2 * r1, 0, Utils.PI2 );
123
ctx.fillStyle = '#fff';
124
ctx.fill();
125
126
ctx.lineWidth = 0.1 * r1;
127
ctx.stroke();
128
};
129
130
// This doesn't handle negative vertex values.
131
TriggerWire.prototype.aabb = function() {
132
if ( !this.source || !this.target ) {
133
return null;
134
}
135
136
var x0 = this.source.x,
137
y0 = this.source.y,
138
r0 = this.source.radius;
139
140
var x1 = this.target.x,
141
y1 = this.target.y;
142
143
var aabb = {
144
xmin: Math.min( x0, x1 ),
145
ymin: Math.min( y0, y1 ),
146
xmax: Math.max( x0, x1 ),
147
ymax: Math.max( y0, y1 )
148
};
149
150
// AABB radius.
151
var radius = 0.1 * r0;
152
return Utils.relativeExpandAABB( aabb, radius, radius );
153
};
154
155
TriggerWire.Direction = Direction;
156
157
return TriggerWire;
158
});
159
160