littlejsengine 1.18.23 β†’ 1.18.25

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 CHANGED
@@ -23,6 +23,8 @@ The code is very clean and well documented with many examples to get you started
23
23
 
24
24
  *The Third Annual LittleJS Game Jam will take place From Oct 2 to Nov 2! Unleash your creativity and develop amazing games using the LittleJS game engine.*
25
25
 
26
+ ### πŸ˜ΌπŸ‘ [LittleJS + JS13k](https://github.com/KilledByAPixel/LittleJS/tree/js13k) - We made a special branch designed for size coding events like JS13k.
27
+
26
28
  <div align='center' markdown='1'>
27
29
 
28
30
  ## [Demos](https://killedbyapixel.github.io/LittleJS/examples) | [Arcade](https://killedbyapixel.github.io/LittleJSArcade) | [Docs](https://killedbyapixel.github.io/LittleJS/docs) | [FAQ](https://github.com/KilledByAPixel/LittleJS/blob/main/FAQ.md) | [Trailer](https://youtu.be/chuBzGjv7Ms) | [Discord](https://discord.gg/zb7hcGkyZe)
@@ -38,6 +40,7 @@ The code is very clean and well documented with many examples to get you started
38
40
  - Blazing fast WebGL2 + Canvas2D hybrid rendering system
39
41
  - Apply [Shadertoy](https://www.shadertoy.com) style shaders for post-processing effects
40
42
  - Robust particle effect system and [effect design tool](https://killedbyapixel.github.io/LittleJS/examples/particles/)
43
+ - Load sprites and animations into texture sheets at runtime, or import [TexturePacker](https://www.codeandweb.com/texturepacker) and [Aseprite](https://www.aseprite.org) atlases
41
44
  - Optional 3D rendering with the [Three.js](https://threejs.org) plugin
42
45
 
43
46
  ### πŸ”Š Audio
@@ -146,6 +146,17 @@ declare module "littlejsengine" {
146
146
  * );
147
147
  * @memberof Engine */
148
148
  export function engineInit(gameInit: GameInitCallback, gameUpdate: GameCallback, gameUpdatePost: GameCallback, gameRender: GameCallback, gameRenderPost: GameCallback, imageSources?: Array<string>, rootElement?: HTMLElement): Promise<void>;
149
+ /** Advance the engine by a number of frames
150
+ * Requires setEngineManualStep(true) before engineInit
151
+ * Respects paused exactly as the normal update loop does
152
+ * @param {number} [frames] - number of engine update ticks, max 36000, each running one fixed update at timeScale 1
153
+ * @example
154
+ * setHeadlessMode(true);
155
+ * setEngineManualStep(true);
156
+ * await engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);
157
+ * engineStep(600); // advance 10 seconds of game time
158
+ * @memberof Engine */
159
+ export function engineStep(frames?: number): void;
149
160
  /** Update each engine object, remove destroyed objects, and update time
150
161
  * can be called manually if objects need to be updated outside of main loop
151
162
  * @memberof Engine */
@@ -417,6 +428,12 @@ declare module "littlejsengine" {
417
428
  * @default
418
429
  * @memberof Settings */
419
430
  export let headlessMode: boolean;
431
+ /** Disables the automatic requestAnimationFrame loop so the engine only
432
+ * advances when engineStep is called, for tests and frame-stepping tools
433
+ * @type {boolean}
434
+ * @default
435
+ * @memberof Settings */
436
+ export let engineManualStep: boolean;
420
437
  /** Default size of tiles in pixels
421
438
  * @type {Vector2}
422
439
  * @default Vector2(16,16)
@@ -690,6 +707,11 @@ declare module "littlejsengine" {
690
707
  * @param {boolean} headless
691
708
  * @memberof Settings */
692
709
  export function setHeadlessMode(headless: boolean): void;
710
+ /** Set if the engine only advances when engineStep is called
711
+ * Must be set before engineInit
712
+ * @param {boolean} [enable]
713
+ * @memberof Settings */
714
+ export function setEngineManualStep(enable?: boolean): void;
693
715
  /** Set if WebGL rendering is enabled
694
716
  * @param {boolean} enable
695
717
  * @memberof Settings */
@@ -1087,7 +1109,7 @@ declare module "littlejsengine" {
1087
1109
  export function shareURL(title: string, url: string, callback?: Function): void;
1088
1110
  /** Read save data from local storage
1089
1111
  * @param {string} saveName - unique name for the game/save
1090
- * @param {Object} [defaultSaveData] - default values for save
1112
+ * @param {Object} [defaultSaveData] - default values, result is {...default, ...loaded} so this must be an object
1091
1113
  * @return {Object}
1092
1114
  * @memberof Utilities */
1093
1115
  export function readSaveData(saveName: string, defaultSaveData?: any): any;
@@ -5841,15 +5863,15 @@ declare module "littlejsengine" {
5841
5863
  * @param {Vector2} imageSize - Size of the source image in pixels
5842
5864
  * @param {Vector2} [frameSize] - Size of each frame, or the whole image if not passed
5843
5865
  * @param {number} [padding] - How many pixels padding around each frame
5844
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image
5866
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image
5845
5867
  * @return {TileInfo} Tile for the packed image, or undefined if the sheet is full */
5846
- tryAdd(imageSize: Vector2, frameSize?: Vector2, padding?: number, sourcePadding?: number): TileInfo;
5868
+ tryAdd(imageSize: Vector2, frameSize?: Vector2, padding?: number, sourcePadding?: number | Vector2): TileInfo;
5847
5869
  /** Draw an image into this sheet at a tile returned by tryAdd
5848
5870
  * @param {HTMLImageElement} image - Source image to copy from
5849
5871
  * @param {TileInfo} tileInfo - Where to put it, from tryAdd
5850
5872
  * @param {boolean} [update] - Upload to webgl now, pass false when batching
5851
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image */
5852
- drawImage(image: HTMLImageElement, tileInfo: TileInfo, update?: boolean, sourcePadding?: number): void;
5873
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image */
5874
+ drawImage(image: HTMLImageElement, tileInfo: TileInfo, update?: boolean, sourcePadding?: number | Vector2): void;
5853
5875
  /** Upload the canvas to webgl if it has changed since the last upload
5854
5876
  * Only needed after batching, drawImage uploads automatically by default */
5855
5877
  updateTexture(): void;
@@ -5863,13 +5885,13 @@ declare module "littlejsengine" {
5863
5885
  * @param {string} src - Image source path
5864
5886
  * @param {Vector2|number} [frameSize] - Size of each animation frame in pixels
5865
5887
  * @param {number} [padding] - How many pixels padding around each frame
5866
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image
5888
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image
5867
5889
  * @return {TileInfo}
5868
5890
  * @example
5869
5891
  * const playerTile = loadSprite('player.png'); // a single sprite
5870
5892
  * const runTile = loadSprite('run.png', vec2(16)); // a 16x16 frame animation
5871
5893
  * @memberof TextureSheets */
5872
- export function loadSprite(src: string, frameSize?: Vector2 | number, padding?: number, sourcePadding?: number): TileInfo;
5894
+ export function loadSprite(src: string, frameSize?: Vector2 | number, padding?: number, sourcePadding?: number | Vector2): TileInfo;
5873
5895
  /** Load a pre-packed texture atlas and repack it onto texture sheets
5874
5896
  * - Supports TexturePacker json (hash and array) and Aseprite json
5875
5897
  * - Returns an empty object which is filled with TileInfos when loaded
@@ -35,7 +35,7 @@ const engineName = 'LittleJS';
35
35
  * @type {string}
36
36
  * @default
37
37
  * @memberof Engine */
38
- const engineVersion = '1.18.23';
38
+ const engineVersion = '1.18.25';
39
39
 
40
40
  /** Frames per second to update
41
41
  * @type {number}
@@ -92,6 +92,7 @@ function setPaused(isPaused=true) { paused = isPaused; }
92
92
 
93
93
  // Engine internal variables
94
94
  let frameTimeLastMS = 0, frameTimeBufferMS = 0, averageFPS = 0;
95
+ let engineUpdateInternal; // assigned by engineInit so engineStep can drive it
95
96
  let showEngineVersion = true;
96
97
 
97
98
  ///////////////////////////////////////////////////////////////////////////////
@@ -280,7 +281,8 @@ async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, game
280
281
 
281
282
  if (!debugVideoCaptureIsActive())
282
283
  renderFrame();
283
- requestAnimationFrame(engineUpdate);
284
+ if (!engineManualStep)
285
+ requestAnimationFrame(engineUpdate);
284
286
 
285
287
  function renderFrame()
286
288
  {
@@ -308,6 +310,7 @@ async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, game
308
310
  primitiveCount = 0;
309
311
  }
310
312
  }
313
+ engineUpdateInternal = engineUpdate;
311
314
 
312
315
  function updateCanvas()
313
316
  {
@@ -462,10 +465,39 @@ async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, game
462
465
  {
463
466
  // wait for gameInit to load
464
467
  await gameInit();
465
- engineUpdate();
468
+ engineManualStep || engineUpdate();
466
469
  }
467
470
  }
468
471
 
472
+ // max frames engineStep can advance in one call, 10 minutes at 60fps
473
+ // large counts block until they finish, so this catches runaway values
474
+ const engineStepMaxFrames = 36000;
475
+
476
+ /** Advance the engine by a number of frames
477
+ * Requires setEngineManualStep(true) before engineInit
478
+ * Respects paused exactly as the normal update loop does
479
+ * @param {number} [frames] - number of engine update ticks, max 36000, each running one fixed update at timeScale 1
480
+ * @example
481
+ * setHeadlessMode(true);
482
+ * setEngineManualStep(true);
483
+ * await engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);
484
+ * engineStep(600); // advance 10 seconds of game time
485
+ * @memberof Engine */
486
+ function engineStep(frames=1)
487
+ {
488
+ ASSERT(engineManualStep,
489
+ 'engineStep requires setEngineManualStep(true) before engineInit');
490
+ ASSERT(engineUpdateInternal, 'engineStep requires engineInit to complete');
491
+ // runtime guard so release builds (where the asserts are stripped) can't
492
+ // start a second requestAnimationFrame chain or call an undefined update
493
+ if (!engineManualStep || !engineUpdateInternal) return;
494
+ ASSERT(Number.isInteger(frames) && frames >= 0 && frames <= engineStepMaxFrames,
495
+ 'engineStep requires a whole frame count from 0 to ' + engineStepMaxFrames);
496
+ frames = min(frames, engineStepMaxFrames); // release has no asserts, don't freeze
497
+ for (let i = frames; i > 0; --i)
498
+ engineUpdateInternal(frameTimeLastMS + 1e3 / frameRate);
499
+ }
500
+
469
501
  /** Update each engine object, remove destroyed objects, and update time
470
502
  * can be called manually if objects need to be updated outside of main loop
471
503
  * @memberof Engine */
@@ -2688,12 +2720,17 @@ function shareURL(title, url, callback)
2688
2720
 
2689
2721
  /** Read save data from local storage
2690
2722
  * @param {string} saveName - unique name for the game/save
2691
- * @param {Object} [defaultSaveData] - default values for save
2723
+ * @param {Object} [defaultSaveData] - default values, result is {...default, ...loaded} so this must be an object
2692
2724
  * @return {Object}
2693
2725
  * @memberof Utilities */
2694
2726
  function readSaveData(saveName, defaultSaveData)
2695
2727
  {
2696
- ASSERT(isStringLike(saveName), 'loadData requires saveName string');
2728
+ ASSERT(isStringLike(saveName), 'readSaveData requires saveName string');
2729
+ ASSERT(defaultSaveData === undefined ||
2730
+ (typeof defaultSaveData === 'object' && defaultSaveData !== null),
2731
+ 'readSaveData: default must be an object - the result is ' +
2732
+ '{...default, ...loaded}, so a scalar default yields {}. ' +
2733
+ 'Use readSaveData(key, {best:0}).best');
2697
2734
 
2698
2735
  // tolerate localStorage being unavailable (iOS private mode, sandboxed
2699
2736
  // iframes) and corrupt JSON in stored data
@@ -2717,7 +2754,7 @@ function readSaveData(saveName, defaultSaveData)
2717
2754
  * @memberof Utilities */
2718
2755
  function writeSaveData(saveName, saveData)
2719
2756
  {
2720
- ASSERT(isStringLike(saveName), 'saveData requires saveName string');
2757
+ ASSERT(isStringLike(saveName), 'writeSaveData requires saveName string');
2721
2758
  // tolerate localStorage being unavailable or quota exceeded
2722
2759
  try { localStorage[saveName] = JSON.stringify(saveData); }
2723
2760
  catch { LOG('writeSaveData: failed to write', saveName); }
@@ -2885,6 +2922,13 @@ let showSplashScreen = false;
2885
2922
  * @memberof Settings */
2886
2923
  let headlessMode = false;
2887
2924
 
2925
+ /** Disables the automatic requestAnimationFrame loop so the engine only
2926
+ * advances when engineStep is called, for tests and frame-stepping tools
2927
+ * @type {boolean}
2928
+ * @default
2929
+ * @memberof Settings */
2930
+ let engineManualStep = false;
2931
+
2888
2932
  ///////////////////////////////////////////////////////////////////////////////
2889
2933
  // WebGL settings
2890
2934
 
@@ -3235,6 +3279,12 @@ function setShowSplashScreen(show) { showSplashScreen = show; }
3235
3279
  * @memberof Settings */
3236
3280
  function setHeadlessMode(headless) { headlessMode = headless; }
3237
3281
 
3282
+ /** Set if the engine only advances when engineStep is called
3283
+ * Must be set before engineInit
3284
+ * @param {boolean} [enable]
3285
+ * @memberof Settings */
3286
+ function setEngineManualStep(enable=true) { engineManualStep = enable; }
3287
+
3238
3288
  /** Set if WebGL rendering is enabled
3239
3289
  * @param {boolean} enable
3240
3290
  * @memberof Settings */
@@ -14998,17 +15048,21 @@ class TextureSheet
14998
15048
  * @param {Vector2} imageSize - Size of the source image in pixels
14999
15049
  * @param {Vector2} [frameSize] - Size of each frame, or the whole image if not passed
15000
15050
  * @param {number} [padding] - How many pixels padding around each frame
15001
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image
15051
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image
15002
15052
  * @return {TileInfo} Tile for the packed image, or undefined if the sheet is full */
15003
15053
  tryAdd(imageSize, frameSize=imageSize, padding=textureSheetPadding, sourcePadding=0)
15004
15054
  {
15005
15055
  ASSERT(isVector2(imageSize) && isVector2(frameSize), 'sizes must be vec2');
15006
15056
  ASSERT(frameSize.x > 0 && frameSize.y > 0, 'frame size must be positive');
15007
- ASSERT(isNumber(sourcePadding) && sourcePadding >= 0, 'sourcePadding must be a number >= 0');
15057
+
15058
+ if (isNumber(sourcePadding))
15059
+ sourcePadding = vec2(sourcePadding);
15060
+ ASSERT(isVector2(sourcePadding) && sourcePadding.x >= 0 && sourcePadding.y >= 0,
15061
+ 'sourcePadding must be a number or vec2 >= 0');
15008
15062
 
15009
15063
  // the source may have its own padding baked in around each frame
15010
- const sourceCellWidth = frameSize.x + sourcePadding*2;
15011
- const sourceCellHeight = frameSize.y + sourcePadding*2;
15064
+ const sourceCellWidth = frameSize.x + sourcePadding.x*2;
15065
+ const sourceCellHeight = frameSize.y + sourcePadding.y*2;
15012
15066
  ASSERT(imageSize.x % sourceCellWidth === 0 && imageSize.y % sourceCellHeight === 0,
15013
15067
  'image size must be a multiple of the padded frame size');
15014
15068
 
@@ -15050,16 +15104,19 @@ class TextureSheet
15050
15104
  * @param {HTMLImageElement} image - Source image to copy from
15051
15105
  * @param {TileInfo} tileInfo - Where to put it, from tryAdd
15052
15106
  * @param {boolean} [update] - Upload to webgl now, pass false when batching
15053
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image */
15107
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image */
15054
15108
  drawImage(image, tileInfo, update=true, sourcePadding=0)
15055
15109
  {
15056
15110
  ASSERT(!!this.context, 'texture sheet has no canvas');
15057
15111
 
15112
+ if (isNumber(sourcePadding))
15113
+ sourcePadding = vec2(sourcePadding);
15114
+
15058
15115
  // copy frames in order, reading the source left to right, top to bottom
15059
15116
  // the destination wraps at tileInfo.columns which may be narrower than the source
15060
15117
  const frameSize = tileInfo.size;
15061
- const sourceCellWidth = frameSize.x + sourcePadding*2;
15062
- const sourceCellHeight = frameSize.y + sourcePadding*2;
15118
+ const sourceCellWidth = frameSize.x + sourcePadding.x*2;
15119
+ const sourceCellHeight = frameSize.y + sourcePadding.y*2;
15063
15120
  const sourceColumns = image.width / sourceCellWidth;
15064
15121
  const frameCount = sourceColumns * (image.height / sourceCellHeight);
15065
15122
  const columns = tileInfo.columns || frameCount;
@@ -15067,8 +15124,8 @@ class TextureSheet
15067
15124
  const cellHeight = frameSize.y + tileInfo.padding*2;
15068
15125
  for (let i = frameCount; i--;)
15069
15126
  {
15070
- const sourceX = (i % sourceColumns) * sourceCellWidth + sourcePadding;
15071
- const sourceY = (i / sourceColumns | 0) * sourceCellHeight + sourcePadding;
15127
+ const sourceX = (i % sourceColumns) * sourceCellWidth + sourcePadding.x;
15128
+ const sourceY = (i / sourceColumns | 0) * sourceCellHeight + sourcePadding.y;
15072
15129
  this.context.drawImage(image,
15073
15130
  sourceX, sourceY, frameSize.x, frameSize.y,
15074
15131
  tileInfo.pos.x + (i % columns) * cellWidth,
@@ -15102,7 +15159,7 @@ class TextureSheet
15102
15159
  * @param {string} src - Image source path
15103
15160
  * @param {Vector2|number} [frameSize] - Size of each animation frame in pixels
15104
15161
  * @param {number} [padding] - How many pixels padding around each frame
15105
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image
15162
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image
15106
15163
  * @return {TileInfo}
15107
15164
  * @example
15108
15165
  * const playerTile = loadSprite('player.png'); // a single sprite
@@ -15113,6 +15170,7 @@ function loadSprite(src, frameSize, padding=textureSheetPadding, sourcePadding=0
15113
15170
  ASSERT(isStringLike(src), 'image src must be a string');
15114
15171
  ASSERT(!frameSize || isVector2(frameSize) || isNumber(frameSize), 'frameSize must be a vec2 or number');
15115
15172
  ASSERT(isNumber(padding), 'padding must be a number');
15173
+ ASSERT(isNumber(sourcePadding) || isVector2(sourcePadding), 'sourcePadding must be a number or vec2');
15116
15174
 
15117
15175
  if (isNumber(frameSize))
15118
15176
  frameSize = vec2(frameSize);
@@ -16863,6 +16921,7 @@ export
16863
16921
  getPaused,
16864
16922
  setPaused,
16865
16923
  engineInit,
16924
+ engineStep,
16866
16925
  engineObjectsUpdate,
16867
16926
  engineObjectsDestroy,
16868
16927
  engineObjectsCollect,
@@ -16910,6 +16969,7 @@ export
16910
16969
  fontDefault,
16911
16970
  showSplashScreen,
16912
16971
  headlessMode,
16972
+ engineManualStep,
16913
16973
  tileDefaultSize,
16914
16974
  tileDefaultPadding,
16915
16975
  tileDefaultBleed,
@@ -16964,6 +17024,7 @@ export
16964
17024
  setFontDefault,
16965
17025
  setShowSplashScreen,
16966
17026
  setHeadlessMode,
17027
+ setEngineManualStep,
16967
17028
  setGLEnable,
16968
17029
  setTileDefaultSize,
16969
17030
  setTileDefaultPadding,