littlejsengine 1.12.6 → 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 (83) hide show
  1. package/dist/littlejs.d.ts +387 -177
  2. package/dist/littlejs.esm.js +5934 -5294
  3. package/dist/littlejs.esm.min.js +4 -1
  4. package/dist/littlejs.js +5887 -5263
  5. package/dist/littlejs.min.js +4 -1
  6. package/dist/littlejs.release.js +5426 -4829
  7. package/examples/box2d/game.js +1 -1
  8. package/examples/box2d/gameObjects.js +7 -6
  9. package/examples/box2d/index.html +1 -1
  10. package/examples/box2d/scenes.js +7 -7
  11. package/examples/breakout/game.js +1 -1
  12. package/examples/breakout/gameObjects.js +2 -2
  13. package/examples/breakout/index.html +1 -1
  14. package/examples/breakoutTutorial/README.md +2 -2
  15. package/examples/breakoutTutorial/game.js +1 -1
  16. package/examples/breakoutTutorial/index.html +1 -1
  17. package/examples/empty/index.html +1 -1
  18. package/examples/htmlMenu/index.html +1 -1
  19. package/examples/index.html +396 -153
  20. package/examples/module/game.js +1 -1
  21. package/examples/module/index.html +1 -1
  22. package/examples/platformer/game.js +4 -4
  23. package/examples/platformer/gameCharacter.js +6 -6
  24. package/examples/platformer/gameEffects.js +74 -57
  25. package/examples/platformer/gameLevel.js +61 -70
  26. package/examples/platformer/gameObjects.js +4 -4
  27. package/examples/platformer/index.html +1 -1
  28. package/examples/puzzle/index.html +1 -1
  29. package/examples/shorts/base.html +23 -3
  30. package/examples/shorts/box2dCar.js +8 -12
  31. package/examples/shorts/flappyGame.js +52 -0
  32. package/examples/shorts/helloWorld.js +6 -3
  33. package/examples/shorts/hillGlideGame.js +56 -0
  34. package/examples/shorts/landerGame.js +32 -0
  35. package/examples/shorts/maze.js +47 -0
  36. package/examples/shorts/medals.js +42 -0
  37. package/examples/shorts/nineSlice.js +21 -0
  38. package/examples/shorts/piano.js +52 -0
  39. package/examples/shorts/platformer.js +24 -20
  40. package/examples/shorts/{pong.js → pongGame.js} +1 -1
  41. package/examples/shorts/raycasting.js +71 -0
  42. package/examples/shorts/slidingPuzzle.js +42 -0
  43. package/examples/shorts/spaceGame.js +56 -0
  44. package/examples/shorts/starfield.js +17 -0
  45. package/examples/shorts/tileLayer.js +3 -5
  46. package/examples/shorts/tiles.png +0 -0
  47. package/examples/shorts/tiltedView.js +8 -7
  48. package/examples/shorts/topDown.js +18 -26
  49. package/examples/shorts/uiSystem.js +4 -7
  50. package/examples/starter/build.bat +6 -1
  51. package/examples/starter/game.js +2 -2
  52. package/examples/starter/index.html +23 -13
  53. package/examples/stress/index.html +2 -3
  54. package/examples/style.css +136 -0
  55. package/examples/typescript/build.js +1 -2
  56. package/examples/typescript/game.js +2 -2
  57. package/examples/typescript/game.ts +1 -1
  58. package/examples/typescript/index.html +1 -1
  59. package/examples/uiSystem/game.js +8 -24
  60. package/examples/uiSystem/index.html +1 -1
  61. package/package.json +1 -1
  62. package/plugins/box2d.js +8 -8
  63. package/plugins/drawUtilities.js +126 -0
  64. package/plugins/newgrounds.js +1 -1
  65. package/plugins/pluginExport.js +7 -1
  66. package/plugins/postProcess.js +2 -2
  67. package/plugins/uiSystem.js +153 -65
  68. package/plugins/zzfxm.js +1 -1
  69. package/reference.md +10 -10
  70. package/src/engine.js +50 -29
  71. package/src/engineAudio.js +14 -5
  72. package/src/engineBuild.bat +6 -1
  73. package/src/engineBuild.js +20 -5
  74. package/src/engineDebug.js +53 -26
  75. package/src/engineDraw.js +108 -57
  76. package/src/engineExport.js +21 -9
  77. package/src/engineInput.js +109 -37
  78. package/src/engineObject.js +68 -67
  79. package/src/engineParticles.js +26 -9
  80. package/src/engineSettings.js +15 -16
  81. package/src/engineTileLayer.js +312 -153
  82. package/src/engineUtilities.js +182 -130
  83. package/src/engineWebGL.js +47 -36
@@ -47,9 +47,9 @@ function glInit()
47
47
  glCanvas = document.createElement('canvas');
48
48
  glContext = glCanvas.getContext('webgl2', {antialias:glAntialias});
49
49
 
50
- // some browsers are much faster without copying the gl buffer so we just overlay it instead
50
+ // create the webgl canvas
51
51
  const rootElement = mainCanvas.parentElement;
52
- glOverlay && rootElement.appendChild(glCanvas);
52
+ rootElement.appendChild(glCanvas);
53
53
 
54
54
  // setup vertex and fragment shaders
55
55
  glShader = glCreateProgram(
@@ -92,7 +92,8 @@ function glInit()
92
92
  glContext.bufferData(glContext.ARRAY_BUFFER, geometry, glContext.STATIC_DRAW);
93
93
  }
94
94
 
95
- // Setup render each frame, called automatically by engine
95
+ // Setup webgl render each frame, called automatically by engine
96
+ // Also used by tile layer rendering when redrawing tiles
96
97
  function glPreRender()
97
98
  {
98
99
  if (!glEnable || headlessMode) return;
@@ -126,18 +127,20 @@ function glPreRender()
126
127
  initVertexAttribArray('c', glContext.UNSIGNED_BYTE, 1, 4); // color
127
128
  initVertexAttribArray('a', glContext.UNSIGNED_BYTE, 1, 4); // additiveColor
128
129
  initVertexAttribArray('r', glContext.FLOAT, 4, 1); // rotation
129
-
130
+
130
131
  // build the transform matrix
131
132
  const s = vec2(2*cameraScale).divide(mainCanvasSize);
132
- const p = vec2(-1).subtract(cameraPos.multiply(s));
133
+ const rotatedCam = cameraPos.rotate(-cameraAngle);
134
+ const p = vec2(-1).subtract(rotatedCam.multiply(s));
135
+ const ca = Math.cos(cameraAngle);
136
+ const sa = Math.sin(cameraAngle);
133
137
  glContext.uniformMatrix4fv(glContext.getUniformLocation(glShader, 'm'), false,
134
- [
135
- s.x, 0, 0, 0,
136
- 0, s.y, 0, 0,
137
- 1, 1, 1, 1,
138
- p.x, p.y, 0, 0
139
- ]
140
- );
138
+ [
139
+ s.x * ca, s.y * sa, 0, 0,
140
+ -s.x * sa, s.y * ca, 0, 0,
141
+ 1, 1, 1, 0,
142
+ p.x, p.y, 0, 1
143
+ ]);
141
144
  }
142
145
 
143
146
  /** Clear the canvas and setup the viewport
@@ -145,15 +148,16 @@ function glPreRender()
145
148
  function glClearCanvas()
146
149
  {
147
150
  // clear and set to same size as main canvas
148
- glContext.viewport(0, 0, glCanvas.width=mainCanvas.width, glCanvas.height=mainCanvas.height);
151
+ glContext.viewport(0, 0, glCanvas.width=drawCanvas.width, glCanvas.height=drawCanvas.height);
149
152
  glContext.clear(glContext.COLOR_BUFFER_BIT);
150
153
  }
151
154
 
152
155
  /** Set the WebGl texture, called automatically if using multiple textures
153
156
  * - This may also flush the gl buffer resulting in more draw calls and worse performance
154
157
  * @param {WebGLTexture} texture
158
+ * @param {boolean} wrap - Should the texture wrap or clamp
155
159
  * @memberof WebGL */
156
- function glSetTexture(texture)
160
+ function glSetTexture(texture, wrap=false)
157
161
  {
158
162
  // must flush cache with the old texture to set a new one
159
163
  if (headlessMode || texture == glActiveTexture)
@@ -161,6 +165,11 @@ function glSetTexture(texture)
161
165
 
162
166
  glFlush();
163
167
  glContext.bindTexture(glContext.TEXTURE_2D, glActiveTexture = texture);
168
+
169
+ // set wrap mode
170
+ const wrapMode = wrap ? glContext.REPEAT : glContext.CLAMP_TO_EDGE;
171
+ glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_WRAP_S, wrapMode);
172
+ glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_WRAP_T, wrapMode);
164
173
  }
165
174
 
166
175
  /** Compile WebGL shader of the given type, will throw errors if in debug mode
@@ -201,7 +210,7 @@ function glCreateProgram(vsSource, fsSource)
201
210
  }
202
211
 
203
212
  /** Create WebGL texture from an image and init the texture settings
204
- * @param {HTMLImageElement} image
213
+ * @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas} [image]
205
214
  * @return {WebGLTexture}
206
215
  * @memberof WebGL */
207
216
  function glCreateTexture(image)
@@ -212,8 +221,6 @@ function glCreateTexture(image)
212
221
  if (image && image.width)
213
222
  {
214
223
  glSetTextureData(texture, image);
215
-
216
- const isPowerOfTwo = (value)=> !(value & (value - 1));
217
224
  if (!tilesPixelated && isPowerOfTwo(image.width) && isPowerOfTwo(image.height))
218
225
  {
219
226
  // use mipmap filtering
@@ -237,9 +244,18 @@ function glCreateTexture(image)
237
244
  return texture;
238
245
  }
239
246
 
247
+
248
+ /** Deletes a WebGL texture
249
+ * @param {WebGLTexture} [texture]
250
+ * @memberof WebGL */
251
+ function glDeleteTexture(texture)
252
+ {
253
+ glContext.deleteTexture(texture);
254
+ }
255
+
240
256
  /** Set WebGL texture data from an image
241
257
  * @param {WebGLTexture} texture
242
- * @param {HTMLImageElement} image
258
+ * @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas} image
243
259
  * @memberof WebGL */
244
260
  function glSetTextureData(texture, image)
245
261
  {
@@ -253,7 +269,7 @@ function glSetTextureData(texture, image)
253
269
  * @memberof WebGL */
254
270
  function glFlush()
255
271
  {
256
- if (!glInstanceCount) return;
272
+ if (!glEnable || !glInstanceCount) return;
257
273
 
258
274
  const destBlend = glBatchAdditive ? glContext.ONE : glContext.ONE_MINUS_SRC_ALPHA;
259
275
  glContext.blendFuncSeparate(glContext.SRC_ALPHA, destBlend, glContext.ONE, destBlend);
@@ -262,25 +278,22 @@ function glFlush()
262
278
  // draw all the sprites in the batch and reset the buffer
263
279
  glContext.bufferSubData(glContext.ARRAY_BUFFER, 0, glPositionData);
264
280
  glContext.drawArraysInstanced(glContext.TRIANGLE_STRIP, 0, 4, glInstanceCount);
265
- if (showWatermark)
281
+ if (debug || showWatermark)
266
282
  drawCount += glInstanceCount;
267
283
  glInstanceCount = 0;
268
284
  glBatchAdditive = glAdditive;
269
285
  }
270
286
 
271
- /** Draw any sprites still in the buffer and copy to main canvas
287
+ /** Flush any sprites still in the buffer and copy to main canvas
272
288
  * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} context
273
- * @param {boolean} [forceDraw]
274
289
  * @memberof WebGL */
275
- function glCopyToContext(context, forceDraw=false)
290
+ function glCopyToContext(context)
276
291
  {
277
- if (!glEnable || !glInstanceCount && !forceDraw) return;
292
+ if (!glEnable)
293
+ return;
278
294
 
279
295
  glFlush();
280
-
281
- // do not draw in overlay mode because the canvas is visible
282
- if (!glOverlay || forceDraw)
283
- context.drawImage(glCanvas, 0, 0);
296
+ context.drawImage(glCanvas, 0, 0);
284
297
  }
285
298
 
286
299
  /** Set anti-aliasing for webgl canvas
@@ -297,18 +310,16 @@ function glSetAntialias(antialias=true)
297
310
  * @param {Number} y
298
311
  * @param {Number} sizeX
299
312
  * @param {Number} sizeY
300
- * @param {Number} angle
301
- * @param {Number} uv0X
302
- * @param {Number} uv0Y
303
- * @param {Number} uv1X
304
- * @param {Number} uv1Y
313
+ * @param {Number} [angle]
314
+ * @param {Number} [uv0X]
315
+ * @param {Number} [uv0Y]
316
+ * @param {Number} [uv1X]
317
+ * @param {Number} [uv1Y]
305
318
  * @param {Number} [rgba=-1] - white is -1
306
319
  * @param {Number} [rgbaAdditive=0] - black is 0
307
320
  * @memberof WebGL */
308
- function glDraw(x, y, sizeX, sizeY, angle, uv0X, uv0Y, uv1X, uv1Y, rgba=-1, rgbaAdditive=0)
321
+ function glDraw(x, y, sizeX, sizeY, angle=0, uv0X=0, uv0Y=0, uv1X=1, uv1Y=1, rgba=-1, rgbaAdditive=0)
309
322
  {
310
- ASSERT(typeof rgba == 'number' && typeof rgbaAdditive == 'number', 'invalid color');
311
-
312
323
  // flush if there is not enough room or if different blend mode
313
324
  if (glInstanceCount >= gl_MAX_INSTANCES || glBatchAdditive != glAdditive)
314
325
  glFlush();