littlejsengine 1.13.1 → 1.13.4
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 +15 -12
- package/dist/littlejs.d.ts +94 -74
- package/dist/littlejs.esm.js +422 -306
- package/dist/littlejs.esm.min.js +1 -1
- package/dist/littlejs.js +416 -306
- package/dist/littlejs.min.js +1 -1
- package/dist/littlejs.release.js +414 -304
- package/examples/box2d/game.js +1 -1
- package/examples/box2d/gameObjects.js +1 -1
- package/examples/box2d/scenes.js +1 -1
- package/examples/breakoutTutorial/README.md +44 -41
- package/examples/breakoutTutorial/game.js +2 -2
- package/examples/electron/build.bat +7 -0
- package/examples/electron/build.js +124 -0
- package/examples/electron/electron.js +35 -0
- package/examples/electron/game.js +140 -0
- package/examples/electron/index.html +13 -0
- package/examples/electron/package.json +12 -0
- package/examples/electron/tiles.png +0 -0
- package/examples/empty/game.js +1 -0
- package/examples/htmlMenu/game.js +1 -1
- package/examples/index.html +154 -93
- package/examples/module/game.js +5 -2
- package/examples/particles/index.html +12 -3
- package/examples/platformer/gameEffects.js +3 -3
- package/examples/shorts/base.html +1 -1
- package/examples/shorts/blending.js +9 -5
- package/examples/shorts/box2d.js +1 -1
- package/examples/shorts/box2dCar.js +1 -1
- package/examples/shorts/clock.js +1 -1
- package/examples/shorts/colors.js +2 -2
- package/examples/shorts/maze.js +1 -1
- package/examples/shorts/nineSlice.js +9 -9
- package/examples/shorts/piano.js +2 -2
- package/examples/shorts/raycasting.js +2 -2
- package/examples/shorts/shapes.js +9 -9
- package/examples/shorts/slidingPuzzle.js +2 -2
- package/examples/shorts/starfield.js +5 -7
- package/examples/shorts/systemFont.js +1 -1
- package/examples/shorts/uiSystem.js +1 -0
- package/examples/starter/build.js +9 -8
- package/examples/starter/game.js +5 -2
- package/examples/starter/index.html +2 -2
- package/examples/stress/index.html +5 -5
- package/examples/style.css +5 -2
- package/examples/typescript/build.js +1 -1
- package/examples/typescript/game.js +2 -2
- package/examples/typescript/game.ts +5 -2
- package/examples/uiSystem/game.js +2 -1
- package/package.json +2 -2
- package/plugins/box2d.js +19 -94
- package/plugins/drawUtilities.js +24 -18
- package/plugins/postProcess.js +7 -0
- package/plugins/uiSystem.js +4 -4
- package/src/engine.js +7 -2
- package/src/engineAudio.js +1 -1
- package/src/engineDebug.js +2 -2
- package/src/engineDraw.js +243 -150
- package/src/engineExport.js +6 -0
- package/src/engineInput.js +2 -2
- package/src/engineMedals.js +4 -4
- package/src/engineObject.js +3 -3
- package/src/engineParticles.js +8 -1
- package/src/engineSettings.js +55 -8
- package/src/engineUtilities.js +7 -7
- package/src/engineWebGL.js +25 -5
package/src/engineSettings.js
CHANGED
|
@@ -30,6 +30,13 @@ let cameraScale = 32;
|
|
|
30
30
|
///////////////////////////////////////////////////////////////////////////////
|
|
31
31
|
// Display settings
|
|
32
32
|
|
|
33
|
+
/** Enable applying color to tiles when using canvas2d
|
|
34
|
+
* - This is slower but should be the same as webgl rendering
|
|
35
|
+
* @type {boolean}
|
|
36
|
+
* @default
|
|
37
|
+
* @memberof Settings */
|
|
38
|
+
let canvasColorTiles = true;
|
|
39
|
+
|
|
33
40
|
/** The max size of the canvas, centered if window is larger
|
|
34
41
|
* @type {Vector2}
|
|
35
42
|
* @default Vector2(1920,1080)
|
|
@@ -43,14 +50,21 @@ let canvasMaxSize = vec2(1920, 1080);
|
|
|
43
50
|
* @memberof Settings */
|
|
44
51
|
let canvasFixedSize = vec2();
|
|
45
52
|
|
|
46
|
-
/** Use nearest
|
|
47
|
-
* - Must be set before startup to take effect
|
|
53
|
+
/** Use nearest canvas scaling for more pixelated look
|
|
48
54
|
* - If enabled sets css image-rendering:pixelated
|
|
49
55
|
* @type {boolean}
|
|
50
56
|
* @default
|
|
51
57
|
* @memberof Settings */
|
|
52
58
|
let canvasPixelated = true;
|
|
53
59
|
|
|
60
|
+
/** Use nearest canvas scaling for more pixelated look
|
|
61
|
+
* - If enabled sets css image-rendering:pixelated
|
|
62
|
+
* - This defaults to false because text looks better with smoothing
|
|
63
|
+
* @type {boolean}
|
|
64
|
+
* @default
|
|
65
|
+
* @memberof Settings */
|
|
66
|
+
let overlayCanvasPixelated = false;
|
|
67
|
+
|
|
54
68
|
/** Disables texture filtering for crisper pixel art
|
|
55
69
|
* @type {boolean}
|
|
56
70
|
* @default
|
|
@@ -167,13 +181,13 @@ let particleEmitRateScale = 1;
|
|
|
167
181
|
* @memberof Settings */
|
|
168
182
|
let gamepadsEnable = true;
|
|
169
183
|
|
|
170
|
-
/** If true, the dpad input is also routed to the left analog stick (for better
|
|
184
|
+
/** If true, the dpad input is also routed to the left analog stick (for better accessibility)
|
|
171
185
|
* @type {boolean}
|
|
172
186
|
* @default
|
|
173
187
|
* @memberof Settings */
|
|
174
188
|
let gamepadDirectionEmulateStick = true;
|
|
175
189
|
|
|
176
|
-
/** If true the WASD keys are also routed to the direction keys (for better
|
|
190
|
+
/** If true the WASD keys are also routed to the direction keys (for better accessibility)
|
|
177
191
|
* @type {boolean}
|
|
178
192
|
* @default
|
|
179
193
|
* @memberof Settings */
|
|
@@ -291,6 +305,14 @@ function setCameraAngle(angle) { cameraAngle = angle; }
|
|
|
291
305
|
* @memberof Settings */
|
|
292
306
|
function setCameraScale(scale) { cameraScale = scale; }
|
|
293
307
|
|
|
308
|
+
/** Set if tiles should be colorized when using canvas2d
|
|
309
|
+
* This can be slower but results should look nearly identical to webgl rendering
|
|
310
|
+
* It can be enabled/disabled at any time
|
|
311
|
+
* Optimized for performance, and will use faster method if color is white or untextured
|
|
312
|
+
* @param {boolean} colorTiles
|
|
313
|
+
* @memberof Settings */
|
|
314
|
+
function setCanvasColorTiles(colorTiles) { canvasColorTiles = colorTiles; }
|
|
315
|
+
|
|
294
316
|
/** Set max size of the canvas
|
|
295
317
|
* @param {Vector2} size
|
|
296
318
|
* @memberof Settings */
|
|
@@ -301,10 +323,30 @@ function setCanvasMaxSize(size) { canvasMaxSize = size; }
|
|
|
301
323
|
* @memberof Settings */
|
|
302
324
|
function setCanvasFixedSize(size) { canvasFixedSize = size; }
|
|
303
325
|
|
|
304
|
-
/** Use nearest
|
|
326
|
+
/** Use nearest scaling algorithm for canvas for more pixelated look
|
|
327
|
+
* - If enabled sets css image-rendering:pixelated
|
|
305
328
|
* @param {boolean} pixelated
|
|
306
329
|
* @memberof Settings */
|
|
307
|
-
function setCanvasPixelated(pixelated)
|
|
330
|
+
function setCanvasPixelated(pixelated)
|
|
331
|
+
{
|
|
332
|
+
canvasPixelated = pixelated;
|
|
333
|
+
if (mainCanvas)
|
|
334
|
+
mainCanvas.style.imageRendering = pixelated ? 'pixelated' : '';
|
|
335
|
+
if (glCanvas)
|
|
336
|
+
glCanvas.style.imageRendering = pixelated ? 'pixelated' : '';
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Use nearest scaling algorithm for canvas for more pixelated look
|
|
340
|
+
* - If enabled sets css image-rendering:pixelated
|
|
341
|
+
* - This defaults to false because text looks better with smoothing
|
|
342
|
+
* @param {boolean} pixelated
|
|
343
|
+
* @memberof Settings */
|
|
344
|
+
function setOverlayCanvasPixelated(pixelated)
|
|
345
|
+
{
|
|
346
|
+
overlayCanvasPixelated = pixelated;
|
|
347
|
+
if (overlayCanvas)
|
|
348
|
+
overlayCanvas.style.imageRendering = pixelated ? 'pixelated' : '';
|
|
349
|
+
}
|
|
308
350
|
|
|
309
351
|
/** Disables texture filtering for crisper pixel art
|
|
310
352
|
* @param {boolean} pixelated
|
|
@@ -329,7 +371,12 @@ function setHeadlessMode(headless) { headlessMode = headless; }
|
|
|
329
371
|
/** Set if webgl rendering is enabled
|
|
330
372
|
* @param {boolean} enable
|
|
331
373
|
* @memberof Settings */
|
|
332
|
-
function setGLEnable(enable)
|
|
374
|
+
function setGLEnable(enable)
|
|
375
|
+
{
|
|
376
|
+
glEnable = enable;
|
|
377
|
+
if (glCanvas) // hide glCanvas if webgl is disabled
|
|
378
|
+
glCanvas.style.visibility = enable ? 'visible' : 'hidden';
|
|
379
|
+
}
|
|
333
380
|
|
|
334
381
|
/** Set default size of tiles in pixels
|
|
335
382
|
* @param {Vector2} size
|
|
@@ -361,7 +408,7 @@ function setObjectDefaultDamping(damp) { objectDefaultDamping = damp; }
|
|
|
361
408
|
* @memberof Settings */
|
|
362
409
|
function setObjectDefaultAngleDamping(damp) { objectDefaultAngleDamping = damp; }
|
|
363
410
|
|
|
364
|
-
/** Set how much to bounce when a collision
|
|
411
|
+
/** Set how much to bounce when a collision occurs
|
|
365
412
|
* @param {number} restitution
|
|
366
413
|
* @memberof Settings */
|
|
367
414
|
function setObjectDefaultRestitution(restitution) { objectDefaultRestitution = restitution; }
|
package/src/engineUtilities.js
CHANGED
|
@@ -133,11 +133,12 @@ function isPowerOfTwo(value) { return !(value & (value - 1)); }
|
|
|
133
133
|
* @memberof Utilities */
|
|
134
134
|
function nearestPowerOfTwo(value) { return 2**Math.ceil(Math.log2(value)); }
|
|
135
135
|
|
|
136
|
-
/** Returns true if two axis aligned bounding boxes are overlapping
|
|
136
|
+
/** Returns true if two axis aligned bounding boxes are overlapping
|
|
137
|
+
* this can be used for simple collision detection between objects
|
|
137
138
|
* @param {Vector2} posA - Center of box A
|
|
138
139
|
* @param {Vector2} sizeA - Size of box A
|
|
139
140
|
* @param {Vector2} posB - Center of box B
|
|
140
|
-
* @param {Vector2} [sizeB=(0,0)] - Size of box B, a point if undefined
|
|
141
|
+
* @param {Vector2} [sizeB=(0,0)] - Size of box B, uses a point if undefined
|
|
141
142
|
* @return {boolean} - True if overlapping
|
|
142
143
|
* @memberof Utilities */
|
|
143
144
|
function isOverlapping(posA, sizeA, posB, sizeB=vec2())
|
|
@@ -191,10 +192,11 @@ function isIntersecting(start, end, pos, size)
|
|
|
191
192
|
* @param {number} [frequency] - Frequency of the wave in Hz
|
|
192
193
|
* @param {number} [amplitude] - Amplitude (max height) of the wave
|
|
193
194
|
* @param {number} [t=time] - Value to use for time of the wave
|
|
195
|
+
* @param {number} [offset] - Value to use for time offset of the wave
|
|
194
196
|
* @return {number} - Value waving between 0 and amplitude
|
|
195
197
|
* @memberof Utilities */
|
|
196
|
-
function wave(frequency=1, amplitude=1, t=time)
|
|
197
|
-
{ return amplitude/2 * (1 - Math.cos(t*frequency*2*PI)); }
|
|
198
|
+
function wave(frequency=1, amplitude=1, t=time, offset=0)
|
|
199
|
+
{ return amplitude/2 * (1 - Math.cos(offset + t*frequency*2*PI)); }
|
|
198
200
|
|
|
199
201
|
/** Formats seconds to mm:ss style for display purposes
|
|
200
202
|
* @param {number} t - time in seconds
|
|
@@ -851,9 +853,7 @@ class Color
|
|
|
851
853
|
/** Checks if this is a valid color
|
|
852
854
|
* @return {boolean} */
|
|
853
855
|
isValid()
|
|
854
|
-
{
|
|
855
|
-
return isNumber(this.r) && isNumber(this.g) && isNumber(this.b) && isNumber(this.a);
|
|
856
|
-
}
|
|
856
|
+
{ return isNumber(this.r) && isNumber(this.g) && isNumber(this.b) && isNumber(this.a); }
|
|
857
857
|
}
|
|
858
858
|
|
|
859
859
|
///////////////////////////////////////////////////////////////////////////////
|
package/src/engineWebGL.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* LittleJS WebGL Interface
|
|
3
3
|
* - All webgl used by the engine is wrapped up here
|
|
4
|
+
* - Will fall back to 2D canvas rendering if webgl is not supported
|
|
4
5
|
* - For normal stuff you won't need to see or call anything in this file
|
|
5
6
|
* - For advanced stuff there are helper functions to create shaders, textures, etc
|
|
6
7
|
* - Can be disabled with glEnable to revert to 2D canvas rendering
|
|
7
8
|
* - Batches sprite rendering on GPU for incredibly fast performance
|
|
8
9
|
* - Sprite transform math is done in the shader where possible
|
|
9
|
-
* - Supports shadertoy style post processing shaders
|
|
10
|
+
* - Supports shadertoy style post processing shaders via plugin
|
|
10
11
|
* @namespace WebGL
|
|
11
12
|
*/
|
|
12
13
|
|
|
@@ -47,6 +48,14 @@ function glInit()
|
|
|
47
48
|
glCanvas = document.createElement('canvas');
|
|
48
49
|
glContext = glCanvas.getContext('webgl2', {antialias:glAntialias});
|
|
49
50
|
|
|
51
|
+
if (!glContext)
|
|
52
|
+
{
|
|
53
|
+
console.warn('WebGL2 not supported, falling back to 2D canvas rendering!');
|
|
54
|
+
glCanvas = glContext = undefined;
|
|
55
|
+
glEnable = false;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
50
59
|
// create the webgl canvas
|
|
51
60
|
const rootElement = mainCanvas.parentElement;
|
|
52
61
|
rootElement.appendChild(glCanvas);
|
|
@@ -96,7 +105,7 @@ function glInit()
|
|
|
96
105
|
// Also used by tile layer rendering when redrawing tiles
|
|
97
106
|
function glPreRender()
|
|
98
107
|
{
|
|
99
|
-
if (!glEnable ||
|
|
108
|
+
if (!glEnable || !glContext) return;
|
|
100
109
|
|
|
101
110
|
// set up the shader and canvas
|
|
102
111
|
glClearCanvas();
|
|
@@ -147,6 +156,8 @@ function glPreRender()
|
|
|
147
156
|
* @memberof WebGL */
|
|
148
157
|
function glClearCanvas()
|
|
149
158
|
{
|
|
159
|
+
if (!glContext) return;
|
|
160
|
+
|
|
150
161
|
// clear and set to same size as main canvas
|
|
151
162
|
glContext.viewport(0, 0, glCanvas.width=drawCanvas.width, glCanvas.height=drawCanvas.height);
|
|
152
163
|
glContext.clear(glContext.COLOR_BUFFER_BIT);
|
|
@@ -160,7 +171,7 @@ function glClearCanvas()
|
|
|
160
171
|
function glSetTexture(texture, wrap=false)
|
|
161
172
|
{
|
|
162
173
|
// must flush cache with the old texture to set a new one
|
|
163
|
-
if (
|
|
174
|
+
if (!glContext || texture == glActiveTexture)
|
|
164
175
|
return;
|
|
165
176
|
|
|
166
177
|
glFlush();
|
|
@@ -179,6 +190,8 @@ function glSetTexture(texture, wrap=false)
|
|
|
179
190
|
* @memberof WebGL */
|
|
180
191
|
function glCompileShader(source, type)
|
|
181
192
|
{
|
|
193
|
+
if (!glContext) return;
|
|
194
|
+
|
|
182
195
|
// build the shader
|
|
183
196
|
const shader = glContext.createShader(type);
|
|
184
197
|
glContext.shaderSource(shader, source);
|
|
@@ -197,6 +210,8 @@ function glCompileShader(source, type)
|
|
|
197
210
|
* @memberof WebGL */
|
|
198
211
|
function glCreateProgram(vsSource, fsSource)
|
|
199
212
|
{
|
|
213
|
+
if (!glContext) return;
|
|
214
|
+
|
|
200
215
|
// build the program
|
|
201
216
|
const program = glContext.createProgram();
|
|
202
217
|
glContext.attachShader(program, glCompileShader(vsSource, glContext.VERTEX_SHADER));
|
|
@@ -215,6 +230,8 @@ function glCreateProgram(vsSource, fsSource)
|
|
|
215
230
|
* @memberof WebGL */
|
|
216
231
|
function glCreateTexture(image)
|
|
217
232
|
{
|
|
233
|
+
if (!glContext) return;
|
|
234
|
+
|
|
218
235
|
// build the texture
|
|
219
236
|
const texture = glContext.createTexture();
|
|
220
237
|
glContext.bindTexture(glContext.TEXTURE_2D, texture);
|
|
@@ -250,6 +267,7 @@ function glCreateTexture(image)
|
|
|
250
267
|
* @memberof WebGL */
|
|
251
268
|
function glDeleteTexture(texture)
|
|
252
269
|
{
|
|
270
|
+
if (!glContext) return;
|
|
253
271
|
glContext.deleteTexture(texture);
|
|
254
272
|
}
|
|
255
273
|
|
|
@@ -259,6 +277,8 @@ function glDeleteTexture(texture)
|
|
|
259
277
|
* @memberof WebGL */
|
|
260
278
|
function glSetTextureData(texture, image)
|
|
261
279
|
{
|
|
280
|
+
if (!glContext) return;
|
|
281
|
+
|
|
262
282
|
// build the texture
|
|
263
283
|
ASSERT(!!image && image.width > 0, 'Invalid image data.');
|
|
264
284
|
glContext.bindTexture(glContext.TEXTURE_2D, texture);
|
|
@@ -269,7 +289,7 @@ function glSetTextureData(texture, image)
|
|
|
269
289
|
* @memberof WebGL */
|
|
270
290
|
function glFlush()
|
|
271
291
|
{
|
|
272
|
-
if (!glEnable || !glInstanceCount) return;
|
|
292
|
+
if (!glEnable || !glContext || !glInstanceCount) return;
|
|
273
293
|
|
|
274
294
|
const destBlend = glBatchAdditive ? glContext.ONE : glContext.ONE_MINUS_SRC_ALPHA;
|
|
275
295
|
glContext.blendFuncSeparate(glContext.SRC_ALPHA, destBlend, glContext.ONE, destBlend);
|
|
@@ -289,7 +309,7 @@ function glFlush()
|
|
|
289
309
|
* @memberof WebGL */
|
|
290
310
|
function glCopyToContext(context)
|
|
291
311
|
{
|
|
292
|
-
if (!glEnable)
|
|
312
|
+
if (!glEnable || !glContext)
|
|
293
313
|
return;
|
|
294
314
|
|
|
295
315
|
glFlush();
|