littlejsengine 1.18.29 → 1.20.0
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/FAQ.md +841 -815
- package/README.md +211 -199
- package/dist/littlejs.d.ts +4383 -812
- package/dist/littlejs.esm.js +31848 -16680
- package/dist/littlejs.esm.min.js +14 -1
- package/dist/littlejs.js +31230 -16166
- package/dist/littlejs.min.js +13 -1
- package/dist/littlejs.release.js +27419 -15457
- package/package.json +10 -5
- package/plugins/audioEffects.js +375 -0
- package/plugins/box2d.js +2516 -2073
- package/plugins/drawUtilities.js +206 -31
- package/plugins/gltf.js +740 -0
- package/plugins/jsconfig.json +19 -0
- package/plugins/lightSystem.js +719 -330
- package/plugins/math3d.js +911 -0
- package/plugins/medalSystem.js +118 -72
- package/plugins/newgrounds.js +400 -199
- package/plugins/pathFinder.js +208 -175
- package/plugins/pluginExport.js +196 -115
- package/plugins/postProcess.js +284 -166
- package/plugins/render3d.js +4072 -0
- package/plugins/render3dDebug.js +109 -0
- package/plugins/render3dExtras.js +1754 -0
- package/plugins/render3dVoxels.js +502 -0
- package/plugins/textureSheet.js +86 -48
- package/plugins/threejs.js +183 -158
- package/plugins/tweakables.js +411 -0
- package/plugins/tweenSystem.js +646 -526
- package/plugins/uiSystem.js +2024 -1687
- package/plugins/zzfxm.js +8 -15
- package/src/engine.js +345 -194
- package/src/engineAudio.js +1022 -777
- package/src/engineBuild.mjs +289 -253
- package/src/engineDebug.js +912 -735
- package/src/engineDraw.js +1774 -1510
- package/src/engineEditor.js +2431 -0
- package/src/engineExport.js +419 -396
- package/src/engineInput.js +1462 -1293
- package/src/engineLogo.js +4 -3
- package/src/engineMath.js +1274 -1183
- package/src/engineObject.js +674 -551
- package/src/engineParticles.js +583 -532
- package/src/engineRelease.js +67 -45
- package/src/engineSettings.js +808 -726
- package/src/engineTileLayer.js +995 -741
- package/src/engineUtilities.js +32 -13
- package/src/engineWebGL.js +1162 -941
- package/src/jsconfig.json +17 -13
- package/plugins/box2d.wasm.js +0 -630
- package/plugins/box2d.wasm.wasm +0 -0
package/src/engineUtilities.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* - File saving (text, canvas, data URLs)
|
|
7
7
|
* - Native share dialog support
|
|
8
8
|
* - Local storage save data management
|
|
9
|
-
* -
|
|
9
|
+
* - Value noise (1D and 2D)
|
|
10
10
|
* @namespace Utilities
|
|
11
11
|
*/
|
|
12
12
|
|
|
@@ -32,7 +32,9 @@ class Timer
|
|
|
32
32
|
ASSERT(timeLeft === undefined || isNumber(timeLeft), 'Constructed Timer is invalid.', timeLeft);
|
|
33
33
|
this.useRealTime = useRealTime;
|
|
34
34
|
const globalTime = this.getGlobalTime();
|
|
35
|
+
/** @type {number|undefined} */
|
|
35
36
|
this.time = timeLeft === undefined ? undefined : globalTime + timeLeft;
|
|
37
|
+
/** @type {number|undefined} */
|
|
36
38
|
this.setTime = timeLeft;
|
|
37
39
|
}
|
|
38
40
|
|
|
@@ -108,7 +110,7 @@ class Timer
|
|
|
108
110
|
* @memberof Utilities */
|
|
109
111
|
function formatTime(t)
|
|
110
112
|
{
|
|
111
|
-
const signStr = t
|
|
113
|
+
const signStr = t <= -1 ? '-' : ''; // no sign when it shows 0:00
|
|
112
114
|
t = abs(t)|0;
|
|
113
115
|
return signStr + (t/60|0) + ':' + (t%60<10?'0':'') + t%60;
|
|
114
116
|
}
|
|
@@ -122,7 +124,9 @@ async function fetchJSON(url)
|
|
|
122
124
|
const response = await fetch(url);
|
|
123
125
|
if (!response.ok)
|
|
124
126
|
throw new Error(`Failed to fetch JSON from ${url}: ${response.status} ${response.statusText}`);
|
|
125
|
-
|
|
127
|
+
const json = await response.json();
|
|
128
|
+
editorJSONFetched(url, json); // debug builds remember the file a level came from, to save it by that name
|
|
129
|
+
return json;
|
|
126
130
|
}
|
|
127
131
|
|
|
128
132
|
///////////////////////////////////////////////////////////////////////////////
|
|
@@ -133,7 +137,20 @@ async function fetchJSON(url)
|
|
|
133
137
|
* @param {string} [type]
|
|
134
138
|
* @memberof Utilities */
|
|
135
139
|
function saveText(text, filename='text', type='text/plain')
|
|
136
|
-
{ saveDataURL(URL.createObjectURL(new Blob([text], {'type':type})), filename); }
|
|
140
|
+
{ saveDataURL(URL.createObjectURL(new Blob([text], {'type':type})), filename, 1e3); } // freed once the download has it
|
|
141
|
+
|
|
142
|
+
/** Create an offscreen canvas to draw into, and return its 2D context
|
|
143
|
+
* - The canvas is context.canvas, which is what TextureInfo and the like take
|
|
144
|
+
* @param {number} width - In pixels
|
|
145
|
+
* @param {number} [height] - In pixels, defaults to the width for a square
|
|
146
|
+
* @param {boolean} [willReadFrequently] - Keep it in software, faster when getImageData is called on it often
|
|
147
|
+
* @return {OffscreenCanvasRenderingContext2D}
|
|
148
|
+
* @memberof Utilities */
|
|
149
|
+
function createCanvasContext(width, height=width, willReadFrequently=false)
|
|
150
|
+
{
|
|
151
|
+
ASSERT(isNumber(width) && isNumber(height), 'canvas width and height must be numbers', width, height);
|
|
152
|
+
return new OffscreenCanvas(width, height).getContext('2d', {willReadFrequently});
|
|
153
|
+
}
|
|
137
154
|
|
|
138
155
|
/** Save a canvas to disk
|
|
139
156
|
* @param {HTMLCanvasElement|OffscreenCanvas} canvas
|
|
@@ -183,15 +200,17 @@ function shareURL(title, url, callback)
|
|
|
183
200
|
{
|
|
184
201
|
ASSERT(isStringLike(title), 'shareURL requires title string');
|
|
185
202
|
ASSERT(isStringLike(url), 'shareURL requires url string');
|
|
186
|
-
navigator.share?.({title, url}).then(()=>callback?.());
|
|
203
|
+
navigator.share?.({title, url}).then(()=>callback?.(), ()=>{}); // a player who cancels is not an error
|
|
187
204
|
}
|
|
188
205
|
|
|
189
206
|
///////////////////////////////////////////////////////////////////////////////
|
|
190
207
|
|
|
191
208
|
/** Read save data from local storage
|
|
209
|
+
* - The result has the type of defaultSaveData, or any when there is none
|
|
210
|
+
* @template {Object<string, any>} [T=any]
|
|
192
211
|
* @param {string} saveName - unique name for the game/save
|
|
193
|
-
* @param {
|
|
194
|
-
* @return {
|
|
212
|
+
* @param {T} [defaultSaveData] - default values, result is {...default, ...loaded} so this must be an object
|
|
213
|
+
* @return {T}
|
|
195
214
|
* @memberof Utilities */
|
|
196
215
|
function readSaveData(saveName, defaultSaveData)
|
|
197
216
|
{
|
|
@@ -207,7 +226,7 @@ function readSaveData(saveName, defaultSaveData)
|
|
|
207
226
|
let loadedData = {};
|
|
208
227
|
try
|
|
209
228
|
{
|
|
210
|
-
const data = localStorage
|
|
229
|
+
const data = localStorage.getItem(saveName); // not the brackets, a name like 'key' or 'length' is a Storage member
|
|
211
230
|
if (data)
|
|
212
231
|
{
|
|
213
232
|
try { loadedData = JSON.parse(data); }
|
|
@@ -215,18 +234,18 @@ function readSaveData(saveName, defaultSaveData)
|
|
|
215
234
|
}
|
|
216
235
|
}
|
|
217
236
|
catch { LOG('readSaveData: localStorage unavailable — using defaults'); }
|
|
218
|
-
return {
|
|
237
|
+
return { .../** @type {object} */ (defaultSaveData), ...loadedData };
|
|
219
238
|
}
|
|
220
239
|
|
|
221
240
|
/** Write save data to local storage
|
|
222
241
|
* @param {string} saveName - unique name for the game/save
|
|
223
|
-
* @param {
|
|
242
|
+
* @param {object} saveData - object containing data to be saved
|
|
224
243
|
* @memberof Utilities */
|
|
225
244
|
function writeSaveData(saveName, saveData)
|
|
226
245
|
{
|
|
227
246
|
ASSERT(isStringLike(saveName), 'writeSaveData requires saveName string');
|
|
228
247
|
// tolerate localStorage being unavailable or quota exceeded
|
|
229
|
-
try { localStorage
|
|
248
|
+
try { localStorage.setItem(saveName, JSON.stringify(saveData)); }
|
|
230
249
|
catch { LOG('writeSaveData: failed to write', saveName); }
|
|
231
250
|
}
|
|
232
251
|
|
|
@@ -243,7 +262,7 @@ function noiseHash(i)
|
|
|
243
262
|
return (h >>> 0) / 2**32;
|
|
244
263
|
}
|
|
245
264
|
|
|
246
|
-
/** 1D
|
|
265
|
+
/** 1D value noise — returns a smooth value in [0, 1] for any real x.
|
|
247
266
|
* Integer inputs land on deterministic lattice values; non-integer inputs
|
|
248
267
|
* are interpolated with smoothStep for C1 continuity.
|
|
249
268
|
* @param {number} x
|
|
@@ -255,7 +274,7 @@ function noise1D(x)
|
|
|
255
274
|
return lerp(noiseHash(i), noiseHash(i + 1), smoothStep(x - i));
|
|
256
275
|
}
|
|
257
276
|
|
|
258
|
-
/** 2D
|
|
277
|
+
/** 2D value noise — returns a smooth value in [0, 1] for any real (x, y).
|
|
259
278
|
* @param {number} x
|
|
260
279
|
* @param {number} y
|
|
261
280
|
* @return {number}
|