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.
Files changed (51) hide show
  1. package/FAQ.md +841 -815
  2. package/README.md +211 -199
  3. package/dist/littlejs.d.ts +4383 -812
  4. package/dist/littlejs.esm.js +31848 -16680
  5. package/dist/littlejs.esm.min.js +14 -1
  6. package/dist/littlejs.js +31230 -16166
  7. package/dist/littlejs.min.js +13 -1
  8. package/dist/littlejs.release.js +27419 -15457
  9. package/package.json +10 -5
  10. package/plugins/audioEffects.js +375 -0
  11. package/plugins/box2d.js +2516 -2073
  12. package/plugins/drawUtilities.js +206 -31
  13. package/plugins/gltf.js +740 -0
  14. package/plugins/jsconfig.json +19 -0
  15. package/plugins/lightSystem.js +719 -330
  16. package/plugins/math3d.js +911 -0
  17. package/plugins/medalSystem.js +118 -72
  18. package/plugins/newgrounds.js +400 -199
  19. package/plugins/pathFinder.js +208 -175
  20. package/plugins/pluginExport.js +196 -115
  21. package/plugins/postProcess.js +284 -166
  22. package/plugins/render3d.js +4072 -0
  23. package/plugins/render3dDebug.js +109 -0
  24. package/plugins/render3dExtras.js +1754 -0
  25. package/plugins/render3dVoxels.js +502 -0
  26. package/plugins/textureSheet.js +86 -48
  27. package/plugins/threejs.js +183 -158
  28. package/plugins/tweakables.js +411 -0
  29. package/plugins/tweenSystem.js +646 -526
  30. package/plugins/uiSystem.js +2024 -1687
  31. package/plugins/zzfxm.js +8 -15
  32. package/src/engine.js +345 -194
  33. package/src/engineAudio.js +1022 -777
  34. package/src/engineBuild.mjs +289 -253
  35. package/src/engineDebug.js +912 -735
  36. package/src/engineDraw.js +1774 -1510
  37. package/src/engineEditor.js +2431 -0
  38. package/src/engineExport.js +419 -396
  39. package/src/engineInput.js +1462 -1293
  40. package/src/engineLogo.js +4 -3
  41. package/src/engineMath.js +1274 -1183
  42. package/src/engineObject.js +674 -551
  43. package/src/engineParticles.js +583 -532
  44. package/src/engineRelease.js +67 -45
  45. package/src/engineSettings.js +808 -726
  46. package/src/engineTileLayer.js +995 -741
  47. package/src/engineUtilities.js +32 -13
  48. package/src/engineWebGL.js +1162 -941
  49. package/src/jsconfig.json +17 -13
  50. package/plugins/box2d.wasm.js +0 -630
  51. package/plugins/box2d.wasm.wasm +0 -0
@@ -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
- * - Gradient noise (1D and 2D)
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 < 0 ? '-' : '';
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
- return response.json();
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 {Object} [defaultSaveData] - default values, result is {...default, ...loaded} so this must be an object
194
- * @return {Object}
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[saveName];
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 { ...defaultSaveData, ...loadedData };
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 {Object} saveData - object containing data to be saved
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[saveName] = JSON.stringify(saveData); }
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 gradient noise — returns a smooth value in [0, 1] for any real x.
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 gradient noise — returns a smooth value in [0, 1] for any real (x, y).
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}