littlejsengine 1.19.3 → 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 (50) hide show
  1. package/FAQ.md +841 -820
  2. package/README.md +29 -25
  3. package/dist/littlejs.d.ts +2455 -935
  4. package/dist/littlejs.esm.js +30344 -21110
  5. package/dist/littlejs.esm.min.js +4 -3
  6. package/dist/littlejs.js +30259 -21052
  7. package/dist/littlejs.min.js +2 -2
  8. package/dist/littlejs.release.js +26567 -20455
  9. package/package.json +10 -5
  10. package/plugins/audioEffects.js +28 -24
  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 -332
  16. package/plugins/math3d.js +58 -17
  17. package/plugins/medalSystem.js +326 -280
  18. package/plugins/newgrounds.js +400 -199
  19. package/plugins/pathFinder.js +208 -175
  20. package/plugins/pluginExport.js +15 -0
  21. package/plugins/postProcess.js +56 -13
  22. package/plugins/render3d.js +1643 -1577
  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 +496 -458
  27. package/plugins/threejs.js +183 -163
  28. package/plugins/tweakables.js +411 -0
  29. package/plugins/tweenSystem.js +263 -145
  30. package/plugins/uiSystem.js +2024 -1687
  31. package/plugins/zzfxm.js +159 -159
  32. package/src/engine.js +191 -75
  33. package/src/engineAudio.js +229 -125
  34. package/src/engineBuild.mjs +289 -256
  35. package/src/engineDebug.js +912 -742
  36. package/src/engineDraw.js +295 -193
  37. package/src/engineEditor.js +2431 -0
  38. package/src/engineExport.js +12 -0
  39. package/src/engineInput.js +1462 -1293
  40. package/src/engineMath.js +1274 -1252
  41. package/src/engineObject.js +194 -80
  42. package/src/engineParticles.js +583 -532
  43. package/src/engineRelease.js +67 -45
  44. package/src/engineSettings.js +77 -29
  45. package/src/engineTileLayer.js +995 -741
  46. package/src/engineUtilities.js +19 -13
  47. package/src/engineWebGL.js +239 -82
  48. package/src/jsconfig.json +17 -13
  49. package/plugins/box2d.wasm.js +0 -630
  50. package/plugins/box2d.wasm.wasm +0 -0
@@ -1,45 +1,67 @@
1
- /**
2
- * LittleJS - Release Mode
3
- * - Replaces engineDebug.js in production builds
4
- * - All debug functions are stubbed out as no-ops
5
- * - Removes ASSERT and LOG calls to reduce file size
6
- * - Disables debug overlay, watermark, and visualizations
7
- * - Improves performance by eliminating debug overhead
8
- * - Significantly reduces final bundle size
9
- */
10
-
11
- 'use strict';
12
-
13
- let debugWatermark = 0;
14
- let debugKey = '';
15
- const debug = 0;
16
- const debugOverlay = 0;
17
- const debugPhysics = 0;
18
- const debugParticles = 0;
19
- const debugRaycast = 0;
20
- const debugGamepads = 0;
21
- const debugSound = 0;
22
- const debugPointSize = .5;
23
-
24
- // debug commands are automatically removed from the final build
25
- function ASSERT (){}
26
- function LOG (){}
27
- function debugInit (){}
28
- function debugUpdate (){}
29
- function debugRender (){}
30
- function debugRenderPost (){}
31
- function debugRect (){}
32
- function debugPoly (){}
33
- function debugCircle (){}
34
- function debugPoint (){}
35
- function debugLine (){}
36
- function debugOverlap (){}
37
- function debugText (){}
38
- function debugClear (){}
39
- function debugScreenshot (){}
40
- function debugShowErrors(){}
41
- function debugVideoCaptureIsActive(){ return false; }
42
- function debugVideoCaptureStart (){}
43
- function debugVideoCaptureStop (){}
44
- function debugVideoCaptureUpdate(){}
45
- function debugProtectConstant(o){ return o; }
1
+ /**
2
+ * LittleJS - Release Mode
3
+ * - Replaces engineDebug.js in production builds
4
+ * - All debug functions are stubbed out as no-ops
5
+ * - Removes ASSERT and LOG calls to reduce file size
6
+ * - Disables debug overlay, watermark, and visualizations
7
+ * - Improves performance by eliminating debug overhead
8
+ * - Significantly reduces final bundle size
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ let debugWatermark = 0;
14
+ let debugKey = '';
15
+ let debugKeysAlways = false;
16
+ let debugTweakables = false;
17
+ let debugClearCount = 0;
18
+ const debug = 0;
19
+ const debugOverlay = 0;
20
+ const debugPhysics = 0;
21
+ const debugParticles = 0;
22
+ const debugRaycast = 0;
23
+ const debugGamepads = 0;
24
+ const debugSound = 0;
25
+ const debugPointSize = .5;
26
+
27
+ // debug commands are automatically removed from the final build
28
+ function ASSERT (){}
29
+ function LOG (){}
30
+ function debugInit (){}
31
+ function debugUpdate (){}
32
+ function debugRender (){}
33
+ function debugRenderPost (){}
34
+ function debugRect (){}
35
+ function debugPoly (){}
36
+ function debugCircle (){}
37
+ function debugPoint (){}
38
+ function debugLine (){}
39
+ function debugOverlap (){}
40
+ function debugText (){}
41
+ function debugClear (){}
42
+ function debugScreenshot (){}
43
+ function debugShowErrors(){}
44
+ function setDebugOverlay(){}
45
+ function debugVideoCaptureIsActive(){ return false; }
46
+ function debugVideoCaptureStart (){}
47
+ function debugVideoCaptureStop (){}
48
+ function debugProtectConstant(o){ return o; }
49
+
50
+ // the tweakables and the level editor are debug only
51
+ function tweak(){}
52
+ function tweakButton(){}
53
+ function tweakDivider(){}
54
+ function tweakEngineDefaults(){}
55
+ const levelEditor = {isOpen: false, open(){}, close(){}, onTile: undefined, onRestart: undefined, onPlayFrom: undefined};
56
+ function editorMapRestore(map){ return map; }
57
+ function editorMapLoaded(){}
58
+ function editorJSONFetched(){}
59
+ function editorPreRender(){}
60
+ function editorObjectMade(){}
61
+
62
+ // the 3D debug draws are debug only too
63
+ function debugBox3D(){}
64
+ function debugSphere3D(){}
65
+ function debugLine3D(){}
66
+ function debugPoint3D(){}
67
+ function render3DRenderDebug(){}
@@ -32,7 +32,7 @@ let cameraScale = 32;
32
32
 
33
33
  /** Scale applied to engine time, can be used for slow motion or fast forward
34
34
  * - 1 is normal speed, 2 is double speed, 0.5 is half speed
35
- * - 0 freezes the simulation without setting the paused flag
35
+ * - 0 freezes the game like a pause without setting the paused flag, gameUpdatePost and input still run
36
36
  * - Should be >= 0; stacks multiplicatively with the debug +/- shortcut
37
37
  * @type {number}
38
38
  * @default
@@ -76,7 +76,6 @@ let canvasMinAspect = 0;
76
76
  let canvasMaxAspect = 0;
77
77
 
78
78
  /** Fixed size of the canvas in css pixels, if enabled canvas size never changes
79
- * - you may also need to set mainCanvasSize if using screen space coords in startup
80
79
  * - canvasPixelRatio still applies, it only scales the backing store
81
80
  * @type {Vector2}
82
81
  * @default Vector2()
@@ -92,7 +91,8 @@ let canvasPixelated = false;
92
91
 
93
92
  /** Disables texture filtering for crisper pixel art
94
93
  * - Leave true for pixel art so sprites stay sharp when scaled (uses NEAREST filtering)
95
- * - Set false for smooth/high-resolution art to enable bilinear filtering and mipmaps
94
+ * - Set false for smooth/high-resolution art to enable bilinear filtering and mipmaps; those textures upload
95
+ * premultiplied, so filtering does not pull dark from see through texels into the edges
96
96
  * @type {boolean}
97
97
  * @default
98
98
  * @memberof Settings */
@@ -116,13 +116,13 @@ let canvasPixelRatio = 1;
116
116
  * @memberof Settings */
117
117
  let fontDefault = 'arial';
118
118
 
119
- /** Enable to show the LittleJS splash screen on startup
119
+ /** Enable to show the LittleJS splash screen on startup, must be set before engineInit
120
120
  * @type {boolean}
121
121
  * @default
122
122
  * @memberof Settings */
123
123
  let showSplashScreen = false;
124
124
 
125
- /** Disables all rendering, audio, and input for servers
125
+ /** Disables all rendering, audio, and input for servers, must be set before engineInit
126
126
  * @type {boolean}
127
127
  * @default
128
128
  * @memberof Settings */
@@ -144,6 +144,17 @@ let engineManualStep = false;
144
144
  * @memberof Settings */
145
145
  let glEnable = true;
146
146
 
147
+ /** Draw in screen space by default: what the screenSpace parameter of the draw and debug functions defaults to, so a
148
+ * heads up display, or a 3D game that only draws 2D on the screen, can turn it on and leave the flag out
149
+ * - The engine says its own space when it draws, so objects, tile layers and particles stay in world space, but a
150
+ * game's own draws follow it, a render() override included: turn it on around the hud, or pass false there
151
+ * - Sizes and widths keep their defaults, which are world sized, so pass them in pixels
152
+ * - drawText has no screenSpace and stays in world space, drawTextScreen is the one for the screen
153
+ * @type {boolean}
154
+ * @default
155
+ * @memberof Settings */
156
+ let drawScreenSpace = false;
157
+
147
158
  /** How many sided poly to use when drawing circles and ellipses with WebGL
148
159
  * @type {number}
149
160
  * @default
@@ -174,7 +185,7 @@ let tileDefaultBleed = 0;
174
185
  ///////////////////////////////////////////////////////////////////////////////
175
186
  // Object settings
176
187
 
177
- /** Enable physics solver for collisions between objects
188
+ /** Enable physics solver for collisions, between objects and with tiles
178
189
  * @type {boolean}
179
190
  * @default
180
191
  * @memberof Settings */
@@ -186,13 +197,13 @@ let enablePhysicsSolver = true;
186
197
  * @memberof Settings */
187
198
  let objectDefaultMass = 1;
188
199
 
189
- /** How much to slow velocity by each frame (0-1)
200
+ /** Fraction of velocity objects keep each frame, 1 keeps all of it, 0 stops at once
190
201
  * @type {number}
191
202
  * @default
192
203
  * @memberof Settings */
193
204
  let objectDefaultDamping = 1;
194
205
 
195
- /** How much to slow angular velocity each frame (0-1)
206
+ /** Fraction of angular velocity objects keep each frame, 1 keeps all of it, 0 stops at once
196
207
  * @type {number}
197
208
  * @default
198
209
  * @memberof Settings */
@@ -204,19 +215,20 @@ let objectDefaultAngleDamping = 1;
204
215
  * @memberof Settings */
205
216
  let objectDefaultRestitution = 0;
206
217
 
207
- /** How much to slow when touching (0-1)
218
+ /** Fraction of sliding speed objects keep each frame on the ground, 1 is no friction, 0 stops at once
219
+ * - The more slippery of an object and its ground is used
208
220
  * @type {number}
209
221
  * @default
210
222
  * @memberof Settings */
211
223
  let objectDefaultFriction = .8;
212
224
 
213
- /** Clamp max speed to avoid fast objects missing collisions
225
+ /** Clamp max speed to avoid fast objects missing collisions, in world units per frame on each axis
214
226
  * @type {number}
215
227
  * @default
216
228
  * @memberof Settings */
217
229
  let objectMaxSpeed = 1;
218
230
 
219
- /** How much gravity to apply to objects, negative Y is down
231
+ /** How much gravity to apply to objects, negative Y is down, in world units per frame per frame
220
232
  * @type {Vector2}
221
233
  * @default
222
234
  * @memberof Settings */
@@ -252,6 +264,7 @@ let gamepadDirectionEmulateStick = true;
252
264
  let gamepadAxisFilterEnable = true;
253
265
 
254
266
  /** If true the WASD keys are also routed to the direction keys (for better accessibility)
267
+ * - Turn it off for a game with two players on one keyboard, one on WASD and one on the arrows
255
268
  * @type {boolean}
256
269
  * @default
257
270
  * @memberof Settings */
@@ -266,9 +279,10 @@ let touchInputEnable = true;
266
279
 
267
280
  /** True if touch gamepad should appear on mobile devices
268
281
  * - Supports left analog stick, 4 face buttons and start button (button 9)
269
- * - setTouchGamepadButtonCount(1) to use face buttons as right analog stick
282
+ * - setTouchGamepadRightStick(true) for a right analog stick in place of the face buttons
270
283
  * - Analog stick buttons 10 and 11 are also activated when virtual sticks are touched
271
284
  * - Rendered as a full-viewport HTML/SVG overlay, so controls may sit outside the game canvas
285
+ * - It is gamepad 0 once touched; a real gamepad being used takes over and hides it until the screen is touched again
272
286
  * @type {boolean}
273
287
  * @default
274
288
  * @memberof Settings */
@@ -425,9 +439,14 @@ function setCameraAngle(angle) { cameraAngle = angle; }
425
439
  function setCameraScale(scale) { cameraScale = scale; }
426
440
 
427
441
  /** Set scale applied to engine time
428
- * @param {number} scale
442
+ * - 0 freezes the game like a pause, gameUpdatePost and input still run so the game can set it back
443
+ * @param {number} scale - 0 or more
429
444
  * @memberof Settings */
430
- function setTimeScale(scale) { timeScale = scale; }
445
+ function setTimeScale(scale)
446
+ {
447
+ ASSERT(scale >= 0, 'time scale must be 0 or more');
448
+ timeScale = max(0, scale); // a negative scale would build a debt of time the game then waits out
449
+ }
431
450
 
432
451
  /** Set if tiles should be colorized when using canvas2d
433
452
  * This can be slower but results should look nearly identical to WebGL rendering
@@ -476,6 +495,7 @@ function setCanvasPixelated(pixelated)
476
495
 
477
496
  /** Disables texture filtering for crisper pixel art
478
497
  * - Leave true for pixel art; set false for smooth/high-resolution art
498
+ * - Set it before engineInit, a texture already loaded keeps the filtering it was made with
479
499
  * @param {boolean} pixelated
480
500
  * @memberof Settings */
481
501
  function setTilesPixelated(pixelated) { tilesPixelated = pixelated; }
@@ -505,21 +525,40 @@ function getCanvasPixelRatio() { return canvasPixelRatio ?? (devicePixelRatio ||
505
525
  * @memberof Settings */
506
526
  function setFontDefault(font) { fontDefault = font; }
507
527
 
508
- /** Set if the LittleJS splash screen should be shown on startup
528
+ /** Set if the LittleJS splash screen should be shown on startup, must be set before engineInit
509
529
  * @param {boolean} show
510
530
  * @memberof Settings */
511
531
  function setShowSplashScreen(show) { showSplashScreen = show; }
512
532
 
513
- /** Set to disable rendering, audio, and input for servers
533
+ /** Set to disable rendering, audio, and input for servers, must be set before engineInit
514
534
  * @param {boolean} headless
515
535
  * @memberof Settings */
516
536
  function setHeadlessMode(headless) { headlessMode = headless; }
517
537
 
518
538
  /** Set if the engine only advances when engineStep is called
519
- * Must be set before engineInit
539
+ * Set before engineInit for a test or a server; turned on in a running game it stops the loop, and turned off
540
+ * again it starts the loop from real time
520
541
  * @param {boolean} [enable]
521
542
  * @memberof Settings */
522
- function setEngineManualStep(enable=true) { engineManualStep = enable; }
543
+ function setEngineManualStep(enable=true)
544
+ {
545
+ const resume = engineManualStep && !enable;
546
+ if (enable && !engineManualStep)
547
+ frameTimeBufferMS = -.5e3 / frameRate; // half a frame short, so each engineStep frame runs one update,
548
+ // not one more from time the last real frame left over
549
+ engineManualStep = enable;
550
+ if (resume && engineUpdateInternal)
551
+ {
552
+ // engineStep may have run the clock ahead of real time, start from now so that is not waited out
553
+ frameTimeLastMS = performance.now();
554
+ engineScheduleFrame();
555
+ }
556
+ }
557
+
558
+ /** Set whether the draw and debug functions draw in screen space when a call leaves screenSpace out
559
+ * @param {boolean} screenSpace
560
+ * @memberof Settings */
561
+ function setDrawScreenSpace(screenSpace) { drawScreenSpace = screenSpace; }
523
562
 
524
563
  /** Set if WebGL rendering is enabled
525
564
  * @param {boolean} enable
@@ -556,7 +595,7 @@ function setTileDefaultPadding(padding) { tileDefaultPadding = padding; }
556
595
  * @memberof Settings */
557
596
  function setTileDefaultBleed(bleed) { tileDefaultBleed = bleed; }
558
597
 
559
- /** Set if collisions between objects are enabled
598
+ /** Set if collisions are enabled, between objects and with tiles
560
599
  * @param {boolean} enable
561
600
  * @memberof Settings */
562
601
  function setEnablePhysicsSolver(enable) { enablePhysicsSolver = enable; }
@@ -566,12 +605,12 @@ function setEnablePhysicsSolver(enable) { enablePhysicsSolver = enable; }
566
605
  * @memberof Settings */
567
606
  function setObjectDefaultMass(mass) { objectDefaultMass = mass; }
568
607
 
569
- /** Set how much to slow velocity by each frame
608
+ /** Set the fraction of velocity objects keep each frame, 1 keeps all of it, 0 stops at once
570
609
  * @param {number} damp
571
610
  * @memberof Settings */
572
611
  function setObjectDefaultDamping(damp) { objectDefaultDamping = damp; }
573
612
 
574
- /** Set how much to slow angular velocity each frame
613
+ /** Set the fraction of angular velocity objects keep each frame, 1 keeps all of it, 0 stops at once
575
614
  * @param {number} damp
576
615
  * @memberof Settings */
577
616
  function setObjectDefaultAngleDamping(damp) { objectDefaultAngleDamping = damp; }
@@ -581,7 +620,7 @@ function setObjectDefaultAngleDamping(damp) { objectDefaultAngleDamping = damp;
581
620
  * @memberof Settings */
582
621
  function setObjectDefaultRestitution(restitution) { objectDefaultRestitution = restitution; }
583
622
 
584
- /** Set how much to slow when touching
623
+ /** Set the fraction of sliding speed objects keep each frame on the ground, 1 is no friction, 0 stops at once
585
624
  * @param {number} friction
586
625
  * @memberof Settings */
587
626
  function setObjectDefaultFriction(friction) { objectDefaultFriction = friction; }
@@ -596,7 +635,7 @@ function setObjectMaxSpeed(speed) { objectMaxSpeed = speed; }
596
635
  * @memberof Settings */
597
636
  function setGravity(newGravity) { gravity = newGravity.copy(); }
598
637
 
599
- /** Set to scales emit rate of particles
638
+ /** Set the scale for the emit rate of particles, 0 disables particle emitters
600
639
  * @param {number} scale
601
640
  * @memberof Settings */
602
641
  function setParticleEmitRateScale(scale) { particleEmitRateScale = scale; }
@@ -616,7 +655,7 @@ function setGamepadDirectionEmulateStick(enable) { gamepadDirectionEmulateStick
616
655
  * @memberof Settings */
617
656
  function setGamepadAxisFilterEnable(enable) { gamepadAxisFilterEnable = enable; }
618
657
 
619
- /** Set if true the WASD keys are also routed to the direction keys
658
+ /** Set if the WASD keys are also routed to the direction keys
620
659
  * @param {boolean} enable
621
660
  * @memberof Settings */
622
661
  function setInputWASDEmulateDirection(enable) { inputWASDEmulateDirection = enable; }
@@ -694,7 +733,7 @@ function setTouchGamepadAnalog(analog) { touchGamepadAnalog = analog; }
694
733
  * @memberof Settings */
695
734
  function setTouchGamepadFloating(floating) { touchGamepadFloating = floating; }
696
735
 
697
- /** Set size of virtual gamepad for touch devices in pixels
736
+ /** Set size of virtual gamepad for touch devices in viewport CSS pixels
698
737
  * @param {number} size
699
738
  * @memberof Settings */
700
739
  function setTouchGamepadSize(size) { touchGamepadSize = size; }
@@ -719,7 +758,7 @@ function setTouchGamepadVibration(ms) { touchGamepadVibration = ms; }
719
758
  * @memberof Settings */
720
759
  function setVibrateEnable(enable) { vibrateEnable = enable; }
721
760
 
722
- /** Set to disable all audio code
761
+ /** Set if audio is enabled, false turns all sound off
723
762
  * @param {boolean} enable
724
763
  * @memberof Settings */
725
764
  function setSoundEnable(enable) { soundEnable = enable; }
@@ -730,8 +769,7 @@ function setSoundEnable(enable) { soundEnable = enable; }
730
769
  function setSoundVolume(volume)
731
770
  {
732
771
  soundVolume = volume;
733
- if (soundEnable && !headlessMode && audioMasterGain)
734
- audioMasterGain.gain.value = volume; // update gain immediately
772
+ audioUpdateVolume(); // update gain immediately, sound off or not
735
773
  }
736
774
 
737
775
  /** Set default range where sound no longer plays
@@ -757,4 +795,14 @@ function setDebugWatermark(show) { debugWatermark = show; }
757
795
  /** Set key code used to toggle debug mode, Esc by default
758
796
  * @param {string} key
759
797
  * @memberof Debug */
760
- function setDebugKey(key) { debugKey = key; }
798
+ function setDebugKey(key) { debugKey = key; }
799
+
800
+ /** Set if the debug keys work while the overlay is closed, the number keys and the +/- time keys
801
+ * @param {boolean} [enable]
802
+ * @memberof Debug */
803
+ function setDebugKeysAlways(enable=true) { debugKeysAlways = enable; }
804
+
805
+ /** Set if the tweakables panel is shown, as 9 does while the debug overlay is open
806
+ * @param {boolean} [show]
807
+ * @memberof Debug */
808
+ function setDebugTweakables(show=true) { debugTweakables = show; }