From e2389927eafea9d29f61154a192c7ae18a515151 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 20 Sep 2022 12:40:15 +0100 Subject: [PATCH] Create BaseTweenData.js --- src/tweens/tween/BaseTweenData.js | 438 ++++++++++++++++++++++++++++++ 1 file changed, 438 insertions(+) create mode 100644 src/tweens/tween/BaseTweenData.js diff --git a/src/tweens/tween/BaseTweenData.js b/src/tweens/tween/BaseTweenData.js new file mode 100644 index 000000000..37ba2c7c6 --- /dev/null +++ b/src/tweens/tween/BaseTweenData.js @@ -0,0 +1,438 @@ +/** + * @author Richard Davey + * @copyright 2022 Photon Storm Ltd. + * @license {@link https://opensource.org/licenses/MIT|MIT License} + */ + +var Class = require('../../utils/Class'); +var TWEEN_CONST = require('./const'); + +/** + * @classdesc + * The TweenData is a class that contains a single target and property that is being tweened. + * + * Tweens create TweenData instances when they are created, with one TweenData instance per + * target, per property. A Tween can own multiple TweenData instances, but a TweenData only + * ever belongs to a single Tween. + * + * You should not typically create these yourself, but rather use the TweenBuilder, + * or the `Tween.add` method. + * + * Prior to Phaser 3.60 the TweenData was just an object, but was refactored to a class, + * to make it responsible for its own state and updating. + * + * @class BaseTweenData + * @memberof Phaser.Tweens + * @constructor + * @since 3.60.0 + * + * @param {Phaser.Tweens.Tween} tween - The tween this TweenData instance belongs to. + * @param {number} targetIndex - The target index within the Tween targets array. + * @param {string} key - The property of the target to tween. + * @param {Phaser.Types.Tweens.GetEndCallback} getEnd - What the property will be at the END of the Tween. + * @param {Phaser.Types.Tweens.GetStartCallback} getStart - What the property will be at the START of the Tween. + * @param {?Phaser.Types.Tweens.GetActiveCallback} getActive - If not null, is invoked _immediately_ as soon as the TweenData is running, and is set on the target property. + * @param {function} ease - The ease function this tween uses. + * @param {function} delay - Function that returns the time in ms/frames before tween will start. + * @param {number} duration - The duration of the tween in ms/frames. + * @param {boolean} yoyo - Determines whether the tween should return back to its start value after hold has expired. + * @param {number} hold - Function that returns the time in ms/frames the tween will pause before repeating or returning to its starting value if yoyo is set to true. + * @param {number} repeat - Function that returns the number of times to repeat the tween. The tween will always run once regardless, so a repeat value of '1' will play the tween twice. + * @param {number} repeatDelay - Function that returns the time in ms/frames before the repeat will start. + * @param {boolean} flipX - Should toggleFlipX be called when yoyo or repeat happens? + * @param {boolean} flipY - Should toggleFlipY be called when yoyo or repeat happens? + * @param {?function} interpolation - The interpolation function to be used for arrays of data. Defaults to 'null'. + * @param {?number[]} interpolationData - The array of interpolation data to be set. Defaults to 'null'. + */ +var BaseTweenData = new Class({ + + initialize: + + function BaseTweenData (tween, targetIndex, delay, duration, yoyo, hold, repeat, repeatDelay, flipX, flipY) + { + /** + * A reference to the Tween that this TweenData instance belongs to. + * + * @name Phaser.Tweens.TweenData#tween + * @type {Phaser.Tweens.Tween} + * @since 3.60.0 + */ + this.tween = tween; + + /** + * The index of the target within the Tween `targets` array. + * + * @name Phaser.Tweens.TweenData#targetIndex + * @type {number} + * @since 3.60.0 + */ + this.targetIndex = targetIndex; + + /** + * The duration of the tween in milliseconds, excluding any time required + * for yoyo or repeats. + * + * @name Phaser.Tweens.TweenData#duration + * @type {number} + * @since 3.60.0 + */ + this.duration = duration; + + /** + * The total calculated duration, in milliseconds, of this TweenData. + * Factoring in the duration, repeats, delays and yoyos. + * + * @name Phaser.Tweens.TweenData#totalDuration + * @type {number} + * @since 3.60.0 + */ + this.totalDuration = 0; + + /** + * The time, in milliseconds, before this tween will start playing. + * + * This value is generated by the `getDelay` function. + * + * @name Phaser.Tweens.TweenData#delay + * @type {number} + * @since 3.60.0 + */ + this.delay = 0; + + /** + * This function returns the value to be used for `TweenData.delay`. + * + * @name Phaser.Tweens.TweenData#getDelay + * @type {function} + * @since 3.60.0 + */ + this.getDelay = delay; + + /** + * Will the Tween ease back to its starting values, after reaching the end + * and any `hold` value that may be set? + * + * @name Phaser.Tweens.TweenData#yoyo + * @type {boolean} + * @since 3.60.0 + */ + this.yoyo = yoyo; + + /** + * The time, in milliseconds, before this tween will start a yoyo to repeat. + * + * @name Phaser.Tweens.TweenData#hold + * @type {number} + * @since 3.60.0 + */ + this.hold = hold; + + /** + * The number of times this tween will repeat. + * + * The tween will always run once regardless of this value, + * so a repeat value of '1' will play the tween twice: I.e. the original + * play-through and then it repeats that once (1). + * + * If this value is set to -1 this tween will repeat forever. + * + * @name Phaser.Tweens.TweenData#repeat + * @type {number} + * @since 3.60.0 + */ + this.repeat = repeat; + + /** + * The time, in milliseconds, before the repeat will start. + * + * @name Phaser.Tweens.TweenData#repeatDelay + * @type {number} + * @since 3.60.0 + */ + this.repeatDelay = repeatDelay; + + /** + * How many repeats are left to run? + * + * @name Phaser.Tweens.TweenData#repeatCounter + * @type {number} + * @since 3.60.0 + */ + this.repeatCounter = 0; + + /** + * If `true` this Tween will call `toggleFlipX` on the Tween target + * whenever it yoyo's or repeats. It will only be called if the target + * has a function matching this name, like most Phaser GameObjects do. + * + * @name Phaser.Tweens.TweenData#flipX + * @type {boolean} + * @since 3.60.0 + */ + this.flipX = flipX; + + /** + * If `true` this Tween will call `toggleFlipY` on the Tween target + * whenever it yoyo's or repeats. It will only be called if the target + * has a function matching this name, like most Phaser GameObjects do. + * + * @name Phaser.Tweens.TweenData#flipY + * @type {boolean} + * @since 3.60.0 + */ + this.flipY = flipY; + + /** + * A value between 0 and 1 holding the progress of this TweenData. + * + * @name Phaser.Tweens.TweenData#progress + * @type {number} + * @since 3.60.0 + */ + this.progress = 0; + + /** + * The amount of time, in milliseconds, that has elapsed since this + * TweenData was made active. + * + * @name Phaser.Tweens.TweenData#elapsed + * @type {number} + * @since 3.60.0 + */ + this.elapsed = 0; + + /** + * The state of this TweenData. + * + * @name Phaser.Tweens.TweenData#state + * @type {Phaser.Types.Tweens.TweenDataState} + * @since 3.60.0 + */ + this.state = 0; + + /** + * Is this Tween Data currently waiting for a countdown to elapse, or not? + * + * @name Phaser.Tweens.TweenData#isCountdown + * @type {boolean} + * @since 3.60.0 + */ + this.isCountdown = false; + }, + + /** + * Sets this TweenData state to CREATED. + * + * @method Phaser.Tweens.TweenData#setCreatedState + * @since 3.60.0 + */ + setCreatedState: function () + { + this.state = TWEEN_CONST.CREATED; + this.isCountdown = false; + }, + + /** + * Sets this TweenData state to DELAY. + * + * @method Phaser.Tweens.TweenData#setDelayState + * @since 3.60.0 + */ + setDelayState: function () + { + this.state = TWEEN_CONST.DELAY; + this.isCountdown = true; + }, + + /** + * Sets this TweenData state to PENDING_RENDER. + * + * @method Phaser.Tweens.TweenData#setPendingRenderState + * @since 3.60.0 + */ + setPendingRenderState: function () + { + this.state = TWEEN_CONST.PENDING_RENDER; + this.isCountdown = false; + }, + + /** + * Sets this TweenData state to PLAYING_FORWARD. + * + * @method Phaser.Tweens.TweenData#setPlayingForwardState + * @since 3.60.0 + */ + setPlayingForwardState: function () + { + this.state = TWEEN_CONST.PLAYING_FORWARD; + this.isCountdown = false; + }, + + /** + * Sets this TweenData state to PLAYING_BACKWARD. + * + * @method Phaser.Tweens.TweenData#setPlayingBackwardState + * @since 3.60.0 + */ + setPlayingBackwardState: function () + { + this.state = TWEEN_CONST.PLAYING_BACKWARD; + this.isCountdown = false; + }, + + /** + * Sets this TweenData state to HOLD_DELAY. + * + * @method Phaser.Tweens.TweenData#setHoldState + * @since 3.60.0 + */ + setHoldState: function () + { + this.state = TWEEN_CONST.HOLD_DELAY; + this.isCountdown = true; + }, + + /** + * Sets this TweenData state to REPEAT_DELAY. + * + * @method Phaser.Tweens.TweenData#setRepeatState + * @since 3.60.0 + */ + setRepeatState: function () + { + this.state = TWEEN_CONST.REPEAT_DELAY; + this.isCountdown = true; + }, + + /** + * Sets this TweenData state to COMPLETE. + * + * @method Phaser.Tweens.TweenData#setCompleteState + * @since 3.60.0 + */ + setCompleteState: function () + { + this.state = TWEEN_CONST.COMPLETE; + this.isCountdown = false; + }, + + /** + * Returns `true` if this TweenData has a _current_ state of CREATED, otherwise `false`. + * + * @method Phaser.Tweens.TweenData#isCreated + * @since 3.60.0 + * + * @return {boolean} `true` if this TweenData has a _current_ state of CREATED, otherwise `false`. + */ + isCreated: function () + { + return (this.state === TWEEN_CONST.CREATED); + }, + + /** + * Returns `true` if this TweenData has a _current_ state of DELAY, otherwise `false`. + * + * @method Phaser.Tweens.TweenData#isDelayed + * @since 3.60.0 + * + * @return {boolean} `true` if this TweenData has a _current_ state of DELAY, otherwise `false`. + */ + isDelayed: function () + { + return (this.state === TWEEN_CONST.DELAY); + }, + + /** + * Returns `true` if this TweenData has a _current_ state of PENDING_RENDER, otherwise `false`. + * + * @method Phaser.Tweens.TweenData#isPendingRender + * @since 3.60.0 + * + * @return {boolean} `true` if this TweenData has a _current_ state of PENDING_RENDER, otherwise `false`. + */ + isPendingRender: function () + { + return (this.state === TWEEN_CONST.PENDING_RENDER); + }, + + /** + * Returns `true` if this TweenData has a _current_ state of PLAYING_FORWARD, otherwise `false`. + * + * @method Phaser.Tweens.TweenData#isPlayingForward + * @since 3.60.0 + * + * @return {boolean} `true` if this TweenData has a _current_ state of PLAYING_FORWARD, otherwise `false`. + */ + isPlayingForward: function () + { + return (this.state === TWEEN_CONST.PLAYING_FORWARD); + }, + + /** + * Returns `true` if this TweenData has a _current_ state of PLAYING_BACKWARD, otherwise `false`. + * + * @method Phaser.Tweens.TweenData#isPlayingBackward + * @since 3.60.0 + * + * @return {boolean} `true` if this TweenData has a _current_ state of PLAYING_BACKWARD, otherwise `false`. + */ + isPlayingBackward: function () + { + return (this.state === TWEEN_CONST.PLAYING_BACKWARD); + }, + + /** + * Returns `true` if this TweenData has a _current_ state of HOLD_DELAY, otherwise `false`. + * + * @method Phaser.Tweens.TweenData#isHolding + * @since 3.60.0 + * + * @return {boolean} `true` if this TweenData has a _current_ state of HOLD_DELAY, otherwise `false`. + */ + isHolding: function () + { + return (this.state === TWEEN_CONST.HOLD_DELAY); + }, + + /** + * Returns `true` if this TweenData has a _current_ state of REPEAT_DELAY, otherwise `false`. + * + * @method Phaser.Tweens.TweenData#isRepeating + * @since 3.60.0 + * + * @return {boolean} `true` if this TweenData has a _current_ state of REPEAT_DELAY, otherwise `false`. + */ + isRepeating: function () + { + return (this.state === TWEEN_CONST.REPEAT_DELAY); + }, + + /** + * Returns `true` if this TweenData has a _current_ state of COMPLETE, otherwise `false`. + * + * @method Phaser.Tweens.TweenData#isComplete + * @since 3.60.0 + * + * @return {boolean} `true` if this TweenData has a _current_ state of COMPLETE, otherwise `false`. + */ + isComplete: function () + { + return (this.state === TWEEN_CONST.COMPLETE); + }, + + /** + * Immediately destroys this TweenData, nulling of all its references. + * + * @method Phaser.Tweens.TweenData#destroy + * @since 3.60.0 + */ + destroy: function () + { + this.tween = null; + this.getDelay = null; + this.setCompleteState(); + } + +}); + +module.exports = BaseTweenData;