littlejsengine 1.6.0 → 1.6.6
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 +138 -34
- package/build/littlejs.d.ts +104 -92
- package/build/littlejs.esm.js +414 -342
- package/build/littlejs.esm.min.js +1 -1
- package/build/littlejs.js +365 -296
- package/build/littlejs.min.js +1 -1
- package/build/littlejs.release.js +359 -291
- package/examples/breakout/game.js +5 -0
- package/examples/breakout/gameObjects.js +41 -48
- package/examples/breakout/index.html +3 -3
- package/examples/breakoutTutorial/README.md +514 -0
- package/examples/breakoutTutorial/game.js +181 -0
- package/examples/breakoutTutorial/images/1.png +0 -0
- package/examples/breakoutTutorial/images/10.png +0 -0
- package/examples/breakoutTutorial/images/11.png +0 -0
- package/examples/breakoutTutorial/images/2.png +0 -0
- package/examples/breakoutTutorial/images/3.png +0 -0
- package/examples/breakoutTutorial/images/4.png +0 -0
- package/examples/breakoutTutorial/images/5.png +0 -0
- package/examples/breakoutTutorial/images/6.png +0 -0
- package/examples/breakoutTutorial/images/7.png +0 -0
- package/examples/breakoutTutorial/images/8.png +0 -0
- package/examples/breakoutTutorial/images/9.png +0 -0
- package/examples/breakoutTutorial/index.html +10 -0
- package/examples/empty/game.js +10 -0
- package/examples/favicon.png +0 -0
- package/examples/module/index.html +1 -1
- package/examples/particles/index.html +1 -1
- package/examples/platformer/gameLevel.js +1 -1
- package/examples/platformer/gamePlayer.js +1 -1
- package/examples/platformer/index.html +6 -6
- package/examples/puzzle/game.js +1 -1
- package/examples/puzzle/index.html +2 -2
- package/examples/starter/build.bat +3 -2
- package/examples/starter/game.js +9 -9
- package/examples/starter/index.html +13 -13
- package/examples/stress/index.html +1 -1
- package/examples/typescript/game.js +89 -89
- package/examples/typescript/game.ts +10 -10
- package/examples/typescript/index.html +1 -1
- package/package.json +4 -4
- package/src/engine.js +29 -31
- package/src/engineAudio.js +53 -29
- package/src/engineBuild.bat +3 -1
- package/src/engineDebug.js +25 -24
- package/src/engineDraw.js +43 -39
- package/src/engineExport.js +49 -46
- package/src/engineInput.js +83 -74
- package/src/engineMedals.js +31 -8
- package/src/engineObject.js +25 -25
- package/src/engineParticles.js +3 -6
- package/src/engineRelease.js +19 -19
- package/src/engineSettings.js +4 -4
- package/src/engineTileLayer.js +18 -14
- package/src/engineUtilities.js +43 -34
- package/src/engineWebGL.js +8 -8
package/src/engineUtilities.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* LittleJS Utility Classes and Functions
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
* - General purpose math library
|
|
4
|
+
* - Vector2 - fast, simple, easy 2D vector class
|
|
5
|
+
* - Color - holds a rgba color with some math functions
|
|
6
|
+
* - Timer - tracks time automatically
|
|
7
7
|
* @namespace Utilities
|
|
8
8
|
*/
|
|
9
9
|
|
|
@@ -19,34 +19,34 @@ const PI = Math.PI;
|
|
|
19
19
|
* @param {Number} value
|
|
20
20
|
* @return {Number}
|
|
21
21
|
* @memberof Utilities */
|
|
22
|
-
|
|
22
|
+
function abs(a) { return a < 0 ? -a : a; }
|
|
23
23
|
|
|
24
24
|
/** Returns lowest of two values passed in
|
|
25
25
|
* @param {Number} valueA
|
|
26
26
|
* @param {Number} valueB
|
|
27
27
|
* @return {Number}
|
|
28
28
|
* @memberof Utilities */
|
|
29
|
-
|
|
29
|
+
function min(a, b) { return a < b ? a : b; }
|
|
30
30
|
|
|
31
31
|
/** Returns highest of two values passed in
|
|
32
32
|
* @param {Number} valueA
|
|
33
33
|
* @param {Number} valueB
|
|
34
34
|
* @return {Number}
|
|
35
35
|
* @memberof Utilities */
|
|
36
|
-
|
|
36
|
+
function max(a, b) { return a > b ? a : b; }
|
|
37
37
|
|
|
38
38
|
/** Returns the sign of value passed in (also returns 1 if 0)
|
|
39
39
|
* @param {Number} value
|
|
40
40
|
* @return {Number}
|
|
41
41
|
* @memberof Utilities */
|
|
42
|
-
|
|
42
|
+
function sign(a) { return a < 0 ? -1 : 1; }
|
|
43
43
|
|
|
44
44
|
/** Returns first parm modulo the second param, but adjusted so negative numbers work as expected
|
|
45
45
|
* @param {Number} dividend
|
|
46
46
|
* @param {Number} [divisor=1]
|
|
47
47
|
* @return {Number}
|
|
48
48
|
* @memberof Utilities */
|
|
49
|
-
|
|
49
|
+
function mod(a, b=1) { return ((a % b) + b) % b; }
|
|
50
50
|
|
|
51
51
|
/** Clamps the value beween max and min
|
|
52
52
|
* @param {Number} value
|
|
@@ -54,7 +54,8 @@ const mod = (a, b=1)=> ((a % b) + b) % b;
|
|
|
54
54
|
* @param {Number} [max=1]
|
|
55
55
|
* @return {Number}
|
|
56
56
|
* @memberof Utilities */
|
|
57
|
-
|
|
57
|
+
function clamp(v, min=0, max=1)
|
|
58
|
+
{ return v < min ? min : v > max ? max : v; }
|
|
58
59
|
|
|
59
60
|
/** Returns what percentage the value is between max and min
|
|
60
61
|
* @param {Number} value
|
|
@@ -62,7 +63,8 @@ const clamp = (v, min=0, max=1)=> v < min ? min : v > max ? max : v;
|
|
|
62
63
|
* @param {Number} [max=1]
|
|
63
64
|
* @return {Number}
|
|
64
65
|
* @memberof Utilities */
|
|
65
|
-
|
|
66
|
+
function percent(v, min=0, max=1)
|
|
67
|
+
{ return max-min ? clamp((v-min) / (max-min)) : 0; }
|
|
66
68
|
|
|
67
69
|
/** Linearly interpolates the percent value between max and min
|
|
68
70
|
* @param {Number} percent
|
|
@@ -70,19 +72,19 @@ const percent = (v, min=0, max=1)=> max-min ? clamp((v-min) / (max-min)) : 0;
|
|
|
70
72
|
* @param {Number} [max=1]
|
|
71
73
|
* @return {Number}
|
|
72
74
|
* @memberof Utilities */
|
|
73
|
-
|
|
75
|
+
function lerp(p, min=0, max=1){ return min + clamp(p) * (max-min); }
|
|
74
76
|
|
|
75
77
|
/** Applies smoothstep function to the percentage value
|
|
76
78
|
* @param {Number} value
|
|
77
79
|
* @return {Number}
|
|
78
80
|
* @memberof Utilities */
|
|
79
|
-
|
|
81
|
+
function smoothStep(p) { return p * p * (3 - 2 * p); }
|
|
80
82
|
|
|
81
83
|
/** Returns the nearest power of two not less then the value
|
|
82
84
|
* @param {Number} value
|
|
83
85
|
* @return {Number}
|
|
84
86
|
* @memberof Utilities */
|
|
85
|
-
|
|
87
|
+
function nearestPowerOfTwo(v) { return 2**Math.ceil(Math.log2(v)); }
|
|
86
88
|
|
|
87
89
|
/** Returns true if two axis aligned bounding boxes are overlapping
|
|
88
90
|
* @param {Vector2} pointA - Center of box A
|
|
@@ -91,7 +93,8 @@ const nearestPowerOfTwo = (v)=> 2**Math.ceil(Math.log2(v));
|
|
|
91
93
|
* @param {Vector2} [sizeB] - Size of box B
|
|
92
94
|
* @return {Boolean} - True if overlapping
|
|
93
95
|
* @memberof Utilities */
|
|
94
|
-
|
|
96
|
+
function isOverlapping(pA, sA, pB, sB)
|
|
97
|
+
{ return abs(pA.x - pB.x)*2 < sA.x + sB.x && abs(pA.y - pB.y)*2 < sA.y + sB.y; }
|
|
95
98
|
|
|
96
99
|
/** Returns an oscillating wave between 0 and amplitude with frequency of 1 Hz by default
|
|
97
100
|
* @param {Number} [frequency=1] - Frequency of the wave in Hz
|
|
@@ -99,13 +102,14 @@ const isOverlapping = (pA, sA, pB, sB)=> abs(pA.x - pB.x)*2 < sA.x + sB.x && abs
|
|
|
99
102
|
* @param {Number} [t=time] - Value to use for time of the wave
|
|
100
103
|
* @return {Number} - Value waving between 0 and amplitude
|
|
101
104
|
* @memberof Utilities */
|
|
102
|
-
|
|
105
|
+
function wave(frequency=1, amplitude=1, t=time)
|
|
106
|
+
{ return amplitude/2 * (1 - Math.cos(t*frequency*2*PI)); }
|
|
103
107
|
|
|
104
108
|
/** Formats seconds to mm:ss style for display purposes
|
|
105
109
|
* @param {Number} t - time in seconds
|
|
106
110
|
* @return {String}
|
|
107
111
|
* @memberof Utilities */
|
|
108
|
-
|
|
112
|
+
function formatTime(t) { return (t/60|0) + ':' + (t%60<10?'0':'') + (t%60|0); }
|
|
109
113
|
|
|
110
114
|
///////////////////////////////////////////////////////////////////////////////
|
|
111
115
|
|
|
@@ -117,32 +121,33 @@ const formatTime = (t)=> (t/60|0) + ':' + (t%60<10?'0':'') + (t%60|0);
|
|
|
117
121
|
* @param {Number} [valueB=0]
|
|
118
122
|
* @return {Number}
|
|
119
123
|
* @memberof Random */
|
|
120
|
-
|
|
124
|
+
function rand(a=1, b=0) { return b + (a-b)*Math.random(); }
|
|
121
125
|
|
|
122
126
|
/** Returns a floored random value the two values passed in
|
|
123
127
|
* @param {Number} [valueA=1]
|
|
124
128
|
* @param {Number} [valueB=0]
|
|
125
129
|
* @return {Number}
|
|
126
130
|
* @memberof Random */
|
|
127
|
-
|
|
131
|
+
function randInt(a=1, b=0) { return rand(a,b)|0; }
|
|
128
132
|
|
|
129
133
|
/** Randomly returns either -1 or 1
|
|
130
134
|
* @return {Number}
|
|
131
135
|
* @memberof Random */
|
|
132
|
-
|
|
136
|
+
function randSign() { return randInt(2) * 2 - 1; }
|
|
133
137
|
|
|
134
138
|
/** Returns a random Vector2 within a circular shape
|
|
135
139
|
* @param {Number} [radius=1]
|
|
136
140
|
* @param {Number} [minRadius=0]
|
|
137
141
|
* @return {Vector2}
|
|
138
142
|
* @memberof Random */
|
|
139
|
-
|
|
143
|
+
function randInCircle(radius=1, minRadius=0)
|
|
144
|
+
{ return radius > 0 ? randVector(radius * rand(minRadius / radius, 1)**.5) : new Vector2; }
|
|
140
145
|
|
|
141
146
|
/** Returns a random Vector2 with the passed in length
|
|
142
147
|
* @param {Number} [length=1]
|
|
143
148
|
* @return {Vector2}
|
|
144
149
|
* @memberof Random */
|
|
145
|
-
|
|
150
|
+
function randVector(length=1) { return new Vector2().setAngle(rand(2*PI), length); }
|
|
146
151
|
|
|
147
152
|
/** Returns a random color between the two passed in colors, combine components if linear
|
|
148
153
|
* @param {Color} [colorA=Color()]
|
|
@@ -150,8 +155,8 @@ const randVector = (length=1)=> new Vector2().setAngle(rand(2*PI), length);
|
|
|
150
155
|
* @param {Boolean} [linear]
|
|
151
156
|
* @return {Color}
|
|
152
157
|
* @memberof Random */
|
|
153
|
-
|
|
154
|
-
|
|
158
|
+
function randColor(cA = new Color, cB = new Color(0,0,0,1), linear)
|
|
159
|
+
{ return linear ? cA.lerp(cB, rand()) : new Color(rand(cA.r,cB.r),rand(cA.g,cB.g),rand(cA.b,cB.b),rand(cA.a,cB.a)); }
|
|
155
160
|
|
|
156
161
|
/** Seed used by the randSeeded function
|
|
157
162
|
* @type {Number}
|
|
@@ -162,16 +167,19 @@ let randSeed = 1;
|
|
|
162
167
|
/** Set seed used by the randSeeded function, should not be 0
|
|
163
168
|
* @param {Number} seed
|
|
164
169
|
* @memberof Random */
|
|
165
|
-
|
|
170
|
+
function setRandSeed(seed) { randSeed = seed; }
|
|
166
171
|
|
|
167
172
|
/** Returns a seeded random value between the two values passed in using randSeed
|
|
168
173
|
* @param {Number} [valueA=1]
|
|
169
174
|
* @param {Number} [valueB=0]
|
|
170
175
|
* @return {Number}
|
|
171
176
|
* @memberof Random */
|
|
172
|
-
|
|
177
|
+
function randSeeded(a=1, b=0)
|
|
173
178
|
{
|
|
174
|
-
|
|
179
|
+
// xorshift algorithm
|
|
180
|
+
randSeed ^= randSeed << 13;
|
|
181
|
+
randSeed ^= randSeed >>> 17;
|
|
182
|
+
randSeed ^= randSeed << 5;
|
|
175
183
|
return b + (a-b) * abs(randSeed % 1e9) / 1e9;
|
|
176
184
|
}
|
|
177
185
|
|
|
@@ -189,7 +197,8 @@ const randSeeded = (a=1, b=0)=>
|
|
|
189
197
|
* b = vec2(); // set b to (0, 0)
|
|
190
198
|
* @memberof Utilities
|
|
191
199
|
*/
|
|
192
|
-
|
|
200
|
+
function vec2(x=0, y)
|
|
201
|
+
{ return x.x == undefined ? new Vector2(x, y == undefined? x : y) : new Vector2(x.x, x.y); }
|
|
193
202
|
|
|
194
203
|
/**
|
|
195
204
|
* Check if object is a valid Vector2
|
|
@@ -197,11 +206,11 @@ const vec2 = (x=0, y)=> x.x == undefined ? new Vector2(x, y == undefined? x : y)
|
|
|
197
206
|
* @return {Boolean}
|
|
198
207
|
* @memberof Utilities
|
|
199
208
|
*/
|
|
200
|
-
|
|
209
|
+
function isVector2(v) { return !isNaN(v.x) && !isNaN(v.y); }
|
|
201
210
|
|
|
202
211
|
/**
|
|
203
212
|
* 2D Vector object with vector math library
|
|
204
|
-
*
|
|
213
|
+
* - Functions do not change this so they can be chained together
|
|
205
214
|
* @example
|
|
206
215
|
* let a = new Vector2(2, 3); // vector with coordinates (2, 3)
|
|
207
216
|
* let b = new Vector2; // vector with coordinates (0, 0)
|
|
@@ -348,7 +357,7 @@ class Vector2
|
|
|
348
357
|
* @return {Color}
|
|
349
358
|
* @memberof Utilities
|
|
350
359
|
*/
|
|
351
|
-
|
|
360
|
+
function rgb(r, g, b, a) { return new Color(r, g, b, a); }
|
|
352
361
|
|
|
353
362
|
/**
|
|
354
363
|
* Create a color object with HSLA values
|
|
@@ -359,7 +368,7 @@ const colorRGBA = (r, g, b, a)=> new Color(r, g, b, a);
|
|
|
359
368
|
* @return {Color}
|
|
360
369
|
* @memberof Utilities
|
|
361
370
|
*/
|
|
362
|
-
|
|
371
|
+
function hsl(h, s, l, a) { return new Color().setHSLA(h, s, l, a); }
|
|
363
372
|
|
|
364
373
|
/**
|
|
365
374
|
* Color object (red, green, blue, alpha) with some helpful functions
|
|
@@ -367,8 +376,8 @@ const colorHSLA = (h, s, l, a)=> new Color().setHSLA(h, s, l, a);
|
|
|
367
376
|
* let a = new Color; // white
|
|
368
377
|
* let b = new Color(1, 0, 0); // red
|
|
369
378
|
* let c = new Color(0, 0, 0, 0); // transparent black
|
|
370
|
-
* let d =
|
|
371
|
-
* let e =
|
|
379
|
+
* let d = RGB(0, 0, 1); // blue using rgb color
|
|
380
|
+
* let e = HSL(.3, 1, .5); // green using hsl color
|
|
372
381
|
*/
|
|
373
382
|
class Color
|
|
374
383
|
{
|
package/src/engineWebGL.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* LittleJS WebGL Interface
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
3
|
+
* - All webgl used by the engine is wrapped up here
|
|
4
|
+
* - For normal stuff you won't need to see or call anything in this file
|
|
5
|
+
* - For advanced stuff there are helper functions to create shaders, textures, etc
|
|
6
|
+
* - Can be disabled with glEnable to revert to 2D canvas rendering
|
|
7
|
+
* - Batches sprite rendering on GPU for incredibly fast performance
|
|
8
|
+
* - Sprite transform math is done in the shader where possible
|
|
9
9
|
* @namespace WebGL
|
|
10
10
|
*/
|
|
11
11
|
|
|
@@ -80,7 +80,7 @@ function glSetBlendMode(additive)
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
/** Set the WebGl texture, not normally necessary unless multiple tile sheets are used
|
|
83
|
-
*
|
|
83
|
+
* - This may also flush the gl buffer resulting in more draw calls and worse performance
|
|
84
84
|
* @param {WebGLTexture} [texture=glTileTexture]
|
|
85
85
|
* @memberof WebGL */
|
|
86
86
|
function glSetTexture(texture=glTileTexture)
|
|
@@ -141,7 +141,7 @@ function glCreateTexture(image)
|
|
|
141
141
|
image && image.width && glContext.texImage2D(gl_TEXTURE_2D, 0, gl_RGBA, gl_RGBA, gl_UNSIGNED_BYTE, image);
|
|
142
142
|
|
|
143
143
|
// use point filtering for pixelated rendering
|
|
144
|
-
const filter =
|
|
144
|
+
const filter = canvasPixelated ? gl_NEAREST : gl_LINEAR;
|
|
145
145
|
glContext.texParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, filter);
|
|
146
146
|
glContext.texParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, filter);
|
|
147
147
|
glContext.texParameteri(gl_TEXTURE_2D, gl_TEXTURE_WRAP_S, gl_CLAMP_TO_EDGE);
|