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 +3 -0
- package/dist/littlejs.d.ts +29 -7
- package/dist/littlejs.esm.js +77 -16
- package/dist/littlejs.esm.min.js +1 -1
- package/dist/littlejs.js +74 -16
- package/dist/littlejs.min.js +1 -1
- package/dist/littlejs.release.js +74 -16
- package/package.json +4 -1
- package/plugins/textureSheet.js +18 -10
- package/src/engine.js +35 -3
- package/src/engineExport.js +3 -0
- package/src/engineSettings.js +13 -0
- package/src/engineUtilities.js +8 -3
package/dist/littlejs.js
CHANGED
|
@@ -35,7 +35,7 @@ const engineName = 'LittleJS';
|
|
|
35
35
|
* @type {string}
|
|
36
36
|
* @default
|
|
37
37
|
* @memberof Engine */
|
|
38
|
-
const engineVersion = '1.18.
|
|
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
|
-
|
|
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
|
|
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), '
|
|
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), '
|
|
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
|
-
|
|
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);
|