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
@@ -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)
@@ -210,7 +219,17 @@ function glCreateTexture(image)
210
219
  const texture = glContext.createTexture();
211
220
  glContext.bindTexture(glContext.TEXTURE_2D, texture);
212
221
  if (image && image.width)
222
+ {
213
223
  glSetTextureData(texture, image);
224
+ if (!tilesPixelated && isPowerOfTwo(image.width) && isPowerOfTwo(image.height))
225
+ {
226
+ // use mipmap filtering
227
+ glContext.generateMipmap(glContext.TEXTURE_2D);
228
+ glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_MIN_FILTER, glContext.LINEAR_MIPMAP_LINEAR);
229
+ glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_MAG_FILTER, glContext.LINEAR);
230
+ return texture;
231
+ }
232
+ }
214
233
  else
215
234
  {
216
235
  // create a white texture
@@ -218,16 +237,25 @@ function glCreateTexture(image)
218
237
  glContext.texImage2D(glContext.TEXTURE_2D, 0, glContext.RGBA, 1, 1, 0, glContext.RGBA, glContext.UNSIGNED_BYTE, whitePixel);
219
238
  }
220
239
 
221
- // use point filtering for pixelated rendering
240
+ // set texture filtering
222
241
  const filter = tilesPixelated ? glContext.NEAREST : glContext.LINEAR;
223
242
  glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_MIN_FILTER, filter);
224
243
  glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_MAG_FILTER, filter);
225
244
  return texture;
226
245
  }
227
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
+
228
256
  /** Set WebGL texture data from an image
229
257
  * @param {WebGLTexture} texture
230
- * @param {HTMLImageElement} image
258
+ * @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas} image
231
259
  * @memberof WebGL */
232
260
  function glSetTextureData(texture, image)
233
261
  {
@@ -241,7 +269,7 @@ function glSetTextureData(texture, image)
241
269
  * @memberof WebGL */
242
270
  function glFlush()
243
271
  {
244
- if (!glInstanceCount) return;
272
+ if (!glEnable || !glInstanceCount) return;
245
273
 
246
274
  const destBlend = glBatchAdditive ? glContext.ONE : glContext.ONE_MINUS_SRC_ALPHA;
247
275
  glContext.blendFuncSeparate(glContext.SRC_ALPHA, destBlend, glContext.ONE, destBlend);
@@ -250,25 +278,22 @@ function glFlush()
250
278
  // draw all the sprites in the batch and reset the buffer
251
279
  glContext.bufferSubData(glContext.ARRAY_BUFFER, 0, glPositionData);
252
280
  glContext.drawArraysInstanced(glContext.TRIANGLE_STRIP, 0, 4, glInstanceCount);
253
- if (showWatermark)
281
+ if (debug || showWatermark)
254
282
  drawCount += glInstanceCount;
255
283
  glInstanceCount = 0;
256
284
  glBatchAdditive = glAdditive;
257
285
  }
258
286
 
259
- /** 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
260
288
  * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} context
261
- * @param {boolean} [forceDraw]
262
289
  * @memberof WebGL */
263
- function glCopyToContext(context, forceDraw=false)
290
+ function glCopyToContext(context)
264
291
  {
265
- if (!glEnable || !glInstanceCount && !forceDraw) return;
292
+ if (!glEnable)
293
+ return;
266
294
 
267
295
  glFlush();
268
-
269
- // do not draw in overlay mode because the canvas is visible
270
- if (!glOverlay || forceDraw)
271
- context.drawImage(glCanvas, 0, 0);
296
+ context.drawImage(glCanvas, 0, 0);
272
297
  }
273
298
 
274
299
  /** Set anti-aliasing for webgl canvas
@@ -285,18 +310,16 @@ function glSetAntialias(antialias=true)
285
310
  * @param {Number} y
286
311
  * @param {Number} sizeX
287
312
  * @param {Number} sizeY
288
- * @param {Number} angle
289
- * @param {Number} uv0X
290
- * @param {Number} uv0Y
291
- * @param {Number} uv1X
292
- * @param {Number} uv1Y
313
+ * @param {Number} [angle]
314
+ * @param {Number} [uv0X]
315
+ * @param {Number} [uv0Y]
316
+ * @param {Number} [uv1X]
317
+ * @param {Number} [uv1Y]
293
318
  * @param {Number} [rgba=-1] - white is -1
294
319
  * @param {Number} [rgbaAdditive=0] - black is 0
295
320
  * @memberof WebGL */
296
- 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)
297
322
  {
298
- ASSERT(typeof rgba == 'number' && typeof rgbaAdditive == 'number', 'invalid color');
299
-
300
323
  // flush if there is not enough room or if different blend mode
301
324
  if (glInstanceCount >= gl_MAX_INSTANCES || glBatchAdditive != glAdditive)
302
325
  glFlush();