littlejsengine 1.12.4 → 1.13.1

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 (88) hide show
  1. package/dist/box2d.wasm.js +630 -0
  2. package/dist/box2d.wasm.wasm +0 -0
  3. package/dist/littlejs.d.ts +437 -219
  4. package/dist/littlejs.esm.js +5950 -5307
  5. package/dist/littlejs.esm.min.js +4 -1
  6. package/dist/littlejs.js +5921 -5295
  7. package/dist/littlejs.min.js +4 -1
  8. package/dist/littlejs.release.js +5485 -4886
  9. package/examples/box2d/game.js +37 -42
  10. package/examples/box2d/gameObjects.js +41 -37
  11. package/examples/box2d/index.html +2 -2
  12. package/examples/box2d/scenes.js +24 -20
  13. package/examples/box2d/tiles.png +0 -0
  14. package/examples/breakout/game.js +1 -1
  15. package/examples/breakout/gameObjects.js +2 -2
  16. package/examples/breakout/index.html +1 -1
  17. package/examples/breakoutTutorial/README.md +2 -2
  18. package/examples/breakoutTutorial/game.js +1 -1
  19. package/examples/breakoutTutorial/index.html +1 -1
  20. package/examples/empty/index.html +1 -1
  21. package/examples/htmlMenu/index.html +1 -1
  22. package/examples/index.html +410 -159
  23. package/examples/module/game.js +1 -1
  24. package/examples/module/index.html +1 -1
  25. package/examples/platformer/game.js +6 -15
  26. package/examples/platformer/gameCharacter.js +6 -6
  27. package/examples/platformer/gameEffects.js +74 -57
  28. package/examples/platformer/gameLevel.js +61 -70
  29. package/examples/platformer/gameObjects.js +4 -4
  30. package/examples/platformer/index.html +1 -1
  31. package/examples/puzzle/index.html +1 -1
  32. package/examples/shorts/base.html +24 -3
  33. package/examples/shorts/box2d.js +46 -0
  34. package/examples/shorts/box2dCar.js +45 -0
  35. package/examples/shorts/flappyGame.js +52 -0
  36. package/examples/shorts/helloWorld.js +6 -3
  37. package/examples/shorts/hillGlideGame.js +56 -0
  38. package/examples/shorts/landerGame.js +32 -0
  39. package/examples/shorts/maze.js +47 -0
  40. package/examples/shorts/medals.js +42 -0
  41. package/examples/shorts/nineSlice.js +21 -0
  42. package/examples/shorts/piano.js +52 -0
  43. package/examples/shorts/platformer.js +24 -20
  44. package/examples/shorts/{pong.js → pongGame.js} +1 -1
  45. package/examples/shorts/postProcess.js +45 -0
  46. package/examples/shorts/raycasting.js +71 -0
  47. package/examples/shorts/slidingPuzzle.js +42 -0
  48. package/examples/shorts/spaceGame.js +56 -0
  49. package/examples/shorts/starfield.js +17 -0
  50. package/examples/shorts/tileLayer.js +3 -5
  51. package/examples/shorts/tiles.png +0 -0
  52. package/examples/shorts/tiltedView.js +8 -7
  53. package/examples/shorts/topDown.js +18 -26
  54. package/examples/shorts/uiSystem.js +36 -0
  55. package/examples/starter/build.bat +6 -1
  56. package/examples/starter/game.js +4 -2
  57. package/examples/starter/index.html +23 -13
  58. package/examples/stress/index.html +2 -3
  59. package/examples/style.css +136 -0
  60. package/examples/typescript/build.js +1 -2
  61. package/examples/typescript/game.js +2 -2
  62. package/examples/typescript/game.ts +1 -1
  63. package/examples/typescript/index.html +1 -1
  64. package/examples/uiSystem/game.js +9 -25
  65. package/examples/uiSystem/index.html +1 -1
  66. package/package.json +1 -1
  67. package/plugins/box2d.js +29 -35
  68. package/plugins/drawUtilities.js +126 -0
  69. package/plugins/newgrounds.js +1 -1
  70. package/plugins/pluginExport.js +8 -2
  71. package/plugins/postProcess.js +2 -2
  72. package/plugins/uiSystem.js +162 -68
  73. package/plugins/zzfxm.js +1 -1
  74. package/reference.md +10 -10
  75. package/src/engine.js +59 -39
  76. package/src/engineAudio.js +38 -20
  77. package/src/engineBuild.bat +6 -1
  78. package/src/engineBuild.js +32 -7
  79. package/src/engineDebug.js +53 -26
  80. package/src/engineDraw.js +108 -57
  81. package/src/engineExport.js +22 -9
  82. package/src/engineInput.js +112 -40
  83. package/src/engineObject.js +68 -67
  84. package/src/engineParticles.js +26 -9
  85. package/src/engineSettings.js +15 -16
  86. package/src/engineTileLayer.js +312 -153
  87. package/src/engineUtilities.js +198 -130
  88. package/src/engineWebGL.js +58 -35
package/plugins/box2d.js CHANGED
@@ -1,31 +1,35 @@
1
1
  /**
2
2
  * LittleJS Box2D Physics Plugin
3
3
  * - Box2dObject extends EngineObject with Box2D physics
4
- * - Call box2dEngineInit() to start instead of normal engineInit()
4
+ * - Call box2dInit() before engineInit() to enable
5
5
  * - You will also need to include box2d.wasm.js
6
- * - Uses box2d.js super fast web assembly port of Box2D
6
+ * - Uses a super fast web assembly port of Box2D
7
7
  * - More info: https://github.com/kripken/box2d.js
8
- * - Fully wraps everything in Box2d
9
8
  * - Functions to create polygon, circle, and edge shapes
10
- * - Raycasting and querying
11
- * - Joint creation
12
9
  * - Contact begin and end callbacks
10
+ * - Wraps b2Vec2 type to/from Vector2
11
+ * - Raycasting and querying
12
+ * - Every type of joint
13
13
  * - Debug physics drawing
14
+ * @namespace Box2D
14
15
  */
15
16
 
16
17
  'use strict';
17
18
 
18
19
  /** Global Box2d Plugin object
19
- * @type {Box2dPlugin} */
20
+ * @type {Box2dPlugin}
21
+ * @memberof Box2D */
20
22
  let box2d;
21
23
 
22
24
  /** Enable Box2D debug drawing
23
25
  * @type {boolean}
24
- * @default */
26
+ * @default
27
+ * @memberof Box2D */
25
28
  let box2dDebug = false;
26
29
 
27
30
  /** Enable Box2D debug drawing
28
- * @param {boolean} enable */
31
+ * @param {boolean} enable
32
+ * @memberof Box2D */
29
33
  function box2dSetDebug(enable) { box2dDebug = enable; }
30
34
 
31
35
  ///////////////////////////////////////////////////////////////////////////////
@@ -72,7 +76,7 @@ class Box2dObject extends EngineObject
72
76
  /** Copy box2d update sim data */
73
77
  update()
74
78
  {
75
- // use box2d physics update
79
+ // use box2d physics update instead of normal engine update
76
80
  this.pos = box2d.vec2From(this.body.GetPosition());
77
81
  this.angle = -this.body.GetAngle();
78
82
  }
@@ -1588,7 +1592,7 @@ class Box2dPlugin
1588
1592
  * @param {Color} [outlineColor]
1589
1593
  * @param {number} [lineWidth]
1590
1594
  * @param {CanvasRenderingContext2D} [context] */
1591
- drawFixture(fixture, pos, angle, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
1595
+ drawFixture(fixture, pos, angle, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=drawContext)
1592
1596
  {
1593
1597
  const shape = box2d.castObjectType(fixture.GetShape());
1594
1598
  switch (shape.GetType())
@@ -1624,7 +1628,7 @@ class Box2dPlugin
1624
1628
  * @param {Color} [outlineColor]
1625
1629
  * @param {number} [lineWidth]
1626
1630
  * @param {CanvasRenderingContext2D} [context] */
1627
- drawCircle(pos, radius, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
1631
+ drawCircle(pos, radius, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=drawContext)
1628
1632
  {
1629
1633
  drawCanvas2D(pos, vec2(1), 0, 0, context=>
1630
1634
  {
@@ -1642,7 +1646,7 @@ class Box2dPlugin
1642
1646
  * @param {Color} [outlineColor]
1643
1647
  * @param {number} [lineWidth]
1644
1648
  * @param {CanvasRenderingContext2D} [context] */
1645
- drawPoly(pos, angle, points, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
1649
+ drawPoly(pos, angle, points, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=drawContext)
1646
1650
  {
1647
1651
  drawCanvas2D(pos, vec2(1), angle, 0, context=>
1648
1652
  {
@@ -1661,7 +1665,7 @@ class Box2dPlugin
1661
1665
  * @param {Color} [color]
1662
1666
  * @param {number} [lineWidth]
1663
1667
  * @param {CanvasRenderingContext2D} [context] */
1664
- drawLine(pos, angle, posA, posB, color=WHITE, lineWidth=.1, context=mainContext)
1668
+ drawLine(pos, angle, posA, posB, color=WHITE, lineWidth=.1, context=drawContext)
1665
1669
  {
1666
1670
  drawCanvas2D(pos, vec2(1), angle, 0, context=>
1667
1671
  {
@@ -1677,7 +1681,7 @@ class Box2dPlugin
1677
1681
  * @param {Color} [outlineColor]
1678
1682
  * @param {number} [lineWidth]
1679
1683
  * @param {CanvasRenderingContext2D} [context] */
1680
- drawFillStroke(color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
1684
+ drawFillStroke(color=WHITE, outlineColor=BLACK, lineWidth=.1, context=drawContext)
1681
1685
  {
1682
1686
  if (color)
1683
1687
  {
@@ -1766,28 +1770,18 @@ class Box2dPlugin
1766
1770
  }
1767
1771
 
1768
1772
  ///////////////////////////////////////////////////////////////////////////////
1769
- /** Box2d Init - Startup LittleJS engine with your callback functions
1770
- * @param {Function|function():Promise} gameInit - Called once after the engine starts up
1771
- * @param {Function} gameUpdate - Called every frame before objects are updated
1772
- * @param {Function} gameUpdatePost - Called after physics and objects are updated, even when paused
1773
- * @param {Function} gameRender - Called before objects are rendered, for drawing the background
1774
- * @param {Function} gameRenderPost - Called after objects are rendered, useful for drawing UI
1775
- * @param {Array<string>} [imageSources=[]] - List of images to load
1776
- * @param {HTMLElement} [rootElement] - Root element to attach to, the document body by default */
1777
- function box2dEngineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources, rootElement)
1773
+ /** Box2d Init - Call with await before starting LittleJS to init box2d
1774
+ * @return {Promise<Box2dPlugin>}
1775
+ * @memberof Box2D */
1776
+ async function box2dInit()
1778
1777
  {
1779
- Box2D().then(box2dInstance=>
1780
- {
1781
- // create box2d object
1782
- new Box2dPlugin(box2dInstance);
1783
- setupDebugDraw();
1778
+ // load box2d
1779
+ new Box2dPlugin(await Box2D());
1780
+ setupDebugDraw();
1781
+ engineAddPlugin(box2dUpdate, box2dRender);
1782
+ return box2d;
1784
1783
 
1785
- // start littlejs
1786
- engineAddPlugin(box2dUpdate, box2dRender);
1787
- engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources, rootElement);
1788
- });
1789
-
1790
- // hook up box2d plugin to update and render
1784
+ // add the box2d plugin to the engine
1791
1785
  function box2dUpdate()
1792
1786
  {
1793
1787
  if (!paused)
@@ -1866,4 +1860,4 @@ function box2dEngineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameR
1866
1860
  //debugDraw.AppendFlags(box2d.instance.b2Draw.e_centerOfMassBit);
1867
1861
  box2d.world.SetDebugDraw(debugDraw);
1868
1862
  }
1869
- }
1863
+ }
@@ -0,0 +1,126 @@
1
+ /**
2
+ * LittleJS Drawing Utilities Plugin
3
+ * - Extra drawing functions for LittleJS
4
+ * - Nine slice and three slice drawing
5
+ * @namespace DrawUtilities
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ ///////////////////////////////////////////////////////////////////////////////
11
+
12
+ /** Draw a scalable nine-slice UI element to the overlay canvas in screen space
13
+ * This function can not apply color because it draws using the overlay 2d context
14
+ * @param {Vector2} pos - Screen space position
15
+ * @param {Vector2} size - Screen space size
16
+ * @param {TileInfo} startTile - Starting tile for the nine-slice pattern
17
+ * @param {number} [borderSize] - Width of the border sections
18
+ * @param {number} [extraSpace] - Extra spacing adjustment
19
+ * @memberof DrawUtilities */
20
+ function drawNineSliceScreen(pos, size, startTile, borderSize=32, extraSpace=1)
21
+ {
22
+ drawNineSlice(pos, size, startTile, WHITE, borderSize, BLACK, extraSpace, false, true, overlayContext);
23
+ }
24
+
25
+ /** Draw a scalable nine-slice UI element in world space
26
+ * This function can apply color and additive color if webgl is enabled
27
+ * @param {Vector2} pos - World space position
28
+ * @param {Vector2} size - World space size
29
+ * @param {TileInfo} startTile - Starting tile for the nine-slice pattern
30
+ * @param {Color} [color] - Color to modulate with
31
+ * @param {number} [borderSize] - Width of the border sections
32
+ * @param {Color} [additiveColor] - Additive color
33
+ * @param {number} [extraSpace] - Extra spacing adjustment
34
+ * @param {boolean} [useWebGL=glEnable] - Use WebGL for rendering
35
+ * @param {boolean} [screenSpace] - Use screen space coordinates
36
+ * @param {CanvasRenderingContext2D} [context] - Canvas context to use
37
+ * @memberof DrawUtilities */
38
+ function drawNineSlice(pos, size, startTile, color, borderSize=1, additiveColor, extraSpace=.01, useWebGL=glEnable, screenSpace, context)
39
+ {
40
+ // setup nine slice tiles
41
+ const centerTile = startTile.offset(startTile.size);
42
+ const centerSize = size.add(vec2(extraSpace-borderSize*2));
43
+ const cornerSize = vec2(borderSize);
44
+ const cornerOffset = size.scale(.5).subtract(cornerSize.scale(.5));
45
+ const flip = screenSpace ? -1 : 1;
46
+
47
+ // center
48
+ drawTile(pos, centerSize, centerTile, color, 0, false, additiveColor, useWebGL, screenSpace, context);
49
+ for(let i=4; i--;)
50
+ {
51
+ // sides
52
+ const horizontal = i%2;
53
+ const sidePos = cornerOffset.multiply(vec2(horizontal?i==1?1:-1:0, horizontal?0:i?-1:1));
54
+ const sideSize = vec2(horizontal ? borderSize : centerSize.x, horizontal ? centerSize.y : borderSize);
55
+ const sideTile = centerTile.offset(startTile.size.multiply(vec2(i==1?1:i==3?-1:0,i==0?-flip:i==2?flip:0)))
56
+ drawTile(pos.add(sidePos), sideSize, sideTile, color, 0, false, additiveColor, useWebGL, screenSpace, context);
57
+ }
58
+ for(let i=4; i--;)
59
+ {
60
+ // corners
61
+ const flipX = i>1;
62
+ const flipY = i && i<3;
63
+ const cornerPos = cornerOffset.multiply(vec2(flipX?-1:1, flipY?-1:1));
64
+ const cornerTile = centerTile.offset(startTile.size.multiply(vec2(flipX?-1:1,flipY?flip:-flip)));
65
+ drawTile(pos.add(cornerPos), cornerSize, cornerTile, color, 0, false, additiveColor, useWebGL, screenSpace, context);
66
+ }
67
+ }
68
+
69
+ /** Draw a scalable three-slice UI element to the overlay canvas in screen space
70
+ * This function can not apply color because it draws using the overlay 2d context
71
+ * @param {Vector2} pos - Screen space position
72
+ * @param {Vector2} size - Screen space size
73
+ * @param {TileInfo} startTile - Starting tile for the three-slice pattern
74
+ * @param {number} [borderSize] - Width of the border sections
75
+ * @param {number} [extraSpace] - Extra spacing adjustment
76
+ * @memberof DrawUtilities */
77
+ function drawThreeSliceScreen(pos, size, startTile, borderSize=32, extraSpace=1)
78
+ {
79
+ drawThreeSlice(pos, size, startTile, WHITE, borderSize, BLACK, extraSpace, false, true, overlayContext);
80
+ }
81
+
82
+ /** Draw a scalable three-slice UI element in world space
83
+ * This function can apply color and additive color if webgl is enabled
84
+ * @param {Vector2} pos - World space position
85
+ * @param {Vector2} size - World space size
86
+ * @param {TileInfo} startTile - Starting tile for the three-slice pattern
87
+ * @param {Color} [color] - Color to modulate with
88
+ * @param {number} [borderSize] - Width of the border sections
89
+ * @param {Color} [additiveColor] - Additive color
90
+ * @param {number} [extraSpace] - Extra spacing adjustment
91
+ * @param {boolean} [useWebGL=glEnable] - Use WebGL for rendering
92
+ * @param {boolean} [screenSpace] - Use screen space coordinates
93
+ * @param {CanvasRenderingContext2D} [context] - Canvas context to use
94
+ * @memberof DrawUtilities */
95
+ function drawThreeSlice(pos, size, startTile, color, borderSize=1, additiveColor, extraSpace=.01, useWebGL=glEnable, screenSpace, context)
96
+ {
97
+ // setup three slice tiles
98
+ const cornerTile = startTile.frame(0);
99
+ const sideTile = startTile.frame(1);
100
+ const centerTile = startTile.frame(2);
101
+ const centerSize = size.add(vec2(extraSpace-borderSize*2));
102
+ const cornerSize = vec2(borderSize);
103
+ const cornerOffset = size.scale(.5).subtract(cornerSize.scale(.5));
104
+ const flip = screenSpace ? -1 : 1;
105
+
106
+ // center
107
+ drawTile(pos, centerSize, centerTile, color, 0, false, additiveColor, useWebGL, screenSpace, context);
108
+ for(let i=4; i--;)
109
+ {
110
+ // sides
111
+ const angle = i*PI/2;
112
+ const horizontal = i%2;
113
+ const sidePos = cornerOffset.multiply(vec2(horizontal?i==1?1:-1:0, horizontal?0:i?-flip:flip));
114
+ const sideSize = vec2(horizontal ? centerSize.y : centerSize.x, borderSize);
115
+ drawTile(pos.add(sidePos), sideSize, sideTile, color, angle, false, additiveColor, useWebGL, screenSpace, context);
116
+ }
117
+ for(let i=4; i--;)
118
+ {
119
+ // corners
120
+ const angle = i*PI/2;
121
+ const flipX = !i || i>2;
122
+ const flipY = i>1;
123
+ const cornerPos = cornerOffset.multiply(vec2(flipX?-1:1, flipY?-flip:flip));
124
+ drawTile(pos.add(cornerPos), cornerSize, cornerTile, color, angle, false, additiveColor, useWebGL, screenSpace, context);
125
+ }
126
+ }
@@ -174,4 +174,4 @@ class NewgroundsPlugin
174
174
  debugMedals && console.log(xmlHttp.responseText);
175
175
  return xmlHttp.responseText && JSON.parse(xmlHttp.responseText);
176
176
  }
177
- }
177
+ }
@@ -30,7 +30,7 @@ export
30
30
  box2d,
31
31
  box2dDebug,
32
32
  box2dSetDebug,
33
- box2dEngineInit,
33
+ box2dInit,
34
34
  Box2dPlugin,
35
35
  Box2dObject,
36
36
  Box2dRaycastResult,
@@ -47,4 +47,10 @@ export
47
47
  Box2dFrictionJoint,
48
48
  Box2dPulleyJoint,
49
49
  Box2dMotorJoint,
50
- };
50
+
51
+ // Drawing Utilities
52
+ drawNineSlice,
53
+ drawNineSliceScreen,
54
+ drawThreeSlice,
55
+ drawThreeSliceScreen,
56
+ };
@@ -78,7 +78,7 @@ class PostProcessPlugin
78
78
  else
79
79
  {
80
80
  // set the viewport
81
- glContext.viewport(0, 0, glCanvas.width = mainCanvas.width, glCanvas.height = mainCanvas.height);
81
+ glContext.viewport(0, 0, glCanvas.width = drawCanvas.width, glCanvas.height = drawCanvas.height);
82
82
  }
83
83
 
84
84
  if (postProcess.includeOverlay)
@@ -113,4 +113,4 @@ class PostProcessPlugin
113
113
  glContext.drawArrays(glContext.TRIANGLE_STRIP, 0, 4);
114
114
  }
115
115
  }
116
- }
116
+ }