littlejsengine 1.4.91 → 1.5.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 (41) hide show
  1. package/README.md +1 -0
  2. package/build.bat +4 -6
  3. package/engine/engine.all.js +202 -105
  4. package/engine/engine.all.min.js +1 -1
  5. package/engine/engine.all.module.js +385 -181
  6. package/engine/engine.all.module.min.js +1 -1
  7. package/engine/engine.all.release.js +192 -100
  8. package/engine/engine.js +47 -16
  9. package/engine/engineAudio.js +14 -1
  10. package/engine/engineBuild.bat +8 -8
  11. package/engine/engineDebug.js +10 -5
  12. package/engine/engineDraw.js +25 -27
  13. package/engine/engineExport.js +183 -76
  14. package/engine/engineInput.js +6 -1
  15. package/engine/engineMedals.js +1 -0
  16. package/engine/engineObject.js +11 -9
  17. package/engine/engineParticles.js +5 -5
  18. package/engine/engineSettings.js +46 -20
  19. package/engine/engineTileLayer.js +13 -12
  20. package/engine/engineUtilities.js +13 -7
  21. package/engine/engineWebGL.js +11 -2
  22. package/engine/index.d.ts +1788 -1700
  23. package/examples/breakout/game.js +3 -1
  24. package/examples/breakout/index.html +4 -4
  25. package/examples/empty/index.html +1 -4
  26. package/examples/module/game.js +1 -4
  27. package/examples/module/index.html +2 -2
  28. package/examples/particles/index.html +2 -2
  29. package/examples/platformer/game.js +2 -0
  30. package/examples/platformer/gameObjects.js +3 -3
  31. package/examples/platformer/index.html +7 -7
  32. package/examples/puzzle/index.html +3 -3
  33. package/examples/stress/index.html +5 -5
  34. package/examples/typescript/build.bat +14 -0
  35. package/examples/typescript/game.js +89 -0
  36. package/examples/typescript/game.ts +117 -0
  37. package/examples/typescript/index.html +9 -0
  38. package/examples/typescript/tiles.png +0 -0
  39. package/examples/typescript/tsconfig.json +8 -0
  40. package/index.html +14 -14
  41. package/package.json +6 -6
package/engine/engine.js CHANGED
@@ -17,43 +17,71 @@
17
17
  - Call engineInit() to start it up!
18
18
  */
19
19
 
20
+ /**
21
+ * LittleJS Engine Globals
22
+ * @namespace Engine
23
+ */
24
+
20
25
  'use strict';
21
26
 
22
- /** Name of engine */
27
+ /** Name of engine
28
+ * @type {String}
29
+ * @default
30
+ * @memberof Engine */
23
31
  const engineName = 'LittleJS';
24
32
 
25
- /** Version of engine */
26
- const engineVersion = '1.4.9';
33
+ /** Version of engine
34
+ * @type {String}
35
+ * @default
36
+ * @memberof Engine */
37
+ const engineVersion = '1.5.1';
27
38
 
28
39
  /** Frames per second to update objects
29
- * @default */
40
+ * @type {Number}
41
+ * @default
42
+ * @memberof Engine */
30
43
  const frameRate = 60;
31
44
 
32
45
  /** How many seconds each frame lasts, engine uses a fixed time step
33
- * @default 1/60 */
46
+ * @type {Number}
47
+ * @default 1/60
48
+ * @memberof Engine */
34
49
  const timeDelta = 1/frameRate;
35
50
 
36
- /** Array containing all engine objects */
51
+ /** Array containing all engine objects
52
+ * @type {Array}
53
+ * @memberof Engine */
37
54
  let engineObjects = [];
38
55
 
39
- /** Array containing only objects that are set to collide with other objects this frame (for optimization) */
56
+ /** Array containing only objects that are set to collide with other objects this frame (for optimization)
57
+ * @type {Array}
58
+ * @memberof Engine */
40
59
  let engineObjectsCollide = [];
41
60
 
42
- /** Current update frame, used to calculate time */
61
+ /** Current update frame, used to calculate time
62
+ * @type {Number}
63
+ * @memberof Engine */
43
64
  let frame = 0;
44
65
 
45
- /** Current engine time since start in seconds, derived from frame */
66
+ /** Current engine time since start in seconds, derived from frame
67
+ * @type {Number}
68
+ * @memberof Engine */
46
69
  let time = 0;
47
70
 
48
- /** Actual clock time since start in seconds (not affected by pause or frame rate clamping) */
71
+ /** Actual clock time since start in seconds (not affected by pause or frame rate clamping)
72
+ * @type {Number}
73
+ * @memberof Engine */
49
74
  let timeReal = 0;
50
75
 
51
- /** Is the game paused? Causes time and objects to not be updated */
76
+ /** Is the game paused? Causes time and objects to not be updated
77
+ * @type {Boolean}
78
+ * @default 0
79
+ * @memberof Engine */
52
80
  let paused = 0;
53
81
 
54
82
  /** Set if game is paused
55
83
  * @param {Boolean} paused
56
- */
84
+ * @memberof Engine */
57
85
  function setPaused(_paused) { paused = _paused; }
58
86
 
59
87
  ///////////////////////////////////////////////////////////////////////////////
@@ -65,7 +93,7 @@ function setPaused(_paused) { paused = _paused; }
65
93
  * @param {Function} gameRender - Called before objects are rendered, draw any background effects that appear behind objects
66
94
  * @param {Function} gameRenderPost - Called after objects are rendered, draw effects or hud that appear above all objects
67
95
  * @param {String} [tileImageSource] - Tile image to use, everything starts when the image is finished loading
68
- */
96
+ * @memberof Engine */
69
97
  function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, tileImageSource)
70
98
  {
71
99
  // init engine when tiles load or fail to load
@@ -229,7 +257,8 @@ function enginePreRender()
229
257
  glEnable && glPreRender();
230
258
  }
231
259
 
232
- /** Update each engine object, remove destroyed objects, and update time */
260
+ /** Update each engine object, remove destroyed objects, and update time
261
+ * @memberof Engine */
233
262
  function engineObjectsUpdate()
234
263
  {
235
264
  // get list of solid objects for physics optimzation
@@ -255,7 +284,8 @@ function engineObjectsUpdate()
255
284
  time = ++frame / frameRate;
256
285
  }
257
286
 
258
- /** Destroy and remove all objects */
287
+ /** Destroy and remove all objects
288
+ * @memberof Engine */
259
289
  function engineObjectsDestroy()
260
290
  {
261
291
  for (const o of engineObjects)
@@ -267,7 +297,8 @@ function engineObjectsDestroy()
267
297
  * @param {Vector2} [pos] - Center of test area
268
298
  * @param {Number} [size] - Radius of circle if float, rectangle size if Vector2
269
299
  * @param {Function} [callbackFunction] - Calls this function on every object that passes the test
270
- * @param {Array} [objects=engineObjects] - List of objects to check */
300
+ * @param {Array} [objects=engineObjects] - List of objects to check
301
+ * @memberof Engine */
271
302
  function engineObjectsCallback(pos, size, callbackFunction, objects=engineObjects)
272
303
  {
273
304
  if (!pos) // all objects
@@ -146,8 +146,21 @@ class Music
146
146
  {
147
147
  if (!soundEnable) return;
148
148
 
149
- return playSamples(this.cachedSamples, volume, 1, 0, loop);
149
+ return this.source = playSamples(this.cachedSamples, volume, 1, 0, loop);
150
150
  }
151
+
152
+ /** Stop the music */
153
+ stop()
154
+ {
155
+ if (this.source)
156
+ this.source.stop();
157
+ this.source = 0;
158
+ }
159
+
160
+ /** Check if music is playing
161
+ * @return {Boolean}
162
+ */
163
+ isPlaying() { return this.source; }
151
164
  }
152
165
 
153
166
  /** Play an mp3 or wav audio from a local file or url
@@ -93,14 +93,6 @@ if %ERRORLEVEL% NEQ 0 (
93
93
  exit /b %ERRORLEVEL%
94
94
  )
95
95
 
96
- rem --- BUILD TYPESCRIPT DEFINITIONS ---
97
-
98
- call npx tsc engine.all.js --declaration --allowJs --emitDeclarationOnly --outFile index.d.ts
99
- if %ERRORLEVEL% NEQ 0 (
100
- pause
101
- exit /b %ERRORLEVEL%
102
- )
103
-
104
96
  rem --- BUILD ENGINE MODULE ---
105
97
 
106
98
  set OUTPUT_FILENAME=engine.all.module.js
@@ -121,6 +113,14 @@ echo.>> %OUTPUT_FILENAME%
121
113
 
122
114
  rem lightly minify with uglify
123
115
  call npx uglifyjs -o %OUTPUT_FILENAME% -- %OUTPUT_FILENAME%
116
+ if %ERRORLEVEL% NEQ 0 (
117
+ pause
118
+ exit /b %ERRORLEVEL%
119
+ )
120
+
121
+ rem --- BUILD TYPESCRIPT DEFINITIONS ---
122
+
123
+ call npx tsc engine.all.module.js --declaration --allowJs --emitDeclarationOnly --outFile index.d.ts
124
124
  if %ERRORLEVEL% NEQ 0 (
125
125
  pause
126
126
  exit /b %ERRORLEVEL%
@@ -16,26 +16,31 @@
16
16
  'use strict';
17
17
 
18
18
  /** True if debug is enabled
19
+ * @type {Boolean}
19
20
  * @default
20
21
  * @memberof Debug */
21
22
  const debug = 1;
22
23
 
23
24
  /** True if asserts are enaled
25
+ * @type {Boolean}
24
26
  * @default
25
27
  * @memberof Debug */
26
28
  const enableAsserts = 1;
27
29
 
28
30
  /** Size to render debug points by default
31
+ * @type {Number}
29
32
  * @default
30
33
  * @memberof Debug */
31
34
  const debugPointSize = .5;
32
35
 
33
36
  /** True if watermark with FPS should be down, false in release builds
37
+ * @type {Boolean}
34
38
  * @default
35
39
  * @memberof Debug */
36
40
  let showWatermark = 1;
37
41
 
38
42
  /** True if god mode is enabled, handle this however you want
43
+ * @type {Boolean}
39
44
  * @default
40
45
  * @memberof Debug */
41
46
  let godMode = 0;
@@ -55,13 +60,13 @@ const ASSERT = enableAsserts ? (...assert)=> console.assert(...assert) : ()=>{};
55
60
 
56
61
  /** Draw a debug rectangle in world space
57
62
  * @param {Vector2} pos
58
- * @param {Vector2} [size=new Vector2()]
63
+ * @param {Vector2} [size=Vector2()]
59
64
  * @param {String} [color='#fff']
60
65
  * @param {Number} [time=0]
61
66
  * @param {Number} [angle=0]
62
- * @param {Boolean} [fill=0]
67
+ * @param {Boolean} [fill=false]
63
68
  * @memberof Debug */
64
- const debugRect = (pos, size=vec2(), color='#fff', time=0, angle=0, fill=0)=>
69
+ const debugRect = (pos, size=vec2(), color='#fff', time=0, angle=0, fill=false)=>
65
70
  {
66
71
  ASSERT(typeof color == 'string'); // pass in regular html strings as colors
67
72
  debugPrimitives.push({pos, size:vec2(size), color, time:new Timer(time), angle, fill});
@@ -72,9 +77,9 @@ const debugRect = (pos, size=vec2(), color='#fff', time=0, angle=0, fill=0)=>
72
77
  * @param {Number} [radius=0]
73
78
  * @param {String} [color='#fff']
74
79
  * @param {Number} [time=0]
75
- * @param {Boolean} [fill=0]
80
+ * @param {Boolean} [fill=false]
76
81
  * @memberof Debug */
77
- const debugCircle = (pos, radius=0, color='#fff', time=0, fill=0)=>
82
+ const debugCircle = (pos, radius=0, color='#fff', time=0, fill=false)=>
78
83
  {
79
84
  ASSERT(typeof color == 'string'); // pass in regular html strings as colors
80
85
  debugPrimitives.push({pos, size:radius, color, time:new Timer(time), angle:0, fill});
@@ -48,7 +48,7 @@ let overlayContext;
48
48
  let mainCanvasSize = vec2();
49
49
 
50
50
  /** Tile sheet for batch rendering system
51
- * @type {Image}
51
+ * @type {CanvasImageSource}
52
52
  * @memberof Draw */
53
53
  const tileImage = new Image;
54
54
 
@@ -78,15 +78,15 @@ const worldToScreen = (worldPos)=>
78
78
  }
79
79
 
80
80
  /** Draw textured tile centered in world space, with color applied if using WebGL
81
- * @param {Vector2} pos - Center of the tile in world space
82
- * @param {Vector2} [size=new Vector2(1,1)] - Size of the tile in world space
83
- * @param {Number} [tileIndex=-1] - Tile index to use, negative is untextured
84
- * @param {Vector2} [tileSize=tileSizeDefault] - Tile size in source pixels
85
- * @param {Color} [color=new Color(1,1,1)] - Color to modulate with
86
- * @param {Number} [angle=0] - Angle to rotate by
87
- * @param {Boolean} [mirror=0] - If true image is flipped along the Y axis
88
- * @param {Color} [additiveColor=new Color(0,0,0,0)] - Additive color to be applied
89
- * @param {Boolean} [useWebGL=glEnable] - Use accelerated WebGL rendering
81
+ * @param {Vector2} pos - Center of the tile in world space
82
+ * @param {Vector2} [size=Vector2(1,1)] - Size of the tile in world space
83
+ * @param {Number} [tileIndex=-1] - Tile index to use, negative is untextured
84
+ * @param {Vector2} [tileSize=tileSizeDefault] - Tile size in source pixels
85
+ * @param {Color} [color=Color()] - Color to modulate with
86
+ * @param {Number} [angle=0] - Angle to rotate by
87
+ * @param {Boolean} [mirror=0] - If true image is flipped along the Y axis
88
+ * @param {Color} [additiveColor=Color(0,0,0,0)] - Additive color to be applied
89
+ * @param {Boolean} [useWebGL=glEnable] - Use accelerated WebGL rendering
90
90
  * @memberof Draw */
91
91
  function drawTile(pos, size=vec2(1), tileIndex=-1, tileSize=tileSizeDefault, color=new Color, angle=0, mirror,
92
92
  additiveColor=new Color(0,0,0,0), useWebGL=glEnable)
@@ -141,8 +141,8 @@ function drawTile(pos, size=vec2(1), tileIndex=-1, tileSize=tileSizeDefault, col
141
141
 
142
142
  /** Draw colored rect centered on pos
143
143
  * @param {Vector2} pos
144
- * @param {Vector2} [size=new Vector2(1,1)]
145
- * @param {Color} [color=new Color(1,1,1)]
144
+ * @param {Vector2} [size=Vector2(1,1)]
145
+ * @param {Color} [color=Color()]
146
146
  * @param {Number} [angle=0]
147
147
  * @param {Boolean} [useWebGL=glEnable]
148
148
  * @memberof Draw */
@@ -153,13 +153,13 @@ function drawRect(pos, size, color, angle, useWebGL)
153
153
 
154
154
  /** Draw textured tile centered on pos in screen space
155
155
  * @param {Vector2} pos - Center of the tile
156
- * @param {Vector2} [size=new Vector2(1,1)] - Size of the tile
156
+ * @param {Vector2} [size=Vector2(1,1)] - Size of the tile
157
157
  * @param {Number} [tileIndex=-1] - Tile index to use, negative is untextured
158
158
  * @param {Vector2} [tileSize=tileSizeDefault] - Tile size in source pixels
159
- * @param {Color} [color=new Color]
159
+ * @param {Color} [color=Color()]
160
160
  * @param {Number} [angle=0]
161
161
  * @param {Boolean} [mirror=0]
162
- * @param {Color} [additiveColor=new Color(0,0,0,0)]
162
+ * @param {Color} [additiveColor=Color(0,0,0,0)]
163
163
  * @param {Boolean} [useWebGL=glEnable]
164
164
  * @memberof Draw */
165
165
  function drawTileScreenSpace(pos, size=vec2(1), tileIndex, tileSize, color, angle, mirror, additiveColor, useWebGL)
@@ -169,8 +169,8 @@ function drawTileScreenSpace(pos, size=vec2(1), tileIndex, tileSize, color, angl
169
169
 
170
170
  /** Draw colored rectangle in screen space
171
171
  * @param {Vector2} pos
172
- * @param {Vector2} [size=new Vector2(1,1)]
173
- * @param {Color} [color=new Color(1,1,1)]
172
+ * @param {Vector2} [size=Vector2(1,1)]
173
+ * @param {Color} [color=Color()]
174
174
  * @param {Number} [angle=0]
175
175
  * @param {Boolean} [useWebGL=glEnable]
176
176
  * @memberof Draw */
@@ -183,7 +183,7 @@ function drawRectScreenSpace(pos, size, color, angle, useWebGL)
183
183
  * @param {Vector2} posA
184
184
  * @param {Vector2} posB
185
185
  * @param {Number} [thickness=.1]
186
- * @param {Color} [color=new Color(1,1,1)]
186
+ * @param {Color} [color=Color()]
187
187
  * @param {Boolean} [useWebGL=glEnable]
188
188
  * @memberof Draw */
189
189
  function drawLine(posA, posB, thickness=.1, color, useWebGL)
@@ -231,9 +231,9 @@ function setBlendMode(additive, useWebGL=glEnable)
231
231
  * @param {String} text
232
232
  * @param {Vector2} pos
233
233
  * @param {Number} [size=1]
234
- * @param {Color} [color=new Color(1,1,1)]
234
+ * @param {Color} [color=Color()]
235
235
  * @param {Number} [lineWidth=0]
236
- * @param {Color} [lineColor=new Color(0,0,0)]
236
+ * @param {Color} [lineColor=Color(0,0,0)]
237
237
  * @param {String} [textAlign='center']
238
238
  * @memberof Draw */
239
239
  function drawTextScreen(text, pos, size=1, color=new Color, lineWidth=0, lineColor=new Color(0,0,0), textAlign='center', font=fontDefault, context=overlayContext)
@@ -260,18 +260,20 @@ function drawTextScreen(text, pos, size=1, color=new Color, lineWidth=0, lineCol
260
260
  * @param {String} text
261
261
  * @param {Vector2} pos
262
262
  * @param {Number} [size=1]
263
- * @param {Color} [color=new Color(1,1,1)]
263
+ * @param {Color} [color=Color()]
264
264
  * @param {Number} [lineWidth=0]
265
- * @param {Color} [lineColor=new Color(0,0,0)]
265
+ * @param {Color} [lineColor=Color(0,0,0)]
266
266
  * @param {String} [textAlign='center']
267
267
  * @memberof Draw */
268
- function drawText(text, pos, size=1, color, lineWidth, lineColor, textAlign, font)
268
+ function drawText(text, pos, size=1, color, lineWidth=0, lineColor, textAlign, font)
269
269
  {
270
270
  drawTextScreen(text, worldToScreen(pos), size*cameraScale, color, lineWidth*cameraScale, lineColor, textAlign, font, mainContext);
271
271
  }
272
272
 
273
273
  ///////////////////////////////////////////////////////////////////////////////
274
274
 
275
+ let engineFontImage;
276
+
275
277
  /**
276
278
  * Font Image Object - Draw text on a 2D canvas by using characters in an image
277
279
  * <br> - 96 characters (from space to tilde) are stored in an image
@@ -284,10 +286,6 @@ function drawText(text, pos, size=1, color, lineWidth, lineColor, textAlign, fon
284
286
  * // draw text
285
287
  * font.drawTextScreen("LittleJS\nHello World!", vec2(200, 50));
286
288
  */
287
-
288
- // default font image created by engine
289
- let engineFontImage;
290
-
291
289
  class FontImage
292
290
  {
293
291
  /** Create an image font