littlejsengine 1.18.2 → 1.18.4

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.
package/dist/littlejs.js CHANGED
@@ -35,7 +35,7 @@ const engineName = 'LittleJS';
35
35
  * @type {string}
36
36
  * @default
37
37
  * @memberof Engine */
38
- const engineVersion = '1.18.2';
38
+ const engineVersion = '1.18.4';
39
39
 
40
40
  /** Frames per second to update
41
41
  * @type {number}
@@ -69,7 +69,7 @@ let frame = 0;
69
69
  * @memberof Engine */
70
70
  let time = 0;
71
71
 
72
- /** Actual clock time since start in seconds (not affected by pause or frame rate clamping)
72
+ /** Actual clock time since start in seconds (not affected by pause, timescale, or frame rate clamping)
73
73
  * @type {number}
74
74
  * @memberof Engine */
75
75
  let timeReal = 0;
@@ -200,11 +200,14 @@ async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, game
200
200
  averageFPS = lerp(averageFPS, 1e3/(frameTimeDeltaMS||1), .05);
201
201
  const debugSpeedUp = debug && keyIsDown('Equal'); // +
202
202
  const debugSpeedDown = debug && keyIsDown('Minus'); // -
203
- if (debug) // +/- to speed/slow time
204
- frameTimeDeltaMS *= debugSpeedUp ? 10 : debugSpeedDown ? .1 : 1;
205
- timeReal += frameTimeDeltaMS / 1e3;
203
+ const debugScale = debugSpeedUp ? 10 : debugSpeedDown ? .1 : 1;
204
+
205
+ // apply time deltas
206
+ timeReal += frameTimeDeltaMS * debugScale / 1e3;
207
+ const combinedScale = timeScale * debugScale;
208
+ frameTimeDeltaMS *= combinedScale;
206
209
  frameTimeBufferMS += paused ? 0 : frameTimeDeltaMS;
207
- if (!debugSpeedUp)
210
+ if (combinedScale <= 1)
208
211
  frameTimeBufferMS = min(frameTimeBufferMS, 50); // clamp min framerate
209
212
 
210
213
  let wasUpdated = false;
@@ -2800,6 +2803,18 @@ let cameraAngle = 0;
2800
2803
  * @memberof Settings */
2801
2804
  let cameraScale = 32;
2802
2805
 
2806
+ ///////////////////////////////////////////////////////////////////////////////
2807
+ // Time settings
2808
+
2809
+ /** Scale applied to engine time, can be used for slow motion or fast forward
2810
+ * - 1 is normal speed, 2 is double speed, 0.5 is half speed
2811
+ * - 0 freezes the simulation without setting the paused flag
2812
+ * - Should be >= 0; stacks multiplicatively with the debug +/- shortcut
2813
+ * @type {number}
2814
+ * @default
2815
+ * @memberof Settings */
2816
+ let timeScale = 1;
2817
+
2803
2818
  ///////////////////////////////////////////////////////////////////////////////
2804
2819
  // Display settings
2805
2820
 
@@ -3128,6 +3143,11 @@ function setCameraAngle(angle) { cameraAngle = angle; }
3128
3143
  * @memberof Settings */
3129
3144
  function setCameraScale(scale) { cameraScale = scale; }
3130
3145
 
3146
+ /** Set scale applied to engine time
3147
+ * @param {number} scale
3148
+ * @memberof Settings */
3149
+ function setTimeScale(scale) { timeScale = scale; }
3150
+
3131
3151
  /** Set if tiles should be colorized when using canvas2d
3132
3152
  * This can be slower but results should look nearly identical to WebGL rendering
3133
3153
  * It can be enabled/disabled at any time
@@ -4002,6 +4022,12 @@ let textureInfos = [];
4002
4022
  * @memberof Draw */
4003
4023
  let drawCount;
4004
4024
 
4025
+ // internal predicates for tint short-circuiting in canvas2D draw paths
4026
+ // isWhite ignores alpha because alpha is applied via globalAlpha, not multiply
4027
+ // isBlack includes alpha so additive colors that only contribute alpha are not skipped
4028
+ /** @param {Color} c */ function isWhite(c) { return c.r >= 1 && c.g >= 1 && c.b >= 1; }
4029
+ /** @param {Color} c */ function isBlack(c) { return c.r <= 0 && c.g <= 0 && c.b <= 0 && c.a <= 0; }
4030
+
4005
4031
  ///////////////////////////////////////////////////////////////////////////////
4006
4032
 
4007
4033
  /**
@@ -4331,6 +4357,96 @@ function drawRectGradient(pos, size, colorTop=WHITE, colorBottom=BLACK, angle=0,
4331
4357
  }
4332
4358
  }
4333
4359
 
4360
+ /** Draw a texture tiled (wrapped) across a rectangle in world space.
4361
+ * Useful for backgrounds, repeating patterns, and seamless fills.
4362
+ * The whole texture is tiled — sub-region (TileInfo) wrapping is not supported.
4363
+ * @param {Vector2} pos - Center of the rect in world space
4364
+ * @param {Vector2} size - Size of the rect in world space
4365
+ * @param {Vector2} wrapCount - How many times the texture repeats (x, y)
4366
+ * @param {TextureInfo|number} [texture=0] - TextureInfo or texture index into textureInfos
4367
+ * @param {Color} [color=WHITE] - Color to modulate with
4368
+ * @param {number} [angle=0] - Angle to rotate by
4369
+ * @param {Color} [additiveColor] - Additive color to be applied if any
4370
+ * @param {boolean} [useWebGL=glEnable] - Use accelerated WebGL rendering?
4371
+ * @param {boolean} [screenSpace=false] - Are pos and size in screen space?
4372
+ * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context] - Canvas 2D context to draw to
4373
+ * @memberof Draw */
4374
+ function drawTextureWrapped(pos, size, wrapCount, texture=0, color=WHITE,
4375
+ angle=0, additiveColor, useWebGL=glEnable, screenSpace=false, context)
4376
+ {
4377
+ ASSERT(isVector2(pos), 'pos must be a vec2');
4378
+ ASSERT(isVector2(size), 'size must be a vec2');
4379
+ ASSERT(isVector2(wrapCount), 'wrapCount must be a vec2');
4380
+ ASSERT(isColor(color), 'color is invalid');
4381
+ ASSERT(isNumber(angle), 'angle must be a number');
4382
+ ASSERT(!additiveColor || isColor(additiveColor), 'additiveColor must be a color');
4383
+ ASSERT(!context || !useWebGL, 'context only supported in canvas 2D mode');
4384
+ ASSERT(!(texture instanceof TileInfo),
4385
+ 'pass a TextureInfo or texture index, not a TileInfo — use tileInfo.textureInfo');
4386
+
4387
+ // short-circuit before texture lookup — textureInfos[0] is undefined in headless mode
4388
+ if (headlessMode) return;
4389
+
4390
+ // resolve texture argument: TextureInfo or index
4391
+ const textureInfo = typeof texture === 'number' ? textureInfos[texture] : texture;
4392
+ ASSERT(textureInfo instanceof TextureInfo, 'texture not loaded');
4393
+ ASSERT(textureInfo.size.x > 0, 'texture not loaded');
4394
+
4395
+ if (useWebGL && glEnable)
4396
+ {
4397
+ ASSERT(!!glContext, 'WebGL is not enabled!');
4398
+ if (screenSpace)
4399
+ [pos, size, angle] = screenToWorldTransform(pos, size, angle);
4400
+ glSetTexture(textureInfo.glTexture);
4401
+ glDraw(pos.x, pos.y, size.x, size.y, angle,
4402
+ 0, 0, wrapCount.x, wrapCount.y,
4403
+ color.rgbaInt(), additiveColor && additiveColor.rgbaInt());
4404
+ return;
4405
+ }
4406
+
4407
+ // Canvas2D path — increment drawCount here (WebGL batch counts via glBatchCount)
4408
+ ++drawCount;
4409
+
4410
+ if (!screenSpace)
4411
+ {
4412
+ pos = worldToScreen(pos);
4413
+ size = size.scale(cameraScale);
4414
+ angle -= cameraAngle;
4415
+ }
4416
+
4417
+ // pick image source: raw, or tinted bake. Match drawImageColor's
4418
+ // "no tint needed" predicate so behavior stays consistent.
4419
+ const noTint = !canvasColorTiles ||
4420
+ (additiveColor
4421
+ ? isWhite(color.add(additiveColor)) && additiveColor.a <= 0
4422
+ : isWhite(color));
4423
+ // alpha is baked into pixels by bakeTintedImage's additive branch;
4424
+ // in that case globalAlpha must NOT also apply color.a
4425
+ const alphaBaked = !noTint && additiveColor && !isBlack(additiveColor);
4426
+ const source = noTint
4427
+ ? textureInfo.image
4428
+ : bakeTintedImage(textureInfo.image, color, additiveColor);
4429
+
4430
+ context = context || drawContext;
4431
+ context.save();
4432
+ context.translate(pos.x + .5, pos.y + .5);
4433
+ context.rotate(angle);
4434
+ context.globalAlpha = alphaBaked ? 1 : color.a;
4435
+
4436
+ const pattern = context.createPattern(source, 'repeat');
4437
+ // map pattern-source pixels into user space so the rect contains
4438
+ // wrapCount.x × wrapCount.y repeats
4439
+ const m = new DOMMatrix()
4440
+ .translate(-size.x/2, -size.y/2)
4441
+ .scale(size.x / (wrapCount.x * source.width),
4442
+ size.y / (wrapCount.y * source.height));
4443
+ pattern.setTransform(m);
4444
+ context.fillStyle = pattern;
4445
+ context.fillRect(-size.x/2, -size.y/2, size.x, size.y);
4446
+ context.globalAlpha = 1;
4447
+ context.restore();
4448
+ }
4449
+
4334
4450
  /** Draw connected lines between a series of points
4335
4451
  * @param {Array<Vector2>} points
4336
4452
  * @param {number} [width]
@@ -4854,6 +4970,43 @@ function combineCanvases()
4854
4970
  mainContext.drawImage(workCanvas, 0, 0);
4855
4971
  }
4856
4972
 
4973
+ // Internal: bake a color/additive-color tint into workReadCanvas at the
4974
+ // image's native resolution. Returns the work canvas, suitable for
4975
+ // passing to context.createPattern. Used by drawTextureWrapped's
4976
+ // Canvas2D path. Caller is responsible for short-circuiting when no
4977
+ // tint is needed (i.e. color is white and additiveColor is black/none).
4978
+ function bakeTintedImage(image, color, additiveColor)
4979
+ {
4980
+ const w = image.width|0, h = image.height|0;
4981
+ workReadCanvas.width = w;
4982
+ workReadCanvas.height = h;
4983
+ workReadContext.drawImage(image, 0, 0);
4984
+
4985
+ const imageData = workReadContext.getImageData(0, 0, w, h);
4986
+ const data = imageData.data;
4987
+ if (additiveColor && !isBlack(additiveColor))
4988
+ {
4989
+ // multiply + additive (slower)
4990
+ const colorMultiply = [color.r, color.g, color.b, color.a];
4991
+ const colorAdd = [additiveColor.r * 255, additiveColor.g * 255,
4992
+ additiveColor.b * 255, additiveColor.a * 255];
4993
+ for (let i = 0; i < data.length; ++i)
4994
+ data[i] = data[i] * colorMultiply[i&3] + colorAdd[i&3] |0;
4995
+ }
4996
+ else
4997
+ {
4998
+ // RGB only, faster — alpha left intact for the caller
4999
+ for (let i = 0; i < data.length; i+=4)
5000
+ {
5001
+ data[i ] *= color.r;
5002
+ data[i+1] *= color.g;
5003
+ data[i+2] *= color.b;
5004
+ }
5005
+ }
5006
+ workReadContext.putImageData(imageData, 0, 0);
5007
+ return workReadCanvas;
5008
+ }
5009
+
4857
5010
  /** Helper function to draw an image with color and additive color applied
4858
5011
  * This is slower then normal drawImage when color is applied
4859
5012
  * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} context
@@ -4872,8 +5025,6 @@ function combineCanvases()
4872
5025
  * @memberof Draw */
4873
5026
  function drawImageColor(context, image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight, color, additiveColor, bleed=0)
4874
5027
  {
4875
- function isWhite(c) { return c.r >= 1 && c.g >= 1 && c.b >= 1; }
4876
- function isBlack(c) { return c.r <= 0 && c.g <= 0 && c.b <= 0 && c.a <= 0; }
4877
5028
  const sx2 = bleed;
4878
5029
  const sy2 = bleed;
4879
5030
  sWidth = max(1,sWidth|0);
@@ -5342,6 +5493,32 @@ function gamepadStickCount(gamepad=gamepadPrimary)
5342
5493
  return gamepadStickData[gamepad]?.length ?? 0;
5343
5494
  }
5344
5495
 
5496
+ /** Pulse a gamepad's vibration hardware using the dual-rumble effect if it exists
5497
+ * Strong magnitude is usually the left side motor, weak magnitude is usually the right side motor
5498
+ * @param {number} [gamepad] - gamepad index
5499
+ * @param {number} [duration] - effect duration in ms
5500
+ * @param {number} [strongMagnitude] - strong (left) motor intensity, 0 to 1
5501
+ * @param {number} [weakMagnitude] - weak (right) motor intensity, 0 to 1
5502
+ * @param {number} [startDelay] - delay in ms before the effect starts
5503
+ * @memberof Input */
5504
+ function gamepadVibrate(gamepad=gamepadPrimary, duration=200, strongMagnitude=1, weakMagnitude=1, startDelay=0)
5505
+ {
5506
+ ASSERT(isNumber(gamepad), 'gamepad must be a number');
5507
+ if (!vibrateEnable || headlessMode) return;
5508
+ const pad = navigator?.getGamepads?.()[gamepad];
5509
+ pad?.vibrationActuator?.playEffect?.('dual-rumble', {duration, strongMagnitude, weakMagnitude, startDelay});
5510
+ }
5511
+
5512
+ /** Stop vibration on a gamepad
5513
+ * @memberof Input */
5514
+ function gamepadVibrateStop(gamepad=gamepadPrimary)
5515
+ {
5516
+ ASSERT(isNumber(gamepad), 'gamepad must be a number');
5517
+ if (!vibrateEnable || headlessMode) return;
5518
+ const pad = navigator?.getGamepads?.()[gamepad];
5519
+ pad?.vibrationActuator?.reset?.();
5520
+ }
5521
+
5345
5522
  ///////////////////////////////////////////////////////////////////////////////
5346
5523
 
5347
5524
  /** Pulse the vibration hardware if it exists
@@ -8317,9 +8494,8 @@ function glClearCanvas()
8317
8494
  /** Set the WebGL texture, called automatically if using multiple textures
8318
8495
  * - This may also flush the gl buffer resulting in more draw calls and worse performance
8319
8496
  * @param {WebGLTexture} texture
8320
- * @param {boolean} [wrap] - Should the texture wrap or clamp
8321
8497
  * @memberof WebGL */
8322
- function glSetTexture(texture, wrap=false)
8498
+ function glSetTexture(texture)
8323
8499
  {
8324
8500
  // must flush cache with the old texture to set a new one
8325
8501
  if (!glContext || texture === glActiveTexture) return;
@@ -8327,11 +8503,6 @@ function glSetTexture(texture, wrap=false)
8327
8503
  glFlush();
8328
8504
  glActiveTexture = texture;
8329
8505
  glContext.bindTexture(glContext.TEXTURE_2D, glActiveTexture);
8330
-
8331
- // set wrap mode
8332
- const wrapMode = wrap ? glContext.REPEAT : glContext.CLAMP_TO_EDGE;
8333
- glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_WRAP_S, wrapMode);
8334
- glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_WRAP_T, wrapMode);
8335
8506
  }
8336
8507
 
8337
8508
  /** Compile WebGL shader of the given type, will throw errors if in debug mode
@@ -8406,6 +8577,8 @@ function glCreateTexture(image)
8406
8577
  const minFilter = mipMap ? glContext.LINEAR_MIPMAP_LINEAR : magFilter;
8407
8578
  glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_MAG_FILTER, magFilter);
8408
8579
  glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_MIN_FILTER, minFilter);
8580
+ glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_WRAP_S, glContext.REPEAT);
8581
+ glContext.texParameteri(glContext.TEXTURE_2D, glContext.TEXTURE_WRAP_T, glContext.REPEAT);
8409
8582
  if (mipMap)
8410
8583
  glContext.generateMipmap(glContext.TEXTURE_2D);
8411
8584
 
@@ -10322,7 +10495,7 @@ class UIObject
10322
10495
 
10323
10496
  /** Internal function called when object is clicked
10324
10497
  * @param {boolean} [playSound] */
10325
- click(playSound)
10498
+ click(playSound=true)
10326
10499
  {
10327
10500
  this.onClick();
10328
10501
  if (playSound && this.soundClick)
@@ -11295,7 +11468,8 @@ class Box2dObject extends EngineObject
11295
11468
  {
11296
11469
  this.pos = pos;
11297
11470
  this.angle = angle;
11298
- this.body.SetTransform(box2d.vec2dTo(pos), angle);
11471
+ // box2d uses reverse angle
11472
+ this.body.SetTransform(box2d.vec2dTo(pos), -angle);
11299
11473
  }
11300
11474
 
11301
11475
  /** Sets the position
@@ -11306,7 +11480,7 @@ class Box2dObject extends EngineObject
11306
11480
  /** Sets the angle
11307
11481
  * @param {number} angle */
11308
11482
  setAngle(angle)
11309
- { this.setTransform(box2d.vec2From(this.body.GetPosition()), -angle); }
11483
+ { this.setTransform(box2d.vec2From(this.body.GetPosition()), angle); }
11310
11484
 
11311
11485
  /** Sets the linear velocity
11312
11486
  * @param {Vector2} velocity */
@@ -12851,6 +13025,7 @@ async function box2dInit()
12851
13025
  {
12852
13026
  if (o.body)
12853
13027
  {
13028
+ // box2d uses reverse angle
12854
13029
  o.pos = box2d.vec2From(o.body.GetPosition());
12855
13030
  o.angle = -o.body.GetAngle();
12856
13031
  }
@@ -13061,3 +13236,511 @@ function drawThreeSlice(pos, size, startTile, color, borderSize=1, additiveColor
13061
13236
  drawTile(pos.add(cornerPos.rotate(rotateAngle)), cornerSize, cornerTile, color, a, false, additiveColor, useWebGL, screenSpace, context);
13062
13237
  }
13063
13238
  }
13239
+ /**
13240
+ * LittleJS Tween System Plugin
13241
+ * - Lightweight tweens for numbers, Vector2, Color, or any .lerp-able type
13242
+ * - Chainable easing, looping, and ping-pong
13243
+ * - Property-path helper for the common case of animating an object field
13244
+ * - Auto-updates via engineAddPlugin; pauses with the game by default
13245
+ * @namespace TweenSystem
13246
+ */
13247
+
13248
+ ///////////////////////////////////////////////////////////////////////////////
13249
+
13250
+ // Module-private list of tweens currently running.
13251
+ const tweenActive = [];
13252
+
13253
+ // Time tracking for delta computation between engine plugin calls.
13254
+ let lastTime = 0;
13255
+ let lastTimeReal = 0;
13256
+
13257
+ // True if the value is an instance of a class that exposes a numeric-percent
13258
+ // `lerp(other, percent)` method (Vector2, Color, or any future class).
13259
+ function isLerpable(v) { return v && typeof v.lerp === 'function'; }
13260
+
13261
+ ///////////////////////////////////////////////////////////////////////////////
13262
+
13263
+ /** A numeric tween: drives a callback with a value interpolated between
13264
+ * `start` and `end` over `duration` seconds. Pauses with the game by default.
13265
+ * @memberof TweenSystem
13266
+ * @example
13267
+ * // Animate a fade-out over 2 seconds with an ease-out sine curve.
13268
+ * new Tween((v) => obj.alpha = v, 1, 0, 2, { ease: Ease.OUT(Ease.SINE) });
13269
+ */
13270
+ class Tween
13271
+ {
13272
+ /** Create a new tween. The callback fires immediately with `start` so the
13273
+ * target snaps to the start value on the same frame the tween is created.
13274
+ *
13275
+ * `start` and `end` may be numbers, Vector2 instances, Color instances, or
13276
+ * any object exposing a `lerp(other, percent) => sameType` method. The
13277
+ * callback receives the interpolated value (a number, or a fresh instance
13278
+ * for lerp-able types). Both endpoints must be the same type.
13279
+ * @param {function(number|Vector2|Color):void} callback - Called with the interpolated value each frame
13280
+ * @param {number|Vector2|Color} [start=0] - Starting value
13281
+ * @param {number|Vector2|Color} [end=1] - Ending value
13282
+ * @param {number} [duration=1] - Duration in seconds
13283
+ * @param {Object} [options]
13284
+ * @param {function(number):number} [options.ease] - Easing function (defaults to LINEAR)
13285
+ * @param {boolean} [options.useRealTime=false] - Advance even when the game is paused (matches Timer's useRealTime)
13286
+ * @param {boolean} [options.paused=false] - Start in paused state */
13287
+ constructor(callback, start = 0, end = 1, duration = 1, options = {})
13288
+ {
13289
+ ASSERT(typeof callback === 'function', 'Tween callback must be a function');
13290
+ if (isLerpable(start))
13291
+ {
13292
+ ASSERT(start.constructor === end.constructor,
13293
+ 'Tween start and end must be the same type');
13294
+ }
13295
+ else
13296
+ {
13297
+ ASSERT(isNumber(start), 'Tween start must be a number or have a .lerp method');
13298
+ ASSERT(isNumber(end), 'Tween end must be a number when start is a number');
13299
+ }
13300
+ ASSERT(isNumber(duration) && duration > 0, 'Tween duration must be > 0');
13301
+
13302
+ this.callback = callback;
13303
+ this.start = start;
13304
+ this.end = end;
13305
+ this.duration = duration;
13306
+ this.life = duration;
13307
+ this.ease = options.ease || Ease.LINEAR;
13308
+ this.useRealTime = !!options.useRealTime;
13309
+ this.paused = !!options.paused;
13310
+
13311
+ /** @private completion callback set by then(), loop(), pingPong(). */
13312
+ this.thenCallback = undefined;
13313
+ /** @private remaining iterations including the current run (loop/pingPong only). */
13314
+ this.loopRemaining = 0;
13315
+
13316
+ tweenActive.push(this);
13317
+ // Snap target to start immediately.
13318
+ callback(this.interp(duration));
13319
+ }
13320
+
13321
+ /** Set the easing curve and return this for chaining.
13322
+ * @param {function(number):number} easeFn
13323
+ * @returns {Tween}
13324
+ * @memberof TweenSystem */
13325
+ setEase(easeFn)
13326
+ {
13327
+ this.ease = easeFn;
13328
+ return this;
13329
+ }
13330
+
13331
+ /** Set a single completion callback. Calling `then` again replaces the
13332
+ * previous callback. Returns this for chaining.
13333
+ *
13334
+ * Calling `then` after `loop` or `pingPong` overrides the loop chain
13335
+ * (last call wins).
13336
+ * @param {function():void} callback
13337
+ * @returns {Tween}
13338
+ * @memberof TweenSystem */
13339
+ then(callback)
13340
+ {
13341
+ this.thenCallback = callback;
13342
+ this.loopRemaining = 0;
13343
+ return this;
13344
+ }
13345
+
13346
+ /** Repeat this tween `n` total times. After each iteration finishes, a
13347
+ * fresh tween with the same parameters takes over via the `then` slot.
13348
+ * `loop()` with no argument loops forever.
13349
+ *
13350
+ * Mutually exclusive with `pingPong`; calling either replaces the other,
13351
+ * and calling `then` after either clears the loop (last call wins).
13352
+ * @param {number} [count=Infinity]
13353
+ * @returns {Tween}
13354
+ * @memberof TweenSystem */
13355
+ loop(count = Infinity)
13356
+ {
13357
+ this.loopRemaining = count;
13358
+ this.thenCallback = () => loopContinuation(this);
13359
+ return this;
13360
+ }
13361
+
13362
+ /** Like `loop`, but swap `start` and `end` between iterations so the value
13363
+ * bounces back and forth. `pingPong()` with no argument bounces forever.
13364
+ *
13365
+ * Mutually exclusive with `loop`; calling either replaces the other, and
13366
+ * calling `then` after either clears the loop (last call wins).
13367
+ * @param {number} [count=Infinity]
13368
+ * @returns {Tween}
13369
+ * @memberof TweenSystem */
13370
+ pingPong(count = Infinity)
13371
+ {
13372
+ this.loopRemaining = count;
13373
+ this.thenCallback = () => pingPongContinuation(this);
13374
+ return this;
13375
+ }
13376
+
13377
+ /** Pause this tween. While paused, tweenUpdate skips it.
13378
+ * @memberof TweenSystem */
13379
+ pause() { this.paused = true; }
13380
+
13381
+ /** Resume a paused tween.
13382
+ * @memberof TweenSystem */
13383
+ resume() { this.paused = false; }
13384
+
13385
+ /** Reset this tween to the start: life back to duration, pause cleared,
13386
+ * re-added to the active list if previously stopped, and the callback
13387
+ * re-fired with the start value.
13388
+ * @memberof TweenSystem */
13389
+ restart()
13390
+ {
13391
+ this.life = this.duration;
13392
+ this.paused = false;
13393
+ if (tweenActive.indexOf(this) < 0) tweenActive.push(this);
13394
+ this.callback(this.interp(this.duration));
13395
+ }
13396
+
13397
+ /** True if this tween is in the active list and not paused.
13398
+ * @returns {boolean}
13399
+ * @memberof TweenSystem */
13400
+ isActive()
13401
+ {
13402
+ return !this.paused && tweenActive.indexOf(this) >= 0;
13403
+ }
13404
+
13405
+ /** Get how far this tween has progressed, from 0 (just started) to 1
13406
+ * (completed). Clamped — overshoot past completion still reads 1.
13407
+ * @returns {number}
13408
+ * @memberof TweenSystem */
13409
+ getPercent()
13410
+ {
13411
+ return percent(this.duration - this.life, 0, this.duration);
13412
+ }
13413
+
13414
+ /** Get the current interpolated value (the value most recently passed to
13415
+ * the callback). Returns a number, Vector2, or Color depending on the
13416
+ * tween's start/end types.
13417
+ * @returns {number|Vector2|Color}
13418
+ * @memberof TweenSystem */
13419
+ getValue()
13420
+ {
13421
+ return this.interp(this.life);
13422
+ }
13423
+
13424
+ /** Compute the interpolated value at the given remaining `life`.
13425
+ * At life === duration the result is `start`; at life === 0 it is `end`.
13426
+ * @param {number} life
13427
+ * @returns {number}
13428
+ * @memberof TweenSystem */
13429
+ interp(life)
13430
+ {
13431
+ const x = this.ease((this.duration - life) / this.duration);
13432
+ if (isLerpable(this.start))
13433
+ return this.start.lerp(this.end, x);
13434
+ return this.start + (this.end - this.start) * x;
13435
+ }
13436
+
13437
+ /** Remove this tween from the active list and prevent any pending then-callback.
13438
+ * @memberof TweenSystem */
13439
+ stop()
13440
+ {
13441
+ const i = tweenActive.indexOf(this);
13442
+ if (i >= 0) tweenActive.splice(i, 1);
13443
+ this.thenCallback = undefined;
13444
+ }
13445
+ }
13446
+
13447
+ /** Library of named easing curves and direction modifiers.
13448
+ * All curves accept `x` in [0,1] and return [0,1] (with possible overshoot
13449
+ * for ELASTIC/BACK/SPRING/BOUNCE). Curves are values you pass to `setEase`
13450
+ * or compose via the IN/OUT/IN_OUT/PIECEWISE/BEZIER modifiers.
13451
+ * @memberof TweenSystem
13452
+ * @example
13453
+ * // Use a basic curve
13454
+ * new Tween(callback, 0, 10, 1).setEase(Ease.SINE);
13455
+ * // Use a modifier on a curve
13456
+ * new Tween(callback, 0, 10, 1).setEase(Ease.OUT(Ease.BACK));
13457
+ */
13458
+ const Ease =
13459
+ {
13460
+ /** Linear (identity) curve.
13461
+ * @param {number} x
13462
+ * @returns {number}
13463
+ * @memberof TweenSystem */
13464
+ LINEAR: (x) => x,
13465
+
13466
+ /** Power curve factory: `Ease.POWER(n)` returns `x => x**n`.
13467
+ * Use n=2 for quadratic, n=3 for cubic, etc.
13468
+ * @param {number} n
13469
+ * @returns {function(number):number}
13470
+ * @memberof TweenSystem */
13471
+ POWER: (n) => (x) => x ** n,
13472
+
13473
+ /** Sine ease-in curve: starts slow, ends fast.
13474
+ * @param {number} x
13475
+ * @returns {number}
13476
+ * @memberof TweenSystem */
13477
+ SINE: (x) => 1 - Math.cos(x * (Math.PI / 2)),
13478
+
13479
+ /** Circular ease-in curve.
13480
+ * @param {number} x
13481
+ * @returns {number}
13482
+ * @memberof TweenSystem */
13483
+ CIRC: (x) => 1 - Math.sqrt(1 - x * x),
13484
+
13485
+ /** Exponential ease-in curve (`2^(10x-10)`).
13486
+ * @param {number} x
13487
+ * @returns {number}
13488
+ * @memberof TweenSystem */
13489
+ EXPO: (x) => 2 ** (10 * x - 10),
13490
+
13491
+ /** Back ease-in: overshoots backward at the start before snapping forward.
13492
+ * @param {number} x
13493
+ * @returns {number}
13494
+ * @memberof TweenSystem */
13495
+ BACK: (x) => x * x * (2.70158 * x - 1.70158),
13496
+
13497
+ /** Elastic ease-in: oscillates with decreasing amplitude.
13498
+ * @param {number} x
13499
+ * @returns {number}
13500
+ * @memberof TweenSystem */
13501
+ ELASTIC: (x) =>
13502
+ -(2 ** (10 * x - 10)) * Math.sin(((37 - 40 * x) * Math.PI) / 6),
13503
+
13504
+ /** Spring-like ease-out: oscillates outward after passing the target.
13505
+ * @param {number} x
13506
+ * @returns {number}
13507
+ * @memberof TweenSystem */
13508
+ SPRING: (x) =>
13509
+ 1 -
13510
+ (Math.sin(Math.PI * (1 - x) * (0.2 + 2.5 * (1 - x) ** 3)) *
13511
+ Math.pow(x, 2.2) +
13512
+ (1 - x)) *
13513
+ (1.0 + 1.2 * x),
13514
+
13515
+ /** Bouncing ease-in: slow ramp with bouncing impacts near the end.
13516
+ * Symmetric with the other base curves, which are all ease-in. To get the
13517
+ * classic "object falls and hits the ground" shape (bounces near x=1),
13518
+ * wrap with `Ease.OUT`: `Ease.OUT(Ease.BOUNCE)`.
13519
+ * @param {number} x
13520
+ * @returns {number}
13521
+ * @memberof TweenSystem
13522
+ * @example
13523
+ * Ease.BOUNCE // ease-in bounce (slow, then bouncy at end)
13524
+ * Ease.OUT(Ease.BOUNCE) // ease-out bounce (object hits ground)
13525
+ * Ease.IN_OUT(Ease.BOUNCE) // bounces at both ends
13526
+ */
13527
+ BOUNCE: (x) =>
13528
+ {
13529
+ // Inverted form of the standard easeOutBounce: 1 - bounceOut(1 - x).
13530
+ let t = 1 - x, f;
13531
+ if (t < 4 / 11) f = 7.5625 * t * t;
13532
+ else if (t < 8 / 11) f = 7.5625 * (t -= 6 / 11) * t + 0.75;
13533
+ else if (t < 10 / 11) f = 7.5625 * (t -= 9 / 11) * t + 0.9375;
13534
+ else f = 7.5625 * (t -= 10.5 / 11) * t + 0.984375;
13535
+ return 1 - f;
13536
+ },
13537
+
13538
+ /** Ease-in direction modifier: returns the curve unchanged. Symmetric
13539
+ * with `OUT` and `IN_OUT`. Base curves are already ease-in by
13540
+ * convention, so wrapping a curve in `IN` is a no-op — useful when
13541
+ * picking the direction programmatically.
13542
+ * @param {function(number):number} f - Curve to use as ease-in (returned unchanged)
13543
+ * @returns {function(number):number}
13544
+ * @memberof TweenSystem
13545
+ * @example
13546
+ * // Pick direction at runtime
13547
+ * const dir = bouncyMode ? Ease.OUT : Ease.IN;
13548
+ * new Tween(cb, 0, 10, 1).setEase(dir(Ease.BACK));
13549
+ */
13550
+ IN: (f) => f,
13551
+
13552
+ /** Reverse a curve so it eases out instead of in: `x => 1 - f(1 - x)`.
13553
+ * @param {function(number):number} f
13554
+ * @returns {function(number):number}
13555
+ * @memberof TweenSystem
13556
+ * @example
13557
+ * Ease.OUT(Ease.POWER(2)) // ease-out quadratic
13558
+ */
13559
+ OUT: (f) => (x) => 1 - f(1 - x),
13560
+
13561
+ /** Combine the first half of `f` with `Ease.OUT(f)` for a symmetric curve.
13562
+ * Bug-fix vs the original library: the original referenced an undefined
13563
+ * global `Piecewise`; this implementation routes through `Ease.PIECEWISE`.
13564
+ * @param {function(number):number} f
13565
+ * @returns {function(number):number}
13566
+ * @memberof TweenSystem */
13567
+ IN_OUT: (f) => Ease.PIECEWISE(f, Ease.OUT(f)),
13568
+
13569
+ /** Split [0,1] into N equal sections and run a different curve in each.
13570
+ * Each curve is mapped to its section: section i runs over [i/n, (i+1)/n]
13571
+ * and its output is mapped to [i/n, (i+1)/n] of the overall range.
13572
+ * @param {...function(number):number} fns
13573
+ * @returns {function(number):number}
13574
+ * @memberof TweenSystem */
13575
+ PIECEWISE: (...fns) =>
13576
+ {
13577
+ const n = fns.length;
13578
+ return (x) =>
13579
+ {
13580
+ const i = (x * n - 1e-9) >> 0;
13581
+ return (fns[i]((x - i / n) * n) + i) / n;
13582
+ };
13583
+ },
13584
+
13585
+ /** Cubic Bezier curve solver in the style of CSS `cubic-bezier`.
13586
+ * Control points (0,0), (x1,y1), (x2,y2), (1,1).
13587
+ * @param {number} x1
13588
+ * @param {number} y1
13589
+ * @param {number} x2
13590
+ * @param {number} y2
13591
+ * @returns {function(number):number}
13592
+ * @memberof TweenSystem
13593
+ * @example
13594
+ * Ease.BEZIER(0.25, 0.1, 0.25, 1) // CSS "ease"
13595
+ */
13596
+ BEZIER: (x1, y1, x2, y2) =>
13597
+ {
13598
+ // Parametric cubic Bezier with implicit (0,0) and (1,1) endpoints.
13599
+ const curve = (t) =>
13600
+ {
13601
+ const u = 1 - t;
13602
+ const c1 = 3 * u * u * t;
13603
+ const c2 = 3 * u * t * t;
13604
+ const t3 = t ** 3;
13605
+ return [c1 * x1 + c2 * x2 + t3, c1 * y1 + c2 * y2 + t3];
13606
+ };
13607
+ return (x) =>
13608
+ {
13609
+ // Binary search for t such that curve(t).x ≈ x, then return curve(t).y.
13610
+ let t0 = 0, t1 = 1;
13611
+ for (let i = 0; i < 128; i++)
13612
+ {
13613
+ const tMid = (t0 + t1) / 2;
13614
+ const [bx, by] = curve(tMid);
13615
+ if (Math.abs(bx - x) < 1e-5) return by;
13616
+ if (bx < x) t0 = tMid; else t1 = tMid;
13617
+ }
13618
+ return curve((t0 + t1) / 2)[1];
13619
+ };
13620
+ },
13621
+ };
13622
+
13623
+ /** Tween a property on an object by dot-path. Returns the underlying Tween
13624
+ * so all chaining methods (`setEase`, `then`, `loop`, `pingPong`, etc.)
13625
+ * remain available.
13626
+ *
13627
+ * `start` and `end` may be numbers, Vector2 instances, Color instances, or
13628
+ * any object with a `lerp(other, percent) => sameType` method.
13629
+ * @param {Object} target - The object whose property is being animated
13630
+ * @param {string} propertyPath - Dot-separated path, e.g. `'pos.x'` or `'color'`
13631
+ * @param {number|Vector2|Color} start - Starting value
13632
+ * @param {number|Vector2|Color} end - Ending value
13633
+ * @param {number} [duration=1] - Duration in seconds
13634
+ * @param {Object} [options] - Same options as the Tween constructor
13635
+ * @returns {Tween}
13636
+ * @memberof TweenSystem
13637
+ * @example
13638
+ * // Numeric: slide an object's x with an ease-out sine curve
13639
+ * tweenProperty(player, 'pos.x', 0, 10, 2).setEase(Ease.OUT(Ease.SINE));
13640
+ * // Vector2: animate a position diagonally
13641
+ * tweenProperty(player, 'pos', vec2(-5, 0), vec2(5, 3), 2);
13642
+ * // Color: pulse between two colors
13643
+ * tweenProperty(sprite, 'color', RED, BLUE, 1).pingPong();
13644
+ */
13645
+ function tweenProperty(target, propertyPath, start, end, duration = 1, options = {})
13646
+ {
13647
+ ASSERT(target != null && typeof target === 'object', 'tweenProperty target must be an object');
13648
+ ASSERT(isString(propertyPath) && propertyPath.length > 0, 'tweenProperty propertyPath must be a non-empty string');
13649
+
13650
+ const parts = propertyPath.split('.');
13651
+ const lastKey = parts.pop();
13652
+ const callback = (value) =>
13653
+ {
13654
+ let obj = target;
13655
+ for (const k of parts) obj = obj[k];
13656
+ obj[lastKey] = value;
13657
+ };
13658
+ return new Tween(callback, start, end, duration, options);
13659
+ }
13660
+
13661
+ // Continuation that schedules the next loop iteration when one finishes.
13662
+ // Called from the completed tween's `then` slot. Decrements the counter and
13663
+ // only spawns a new tween if more iterations remain.
13664
+ function loopContinuation(prev)
13665
+ {
13666
+ if (prev.loopRemaining !== Infinity && prev.loopRemaining <= 1) return;
13667
+ const next = new Tween(prev.callback, prev.start, prev.end, prev.duration,
13668
+ { ease: prev.ease, useRealTime: prev.useRealTime });
13669
+ next.loopRemaining = prev.loopRemaining === Infinity
13670
+ ? Infinity
13671
+ : prev.loopRemaining - 1;
13672
+ next.thenCallback = () => loopContinuation(next);
13673
+ }
13674
+
13675
+ // Continuation for pingPong: spawns a new tween with start and end swapped.
13676
+ function pingPongContinuation(prev)
13677
+ {
13678
+ if (prev.loopRemaining !== Infinity && prev.loopRemaining <= 1) return;
13679
+ const next = new Tween(prev.callback, prev.end, prev.start, prev.duration,
13680
+ { ease: prev.ease, useRealTime: prev.useRealTime });
13681
+ next.loopRemaining = prev.loopRemaining === Infinity
13682
+ ? Infinity
13683
+ : prev.loopRemaining - 1;
13684
+ next.thenCallback = () => pingPongContinuation(next);
13685
+ }
13686
+
13687
+ /** Engine plugin hook: advance every active tween by the appropriate delta.
13688
+ * Called once per render frame by the engine (no arguments). May also be
13689
+ * called explicitly with `(gameDelta, realDelta)` to drive tweens manually
13690
+ * — useful for headless tests or custom replay/scrubbing systems.
13691
+ * @param {number} [gameDelta] - Game-time delta in seconds; default: time - lastTime
13692
+ * @param {number} [realDelta] - Real-time delta in seconds; default: timeReal - lastTimeReal
13693
+ * @memberof TweenSystem */
13694
+ function tweenUpdate(gameDelta, realDelta)
13695
+ {
13696
+ if (gameDelta === undefined)
13697
+ {
13698
+ // Engine path: compute deltas from engine time globals.
13699
+ gameDelta = time - lastTime;
13700
+ realDelta = timeReal - lastTimeReal;
13701
+ lastTime = time;
13702
+ lastTimeReal = timeReal;
13703
+ }
13704
+ else if (realDelta === undefined)
13705
+ {
13706
+ // Manual path with one arg: real and game advance together.
13707
+ realDelta = gameDelta;
13708
+ }
13709
+
13710
+ // Iterate in reverse so removals don't disturb iteration.
13711
+ for (let i = tweenActive.length; i--;)
13712
+ {
13713
+ const t = tweenActive[i];
13714
+ if (t.paused) continue;
13715
+ const dt = t.useRealTime ? realDelta : gameDelta;
13716
+ if (dt <= 0) continue;
13717
+
13718
+ t.life -= dt;
13719
+ if (t.life > 0)
13720
+ {
13721
+ t.callback(t.interp(t.life));
13722
+ }
13723
+ else
13724
+ {
13725
+ // Completion: fire end value, remove from active, fire then-callback.
13726
+ t.callback(t.interp(0));
13727
+ tweenActive.splice(i, 1);
13728
+ const cb = t.thenCallback;
13729
+ t.thenCallback = undefined;
13730
+ if (cb) cb();
13731
+ }
13732
+ }
13733
+ }
13734
+
13735
+ /** Stop every active tween and clear their then-callbacks. Useful for resets
13736
+ * on level transitions or when changing scenes.
13737
+ * @memberof TweenSystem */
13738
+ function tweenStopAll()
13739
+ {
13740
+ for (const t of tweenActive) t.thenCallback = undefined;
13741
+ tweenActive.length = 0;
13742
+ }
13743
+
13744
+ // Register with the engine so tweens auto-advance.
13745
+ engineAddPlugin(tweenUpdate);
13746
+