littlejsengine 1.11.9 → 1.11.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/littlejs.d.ts +485 -492
- package/dist/littlejs.esm.js +548 -529
- package/dist/littlejs.esm.min.js +1 -1
- package/dist/littlejs.js +546 -525
- package/dist/littlejs.min.js +1 -1
- package/dist/littlejs.release.js +502 -484
- package/examples/htmlMenu/game.js +11 -1
- package/examples/htmlMenu/index.html +1 -10
- package/examples/module/game.js +2 -2
- package/examples/shorts/base.html +1 -1
- package/examples/starter/build/index.html +2 -0
- package/examples/starter/build/index.js +1 -0
- package/examples/starter/build/tiles.png +0 -0
- package/examples/starter/build.js +13 -3
- package/examples/starter/game.js +8 -1
- package/examples/starter/game.zip +0 -0
- package/examples/typescript/build/dist/littlejs.esm.js +4834 -0
- package/examples/typescript/build/examples/typescript/build.js +24 -0
- package/examples/typescript/build/examples/typescript/game.js +102 -0
- package/examples/typescript/build.bat +5 -0
- package/examples/typescript/build.js +33 -0
- package/examples/typescript/game.js +102 -0
- package/examples/typescript/game.ts +134 -0
- package/examples/typescript/index.html +10 -0
- package/examples/typescript/tiles.png +0 -0
- package/examples/typescript/tsconfig.json +8 -0
- package/package.json +1 -1
- package/plugins/newgrounds.js +44 -35
- package/plugins/postProcess.js +18 -4
- package/plugins/uiSystem.js +196 -30
- package/src/engine.js +21 -20
- package/src/engineAudio.js +59 -59
- package/src/engineDebug.js +44 -41
- package/src/engineDraw.js +58 -58
- package/src/engineExport.js +2 -4
- package/src/engineInput.js +40 -35
- package/src/engineMedals.js +21 -11
- package/src/engineObject.js +42 -35
- package/src/engineParticles.js +10 -10
- package/src/engineSettings.js +77 -82
- package/src/engineTileLayer.js +21 -21
- package/src/engineUtilities.js +150 -150
- package/src/engineWebGL.js +3 -3
package/src/engineUtilities.js
CHANGED
|
@@ -11,112 +11,112 @@
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
13
|
/** A shortcut to get Math.PI
|
|
14
|
-
* @type {
|
|
14
|
+
* @type {number}
|
|
15
15
|
* @default Math.PI
|
|
16
16
|
* @memberof Utilities */
|
|
17
17
|
const PI = Math.PI;
|
|
18
18
|
|
|
19
19
|
/** Returns absolute value of value passed in
|
|
20
|
-
* @param {
|
|
21
|
-
* @return {
|
|
20
|
+
* @param {number} value
|
|
21
|
+
* @return {number}
|
|
22
22
|
* @memberof Utilities */
|
|
23
23
|
function abs(value) { return Math.abs(value); }
|
|
24
24
|
|
|
25
25
|
/** Returns lowest of two values passed in
|
|
26
|
-
* @param {
|
|
27
|
-
* @param {
|
|
28
|
-
* @return {
|
|
26
|
+
* @param {number} valueA
|
|
27
|
+
* @param {number} valueB
|
|
28
|
+
* @return {number}
|
|
29
29
|
* @memberof Utilities */
|
|
30
30
|
function min(valueA, valueB) { return Math.min(valueA, valueB); }
|
|
31
31
|
|
|
32
32
|
/** Returns highest of two values passed in
|
|
33
|
-
* @param {
|
|
34
|
-
* @param {
|
|
35
|
-
* @return {
|
|
33
|
+
* @param {number} valueA
|
|
34
|
+
* @param {number} valueB
|
|
35
|
+
* @return {number}
|
|
36
36
|
* @memberof Utilities */
|
|
37
37
|
function max(valueA, valueB) { return Math.max(valueA, valueB); }
|
|
38
38
|
|
|
39
39
|
/** Returns the sign of value passed in
|
|
40
|
-
* @param {
|
|
41
|
-
* @return {
|
|
40
|
+
* @param {number} value
|
|
41
|
+
* @return {number}
|
|
42
42
|
* @memberof Utilities */
|
|
43
43
|
function sign(value) { return Math.sign(value); }
|
|
44
44
|
|
|
45
45
|
/** Returns first parm modulo the second param, but adjusted so negative numbers work as expected
|
|
46
|
-
* @param {
|
|
47
|
-
* @param {
|
|
48
|
-
* @return {
|
|
46
|
+
* @param {number} dividend
|
|
47
|
+
* @param {number} [divisor]
|
|
48
|
+
* @return {number}
|
|
49
49
|
* @memberof Utilities */
|
|
50
50
|
function mod(dividend, divisor=1) { return ((dividend % divisor) + divisor) % divisor; }
|
|
51
51
|
|
|
52
52
|
/** Clamps the value between max and min
|
|
53
|
-
* @param {
|
|
54
|
-
* @param {
|
|
55
|
-
* @param {
|
|
56
|
-
* @return {
|
|
53
|
+
* @param {number} value
|
|
54
|
+
* @param {number} [min]
|
|
55
|
+
* @param {number} [max]
|
|
56
|
+
* @return {number}
|
|
57
57
|
* @memberof Utilities */
|
|
58
58
|
function clamp(value, min=0, max=1) { return value < min ? min : value > max ? max : value; }
|
|
59
59
|
|
|
60
60
|
/** Returns what percentage the value is between valueA and valueB
|
|
61
|
-
* @param {
|
|
62
|
-
* @param {
|
|
63
|
-
* @param {
|
|
64
|
-
* @return {
|
|
61
|
+
* @param {number} value
|
|
62
|
+
* @param {number} valueA
|
|
63
|
+
* @param {number} valueB
|
|
64
|
+
* @return {number}
|
|
65
65
|
* @memberof Utilities */
|
|
66
66
|
function percent(value, valueA, valueB)
|
|
67
67
|
{ return (valueB-=valueA) ? clamp((value-valueA)/valueB) : 0; }
|
|
68
68
|
|
|
69
69
|
/** Linearly interpolates between values passed in using percent
|
|
70
|
-
* @param {
|
|
71
|
-
* @param {
|
|
72
|
-
* @param {
|
|
73
|
-
* @return {
|
|
70
|
+
* @param {number} percent
|
|
71
|
+
* @param {number} valueA
|
|
72
|
+
* @param {number} valueB
|
|
73
|
+
* @return {number}
|
|
74
74
|
* @memberof Utilities */
|
|
75
75
|
function lerp(percent, valueA, valueB) { return valueA + clamp(percent) * (valueB-valueA); }
|
|
76
76
|
|
|
77
77
|
/** Returns signed wrapped distance between the two values passed in
|
|
78
|
-
* @param {
|
|
79
|
-
* @param {
|
|
80
|
-
* @param {
|
|
81
|
-
* @returns {
|
|
78
|
+
* @param {number} valueA
|
|
79
|
+
* @param {number} valueB
|
|
80
|
+
* @param {number} [wrapSize]
|
|
81
|
+
* @returns {number}
|
|
82
82
|
* @memberof Utilities */
|
|
83
83
|
function distanceWrap(valueA, valueB, wrapSize=1)
|
|
84
84
|
{ const d = (valueA - valueB) % wrapSize; return d*2 % wrapSize - d; }
|
|
85
85
|
|
|
86
86
|
/** Linearly interpolates between values passed in with wrapping
|
|
87
|
-
* @param {
|
|
88
|
-
* @param {
|
|
89
|
-
* @param {
|
|
90
|
-
* @param {
|
|
91
|
-
* @returns {
|
|
87
|
+
* @param {number} percent
|
|
88
|
+
* @param {number} valueA
|
|
89
|
+
* @param {number} valueB
|
|
90
|
+
* @param {number} [wrapSize]
|
|
91
|
+
* @returns {number}
|
|
92
92
|
* @memberof Utilities */
|
|
93
93
|
function lerpWrap(percent, valueA, valueB, wrapSize=1)
|
|
94
94
|
{ return valueB + clamp(percent) * distanceWrap(valueA, valueB, wrapSize); }
|
|
95
95
|
|
|
96
96
|
/** Returns signed wrapped distance between the two angles passed in
|
|
97
|
-
* @param {
|
|
98
|
-
* @param {
|
|
99
|
-
* @returns {
|
|
97
|
+
* @param {number} angleA
|
|
98
|
+
* @param {number} angleB
|
|
99
|
+
* @returns {number}
|
|
100
100
|
* @memberof Utilities */
|
|
101
101
|
function distanceAngle(angleA, angleB) { return distanceWrap(angleA, angleB, 2*PI); }
|
|
102
102
|
|
|
103
103
|
/** Linearly interpolates between the angles passed in with wrapping
|
|
104
|
-
* @param {
|
|
105
|
-
* @param {
|
|
106
|
-
* @param {
|
|
107
|
-
* @returns {
|
|
104
|
+
* @param {number} percent
|
|
105
|
+
* @param {number} angleA
|
|
106
|
+
* @param {number} angleB
|
|
107
|
+
* @returns {number}
|
|
108
108
|
* @memberof Utilities */
|
|
109
109
|
function lerpAngle(percent, angleA, angleB) { return lerpWrap(percent, angleA, angleB, 2*PI); }
|
|
110
110
|
|
|
111
111
|
/** Applies smoothstep function to the percentage value
|
|
112
|
-
* @param {
|
|
113
|
-
* @return {
|
|
112
|
+
* @param {number} percent
|
|
113
|
+
* @return {number}
|
|
114
114
|
* @memberof Utilities */
|
|
115
115
|
function smoothStep(percent) { return percent * percent * (3 - 2 * percent); }
|
|
116
116
|
|
|
117
117
|
/** Returns the nearest power of two not less then the value
|
|
118
|
-
* @param {
|
|
119
|
-
* @return {
|
|
118
|
+
* @param {number} value
|
|
119
|
+
* @return {number}
|
|
120
120
|
* @memberof Utilities */
|
|
121
121
|
function nearestPowerOfTwo(value) { return 2**Math.ceil(Math.log2(value)); }
|
|
122
122
|
|
|
@@ -125,7 +125,7 @@ function nearestPowerOfTwo(value) { return 2**Math.ceil(Math.log2(value)); }
|
|
|
125
125
|
* @param {Vector2} sizeA - Size of box A
|
|
126
126
|
* @param {Vector2} posB - Center of box B
|
|
127
127
|
* @param {Vector2} [sizeB=(0,0)] - Size of box B, a point if undefined
|
|
128
|
-
* @return {
|
|
128
|
+
* @return {boolean} - True if overlapping
|
|
129
129
|
* @memberof Utilities */
|
|
130
130
|
function isOverlapping(posA, sizeA, posB, sizeB=vec2())
|
|
131
131
|
{
|
|
@@ -138,7 +138,7 @@ function isOverlapping(posA, sizeA, posB, sizeB=vec2())
|
|
|
138
138
|
* @param {Vector2} end - End of raycast
|
|
139
139
|
* @param {Vector2} pos - Center of box
|
|
140
140
|
* @param {Vector2} size - Size of box
|
|
141
|
-
* @return {
|
|
141
|
+
* @return {boolean} - True if intersecting
|
|
142
142
|
* @memberof Utilities */
|
|
143
143
|
function isIntersecting(start, end, pos, size)
|
|
144
144
|
{
|
|
@@ -175,17 +175,17 @@ function isIntersecting(start, end, pos, size)
|
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
/** Returns an oscillating wave between 0 and amplitude with frequency of 1 Hz by default
|
|
178
|
-
* @param {
|
|
179
|
-
* @param {
|
|
180
|
-
* @param {
|
|
181
|
-
* @return {
|
|
178
|
+
* @param {number} [frequency] - Frequency of the wave in Hz
|
|
179
|
+
* @param {number} [amplitude] - Amplitude (max height) of the wave
|
|
180
|
+
* @param {number} [t=time] - Value to use for time of the wave
|
|
181
|
+
* @return {number} - Value waving between 0 and amplitude
|
|
182
182
|
* @memberof Utilities */
|
|
183
183
|
function wave(frequency=1, amplitude=1, t=time)
|
|
184
184
|
{ return amplitude/2 * (1 - Math.cos(t*frequency*2*PI)); }
|
|
185
185
|
|
|
186
186
|
/** Formats seconds to mm:ss style for display purposes
|
|
187
|
-
* @param {
|
|
188
|
-
* @return {
|
|
187
|
+
* @param {number} t - time in seconds
|
|
188
|
+
* @return {string}
|
|
189
189
|
* @memberof Utilities */
|
|
190
190
|
function formatTime(t) { return (t/60|0) + ':' + (t%60<10?'0':'') + (t%60|0); }
|
|
191
191
|
|
|
@@ -195,34 +195,34 @@ function formatTime(t) { return (t/60|0) + ':' + (t%60<10?'0':'') + (t%60|0); }
|
|
|
195
195
|
* @namespace Random */
|
|
196
196
|
|
|
197
197
|
/** Returns a random value between the two values passed in
|
|
198
|
-
* @param {
|
|
199
|
-
* @param {
|
|
200
|
-
* @return {
|
|
198
|
+
* @param {number} [valueA]
|
|
199
|
+
* @param {number} [valueB]
|
|
200
|
+
* @return {number}
|
|
201
201
|
* @memberof Random */
|
|
202
202
|
function rand(valueA=1, valueB=0) { return valueB + Math.random() * (valueA-valueB); }
|
|
203
203
|
|
|
204
204
|
/** Returns a floored random value between the two values passed in
|
|
205
205
|
* The upper bound is exclusive. (If 2 is passed in, result will be 0 or 1)
|
|
206
|
-
* @param {
|
|
207
|
-
* @param {
|
|
208
|
-
* @return {
|
|
206
|
+
* @param {number} valueA
|
|
207
|
+
* @param {number} [valueB]
|
|
208
|
+
* @return {number}
|
|
209
209
|
* @memberof Random */
|
|
210
210
|
function randInt(valueA, valueB=0) { return Math.floor(rand(valueA,valueB)); }
|
|
211
211
|
|
|
212
212
|
/** Randomly returns either -1 or 1
|
|
213
|
-
* @return {
|
|
213
|
+
* @return {number}
|
|
214
214
|
* @memberof Random */
|
|
215
215
|
function randSign() { return randInt(2) * 2 - 1; }
|
|
216
216
|
|
|
217
217
|
/** Returns a random Vector2 with the passed in length
|
|
218
|
-
* @param {
|
|
218
|
+
* @param {number} [length]
|
|
219
219
|
* @return {Vector2}
|
|
220
220
|
* @memberof Random */
|
|
221
221
|
function randVector(length=1) { return new Vector2().setAngle(rand(2*PI), length); }
|
|
222
222
|
|
|
223
223
|
/** Returns a random Vector2 within a circular shape
|
|
224
|
-
* @param {
|
|
225
|
-
* @param {
|
|
224
|
+
* @param {number} [radius]
|
|
225
|
+
* @param {number} [minRadius]
|
|
226
226
|
* @return {Vector2}
|
|
227
227
|
* @memberof Random */
|
|
228
228
|
function randInCircle(radius=1, minRadius=0)
|
|
@@ -231,7 +231,7 @@ function randInCircle(radius=1, minRadius=0)
|
|
|
231
231
|
/** Returns a random color between the two passed in colors, combine components if linear
|
|
232
232
|
* @param {Color} [colorA=(1,1,1,1)]
|
|
233
233
|
* @param {Color} [colorB=(0,0,0,1)]
|
|
234
|
-
* @param {
|
|
234
|
+
* @param {boolean} [linear]
|
|
235
235
|
* @return {Color}
|
|
236
236
|
* @memberof Random */
|
|
237
237
|
function randColor(colorA=new Color, colorB=new Color(0,0,0,1), linear=false)
|
|
@@ -255,17 +255,17 @@ function randColor(colorA=new Color, colorB=new Color(0,0,0,1), linear=false)
|
|
|
255
255
|
class RandomGenerator
|
|
256
256
|
{
|
|
257
257
|
/** Create a random number generator with the seed passed in
|
|
258
|
-
* @param {
|
|
258
|
+
* @param {number} seed - Starting seed */
|
|
259
259
|
constructor(seed)
|
|
260
260
|
{
|
|
261
|
-
/** @property {
|
|
261
|
+
/** @property {number} - random seed */
|
|
262
262
|
this.seed = seed;
|
|
263
263
|
}
|
|
264
264
|
|
|
265
265
|
/** Returns a seeded random value between the two values passed in
|
|
266
|
-
* @param {
|
|
267
|
-
* @param {
|
|
268
|
-
* @return {
|
|
266
|
+
* @param {number} [valueA]
|
|
267
|
+
* @param {number} [valueB]
|
|
268
|
+
* @return {number} */
|
|
269
269
|
float(valueA=1, valueB=0)
|
|
270
270
|
{
|
|
271
271
|
// xorshift algorithm
|
|
@@ -276,13 +276,13 @@ class RandomGenerator
|
|
|
276
276
|
}
|
|
277
277
|
|
|
278
278
|
/** Returns a floored seeded random value the two values passed in
|
|
279
|
-
* @param {
|
|
280
|
-
* @param {
|
|
281
|
-
* @return {
|
|
279
|
+
* @param {number} valueA
|
|
280
|
+
* @param {number} [valueB]
|
|
281
|
+
* @return {number} */
|
|
282
282
|
int(valueA, valueB=0) { return Math.floor(this.float(valueA, valueB)); }
|
|
283
283
|
|
|
284
284
|
/** Randomly returns either -1 or 1 deterministically
|
|
285
|
-
* @return {
|
|
285
|
+
* @return {number} */
|
|
286
286
|
sign() { return this.float() > .5 ? 1 : -1; }
|
|
287
287
|
}
|
|
288
288
|
|
|
@@ -290,8 +290,8 @@ class RandomGenerator
|
|
|
290
290
|
|
|
291
291
|
/**
|
|
292
292
|
* Create a 2d vector, can take another Vector2 to copy, 2 scalars, or 1 scalar
|
|
293
|
-
* @param {
|
|
294
|
-
* @param {
|
|
293
|
+
* @param {Vector2|number} [x]
|
|
294
|
+
* @param {number} [y]
|
|
295
295
|
* @return {Vector2}
|
|
296
296
|
* @example
|
|
297
297
|
* let a = vec2(0, 1); // vector with coordinates (0, 1)
|
|
@@ -310,7 +310,7 @@ function vec2(x=0, y)
|
|
|
310
310
|
/**
|
|
311
311
|
* Check if object is a valid Vector2
|
|
312
312
|
* @param {any} v
|
|
313
|
-
* @return {
|
|
313
|
+
* @return {boolean}
|
|
314
314
|
* @memberof Utilities
|
|
315
315
|
*/
|
|
316
316
|
function isVector2(v) { return v instanceof Vector2; }
|
|
@@ -327,20 +327,20 @@ function isVector2(v) { return v instanceof Vector2; }
|
|
|
327
327
|
class Vector2
|
|
328
328
|
{
|
|
329
329
|
/** Create a 2D vector with the x and y passed in, can also be created with vec2()
|
|
330
|
-
* @param {
|
|
331
|
-
* @param {
|
|
330
|
+
* @param {number} [x] - X axis location
|
|
331
|
+
* @param {number} [y] - Y axis location */
|
|
332
332
|
constructor(x=0, y=0)
|
|
333
333
|
{
|
|
334
|
-
/** @property {
|
|
334
|
+
/** @property {number} - X axis location */
|
|
335
335
|
this.x = x;
|
|
336
|
-
/** @property {
|
|
336
|
+
/** @property {number} - Y axis location */
|
|
337
337
|
this.y = y;
|
|
338
338
|
ASSERT(this.isValid());
|
|
339
339
|
}
|
|
340
340
|
|
|
341
341
|
/** Sets values of this vector and returns self
|
|
342
|
-
* @param {
|
|
343
|
-
* @param {
|
|
342
|
+
* @param {number} [x] - X axis location
|
|
343
|
+
* @param {number} [y] - Y axis location
|
|
344
344
|
* @return {Vector2} */
|
|
345
345
|
set(x=0, y=0)
|
|
346
346
|
{
|
|
@@ -391,7 +391,7 @@ class Vector2
|
|
|
391
391
|
}
|
|
392
392
|
|
|
393
393
|
/** Returns a copy of this vector scaled by the vector passed in
|
|
394
|
-
* @param {
|
|
394
|
+
* @param {number} s - scale
|
|
395
395
|
* @return {Vector2} */
|
|
396
396
|
scale(s)
|
|
397
397
|
{
|
|
@@ -400,16 +400,16 @@ class Vector2
|
|
|
400
400
|
}
|
|
401
401
|
|
|
402
402
|
/** Returns the length of this vector
|
|
403
|
-
* @return {
|
|
403
|
+
* @return {number} */
|
|
404
404
|
length() { return this.lengthSquared()**.5; }
|
|
405
405
|
|
|
406
406
|
/** Returns the length of this vector squared
|
|
407
|
-
* @return {
|
|
407
|
+
* @return {number} */
|
|
408
408
|
lengthSquared() { return this.x**2 + this.y**2; }
|
|
409
409
|
|
|
410
410
|
/** Returns the distance from this vector to vector passed in
|
|
411
411
|
* @param {Vector2} v - other vector
|
|
412
|
-
* @return {
|
|
412
|
+
* @return {number} */
|
|
413
413
|
distance(v)
|
|
414
414
|
{
|
|
415
415
|
ASSERT(isVector2(v));
|
|
@@ -418,7 +418,7 @@ class Vector2
|
|
|
418
418
|
|
|
419
419
|
/** Returns the distance squared from this vector to vector passed in
|
|
420
420
|
* @param {Vector2} v - other vector
|
|
421
|
-
* @return {
|
|
421
|
+
* @return {number} */
|
|
422
422
|
distanceSquared(v)
|
|
423
423
|
{
|
|
424
424
|
ASSERT(isVector2(v));
|
|
@@ -426,7 +426,7 @@ class Vector2
|
|
|
426
426
|
}
|
|
427
427
|
|
|
428
428
|
/** Returns a new vector in same direction as this one with the length passed in
|
|
429
|
-
* @param {
|
|
429
|
+
* @param {number} [length]
|
|
430
430
|
* @return {Vector2} */
|
|
431
431
|
normalize(length=1)
|
|
432
432
|
{
|
|
@@ -435,7 +435,7 @@ class Vector2
|
|
|
435
435
|
}
|
|
436
436
|
|
|
437
437
|
/** Returns a new vector clamped to length passed in
|
|
438
|
-
* @param {
|
|
438
|
+
* @param {number} [length]
|
|
439
439
|
* @return {Vector2} */
|
|
440
440
|
clampLength(length=1)
|
|
441
441
|
{
|
|
@@ -445,7 +445,7 @@ class Vector2
|
|
|
445
445
|
|
|
446
446
|
/** Returns the dot product of this and the vector passed in
|
|
447
447
|
* @param {Vector2} v - other vector
|
|
448
|
-
* @return {
|
|
448
|
+
* @return {number} */
|
|
449
449
|
dot(v)
|
|
450
450
|
{
|
|
451
451
|
ASSERT(isVector2(v));
|
|
@@ -454,7 +454,7 @@ class Vector2
|
|
|
454
454
|
|
|
455
455
|
/** Returns the cross product of this and the vector passed in
|
|
456
456
|
* @param {Vector2} v - other vector
|
|
457
|
-
* @return {
|
|
457
|
+
* @return {number} */
|
|
458
458
|
cross(v)
|
|
459
459
|
{
|
|
460
460
|
ASSERT(isVector2(v));
|
|
@@ -462,12 +462,12 @@ class Vector2
|
|
|
462
462
|
}
|
|
463
463
|
|
|
464
464
|
/** Returns the clockwise angle of this vector, up is angle 0
|
|
465
|
-
* @return {
|
|
465
|
+
* @return {number} */
|
|
466
466
|
angle() { return Math.atan2(this.x, this.y); }
|
|
467
467
|
|
|
468
468
|
/** Sets this vector with clockwise angle and length passed in
|
|
469
|
-
* @param {
|
|
470
|
-
* @param {
|
|
469
|
+
* @param {number} [angle]
|
|
470
|
+
* @param {number} [length]
|
|
471
471
|
* @return {Vector2} */
|
|
472
472
|
setAngle(angle=0, length=1)
|
|
473
473
|
{
|
|
@@ -477,7 +477,7 @@ class Vector2
|
|
|
477
477
|
}
|
|
478
478
|
|
|
479
479
|
/** Returns copy of this vector rotated by the clockwise angle passed in
|
|
480
|
-
* @param {
|
|
480
|
+
* @param {number} angle
|
|
481
481
|
* @return {Vector2} */
|
|
482
482
|
rotate(angle)
|
|
483
483
|
{
|
|
@@ -486,8 +486,8 @@ class Vector2
|
|
|
486
486
|
}
|
|
487
487
|
|
|
488
488
|
/** Set the integer direction of this vector, corresponding to multiples of 90 degree rotation (0-3)
|
|
489
|
-
* @param {
|
|
490
|
-
* @param {
|
|
489
|
+
* @param {number} [direction]
|
|
490
|
+
* @param {number} [length] */
|
|
491
491
|
setDirection(direction, length=1)
|
|
492
492
|
{
|
|
493
493
|
direction = mod(direction, 4);
|
|
@@ -497,7 +497,7 @@ class Vector2
|
|
|
497
497
|
}
|
|
498
498
|
|
|
499
499
|
/** Returns the integer direction of this vector, corresponding to multiples of 90 degree rotation (0-3)
|
|
500
|
-
* @return {
|
|
500
|
+
* @return {number} */
|
|
501
501
|
direction()
|
|
502
502
|
{ return abs(this.x) > abs(this.y) ? this.x < 0 ? 3 : 1 : this.y < 0 ? 2 : 0; }
|
|
503
503
|
|
|
@@ -510,12 +510,12 @@ class Vector2
|
|
|
510
510
|
floor() { return new Vector2(Math.floor(this.x), Math.floor(this.y)); }
|
|
511
511
|
|
|
512
512
|
/** Returns the area this vector covers as a rectangle
|
|
513
|
-
* @return {
|
|
513
|
+
* @return {number} */
|
|
514
514
|
area() { return abs(this.x * this.y); }
|
|
515
515
|
|
|
516
516
|
/** Returns a new vector that is p percent between this and the vector passed in
|
|
517
517
|
* @param {Vector2} v - other vector
|
|
518
|
-
* @param {
|
|
518
|
+
* @param {number} percent
|
|
519
519
|
* @return {Vector2} */
|
|
520
520
|
lerp(v, percent)
|
|
521
521
|
{
|
|
@@ -525,7 +525,7 @@ class Vector2
|
|
|
525
525
|
|
|
526
526
|
/** Returns true if this vector is within the bounds of an array size passed in
|
|
527
527
|
* @param {Vector2} arraySize
|
|
528
|
-
* @return {
|
|
528
|
+
* @return {boolean} */
|
|
529
529
|
arrayCheck(arraySize)
|
|
530
530
|
{
|
|
531
531
|
ASSERT(isVector2(arraySize));
|
|
@@ -533,8 +533,8 @@ class Vector2
|
|
|
533
533
|
}
|
|
534
534
|
|
|
535
535
|
/** Returns this vector expressed as a string
|
|
536
|
-
* @param {
|
|
537
|
-
* @return {
|
|
536
|
+
* @param {number} digits - precision to display
|
|
537
|
+
* @return {string} */
|
|
538
538
|
toString(digits=3)
|
|
539
539
|
{
|
|
540
540
|
if (debug)
|
|
@@ -542,7 +542,7 @@ class Vector2
|
|
|
542
542
|
}
|
|
543
543
|
|
|
544
544
|
/** Checks if this is a valid vector
|
|
545
|
-
* @return {
|
|
545
|
+
* @return {boolean} */
|
|
546
546
|
isValid()
|
|
547
547
|
{
|
|
548
548
|
return typeof this.x == 'number' && !isNaN(this.x)
|
|
@@ -554,10 +554,10 @@ class Vector2
|
|
|
554
554
|
|
|
555
555
|
/**
|
|
556
556
|
* Create a color object with RGBA values, white by default
|
|
557
|
-
* @param {
|
|
558
|
-
* @param {
|
|
559
|
-
* @param {
|
|
560
|
-
* @param {
|
|
557
|
+
* @param {number} [r=1] - red
|
|
558
|
+
* @param {number} [g=1] - green
|
|
559
|
+
* @param {number} [b=1] - blue
|
|
560
|
+
* @param {number} [a=1] - alpha
|
|
561
561
|
* @return {Color}
|
|
562
562
|
* @memberof Utilities
|
|
563
563
|
*/
|
|
@@ -565,10 +565,10 @@ function rgb(r, g, b, a) { return new Color(r, g, b, a); }
|
|
|
565
565
|
|
|
566
566
|
/**
|
|
567
567
|
* Create a color object with HSLA values, white by default
|
|
568
|
-
* @param {
|
|
569
|
-
* @param {
|
|
570
|
-
* @param {
|
|
571
|
-
* @param {
|
|
568
|
+
* @param {number} [h=0] - hue
|
|
569
|
+
* @param {number} [s=0] - saturation
|
|
570
|
+
* @param {number} [l=1] - lightness
|
|
571
|
+
* @param {number} [a=1] - alpha
|
|
572
572
|
* @return {Color}
|
|
573
573
|
* @memberof Utilities
|
|
574
574
|
*/
|
|
@@ -577,7 +577,7 @@ function hsl(h, s, l, a) { return new Color().setHSLA(h, s, l, a); }
|
|
|
577
577
|
/**
|
|
578
578
|
* Check if object is a valid Color
|
|
579
579
|
* @param {any} c
|
|
580
|
-
* @return {
|
|
580
|
+
* @return {boolean}
|
|
581
581
|
* @memberof Utilities
|
|
582
582
|
*/
|
|
583
583
|
function isColor(c) { return c instanceof Color; }
|
|
@@ -594,28 +594,28 @@ function isColor(c) { return c instanceof Color; }
|
|
|
594
594
|
class Color
|
|
595
595
|
{
|
|
596
596
|
/** Create a color with the rgba components passed in, white by default
|
|
597
|
-
* @param {
|
|
598
|
-
* @param {
|
|
599
|
-
* @param {
|
|
600
|
-
* @param {
|
|
597
|
+
* @param {number} [r] - red
|
|
598
|
+
* @param {number} [g] - green
|
|
599
|
+
* @param {number} [b] - blue
|
|
600
|
+
* @param {number} [a] - alpha*/
|
|
601
601
|
constructor(r=1, g=1, b=1, a=1)
|
|
602
602
|
{
|
|
603
|
-
/** @property {
|
|
603
|
+
/** @property {number} - Red */
|
|
604
604
|
this.r = r;
|
|
605
|
-
/** @property {
|
|
605
|
+
/** @property {number} - Green */
|
|
606
606
|
this.g = g;
|
|
607
|
-
/** @property {
|
|
607
|
+
/** @property {number} - Blue */
|
|
608
608
|
this.b = b;
|
|
609
|
-
/** @property {
|
|
609
|
+
/** @property {number} - Alpha */
|
|
610
610
|
this.a = a;
|
|
611
611
|
ASSERT(this.isValid());
|
|
612
612
|
}
|
|
613
613
|
|
|
614
614
|
/** Sets values of this color and returns self
|
|
615
|
-
* @param {
|
|
616
|
-
* @param {
|
|
617
|
-
* @param {
|
|
618
|
-
* @param {
|
|
615
|
+
* @param {number} [r] - red
|
|
616
|
+
* @param {number} [g] - green
|
|
617
|
+
* @param {number} [b] - blue
|
|
618
|
+
* @param {number} [a] - alpha
|
|
619
619
|
* @return {Color} */
|
|
620
620
|
set(r=1, g=1, b=1, a=1)
|
|
621
621
|
{
|
|
@@ -668,8 +668,8 @@ class Color
|
|
|
668
668
|
}
|
|
669
669
|
|
|
670
670
|
/** Returns a copy of this color scaled by the value passed in, alpha can be scaled separately
|
|
671
|
-
* @param {
|
|
672
|
-
* @param {
|
|
671
|
+
* @param {number} scale
|
|
672
|
+
* @param {number} [alphaScale=scale]
|
|
673
673
|
* @return {Color} */
|
|
674
674
|
scale(scale, alphaScale=scale)
|
|
675
675
|
{ return new Color(this.r*scale, this.g*scale, this.b*scale, this.a*alphaScale); }
|
|
@@ -680,7 +680,7 @@ class Color
|
|
|
680
680
|
|
|
681
681
|
/** Returns a new color that is p percent between this and the color passed in
|
|
682
682
|
* @param {Color} c - other color
|
|
683
|
-
* @param {
|
|
683
|
+
* @param {number} percent
|
|
684
684
|
* @return {Color} */
|
|
685
685
|
lerp(c, percent)
|
|
686
686
|
{
|
|
@@ -689,10 +689,10 @@ class Color
|
|
|
689
689
|
}
|
|
690
690
|
|
|
691
691
|
/** Sets this color given a hue, saturation, lightness, and alpha
|
|
692
|
-
* @param {
|
|
693
|
-
* @param {
|
|
694
|
-
* @param {
|
|
695
|
-
* @param {
|
|
692
|
+
* @param {number} [h] - hue
|
|
693
|
+
* @param {number} [s] - saturation
|
|
694
|
+
* @param {number} [l] - lightness
|
|
695
|
+
* @param {number} [a] - alpha
|
|
696
696
|
* @return {Color} */
|
|
697
697
|
setHSLA(h=0, s=0, l=1, a=1)
|
|
698
698
|
{
|
|
@@ -713,7 +713,7 @@ class Color
|
|
|
713
713
|
}
|
|
714
714
|
|
|
715
715
|
/** Returns this color expressed in hsla format
|
|
716
|
-
* @return {Array} */
|
|
716
|
+
* @return {Array<number>} */
|
|
717
717
|
HSLA()
|
|
718
718
|
{
|
|
719
719
|
const r = clamp(this.r);
|
|
@@ -740,8 +740,8 @@ class Color
|
|
|
740
740
|
}
|
|
741
741
|
|
|
742
742
|
/** Returns a new color that has each component randomly adjusted
|
|
743
|
-
* @param {
|
|
744
|
-
* @param {
|
|
743
|
+
* @param {number} [amount]
|
|
744
|
+
* @param {number} [alphaAmount]
|
|
745
745
|
* @return {Color} */
|
|
746
746
|
mutate(amount=.05, alphaAmount=0)
|
|
747
747
|
{
|
|
@@ -755,8 +755,8 @@ class Color
|
|
|
755
755
|
}
|
|
756
756
|
|
|
757
757
|
/** Returns this color expressed as a hex color code
|
|
758
|
-
* @param {
|
|
759
|
-
* @return {
|
|
758
|
+
* @param {boolean} [useAlpha] - if alpha should be included in result
|
|
759
|
+
* @return {string} */
|
|
760
760
|
toString(useAlpha = true)
|
|
761
761
|
{
|
|
762
762
|
const toHex = (c)=> ((c=clamp(c)*255|0)<16 ? '0' : '') + c.toString(16);
|
|
@@ -764,7 +764,7 @@ class Color
|
|
|
764
764
|
}
|
|
765
765
|
|
|
766
766
|
/** Set this color from a hex code
|
|
767
|
-
* @param {
|
|
767
|
+
* @param {string} hex - html hex code
|
|
768
768
|
* @return {Color} */
|
|
769
769
|
setHex(hex)
|
|
770
770
|
{
|
|
@@ -793,7 +793,7 @@ class Color
|
|
|
793
793
|
}
|
|
794
794
|
|
|
795
795
|
/** Returns this color expressed as 32 bit RGBA value
|
|
796
|
-
* @return {
|
|
796
|
+
* @return {number} */
|
|
797
797
|
rgbaInt()
|
|
798
798
|
{
|
|
799
799
|
const r = clamp(this.r)*255|0;
|
|
@@ -804,7 +804,7 @@ class Color
|
|
|
804
804
|
}
|
|
805
805
|
|
|
806
806
|
/** Checks if this is a valid color
|
|
807
|
-
* @return {
|
|
807
|
+
* @return {boolean} */
|
|
808
808
|
isValid()
|
|
809
809
|
{
|
|
810
810
|
return typeof this.r == 'number' && !isNaN(this.r)
|
|
@@ -886,41 +886,41 @@ const MAGENTA = rgb(1,0,1);
|
|
|
886
886
|
class Timer
|
|
887
887
|
{
|
|
888
888
|
/** Create a timer object set time passed in
|
|
889
|
-
* @param {
|
|
889
|
+
* @param {number} [timeLeft] - How much time left before the timer elapses in seconds */
|
|
890
890
|
constructor(timeLeft) { this.time = timeLeft == undefined ? undefined : time + timeLeft; this.setTime = timeLeft; }
|
|
891
891
|
|
|
892
892
|
/** Set the timer with seconds passed in
|
|
893
|
-
* @param {
|
|
893
|
+
* @param {number} [timeLeft] - How much time left before the timer is elapsed in seconds */
|
|
894
894
|
set(timeLeft=0) { this.time = time + timeLeft; this.setTime = timeLeft; }
|
|
895
895
|
|
|
896
896
|
/** Unset the timer */
|
|
897
897
|
unset() { this.time = undefined; }
|
|
898
898
|
|
|
899
899
|
/** Returns true if set
|
|
900
|
-
* @return {
|
|
900
|
+
* @return {boolean} */
|
|
901
901
|
isSet() { return this.time != undefined; }
|
|
902
902
|
|
|
903
903
|
/** Returns true if set and has not elapsed
|
|
904
|
-
* @return {
|
|
904
|
+
* @return {boolean} */
|
|
905
905
|
active() { return time < this.time; }
|
|
906
906
|
|
|
907
907
|
/** Returns true if set and elapsed
|
|
908
|
-
* @return {
|
|
908
|
+
* @return {boolean} */
|
|
909
909
|
elapsed() { return time >= this.time; }
|
|
910
910
|
|
|
911
911
|
/** Get how long since elapsed, returns 0 if not set (returns negative if currently active)
|
|
912
|
-
* @return {
|
|
912
|
+
* @return {number} */
|
|
913
913
|
get() { return this.isSet()? time - this.time : 0; }
|
|
914
914
|
|
|
915
915
|
/** Get percentage elapsed based on time it was set to, returns 0 if not set
|
|
916
|
-
* @return {
|
|
916
|
+
* @return {number} */
|
|
917
917
|
getPercent() { return this.isSet()? 1-percent(this.time - time, 0, this.setTime) : 0; }
|
|
918
918
|
|
|
919
919
|
/** Returns this timer expressed as a string
|
|
920
|
-
* @return {
|
|
920
|
+
* @return {string} */
|
|
921
921
|
toString() { if (debug) { return this.isSet() ? Math.abs(this.get()) + ' seconds ' + (this.get()<0 ? 'before' : 'after' ) : 'unset'; }}
|
|
922
922
|
|
|
923
923
|
/** Get how long since elapsed, returns 0 if not set (returns negative if currently active)
|
|
924
|
-
* @return {
|
|
924
|
+
* @return {number} */
|
|
925
925
|
valueOf() { return this.get(); }
|
|
926
926
|
}
|