littlejsengine 1.6.0 → 1.6.6

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 (56) hide show
  1. package/README.md +138 -34
  2. package/build/littlejs.d.ts +104 -92
  3. package/build/littlejs.esm.js +414 -342
  4. package/build/littlejs.esm.min.js +1 -1
  5. package/build/littlejs.js +365 -296
  6. package/build/littlejs.min.js +1 -1
  7. package/build/littlejs.release.js +359 -291
  8. package/examples/breakout/game.js +5 -0
  9. package/examples/breakout/gameObjects.js +41 -48
  10. package/examples/breakout/index.html +3 -3
  11. package/examples/breakoutTutorial/README.md +514 -0
  12. package/examples/breakoutTutorial/game.js +181 -0
  13. package/examples/breakoutTutorial/images/1.png +0 -0
  14. package/examples/breakoutTutorial/images/10.png +0 -0
  15. package/examples/breakoutTutorial/images/11.png +0 -0
  16. package/examples/breakoutTutorial/images/2.png +0 -0
  17. package/examples/breakoutTutorial/images/3.png +0 -0
  18. package/examples/breakoutTutorial/images/4.png +0 -0
  19. package/examples/breakoutTutorial/images/5.png +0 -0
  20. package/examples/breakoutTutorial/images/6.png +0 -0
  21. package/examples/breakoutTutorial/images/7.png +0 -0
  22. package/examples/breakoutTutorial/images/8.png +0 -0
  23. package/examples/breakoutTutorial/images/9.png +0 -0
  24. package/examples/breakoutTutorial/index.html +10 -0
  25. package/examples/empty/game.js +10 -0
  26. package/examples/favicon.png +0 -0
  27. package/examples/module/index.html +1 -1
  28. package/examples/particles/index.html +1 -1
  29. package/examples/platformer/gameLevel.js +1 -1
  30. package/examples/platformer/gamePlayer.js +1 -1
  31. package/examples/platformer/index.html +6 -6
  32. package/examples/puzzle/game.js +1 -1
  33. package/examples/puzzle/index.html +2 -2
  34. package/examples/starter/build.bat +3 -2
  35. package/examples/starter/game.js +9 -9
  36. package/examples/starter/index.html +13 -13
  37. package/examples/stress/index.html +1 -1
  38. package/examples/typescript/game.js +89 -89
  39. package/examples/typescript/game.ts +10 -10
  40. package/examples/typescript/index.html +1 -1
  41. package/package.json +4 -4
  42. package/src/engine.js +29 -31
  43. package/src/engineAudio.js +53 -29
  44. package/src/engineBuild.bat +3 -1
  45. package/src/engineDebug.js +25 -24
  46. package/src/engineDraw.js +43 -39
  47. package/src/engineExport.js +49 -46
  48. package/src/engineInput.js +83 -74
  49. package/src/engineMedals.js +31 -8
  50. package/src/engineObject.js +25 -25
  51. package/src/engineParticles.js +3 -6
  52. package/src/engineRelease.js +19 -19
  53. package/src/engineSettings.js +4 -4
  54. package/src/engineTileLayer.js +18 -14
  55. package/src/engineUtilities.js +43 -34
  56. package/src/engineWebGL.js +8 -8
package/src/engineDraw.js CHANGED
@@ -1,22 +1,22 @@
1
1
  /**
2
2
  * LittleJS Drawing System
3
- * <br> - Hybrid with both Canvas2D and WebGL available
4
- * <br> - Super fast tile sheet rendering with WebGL
5
- * <br> - Can apply rotation, mirror, color and additive color
6
- * <br> - Many useful utility functions
7
- * <br>
8
- * <br>LittleJS uses a hybrid rendering solution with the best of both Canvas2D and WebGL.
9
- * <br>There are 3 canvas/contexts available to draw to...
10
- * <br> - mainCanvas - 2D background canvas, non WebGL stuff like tile layers are drawn here.
11
- * <br> - glCanvas - Used by the accelerated WebGL batch rendering system.
12
- * <br> - overlayCanvas - Another 2D canvas that appears on top of the other 2 canvases.
13
- * <br>
14
- * <br>The WebGL rendering system is very fast with some caveats...
15
- * <br> - The default setup supports only 1 tile sheet, to support more call glCreateTexture and glSetTexture
16
- * <br> - Switching blend modes (additive) or textures causes another draw call which is expensive in excess
17
- * <br> - Group additive rendering together using renderOrder to mitigate this issue
18
- * <br>
19
- * <br>The LittleJS rendering solution is intentionally simple, feel free to adjust it for your needs!
3
+ * - Hybrid with both Canvas2D and WebGL available
4
+ * - Super fast tile sheet rendering with WebGL
5
+ * - Can apply rotation, mirror, color and additive color
6
+ * - Many useful utility functions
7
+ *
8
+ * LittleJS uses a hybrid rendering solution with the best of both Canvas2D and WebGL.
9
+ * There are 3 canvas/contexts available to draw to...
10
+ * mainCanvas - 2D background canvas, non WebGL stuff like tile layers are drawn here.
11
+ * glCanvas - Used by the accelerated WebGL batch rendering system.
12
+ * overlayCanvas - Another 2D canvas that appears on top of the other 2 canvases.
13
+ *
14
+ * The WebGL rendering system is very fast with some caveats...
15
+ * - The default setup supports only 1 tile sheet, to support more call glCreateTexture and glSetTexture
16
+ * - Switching blend modes (additive) or textures causes another draw call which is expensive in excess
17
+ * - Group additive rendering together using renderOrder to mitigate this issue
18
+ *
19
+ * The LittleJS rendering solution is intentionally simple, feel free to adjust it for your needs!
20
20
  * @namespace Draw
21
21
  */
22
22
 
@@ -60,7 +60,7 @@ let tileImageSize, tileImageFixBleed, drawCount;
60
60
  * @param {Vector2} screenPos
61
61
  * @return {Vector2}
62
62
  * @memberof Draw */
63
- const screenToWorld = (screenPos)=>
63
+ function screenToWorld(screenPos)
64
64
  {
65
65
  ASSERT(mainCanvasSize.x && mainCanvasSize.y, 'mainCanvasSize is invalid');
66
66
  return screenPos.add(vec2(.5)).subtract(mainCanvasSize.scale(.5)).multiply(vec2(1/cameraScale,-1/cameraScale)).add(cameraPos);
@@ -71,7 +71,7 @@ const screenToWorld = (screenPos)=>
71
71
  * @param {Vector2} worldPos
72
72
  * @return {Vector2}
73
73
  * @memberof Draw */
74
- const worldToScreen = (worldPos)=>
74
+ function worldToScreen(worldPos)
75
75
  {
76
76
  ASSERT(mainCanvasSize.x && mainCanvasSize.y, 'mainCanvasSize is invalid');
77
77
  return worldPos.subtract(cameraPos).multiply(vec2(cameraScale,-cameraScale)).add(mainCanvasSize.scale(.5)).subtract(vec2(.5));
@@ -226,6 +226,23 @@ function setBlendMode(additive, useWebGL=glEnable)
226
226
  mainContext.globalCompositeOperation = additive ? 'lighter' : 'source-over';
227
227
  }
228
228
 
229
+ /** Draw text on overlay canvas in world space
230
+ * Automatically splits new lines into rows
231
+ * @param {String} text
232
+ * @param {Vector2} pos
233
+ * @param {Number} [size=1]
234
+ * @param {Color} [color=Color()]
235
+ * @param {Number} [lineWidth=0]
236
+ * @param {Color} [lineColor=Color(0,0,0)]
237
+ * @param {String} [textAlign='center']
238
+ * @param {String} [font=fontDefault]
239
+ * @param {CanvasRenderingContext2D} [context=overlayContext]
240
+ * @memberof Draw */
241
+ function drawText(text, pos, size=1, color, lineWidth=0, lineColor, textAlign, font, context)
242
+ {
243
+ drawTextScreen(text, worldToScreen(pos), size*cameraScale, color, lineWidth*cameraScale, lineColor, textAlign, font, context);
244
+ }
245
+
229
246
  /** Draw text on overlay canvas in screen space
230
247
  * Automatically splits new lines into rows
231
248
  * @param {String} text
@@ -235,6 +252,8 @@ function setBlendMode(additive, useWebGL=glEnable)
235
252
  * @param {Number} [lineWidth=0]
236
253
  * @param {Color} [lineColor=Color(0,0,0)]
237
254
  * @param {String} [textAlign='center']
255
+ * @param {String} [font=fontDefault]
256
+ * @param {CanvasRenderingContext2D} [context=overlayContext]
238
257
  * @memberof Draw */
239
258
  function drawTextScreen(text, pos, size=1, color=new Color, lineWidth=0, lineColor=new Color(0,0,0), textAlign='center', font=fontDefault, context=overlayContext)
240
259
  {
@@ -255,30 +274,15 @@ function drawTextScreen(text, pos, size=1, color=new Color, lineWidth=0, lineCol
255
274
  });
256
275
  }
257
276
 
258
- /** Draw text on overlay canvas in world space
259
- * Automatically splits new lines into rows
260
- * @param {String} text
261
- * @param {Vector2} pos
262
- * @param {Number} [size=1]
263
- * @param {Color} [color=Color()]
264
- * @param {Number} [lineWidth=0]
265
- * @param {Color} [lineColor=Color(0,0,0)]
266
- * @param {String} [textAlign='center']
267
- * @memberof Draw */
268
- function drawText(text, pos, size=1, color, lineWidth=0, lineColor, textAlign, font)
269
- {
270
- drawTextScreen(text, worldToScreen(pos), size*cameraScale, color, lineWidth*cameraScale, lineColor, textAlign, font, mainContext);
271
- }
272
-
273
277
  ///////////////////////////////////////////////////////////////////////////////
274
278
 
275
279
  let engineFontImage;
276
280
 
277
281
  /**
278
282
  * Font Image Object - Draw text on a 2D canvas by using characters in an image
279
- * <br> - 96 characters (from space to tilde) are stored in an image
280
- * <br> - Uses a default 8x8 font if none is supplied
281
- * <br> - You can also use fonts from the main tile sheet
283
+ * - 96 characters (from space to tilde) are stored in an image
284
+ * - Uses a default 8x8 font if none is supplied
285
+ * - You can also use fonts from the main tile sheet
282
286
  * @example
283
287
  * // use built in font
284
288
  * const font = new ImageFont;
@@ -318,7 +322,7 @@ class FontImage
318
322
  {
319
323
  const context = this.context;
320
324
  context.save();
321
- context.imageSmoothingEnabled = !cavasPixelated;
325
+ context.imageSmoothingEnabled = !canvasPixelated;
322
326
 
323
327
  const size = this.tileSize;
324
328
  const drawSize = size.add(this.paddingSize).scale(scale);
@@ -364,7 +368,7 @@ class FontImage
364
368
  /** Returns true if fullscreen mode is active
365
369
  * @return {Boolean}
366
370
  * @memberof Draw */
367
- const isFullscreen = ()=> document.fullscreenElement;
371
+ function isFullscreen() { return document.fullscreenElement; }
368
372
 
369
373
  /** Toggle fullsceen mode
370
374
  * @memberof Draw */
@@ -1,200 +1,202 @@
1
1
  /**
2
2
  * LittleJS Module Export
3
- * <br> - Export engine as a module with extra functions where necessary
3
+ * - Export engine as a module with extra functions where necessary
4
4
  */
5
5
 
6
- // Setters for all variables that devs will need to modify
7
-
8
-
9
6
  /** Set position of camera in world space
10
7
  * @param {Vector2} pos
11
8
  * @memberof Settings */
12
- const setCameraPos = (pos)=> cameraPos = pos;
9
+ function setCameraPos(pos) { cameraPos = pos; }
13
10
 
14
11
  /** Set scale of camera in world space
15
12
  * @param {Number} scale
16
13
  * @memberof Settings */
17
- const setCameraScale = (scale)=> cameraScale = scale;
14
+ function setCameraScale(scale) { cameraScale = scale; }
18
15
 
19
16
  /** Set max size of the canvas
20
17
  * @param {Vector2} size
21
18
  * @memberof Settings */
22
- const setCanvasMaxSize = (size)=> canvasMaxSize = size;
19
+ function setCanvasMaxSize(size) { canvasMaxSize = size; }
23
20
 
24
21
  /** Set fixed size of the canvas
25
22
  * @param {Vector2} size
26
23
  * @memberof Settings */
27
- const setCanvasFixedSize = (size)=> canvasFixedSize = size;
24
+ function setCanvasFixedSize(size) { canvasFixedSize = size; }
28
25
 
29
26
  /** Disables anti aliasing for pixel art if true
30
27
  * @param {Boolean} pixelated
31
28
  * @memberof Settings */
32
- const setCavasPixelated = (pixelated)=> cavasPixelated = pixelated;
29
+ function setCanvasPixelated(pixelated) { canvasPixelated = pixelated; }
33
30
 
34
31
  /** Set default font used for text rendering
35
32
  * @param {String} font
36
33
  * @memberof Settings */
37
- const setFontDefault = (font)=> fontDefault = font;
34
+ function setFontDefault(font) { fontDefault = font; }
38
35
 
39
36
  /** Set if webgl rendering is enabled
40
37
  * @param {Boolean} enable
41
38
  * @memberof Settings */
42
- const setGlEnable = (enable)=> glEnable = enable;
39
+ function setGlEnable(enable) { glEnable = enable; }
43
40
 
44
41
  /** Set to not composite the WebGL canvas
45
42
  * @param {Boolean} overlay
46
43
  * @memberof Settings */
47
- const setGlOverlay = (overlay)=> glOverlay = overlay;
44
+ function setGlOverlay(overlay) { glOverlay = overlay; }
48
45
 
49
46
  /** Set default size of tiles in pixels
50
47
  * @param {Vector2} size
51
48
  * @memberof Settings */
52
- const setTileSizeDefault = (size)=> tileSizeDefault = size;
49
+ function setTileSizeDefault(size) { tileSizeDefault = size; }
53
50
 
54
51
  /** Set to prevent tile bleeding from neighbors in pixels
55
52
  * @param {Number} scale
56
53
  * @memberof Settings */
57
- const setTileFixBleedScale = (scale)=> tileFixBleedScale = scale;
54
+ function setTileFixBleedScale(scale) { tileFixBleedScale = scale; }
58
55
 
59
56
  /** Set if collisions between objects are enabled
60
57
  * @param {Boolean} enable
61
58
  * @memberof Settings */
62
- const setEnablePhysicsSolver = (enable)=> enablePhysicsSolver = enable;
59
+ function setEnablePhysicsSolver(enable) { enablePhysicsSolver = enable; }
63
60
 
64
61
  /** Set default object mass for collison calcuations
65
62
  * @param {Number} mass
66
63
  * @memberof Settings */
67
- const setObjectDefaultMass = (mass)=> objectDefaultMass = mass;
64
+ function setObjectDefaultMass(mass) { objectDefaultMass = mass; }
68
65
 
69
66
  /** Set how much to slow velocity by each frame
70
67
  * @param {Number} damping
71
68
  * @memberof Settings */
72
- const setObjectDefaultDamping = (damp)=> objectDefaultDamping = damp;
69
+ function setObjectDefaultDamping(damp) { objectDefaultDamping = damp; }
73
70
 
74
71
  /** Set how much to slow angular velocity each frame
75
72
  * @param {Number} damping
76
73
  * @memberof Settings */
77
- const setObjectDefaultAngleDamping = (damp)=> objectDefaultAngleDamping = damp;
74
+ function setObjectDefaultAngleDamping(damp) { objectDefaultAngleDamping = damp; }
78
75
 
79
76
  /** Set how much to bounce when a collision occur
80
77
  * @param {Number} elasticity
81
78
  * @memberof Settings */
82
- const setObjectDefaultElasticity = (elasticity)=> objectDefaultElasticity = elasticity;
79
+ function setObjectDefaultElasticity(elasticity) { objectDefaultElasticity = elasticity; }
83
80
 
84
81
  /** Set how much to slow when touching
85
82
  * @param {Number} friction
86
83
  * @memberof Settings */
87
- const setObjectDefaultFriction = (friction)=> objectDefaultFriction = friction;
84
+ function setObjectDefaultFriction(friction) { objectDefaultFriction = friction; }
88
85
 
89
86
  /** Set max speed to avoid fast objects missing collisions
90
87
  * @param {Number} speed
91
88
  * @memberof Settings */
92
- const setObjectMaxSpeed = (speed)=> objectMaxSpeed = speed;
89
+ function setObjectMaxSpeed(speed) { objectMaxSpeed = speed; }
93
90
 
94
91
  /** Set how much gravity to apply to objects along the Y axis
95
92
  * @param {Number} gravity
96
93
  * @memberof Settings */
97
- const setGravity = (g)=> gravity = g;
94
+ function setGravity(g) { gravity = g; }
98
95
 
99
96
  /** Set to scales emit rate of particles
100
97
  * @param {Number} scale
101
98
  * @memberof Settings */
102
- const setParticleEmitRateScale = (scale)=> particleEmitRateScale = scale;
99
+ function setParticleEmitRateScale(scale) { particleEmitRateScale = scale; }
103
100
 
104
101
  /** Set if gamepads are enabled
105
102
  * @param {Boolean} enable
106
103
  * @memberof Settings */
107
- const setGamepadsEnable = (enable)=> gamepadsEnable = enable;
104
+ function setGamepadsEnable(enable) { gamepadsEnable = enable; }
108
105
 
109
106
  /** Set if the dpad input is also routed to the left analog stick
110
107
  * @param {Boolean} enable
111
108
  * @memberof Settings */
112
- const setGamepadDirectionEmulateStick = (enable)=> gamepadDirectionEmulateStick = enable;
109
+ function setGamepadDirectionEmulateStick(enable) { gamepadDirectionEmulateStick = enable; }
113
110
 
114
111
  /** Set if true the WASD keys are also routed to the direction keys
115
112
  * @param {Boolean} enable
116
113
  * @memberof Settings */
117
- const setInputWASDEmulateDirection = (enable)=> inputWASDEmulateDirection = enable;
114
+ function setInputWASDEmulateDirection(enable) { inputWASDEmulateDirection = enable; }
118
115
 
119
116
  /** Set if touch gamepad should appear on mobile devices
120
117
  * @param {Boolean} enable
121
118
  * @memberof Settings */
122
- const setTouchGamepadEnable = (enable)=> touchGamepadEnable = enable;
119
+ function setTouchGamepadEnable(enable) { touchGamepadEnable = enable; }
123
120
 
124
121
  /** Set if touch gamepad should be analog stick or 8 way dpad
125
122
  * @param {Boolean} analog
126
123
  * @memberof Settings */
127
- const setTouchGamepadAnalog = (analog)=> touchGamepadAnalog = analog;
124
+ function setTouchGamepadAnalog(analog) { touchGamepadAnalog = analog; }
128
125
 
129
126
  /** Set size of virutal gamepad for touch devices in pixels
130
127
  * @param {Number} size
131
128
  * @memberof Settings */
132
- const setTouchGamepadSize = (size)=> touchGamepadSize = size;
129
+ function setTouchGamepadSize(size) { touchGamepadSize = size; }
133
130
 
134
131
  /** Set transparency of touch gamepad overlay
135
132
  * @param {Number} alpha
136
133
  * @memberof Settings */
137
- const setTouchGamepadAlpha = (alpha)=> touchGamepadAlpha = alpha;
134
+ function setTouchGamepadAlpha(alpha) { touchGamepadAlpha = alpha; }
138
135
 
139
136
  /** Set to allow vibration hardware if it exists
140
137
  * @param {Boolean} enable
141
138
  * @memberof Settings */
142
- const setVibrateEnable = (enable)=> vibrateEnable = enable;
139
+ function setVibrateEnable(enable) { vibrateEnable = enable; }
143
140
 
144
141
  /** Set to disable all audio code
145
142
  * @param {Boolean} enable
146
143
  * @memberof Settings */
147
- const setSoundEnable = (enable)=> soundEnable = enable;
144
+ function setSoundEnable(enable) { soundEnable = enable; }
148
145
 
149
146
  /** Set volume scale to apply to all sound, music and speech
150
147
  * @param {Number} volume
151
148
  * @memberof Settings */
152
- const setSoundVolume = (volume)=> soundVolume = volume;
149
+ function setSoundVolume(volume) { soundVolume = volume; }
153
150
 
154
151
  /** Set default range where sound no longer plays
155
152
  * @param {Number} range
156
153
  * @memberof Settings */
157
- const setSoundDefaultRange = (range)=> soundDefaultRange = range;
154
+ function setSoundDefaultRange(range) { soundDefaultRange = range; }
158
155
 
159
156
  /** Set default range percent to start tapering off sound
160
157
  * @param {Number} taper
161
158
  * @memberof Settings */
162
- const setSoundDefaultTaper = (taper)=> soundDefaultTaper = taper;
159
+ function setSoundDefaultTaper(taper) { soundDefaultTaper = taper; }
163
160
 
164
161
  /** Set how long to show medals for in seconds
165
162
  * @param {Number} time
166
163
  * @memberof Settings */
167
- const setMedalDisplayTime = (time)=> medalDisplayTime = time;
164
+ function setMedalDisplayTime(time) { medalDisplayTime = time; }
168
165
 
169
166
  /** Set how quickly to slide on/off medals in seconds
170
167
  * @param {Number} time
171
168
  * @memberof Settings */
172
- const setMedalDisplaySlideTime = (time)=> medalDisplaySlideTime = time;
169
+ function setMedalDisplaySlideTime(time) { medalDisplaySlideTime = time; }
173
170
 
174
171
  /** Set size of medal display
175
172
  * @param {Vector2} size
176
173
  * @memberof Settings */
177
- const setMedalDisplaySize = (size)=> medalDisplaySize = size;
174
+ function setMedalDisplaySize(size) { medalDisplaySize = size; }
178
175
 
179
176
  /** Set size of icon in medal display
180
177
  * @param {Number} size
181
178
  * @memberof Settings */
182
- const setMedalDisplayIconSize = (size)=> medalDisplayIconSize = size;
179
+ function setMedalDisplayIconSize(size) { medalDisplayIconSize = size; }
183
180
 
184
181
  /** Set to stop medals from being unlockable
185
182
  * @param {Boolean} preventUnlock
186
183
  * @memberof Settings */
187
- const setMedalsPreventUnlock = (prevent)=> medalsPreventUnlock = prevent;
184
+ function setMedalsPreventUnlock(prevent) { medalsPreventUnlock = prevent; }
188
185
 
189
186
  /** Set if watermark with FPS should be shown
190
187
  * @param {Boolean} show
191
188
  * @memberof Debug */
192
- const setShowWatermark = (show)=> showWatermark = show;
189
+ function setShowWatermark(show) { showWatermark = show; }
193
190
 
194
191
  /** Set if god mode is enabled
195
192
  * @param {Boolean} enable
196
193
  * @memberof Debug */
197
- const setGodMode = (enable)=> godMode = enable;
194
+ function setGodMode(enable) { godMode = enable; }
195
+
196
+ /** Set key code used to toggle debug mode, Esc by default
197
+ * @param {Number} key
198
+ * @memberof Debug */
199
+ function setDebugKey(key) { debugKey = key; }
198
200
 
199
201
  export {
200
202
  // Setters for global variables
@@ -202,7 +204,7 @@ export {
202
204
  setCameraScale,
203
205
  setCanvasMaxSize,
204
206
  setCanvasFixedSize,
205
- setCavasPixelated,
207
+ setCanvasPixelated,
206
208
  setFontDefault,
207
209
  setGlEnable,
208
210
  setGlOverlay,
@@ -236,11 +238,12 @@ export {
236
238
  setMedalsPreventUnlock,
237
239
  setShowWatermark,
238
240
  setGodMode,
241
+ setDebugKey,
239
242
 
240
243
  // Settings
241
244
  canvasMaxSize,
242
245
  canvasFixedSize,
243
- cavasPixelated,
246
+ canvasPixelated,
244
247
  fontDefault,
245
248
  tileSizeDefault,
246
249
  tileFixBleedScale,
@@ -322,8 +325,8 @@ export {
322
325
  Color,
323
326
  Timer,
324
327
  vec2,
325
- colorRGBA,
326
- colorHSLA,
328
+ rgb,
329
+ hsl,
327
330
 
328
331
  // Base
329
332
  EngineObject,