import type { Game } from './game.js'; import * as MathUtils from '../util/math.js'; import { TWEEN_COMPLETE, TWEEN_LOOPED, TWEEN_PENDING, TWEEN_RUNNING } from './const.js'; import type { Tween } from './tween.js'; import type { EasingFunction, InterpolationFunction } from './callback.js'; /** The properties a tween drives, each holding either a single target or a list of waypoints. */ export type TweenValues = Record; export class TweenData { public parent!: Tween; public game!: Game; public vStart!: TweenValues; public vStartCache!: TweenValues; public vEnd!: TweenValues; public vEndCache!: TweenValues; public duration!: number; public percent!: number; public value!: number; public repeatCounter!: number; public repeatDelay!: number; public repeatTotal!: number; public interpolate!: boolean; public yoyo!: boolean; public yoyoDelay!: number; public inReverse!: boolean; public delay!: number; public dt!: number; public startTime!: number | null; public easingFunction!: (k: number) => number; public interpolationFunction!: InterpolationFunction; public interpolationContext!: unknown; public isRunning!: boolean; public isFrom!: boolean; public yoyoCounter!: number; /** * Creates a new TweenData instance. * @param {Tween} parent - The parent Tween instance. */ public constructor(parent: Tween) { this.parent = parent; this.game = parent.game; this.vStart = {}; this.vStartCache = {}; this.vEnd = {}; this.vEndCache = {}; this.duration = 1000; this.percent = 0; this.value = 0; this.repeatCounter = 0; this.repeatDelay = 0; this.repeatTotal = 0; this.interpolate = false; this.yoyo = false; this.yoyoDelay = 0; this.inReverse = false; this.delay = 0; this.dt = 0; this.startTime = null; this.easingFunction = (k: number): number => k; this.interpolationFunction = MathUtils.linearInterpolation; this.interpolationContext = MathUtils; this.isRunning = false; this.isFrom = false; } /** * Sets the target properties to tween to. * @param {object} properties - The properties to tween to. * @param {number} duration - The duration of the tween in milliseconds. * @param {(number) => number} ease - The easing function to use. * @param {number} delay - The delay before starting the tween in milliseconds. * @param {number} repeat - The number of times to repeat the tween (0 = no repeat, -1 = infinite). * @param {boolean} yoyo - Whether to reverse the tween on each repeat. * @returns {TweenData} This TweenData object for chaining. */ public to( properties: TweenValues, duration: number, ease: EasingFunction, delay: number, repeat: number, yoyo: boolean ): this { this.vEnd = properties; this.duration = duration; this.easingFunction = ease; this.delay = delay; this.repeatTotal = repeat; this.yoyo = yoyo; this.isFrom = false; return this; } /** * Sets the target properties to tween from (in reverse). * @param {object} properties - The properties to tween from. * @param {number} duration - The duration of the tween in milliseconds. * @param {(number) => number} ease - The easing function to use. * @param {number} delay - The delay before starting the tween in milliseconds. * @param {number} repeat - The number of times to repeat the tween (0 = no repeat, -1 = infinite). * @param {boolean} yoyo - Whether to reverse the tween on each repeat. * @returns {TweenData} This TweenData object for chaining. */ public from( properties: TweenValues, duration: number, ease: EasingFunction, delay: number, repeat: number, yoyo: boolean ): this { this.vEnd = properties; this.duration = duration; this.easingFunction = ease; this.delay = delay; this.repeatTotal = repeat; this.yoyo = yoyo; this.isFrom = true; return this; } /** * Starts the tween. * @returns {TweenData} This TweenData object for chaining. */ public start(): this { this.startTime = this.game.time.time + this.delay; this.dt = this.parent.reverse ? this.duration : 0; this.isRunning = this.delay <= 0; if (this.isFrom) { // Reverse them all and instant set them const keys = Object.keys(this.vStartCache); for (const property of keys) { this.vStart[property] = this.vEndCache[property]!; this.vEnd[property] = this.vStartCache[property]!; (this.parent.target as unknown as Record)[property] = this.vStart[property]; } } this.value = 0; this.yoyoCounter = 0; this.repeatCounter = this.repeatTotal; return this; } /** * Loads the tween values from the parent object. * @returns {TweenData} This TweenData object for chaining. */ public loadValues(): this { const keys = Object.keys(this.parent.properties); for (const property of keys) { // Load the property from the parent object const start = this.parent.properties[property]!; this.vStart[property] = start; // Check if an Array was provided as property value const end = this.vEnd[property]; if (Array.isArray(end) && end.length > 0 && this.percent === 0) { // Put the start value at the beginning of the array // but we only want to do this once, if the Tween hasn't run before this.vEnd[property] = [Number(start), ...end]; } if (end === undefined) { // Null tween this.vEnd[property] = start; } else { if (typeof end === 'string') { // Parses relative end values with start as base (e.g.: +10, -3) this.vEnd[property] = Number(start) + Number(end); } this.parent.properties[property] = this.vEnd[property]!; } this.vStartCache[property] = this.vStart[property]!; this.vEndCache[property] = this.vEnd[property]!; } return this; } /** * Updates the tween at a given time. * @param {number} time - The current time in milliseconds. * @returns {number} The tween status (TWEEN_PENDING, TWEEN_RUNNING, or TWEEN_COMPLETE). */ public update(time: number): 1 | 0 | 3 | 2 { if (!this.isRunning) { if (time >= (this.startTime ?? 0)) { this.isRunning = true; } else { return TWEEN_PENDING; } } else if (time < (this.startTime ?? 0)) { // Is Running, but is waiting to repeat return TWEEN_RUNNING; } const ms = this.game.time.elapsedMS; if (this.parent.reverse) { this.dt -= ms * this.parent.timeScale; this.dt = Math.max(this.dt, 0); } else { this.dt += ms * this.parent.timeScale; this.dt = Math.min(this.dt, this.duration); } this.percent = this.dt / this.duration; this.value = this.easingFunction(this.percent); const keys = Object.keys(this.vEnd); for (const property of keys) { const end = this.vEnd[property]; const start = Number(this.vStart[property]); (this.parent.target as unknown as Record)[property] = Array.isArray(end) ? this.interpolationFunction.call(this.interpolationContext, end, this.value) : start + (Number(end) - start) * this.value; } if ((!this.parent.reverse && this.percent === 1) || (this.parent.reverse && this.percent === 0)) { return this.repeat(); } return TWEEN_RUNNING; } /** * Generates an array of tween data points for a given frame rate. * @param {number} frameRate - The frame rate to generate data for. * @returns {object[]} An array of tween data points. */ public generateData(frameRate: number): Record[] { this.dt = this.parent.reverse ? this.duration : 0; let data = []; let complete = false; const fps = (1 / frameRate) * 1000; do { if (this.parent.reverse) { this.dt -= fps; this.dt = Math.max(this.dt, 0); } else { this.dt += fps; this.dt = Math.min(this.dt, this.duration); } this.percent = this.dt / this.duration; this.value = this.easingFunction(this.percent); const blob: Record = {}; const keys = Object.keys(this.vEnd); for (const property of keys) { const end = this.vEnd[property]; const start = Number(this.vStart[property]); blob[property] = Array.isArray(end) ? this.interpolationFunction(end, this.value) : start + (Number(end) - start) * this.value; } data.push(blob); if ((!this.parent.reverse && this.percent === 1) || (this.parent.reverse && this.percent === 0)) { complete = true; } } while (!complete); if (this.yoyo) { data = [...data, ...data.toReversed()]; } return data; } /** * Handles tween repetition logic. * @returns {number} The tween status (TWEEN_LOOPED or TWEEN_COMPLETE). */ public repeat(): 3 | 2 { // If not a yoyo and repeatCounter = 0 then we're done if (this.yoyo) { // We're already in reverse mode, which means the yoyo has finished and there's no repeats, so end if (this.inReverse && this.repeatCounter === 0) { // Restore the properties const keys = Object.keys(this.vStartCache); for (const property of keys) { this.vStart[property] = this.vStartCache[property]!; this.vEnd[property] = this.vEndCache[property]!; } this.inReverse = false; return TWEEN_COMPLETE; } this.inReverse = !this.inReverse; } else if (this.repeatCounter === 0) { return TWEEN_COMPLETE; } if (this.inReverse) { // If inReverse we're going from vEnd to vStartCache const keys = Object.keys(this.vStartCache); for (const property of keys) { this.vStart[property] = this.vEndCache[property]!; this.vEnd[property] = this.vStartCache[property]!; } } else { // If not inReverse we're just repopulating the cache again const keys = Object.keys(this.vStartCache); for (const property of keys) { this.vStart[property] = this.vStartCache[property]!; this.vEnd[property] = this.vEndCache[property]!; } // -1 means repeat forever, otherwise decrement the repeatCounter // We only decrement this counter if the tween isn't doing a yoyo, as that doesn't count towards the repeat total if (this.repeatCounter > 0) { this.repeatCounter -= 1; } } this.startTime = this.game.time.time; if (this.yoyo && this.inReverse) { this.startTime += this.yoyoDelay; } else if (!this.inReverse) { this.startTime += this.repeatDelay; } this.dt = this.parent.reverse ? this.duration : 0; return TWEEN_LOOPED; } }