Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/utils.js
1036 views
1
/*globals define*/
2
define(function() {
3
'use strict';
4
5
var PI2 = 2 * Math.PI,
6
HALF_PI = 0.5 * Math.PI;
7
8
var RAD_TO_DEG = 180 / Math.PI,
9
DEG_TO_RAD = Math.PI / 180;
10
11
var EPSILON = 1e-1;
12
13
function clamp( value, min, max ) {
14
return Math.min( Math.max( value, min ), max );
15
}
16
17
function randomFloat( min, max ) {
18
return min + Math.random() * ( max - min );
19
}
20
21
function randomInt( min, max ) {
22
return Math.round( randomFloat( min, max ) );
23
}
24
25
/**
26
* Returns a float: value +/- spread.
27
*/
28
function floatSpread( value, spread ) {
29
return randomFloat( value - spread, value + spread );
30
}
31
32
/**
33
* Returns an int: value +/- spread.
34
*/
35
function intSpread( value, spread ) {
36
return randomInt( value - spread, value + spread );
37
}
38
39
function lerp( a, b, t ) {
40
return a + t * ( b - a );
41
}
42
43
function inverseLerp( value, a, b ) {
44
return ( value - a ) / ( b - a );
45
}
46
47
/**
48
* Project along the line given by [(x0, y0), (x1, y1)] by parameter.
49
*/
50
function lerp2d( x0, y0, x1, y1, parameter ) {
51
if ( parameter === null ) {
52
return null;
53
}
54
55
return {
56
x: lerp( x0, x1, parameter ),
57
y: lerp( y0, y1, parameter )
58
};
59
}
60
61
function distanceSquared( x0, y0, x1, y1 ) {
62
var dx = x1 - x0,
63
dy = y1 - y0;
64
65
return dx * dx + dy * dy;
66
}
67
68
function distance( x0, y0, x1, y1 ) {
69
return Math.sqrt( distanceSquared( x0, y0, x1, y1 ) );
70
}
71
72
/**
73
* Rounds a value to the given precision, removes any trailing zeros produced
74
* by Number.prototype.toFixed().
75
*
76
* Example:
77
* var x = 100;
78
* x.toFixed(2); // "100.00"
79
* round( 100, 2 ); // "100"
80
*/
81
function round( value, precision ) {
82
return parseFloat( value.toFixed( precision ) );
83
}
84
85
function roundNearZero( value, epsilon ) {
86
return Math.abs( value ) > ( epsilon || EPSILON ) ? value : 0;
87
}
88
89
/**
90
* Assuming the line is CW, the normal of the line is (-dy, dx).
91
*/
92
function lineNormal( x0, y0, x1, y1 ) {
93
var dx = x1 - x0,
94
dy = y1 - y0;
95
96
var lengthSquared = dx * dx + dy * dy;
97
if ( !lengthSquared ) {
98
return null;
99
}
100
101
var invLength = 1 / Math.sqrt( lengthSquared );
102
return {
103
x: dy * invLength,
104
y: -dx * invLength
105
};
106
}
107
108
function angleFrom( x0, y0, x1, y1 ) {
109
return Math.atan2( y1 - y0, x1 - x0 );
110
}
111
112
/**
113
* Remove all values in array with an index in indices.
114
*/
115
function removeIndices( array, indices ) {
116
indices.sort();
117
118
var index = indices.length;
119
while( index-- ) {
120
array.splice( indices[ index ], 1 );
121
}
122
}
123
124
/**
125
* Set the pre-existing properties of a given object with the values in attrs.
126
* Recursively handles properties that are also objects.
127
*/
128
function set( object, attrs ) {
129
if ( !object || !attrs ) {
130
return;
131
}
132
133
for ( var key in attrs ) {
134
if ( object.hasOwnProperty( key ) ) {
135
if ( typeof object[ key ] === 'object' &&
136
typeof attrs[ key ] === 'object' ) {
137
set( object[ key ], attrs[ key ] );
138
} else {
139
object[ key ] = attrs[ key ];
140
}
141
}
142
}
143
}
144
145
/**
146
* Sets any undefined values to given default values.
147
* Can have more than one defaults object.
148
*
149
* This is pretty much underscore.js's defaults().
150
*/
151
function defaults( object ) {
152
var args = [].slice.call( arguments, 1 );
153
154
args.forEach(function( arg ) {
155
if ( arg ) {
156
for ( var key in arg ) {
157
if ( typeof object[ key ] === 'undefined' ) {
158
object[ key ] = arg[ key ];
159
}
160
}
161
}
162
});
163
164
return object;
165
}
166
167
/**
168
* Rotates the axis-aligned bounding box defined by
169
* [(left, top), (right, bottom)] by rotation. Translates by (tx, ty).
170
*/
171
function rotateAABB( tx, ty, left, top, right, bottom, rotation ) {
172
var cos = Math.cos( -rotation ),
173
sin = Math.sin( -rotation );
174
175
// Coordinates of rotated extents.
176
var x = [],
177
y = [];
178
179
// Top left.
180
x.push( cos * left - sin * top );
181
y.push( sin * left + cos * top );
182
183
// Bottom left.
184
x.push( cos * left - sin * bottom );
185
y.push( sin * left + cos * bottom );
186
187
// Top right.
188
x.push( cos * right - sin * top );
189
y.push( sin * right + cos * top );
190
191
// Bottom right.
192
x.push( cos * right - sin * bottom );
193
y.push( sin * right + cos * bottom );
194
195
return {
196
xmin: Math.min.apply( null, x ) + tx,
197
ymin: Math.min.apply( null, y ) + ty,
198
xmax: Math.max.apply( null, x ) + tx,
199
ymax: Math.max.apply( null, y ) + ty
200
};
201
}
202
203
function relativeExpandAABB( aabb, dw, dh ) {
204
dw *= 0.5;
205
dh *= 0.5;
206
207
return {
208
xmin: aabb.xmin - dw,
209
ymin: aabb.ymin - dh,
210
xmax: aabb.xmax + dw,
211
ymax: aabb.ymax + dh
212
};
213
}
214
215
function expandAABB( aabb, width, height ) {
216
var dw = width - ( aabb.xmax - aabb.xmin ),
217
dh = height - ( aabb.ymax - aabb.ymin );
218
219
return relativeExpandAABB( aabb, dw, dh );
220
}
221
222
function ratioExpandAABB( aabb, widthRatio, heightRatio ) {
223
var dw = aabb.xmax - aabb.xmin,
224
dh = aabb.ymax - aabb.ymin;
225
226
dw = ( dw * widthRatio ) - dw;
227
dh = ( dh * heightRatio ) - dh;
228
229
return relativeExpandAABB( aabb, dw, dh );
230
}
231
232
233
return {
234
PI2: PI2,
235
HALF_PI: HALF_PI,
236
237
RAD_TO_DEG: RAD_TO_DEG,
238
DEG_TO_RAD: DEG_TO_RAD,
239
240
EPSILON: EPSILON,
241
242
clamp: clamp,
243
244
randomFloat: randomFloat,
245
randomInt: randomInt,
246
247
floatSpread: floatSpread,
248
intSpread: intSpread,
249
250
lerp: lerp,
251
inverseLerp : inverseLerp,
252
lerp2d: lerp2d,
253
254
distanceSquared: distanceSquared,
255
distance: distance,
256
257
round: round,
258
roundNearZero: roundNearZero,
259
lineNormal: lineNormal,
260
angleFrom: angleFrom,
261
262
removeIndices: removeIndices,
263
264
set: set,
265
defaults: defaults,
266
267
rotateAABB: rotateAABB,
268
269
expandAABB: expandAABB,
270
ratioExpandAABB: ratioExpandAABB,
271
relativeExpandAABB: relativeExpandAABB
272
};
273
});
274
275