littlejsengine 1.20.0 → 1.22.0

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.
@@ -165,6 +165,13 @@ class ParticleEmitter extends EngineObject
165
165
  this.localSpace = localSpace;
166
166
  /** @property {number} - If non zero the particle is drawn as a trail, stretched in the direction of velocity */
167
167
  this.trailScale = 0;
168
+ /** @property {number} - Grows the whole effect as each particle is born: the spawn area, the particles' sizes,
169
+ * speed and fall, so it looks the same only bigger; 1 is as made, and a particle keeps the scale it was born
170
+ * with, so a change never moves the ones already out */
171
+ this.scale = 1;
172
+ /** @property {number} - The effect's own fall, added to each particle's velocity y every frame, on top of the
173
+ * world's gravity times gravityScale; an effect that sets its own looks the same in any game */
174
+ this.gravity = 0;
168
175
  /** @property {ParticleCallback|undefined} - Callback when particle is created
169
176
  * @type {ParticleCallback|undefined} */
170
177
  this.particleCreateCallback = undefined;
@@ -251,9 +258,9 @@ class ParticleEmitter extends EngineObject
251
258
  {
252
259
  // show emitter bounds
253
260
  if (this.emitCircle)
254
- debugCircle(this.pos, this.emitSize.x, '#0f0', 0, false, false);
261
+ debugCircle(this.pos, this.emitSize.x * this.scale, '#0f0', 0, false, false);
255
262
  else
256
- debugRect(this.pos, this.emitSize, '#0f0', 0, this.angle, false, false);
263
+ debugRect(this.pos, this.emitSize.scale(this.scale), '#0f0', 0, this.angle, false, false);
257
264
  }
258
265
  }
259
266
 
@@ -262,10 +269,11 @@ class ParticleEmitter extends EngineObject
262
269
  emitParticle()
263
270
  {
264
271
  // spawn a particle
272
+ const scale = this.scale; // grows the spawn area, the sizes and the speed of each particle born
265
273
  let pos = this.emitCircle ? // check if circle emitter
266
- randInCircle(this.emitSize.x/2) // circle emitter
267
- : vec2(rand(-.5,.5), rand(-.5,.5)) // box emitter
268
- .multiply(this.emitSize);
274
+ randInCircle(this.emitSize.x/2 * scale) // circle emitter
275
+ : vec2(rand(-.5,.5) * this.emitSize.x * scale, // box emitter, one vector made
276
+ rand(-.5,.5) * this.emitSize.y * scale);
269
277
  let angle = rand(this.particleConeAngle, -this.particleConeAngle);
270
278
  if (!this.localSpace)
271
279
  {
@@ -282,9 +290,9 @@ class ParticleEmitter extends EngineObject
282
290
 
283
291
  // randomize particle settings
284
292
  const particleTime = randomizeScale(this.particleTime);
285
- const sizeStart = randomizeScale(this.sizeStart);
286
- const sizeEnd = randomizeScale(this.sizeEnd);
287
- const speed = randomizeScale(this.speed);
293
+ const sizeStart = randomizeScale(this.sizeStart) * scale;
294
+ const sizeEnd = randomizeScale(this.sizeEnd) * scale;
295
+ const speed = randomizeScale(this.speed) * scale;
288
296
  const angleSpeed = randomizeScale(this.angleSpeed) * randSign();
289
297
  const coneAngle = rand(this.emitConeAngle, -this.emitConeAngle);
290
298
  const colorStart = randColor(this.colorStartA, this.colorStartB, this.randomColorLinear);
@@ -318,9 +326,13 @@ class ParticleEmitter extends EngineObject
318
326
  render()
319
327
  {
320
328
  // render all particles, the blend switched once for them all, not around each one
329
+ // and the tile's draw worked out once for them all, fresh each frame so a tile changed in place draws changed
321
330
  this.additive && setAdditiveBlendMode();
331
+ particleTile = null; // none yet, undefined is the untextured tile
332
+ particleTileBatch = true;
322
333
  for (const particle of this.particles)
323
334
  particle.render();
335
+ particleTileBatch = false;
324
336
  this.additive && setAdditiveBlendMode(false);
325
337
  }
326
338
 
@@ -347,6 +359,39 @@ class ParticleEmitter extends EngineObject
347
359
  // scratch vectors reused by Particle.render and the tile collision to avoid per-frame allocations
348
360
  const particleDrawPos = new Vector2, particleDrawSize = new Vector2, particleCollidePos = new Vector2;
349
361
 
362
+ // the WebGL draw of the tile particles draw with, what drawTile works out for each sprite: kept while an emitter
363
+ // renders its particles, so each one only writes its own values into the batch
364
+ let particleTile, particleTileBatch = false, particleTileTextured = false, particleTileEmpty = false;
365
+ let particleTileU0 = 0, particleTileV0 = 0, particleTileU1 = 0, particleTileV1 = 0;
366
+ function particleTileSet(tileInfo)
367
+ {
368
+ particleTile = tileInfo;
369
+ const textureInfo = tileInfo?.textureInfo;
370
+ particleTileTextured = !!textureInfo;
371
+ particleTileEmpty = !!textureInfo && !(tileInfo.size.x && tileInfo.size.y); // like a sprite still loading
372
+ if (!textureInfo || particleTileEmpty) return;
373
+
374
+ ASSERT(!!textureInfo.glTexture, 'texture has no WebGL texture, draw it with useWebGL false');
375
+ glSetTexture(textureInfo.glTexture); // bound, and its mipmaps brought up to date, once for the emitter
376
+ const sizeInverse = textureInfo.sizeInverse, bleed = tileInfo.bleed;
377
+ const x = tileInfo.pos.x * sizeInverse.x;
378
+ const y = tileInfo.pos.y * sizeInverse.y;
379
+ const w = tileInfo.size.x * sizeInverse.x;
380
+ const h = tileInfo.size.y * sizeInverse.y;
381
+ if (bleed)
382
+ {
383
+ const bleedX = sizeInverse.x*bleed;
384
+ const bleedY = sizeInverse.y*bleed;
385
+ particleTileU0 = x + bleedX; particleTileV0 = y + bleedY;
386
+ particleTileU1 = x - bleedX + w; particleTileV1 = y - bleedY + h;
387
+ }
388
+ else
389
+ {
390
+ particleTileU0 = x; particleTileV0 = y;
391
+ particleTileU1 = x + w; particleTileV1 = y + h;
392
+ }
393
+ }
394
+
350
395
  // tests if a particle collides with tiles at x, y, through its emitter's collide callback if it has one
351
396
  function particleCollideTest(particle, collideCallback, x, y)
352
397
  {
@@ -382,6 +427,8 @@ class Particle
382
427
  {
383
428
  /** @property {ParticleEmitter} - The emitter this particle came from */
384
429
  this.emitter = emitter;
430
+ /** @property {number} - The emitter's scale when it was made, which grows its fall */
431
+ this.scale = emitter.scale ?? 1; // a hand made emitter-like object may have none
385
432
  /** @property {Vector2} - Position, world space or local to the emitter when localSpace is set */
386
433
  this.pos = pos;
387
434
  /** @property {number} - Angle in radians */
@@ -444,7 +491,10 @@ class Particle
444
491
  // apply physics; only the tile collision needs where the particle was
445
492
  const solve = enablePhysicsSolver && collideLevel;
446
493
  const oldX = this.pos.x, oldY = this.pos.y;
447
- let gravityX = gravity.x * gravityScale, gravityY = gravity.y * gravityScale;
494
+ // the world's gravity times gravityScale plus the emitter's own, grown by the particle's scale
495
+ const fall = this.scale;
496
+ let gravityX = gravity.x * gravityScale * fall;
497
+ let gravityY = (gravity.y * gravityScale + (emitter.gravity || 0)) * fall;
448
498
  if (emitter.localSpace && emitter.angle)
449
499
  {
450
500
  // world gravity turned into the emitter's space, the render turns it back
@@ -498,7 +548,9 @@ class Particle
498
548
  }
499
549
  if (isBlockedY || !isBlockedX)
500
550
  {
501
- const wasFalling = this.velocity.y < 0 && gravity.y < 0 || this.velocity.y > 0 && gravity.y > 0;
551
+ // down is the world's gravity, or the emitter's own fall in a world with none
552
+ const down = gravity.y || emitter.gravity || 0;
553
+ const wasFalling = this.velocity.y < 0 && down < 0 || this.velocity.y > 0 && down > 0;
502
554
  if (wasFalling)
503
555
  this.groundObject = hitLayer;
504
556
 
@@ -577,7 +629,24 @@ class Particle
577
629
  angle = atan2(velocity.x, velocity.y);
578
630
  }
579
631
  }
580
- drawTile(pos, size, this.tileInfo, this.color, angle, this.mirror, undefined, glEnable, false);
632
+ if (glEnable && !headlessMode)
633
+ {
634
+ // straight into the batch, the tile's draw worked out once for the emitter's particles
635
+ const tileInfo = this.tileInfo;
636
+ if (!particleTileBatch || tileInfo !== particleTile)
637
+ particleTileSet(tileInfo);
638
+ if (!particleTileTextured)
639
+ glDrawUntextured(pos.x, pos.y, size.x, size.y, angle, this.color.rgbaInt());
640
+ else if (!particleTileEmpty)
641
+ {
642
+ const texture = tileInfo.textureInfo.glTexture;
643
+ glActiveTexture === texture || glSetTexture(texture); // bound again only if a draw between changed it
644
+ glDraw(pos.x, pos.y, this.mirror ? -size.x : size.x, size.y, angle,
645
+ particleTileU0, particleTileV0, particleTileU1, particleTileV1, this.color.rgbaInt());
646
+ }
647
+ }
648
+ else
649
+ drawTile(pos, size, this.tileInfo, this.color, angle, this.mirror, undefined, false, false);
581
650
  additive && setAdditiveBlendMode(false);
582
651
  debugParticles && debugRect(pos, size, '#f005', 0, angle, false, false);
583
652
  }
@@ -52,16 +52,28 @@ function tweak(){}
52
52
  function tweakButton(){}
53
53
  function tweakDivider(){}
54
54
  function tweakEngineDefaults(){}
55
- const levelEditor = {isOpen: false, open(){}, close(){}, onTile: undefined, onRestart: undefined, onPlayFrom: undefined};
55
+ const levelEditor = {isOpen: false, open(){}, close(){}, onTile: undefined, onRestart: undefined, onPlayFrom: undefined,
56
+ paletteTiles: undefined, use3D: undefined};
56
57
  function editorMapRestore(map){ return map; }
57
58
  function editorMapLoaded(){}
58
59
  function editorJSONFetched(){}
59
60
  function editorPreRender(){}
60
61
  function editorObjectMade(){}
61
62
 
63
+ // the input capture of the free camera and the 3D editor
64
+ function inputCaptureHides(){ return false; }
65
+ function inputCaptureMouse(){}
66
+ function inputLockExit(){}
67
+
62
68
  // the 3D debug draws are debug only too
63
69
  function debugBox3D(){}
64
70
  function debugSphere3D(){}
65
71
  function debugLine3D(){}
66
72
  function debugPoint3D(){}
67
73
  function render3DRenderDebug(){}
74
+
75
+ // the free camera and the 3D level editor are debug only
76
+ function editor3DCameraBegin(){}
77
+ function editor3DCameraEnd(){}
78
+ function editor3DLevelLoaded(){}
79
+ function editor3DObjectMade(){}
@@ -122,6 +122,13 @@ let fontDefault = 'arial';
122
122
  * @memberof Settings */
123
123
  let showSplashScreen = false;
124
124
 
125
+ /** The loading screen, drawn after the splash while the images, gameInit and what they load are loading, once that
126
+ * takes more than half a second: a function given the part done, 0 to 1, or undefined for none, which leaves the
127
+ * screen blank; the default says Loading over a bar
128
+ * @type {LoadingScreenCallback|undefined}
129
+ * @memberof Settings */
130
+ let loadingScreen = drawLoadingScreen;
131
+
125
132
  /** Disables all rendering, audio, and input for servers, must be set before engineInit
126
133
  * @type {boolean}
127
134
  * @default
@@ -135,6 +142,15 @@ let headlessMode = false;
135
142
  * @memberof Settings */
136
143
  let engineManualStep = false;
137
144
 
145
+ /** Run one update per display frame with timeDelta the time it covers, in place of fixed updates at frameRate
146
+ * - Per-frame values are not scaled: velocity, gravity, damping and particle speeds act once per update, so a
147
+ * game that turns this on scales its own movement by timeDelta
148
+ * - Values in seconds follow real time as they are: time, timers, particle emit rate and life, animation, Box2D
149
+ * @type {boolean}
150
+ * @default
151
+ * @memberof Settings */
152
+ let engineVariableStep = false;
153
+
138
154
  ///////////////////////////////////////////////////////////////////////////////
139
155
  // WebGL settings
140
156
 
@@ -530,6 +546,11 @@ function setFontDefault(font) { fontDefault = font; }
530
546
  * @memberof Settings */
531
547
  function setShowSplashScreen(show) { showSplashScreen = show; }
532
548
 
549
+ /** Set the loading screen, drawn after the splash while the game loads, once that takes more than half a second
550
+ * @param {LoadingScreenCallback} [callback] - Draws on mainContext given the part done, 0 to 1; undefined for none
551
+ * @memberof Settings */
552
+ function setLoadingScreen(callback) { loadingScreen = callback; }
553
+
533
554
  /** Set to disable rendering, audio, and input for servers, must be set before engineInit
534
555
  * @param {boolean} headless
535
556
  * @memberof Settings */
@@ -555,6 +576,23 @@ function setEngineManualStep(enable=true)
555
576
  }
556
577
  }
557
578
 
579
+ /** Set if the engine runs one update per display frame with timeDelta the time it covers
580
+ * Can be set before engineInit or while running; turned off it goes back to fixed updates at frameRate
581
+ * @param {boolean} [enable]
582
+ * @memberof Settings */
583
+ function setEngineVariableStep(enable=true)
584
+ {
585
+ if (engineVariableStep && !enable)
586
+ {
587
+ // fixed time goes on one fixed step after the last update, if one ran, and timeDelta is the fixed step again
588
+ timeFixedStart = frame ? time + 1 / frameRate : time;
589
+ frameFixedStart = frame;
590
+ timeDelta = 1 / frameRate;
591
+ frameTimeBufferMS = engineManualStep ? -.5e3 / frameRate : 0;
592
+ }
593
+ engineVariableStep = enable;
594
+ }
595
+
558
596
  /** Set whether the draw and debug functions draw in screen space when a call leaves screenSpace out
559
597
  * @param {boolean} screenSpace
560
598
  * @memberof Settings */
@@ -26,6 +26,19 @@ let glCanvas;
26
26
  * @memberof WebGL */
27
27
  let glContext;
28
28
 
29
+ /** The largest texture this device makes, in pixels across, 0 until WebGL starts; at least 2048 with WebGL2
30
+ * @type {number}
31
+ * @memberof WebGL */
32
+ let glMaxTextureSize = 0;
33
+
34
+ /** A texture size this device can make: the size asked for, or the largest it supports when that is smaller
35
+ * - The engine sizes the textures it makes itself through this, the shadow maps and the like, so a size a device
36
+ * does not have falls back instead of failing
37
+ * @param {number} size - Pixels across
38
+ * @return {number}
39
+ * @memberof WebGL */
40
+ function glClampTextureSize(size) { return glMaxTextureSize ? min(size, glMaxTextureSize) : size; }
41
+
29
42
  /** Should WebGL be setup with anti-aliasing? must be set before calling engineInit
30
43
  * @type {boolean}
31
44
  * @memberof WebGL */
@@ -115,7 +128,7 @@ function glInit(rootElement)
115
128
  // startup webgl, and make the textures of any texture infos made before it
116
129
  initWebGL();
117
130
  for (const info of glTextureInfos)
118
- info.glTexture ||= glCreateTexture(info.image, info.wrap);
131
+ info.glTexture ||= glCreateTexture(info.image, info.wrap, info.pixelated);
119
132
 
120
133
  // setup context lost and restore handlers
121
134
  glCanvas.addEventListener('webglcontextlost', (e)=>
@@ -155,7 +168,7 @@ function glInit(rootElement)
155
168
  glPremultipliedTextures = new WeakSet; // the tile layers draw into their new textures again below
156
169
  initWebGL();
157
170
  for (const info of glTextureInfos)
158
- info.glTexture = glCreateTexture(info.image, info.wrap);
171
+ info.glTexture = glCreateTexture(info.image, info.wrap, info.pixelated);
159
172
  pluginList.forEach(plugin=>plugin.glContextRestored?.());
160
173
 
161
174
  // a tile layer drawn on the GPU only had its tiles in the lost texture, it draws them again
@@ -165,6 +178,8 @@ function glInit(rootElement)
165
178
 
166
179
  function initWebGL()
167
180
  {
181
+ glMaxTextureSize = glContext.getParameter(glContext.MAX_TEXTURE_SIZE) | 0;
182
+
168
183
  // setup instanced rendering shader program
169
184
  glShader = glCreateProgram(gl_VERTEX_SOURCE,
170
185
  '#version 300 es\n' + // specify GLSL ES version
@@ -488,9 +503,10 @@ function glShaderProgram(shader)
488
503
  * Restores the active texture when done
489
504
  * @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas|ImageBitmap} [image]
490
505
  * @param {boolean|Array<number>} [wrap] - true for REPEAT, false for CLAMP_TO_EDGE, or the WebGL modes across and down
506
+ * @param {boolean} [pixelated] - Hard edged or smooth, undefined follows tilesPixelated
491
507
  * @return {WebGLTexture}
492
508
  * @memberof WebGL */
493
- function glCreateTexture(image, wrap=false)
509
+ function glCreateTexture(image, wrap=false, pixelated=tilesPixelated)
494
510
  {
495
511
  if (!glContext) return;
496
512
 
@@ -499,10 +515,10 @@ function glCreateTexture(image, wrap=false)
499
515
  let mipMap = false;
500
516
  if (image?.width)
501
517
  {
502
- glSetTextureData(texture, image);
518
+ glSetTextureData(texture, image, pixelated);
503
519
  glContext.bindTexture(glContext.TEXTURE_2D, texture);
504
520
  // WebGL2 makes mipmaps at any size, a texture that becomes a render target keeps them only at powers of two
505
- mipMap = !tilesPixelated;
521
+ mipMap = !pixelated;
506
522
  if (mipMap && !(isPowerOfTwo(image.width) && isPowerOfTwo(image.height)))
507
523
  glMipmapsUntilTarget.add(texture);
508
524
  }
@@ -515,7 +531,7 @@ function glCreateTexture(image, wrap=false)
515
531
  }
516
532
 
517
533
  // set texture filtering
518
- const magFilter = tilesPixelated ? glContext.NEAREST : glContext.LINEAR;
534
+ const magFilter = pixelated ? glContext.NEAREST : glContext.LINEAR;
519
535
  const minFilter = mipMap ? glContext.LINEAR_MIPMAP_LINEAR : magFilter;
520
536
  glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_MAG_FILTER, magFilter);
521
537
  glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_MIN_FILTER, minFilter);
@@ -546,8 +562,9 @@ function glDeleteTexture(texture)
546
562
  /** Set WebGL texture data from an image, restores the active texture when done
547
563
  * @param {WebGLTexture} texture
548
564
  * @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas|ImageBitmap} image
565
+ * @param {boolean} [pixelated] - Hard edged or smooth, undefined follows tilesPixelated
549
566
  * @memberof WebGL */
550
- function glSetTextureData(texture, image)
567
+ function glSetTextureData(texture, image, pixelated=tilesPixelated)
551
568
  {
552
569
  if (!glContext) return;
553
570
 
@@ -565,7 +582,7 @@ function glSetTextureData(texture, image)
565
582
  }
566
583
  // smooth filtering mixes a texel with its see through neighbors, right only for premultiplied color, or the
567
584
  // edges go dark; pixel art is sampled a texel at a time, and uploads straight color as it always has
568
- const premultiply = !tilesPixelated;
585
+ const premultiply = !pixelated;
569
586
  glContext.pixelStorei(glContext.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiply);
570
587
  glContext.texImage2D(glContext.TEXTURE_2D, 0, glContext.RGBA, glContext.RGBA, glContext.UNSIGNED_BYTE, image);
571
588
  glContext.pixelStorei(glContext.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
@@ -596,9 +613,9 @@ function glRegisterTextureInfo(textureInfo)
596
613
 
597
614
  // create or set the texture data
598
615
  if (textureInfo.glTexture)
599
- glSetTextureData(textureInfo.glTexture, textureInfo.image);
616
+ glSetTextureData(textureInfo.glTexture, textureInfo.image, textureInfo.pixelated);
600
617
  else
601
- textureInfo.glTexture = glCreateTexture(textureInfo.image, textureInfo.wrap);
618
+ textureInfo.glTexture = glCreateTexture(textureInfo.image, textureInfo.wrap, textureInfo.pixelated);
602
619
  }
603
620
 
604
621
  /** Internal: tells WebGL to destroy the glTexture and stop tracking it, TextureInfo calls it