Extends BaseTween and start delay and lots of other fixes

This commit is contained in:
Richard Davey 2022-09-13 18:47:12 +01:00
parent a2b349d745
commit b8a3cf9232

View file

@ -4,9 +4,12 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var BaseTween = require('./BaseTween');
var Class = require('../../utils/Class');
var EventEmitter = require('eventemitter3');
var Events = require('../events');
var GameObjectCreator = require('../../gameobjects/GameObjectCreator');
var GameObjectFactory = require('../../gameobjects/GameObjectFactory');
var Tween = require('./Tween');
var TWEEN_CONST = require('./const');
var TweenBuilder = require('../builders/TweenBuilder');
@ -16,180 +19,56 @@ var TweenBuilder = require('../builders/TweenBuilder');
*
* @class TweenChain
* @memberof Phaser.Tweens
* @extends Phaser.Events.EventEmitter
* @extends Phaser.Tweens.BaseTween
* @constructor
* @since 3.60.0
*
* @param {Phaser.Tweens.TweenManager} manager - A reference to the TweenManager this TweenChain belongs to.
* @param {Phaser.Tweens.TweenManager} parent - A reference to the Tween Manager that owns this Tween.
*/
var TweenChain = new Class({
Extends: EventEmitter,
Extends: BaseTween,
initialize:
function TweenChain (manager)
function TweenChain (parent)
{
EventEmitter.call(this);
/**
* A reference to the TweenManager that this TweenChain instance belongs to.
*
* @name Phaser.Tweens.TweenData#manager
* @type {Phaser.Tweens.TweenManager}
* @since 3.60.0
*/
this.manager = manager;
// The tweens
this.data = [];
BaseTween.call(this, parent);
this.currentTween = null;
this.currentIndex = 0;
},
// Chain States
this.state = 0;
/**
* Prepares this Tween for playback.
*
* Called automatically by the TweenManager. Should not be called directly.
*
* @method Phaser.Tweens.Tween#init
* @fires Phaser.Tweens.Events#TWEEN_ACTIVE
* @since 3.0.0
*
* @return {this} This Tween instance.
*/
init: function ()
{
this.loopCounter = (this.loop === -1) ? 999999999999 : this.loop;
this.paused = true;
this.setCurrentTween(0);
/**
* Scales the time applied to this Tween. A value of 1 runs in real-time. A value of 0.5 runs 50% slower, and so on.
*
* The value isn't used when calculating total duration of the tween, it's a run-time delta adjustment only.
*
* @name Phaser.Tweens.Tween#timeScale
* @type {number}
* @default 1
* @since 3.60.0
*/
this.timeScale = 1;
if (this.startDelay > 0)
{
this.setStartDelayState();
}
else
{
this.setActiveState();
}
/**
* Loop this tween? Can be -1 for an infinite loop, or a positive integer.
*
* When enabled it will play through ALL TweenDatas again. Use TweenData.repeat to loop a single element.
*
* @name Phaser.Tweens.Tween#loop
* @type {number}
* @default 0
* @since 3.60.0
*/
this.loop = 0;
this.dispatchEvent(Events.TWEEN_ACTIVE, 'onActive');
/**
* Time in ms/frames before the Tween loops.
*
* @name Phaser.Tweens.Tween#loopDelay
* @type {number}
* @default 0
* @since 3.60.0
*/
this.loopDelay = 0;
/**
* Internal counter recording how many loops are left to run.
*
* @name Phaser.Tweens.Tween#loopCounter
* @type {number}
* @default 0
* @since 3.60.0
*/
this.loopCounter = 0;
/**
* The time in ms/frames before the 'onComplete' event fires.
*
* This never fires if loop = -1 (as it never completes)
*
* @name Phaser.Tweens.Tween#completeDelay
* @type {number}
* @default 0
* @since 3.60.0
*/
this.completeDelay = 0;
/**
* An internal countdown timer (used by loopDelay and completeDelay)
*
* @name Phaser.Tweens.Tween#countdown
* @type {number}
* @default 0
* @since 3.60.0
*/
this.countdown = 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;
/**
* 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 = false;
/**
* 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 = 0;
/**
* 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 = 0;
/**
* The time, in milliseconds, before the repeat will start.
*
* @name Phaser.Tweens.TweenData#repeatDelay
* @type {number}
* @since 3.60.0
*/
this.repeatDelay = 0;
/**
* How many repeats are left to run?
*
* @name Phaser.Tweens.TweenData#repeatCounter
* @type {number}
* @since 3.60.0
*/
this.repeatCounter = 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;
return this;
},
/**
@ -218,80 +97,140 @@ var TweenChain = new Class({
tweens = [ tweens ];
}
var tween;
var data = this.data;
for (var i = 0; i < tweens.length; i++)
{
tween = TweenBuilder(this.manager, tweens[i]);
var tween = tweens[i];
this.data.push(tween.init());
if (tween instanceof Tween)
{
data.push(tween.init());
}
else
{
tween = TweenBuilder(this, tween);
data.push(tween.init());
}
}
this.totalData = data.length;
return this;
},
/**
* Internal method that advances the Tween based on the time values.
* See if this Tween is currently acting upon the given target.
*
* @method Phaser.Tweens.Tween#update
* @fires Phaser.Tweens.Events#TWEEN_COMPLETE
* @fires Phaser.Tweens.Events#TWEEN_LOOP
* @fires Phaser.Tweens.Events#TWEEN_START
* @method Phaser.Tweens.Tween#hasTarget
* @since 3.0.0
*
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
* @param {object} target - The target to check against this Tween.
*
* @return {boolean} Returns `true` if this Tween has finished and should be removed from the Tween Manager, otherwise returns `false`.
* @return {boolean} `true` if the given target is a target of this Tween, otherwise `false`.
*/
update: function (delta)
hasTarget: function (target)
{
if (this.paused)
var data = this.data;
for (var i = 0; i < this.totalData; i++)
{
return false;
}
// if (this.isPendingRemove() || this.isDestroyed())
// {
// return true;
// }
// else if (this.isFinished() || this.paused)
// {
// return false;
// }
delta *= this.timeScale * this.manager.timeScale;
if (!this.currentTween)
{
this.currentTween = this.data[0];
this.currentTween.setActiveState();
this.currentTween.dispatchEvent(Events.TWEEN_ACTIVE, 'onActive');
}
if (this.currentTween.update(delta))
{
// This tween has finshed playback, so move to the next one
this.currentIndex++;
if (this.currentIndex === this.data.length)
if (data[i].hasTarget(target))
{
this.nextState();
}
else
{
this.currentTween = this.data[this.currentIndex];
this.currentTween.setActiveState();
this.currentTween.dispatchEvent(Events.TWEEN_ACTIVE, 'onActive');
return true;
}
}
return false;
},
/**
* Restarts the Tween from the beginning.
*
* You can only restart a Tween that is currently playing. If the Tween has already been stopped, restarting
* it will throw an error.
*
* If you wish to restart the Tween from a specific point, use the `Tween.seek` method instead.
*
* @method Phaser.Tweens.Tween#restart
* @since 3.0.0
*
* @return {this} This Tween instance.
*/
restart: function ()
{
switch (this.state)
{
case TWEEN_CONST.REMOVED:
case TWEEN_CONST.FINISHED:
this.parent.makeActive(this);
break;
case TWEEN_CONST.PENDING:
case TWEEN_CONST.PENDING_REMOVE:
this.parent.reset(this);
break;
case TWEEN_CONST.DESTROYED:
console.warn('Cannot restart destroyed TweenChain');
break;
default:
// this.seek();
break;
}
this.paused = false;
this.hasStarted = false;
this.currentIndex = 0;
this.currentTween = this.data[0];
return this;
},
/**
* Resets the given Tween.
*
* If the Tween does not belong to this Tween Manager, it will first be added.
*
* Then it will seek to position 0 and playback will start on the next frame.
*
* @method Phaser.Tweens.TweenManager#reset
* @since 3.60.0
*
* @param {Phaser.Tweens.Tween} tween - The Tween to be reset.
*
* @return {this} This Tween Manager instance.
*/
reset: function (tween)
{
tween.seek();
tween.setActiveState();
return this;
},
/**
* Checks if a Tween is active and adds it to the Tween Manager at the start of the frame if it isn't.
*
* @method Phaser.Tweens.TweenManager#makeActive
* @since 3.0.0
*
* @param {Phaser.Tweens.Tween} tween - The Tween to check.
*
* @return {this} This Tween Manager instance.
*/
makeActive: function (tween)
{
tween.init();
tween.setActiveState();
return this;
},
/**
* Internal method that advances to the next state of the Tween during playback.
*
@ -339,135 +278,6 @@ var TweenChain = new Class({
return false;
},
/**
* Internal method that resets all of the Tween Data, including the progress and elapsed values.
*
* @method Phaser.Tweens.Tween#resetTweenData
* @since 3.0.0
*
* @param {boolean} resetFromLoop - Has this method been called as part of a loop?
*/
resetTweens: function ()
{
var data = this.data;
for (var i = 0; i < data.length; i++)
{
data[i].resetTweenData(true);
}
this.currentIndex = 0;
this.currentTween = this.data[0];
},
/**
* Sets the value of the time scale applied to this Tween. A value of 1 runs in real-time.
* A value of 0.5 runs 50% slower, and so on.
*
* The value isn't used when calculating total duration of the tween, it's a run-time delta adjustment only.
*
* @method Phaser.Tweens.Tween#setTimeScale
* @since 3.60.0
*
* @param {number} value - The time scale value to set.
*
* @return {this} This Tween instance.
*/
setTimeScale: function (value)
{
this.timeScale = value;
return this;
},
/**
* Gets the value of the time scale applied to this Tween. A value of 1 runs in real-time.
* A value of 0.5 runs 50% slower, and so on.
*
* @method Phaser.Tweens.Tween#getTimeScale
* @since 3.60.0
*
* @return {number} The value of the time scale applied to this Tween.
*/
getTimeScale: function ()
{
return this.timeScale;
},
/**
* Checks if this Tween is currently playing.
*
* If this Tween is paused, this method will return false.
*
* @method Phaser.Tweens.Tween#isPlaying
* @since 3.60.0
*
* @return {boolean} `true` if the Tween is playing, otherwise `false`.
*/
isPlaying: function ()
{
return (!this.paused && this.isActive());
},
/**
* Checks if the Tween is currently paused.
*
* @method Phaser.Tweens.Tween#isPaused
* @since 3.60.0
*
* @return {boolean} `true` if the Tween is paused, otherwise `false`.
*/
isPaused: function ()
{
return this.paused;
},
/**
* Pauses the Tween immediately. Use `resume` to continue playback.
*
* You can also toggle the `Tween.paused` boolean property, but doing so will not trigger the PAUSE event.
*
* @method Phaser.Tweens.Tween#pause
* @fires Phaser.Tweens.Events#TWEEN_PAUSE
* @since 3.60.0
*
* @return {this} This Tween instance.
*/
pause: function ()
{
if (!this.paused)
{
this.paused = true;
this.dispatchEvent(Events.TWEEN_PAUSE, 'onPause');
}
return this;
},
/**
* Resumes the playback of a previously paused Tween.
*
* You can also toggle the `Tween.paused` boolean property, but doing so will not trigger the RESUME event.
*
* @method Phaser.Tweens.Tween#resume
* @fires Phaser.Tweens.Events#TWEEN_RESUME
* @since 3.60.0
*
* @return {this} This Tween instance.
*/
resume: function ()
{
if (this.paused)
{
this.paused = false;
this.dispatchEvent(Events.TWEEN_RESUME, 'onResume');
}
return this;
},
/**
* Starts a Tween playing.
*
@ -488,214 +298,167 @@ var TweenChain = new Class({
{
if (this.isDestroyed())
{
console.warn('Cannot play destroyed TweenChain');
console.warn('Cannot play destroyed TweenChain', this);
return this;
}
if (this.isPendingRemove() || this.isPending())
{
// ?
this.resetTweens();
}
this.paused = false;
this.setActiveState();
if (this.startDelay > 0)
{
this.setStartDelayState();
}
else
{
this.setActiveState();
}
return this;
},
/**
* Sets this Tween state to PENDING.
* Internal method that resets all of the Tween Data, including the progress and elapsed values.
*
* @method Phaser.Tweens.Tween#setPendingState
* @since 3.60.0
* @method Phaser.Tweens.Tween#resetTweens
* @since 3.0.0
*/
setPendingState: function ()
resetTweens: function ()
{
this.state = TWEEN_CONST.PENDING;
var data = this.data;
var total = this.totalData;
for (var i = 0; i < total; i++)
{
data[i].reset(false);
}
this.currentIndex = 0;
this.currentTween = data[0];
},
/**
* Sets this Tween state to ACTIVE.
* Internal method that advances the Tween based on the time values.
*
* @method Phaser.Tweens.Tween#setActiveState
* @since 3.60.0
* @method Phaser.Tweens.Tween#update
* @fires Phaser.Tweens.Events#TWEEN_COMPLETE
* @fires Phaser.Tweens.Events#TWEEN_LOOP
* @fires Phaser.Tweens.Events#TWEEN_START
* @since 3.0.0
*
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
*
* @return {boolean} Returns `true` if this Tween has finished and should be removed from the Tween Manager, otherwise returns `false`.
*/
setActiveState: function ()
update: function (delta)
{
this.state = TWEEN_CONST.ACTIVE;
if (this.isPendingRemove() || this.isDestroyed())
{
return true;
}
else if (this.isFinished() || this.paused)
{
return false;
}
// The TweehChain.timeScale is applied within Tween.update, so doesn't need including here
delta *= this.parent.timeScale;
if (this.isLoopDelayed())
{
this.updateLoopCountdown(delta);
}
else if (this.isCompleteDelayed())
{
this.updateCompleteDelay(delta);
}
else if (this.isStartDelayed())
{
// Reset the delta so we always start progress from zero
delta = this.updateStartCountdown(delta);
}
var remove = false;
if (this.isActive() && this.currentTween)
{
if (this.currentTween.update(delta))
{
// This tween has finshed playback, so move to the next one
if (this.nextTween())
{
this.nextState();
}
}
// if nextState called onCompleteHandler then we're ready to be removed, unless we persist
remove = this.isPendingRemove();
if (remove && this.persist)
{
this.setFinishedState();
remove = false;
}
}
return remove;
},
getCurrentTween: function ()
{
return this.data[this.currentIndex];
},
nextTween: function ()
{
this.currentIndex++;
if (this.currentIndex === this.totalData)
{
return true;
}
else
{
this.setCurrentTween(this.currentIndex);
}
},
setCurrentTween: function (index)
{
this.currentIndex = index;
this.currentTween = this.data[index];
this.currentTween.setActiveState();
this.currentTween.dispatchEvent(Events.TWEEN_ACTIVE, 'onActive');
},
/**
* Sets this Tween state to LOOP_DELAY.
* Internal method that will emit a Tween based Event and invoke the given callback.
*
* @method Phaser.Tweens.Tween#setLoopDelayState
* @since 3.60.0
*/
setLoopDelayState: function ()
{
this.state = TWEEN_CONST.LOOP_DELAY;
},
/**
* Sets this Tween state to COMPLETE_DELAY.
*
* @method Phaser.Tweens.Tween#setCompleteDelayState
* @since 3.60.0
*/
setCompleteDelayState: function ()
{
this.state = TWEEN_CONST.COMPLETE_DELAY;
},
/**
* Sets this Tween state to PENDING_REMOVE.
*
* @method Phaser.Tweens.Tween#setPendingRemoveState
* @since 3.60.0
*/
setPendingRemoveState: function ()
{
this.state = TWEEN_CONST.PENDING_REMOVE;
},
/**
* Sets this Tween state to REMOVED.
*
* @method Phaser.Tweens.Tween#setRemovedState
* @since 3.60.0
*/
setRemovedState: function ()
{
this.state = TWEEN_CONST.REMOVED;
},
/**
* Sets this Tween state to FINISHED.
*
* @method Phaser.Tweens.Tween#setFinishedState
* @since 3.60.0
*/
setFinishedState: function ()
{
this.state = TWEEN_CONST.FINISHED;
},
/**
* Sets this Tween state to DESTROYED.
*
* @method Phaser.Tweens.Tween#setDestroyedState
* @since 3.60.0
*/
setDestroyedState: function ()
{
this.state = TWEEN_CONST.DESTROYED;
},
/**
* Returns `true` if this Tween has a _current_ state of PENDING, otherwise `false`.
*
* @method Phaser.Tweens.Tween#isPending
* @method Phaser.Tweens.Tween#dispatchEvent
* @since 3.60.0
*
* @return {boolean} `true` if this Tween has a _current_ state of PENDING, otherwise `false`.
* @param {Phaser.Types.Tweens.Event} event - The Event to be dispatched.
* @param {Phaser.Types.Tweens.TweenCallbackTypes} [callback] - The name of the callback to be invoked. Can be `null` or `undefined` to skip invocation.
*/
isPending: function ()
dispatchEvent: function (event, callback)
{
return (this.state === TWEEN_CONST.PENDING);
},
this.emit(event, this, this.targets);
/**
* Returns `true` if this Tween has a _current_ state of ACTIVE, otherwise `false`.
*
* @method Phaser.Tweens.Tween#isActive
* @since 3.60.0
*
* @return {boolean} `true` if this Tween has a _current_ state of ACTIVE, otherwise `false`.
*/
isActive: function ()
{
return (this.state === TWEEN_CONST.ACTIVE);
},
var handler = this.callbacks[callback];
/**
* Returns `true` if this Tween has a _current_ state of LOOP_DELAY, otherwise `false`.
*
* @method Phaser.Tweens.Tween#isLoopDelayed
* @since 3.60.0
*
* @return {boolean} `true` if this Tween has a _current_ state of LOOP_DELAY, otherwise `false`.
*/
isLoopDelayed: function ()
{
return (this.state === TWEEN_CONST.LOOP_DELAY);
},
/**
* Returns `true` if this Tween has a _current_ state of COMPLETE_DELAY, otherwise `false`.
*
* @method Phaser.Tweens.Tween#isCompleteDelayed
* @since 3.60.0
*
* @return {boolean} `true` if this Tween has a _current_ state of COMPLETE_DELAY, otherwise `false`.
*/
isCompleteDelayed: function ()
{
return (this.state === TWEEN_CONST.COMPLETE_DELAY);
},
/**
* Returns `true` if this Tween has a _current_ state of PENDING_REMOVE, otherwise `false`.
*
* @method Phaser.Tweens.Tween#isPendingRemove
* @since 3.60.0
*
* @return {boolean} `true` if this Tween has a _current_ state of PENDING_REMOVE, otherwise `false`.
*/
isPendingRemove: function ()
{
return (this.state === TWEEN_CONST.PENDING_REMOVE);
},
/**
* Returns `true` if this Tween has a _current_ state of REMOVED, otherwise `false`.
*
* @method Phaser.Tweens.Tween#isRemoved
* @since 3.60.0
*
* @return {boolean} `true` if this Tween has a _current_ state of REMOVED, otherwise `false`.
*/
isRemoved: function ()
{
return (this.state === TWEEN_CONST.REMOVED);
},
/**
* Returns `true` if this Tween has a _current_ state of FINISHED, otherwise `false`.
*
* @method Phaser.Tweens.Tween#isFinished
* @since 3.60.0
*
* @return {boolean} `true` if this Tween has a _current_ state of FINISHED, otherwise `false`.
*/
isFinished: function ()
{
return (this.state === TWEEN_CONST.FINISHED);
},
/**
* Returns `true` if this Tween has a _current_ state of DESTROYED, otherwise `false`.
*
* @method Phaser.Tweens.Tween#isDestroyed
* @since 3.60.0
*
* @return {boolean} `true` if this Tween has a _current_ state of DESTROYED, otherwise `false`.
*/
isDestroyed: function ()
{
return (this.state === TWEEN_CONST.DESTROYED);
if (handler)
{
handler.func.apply(handler.scope, [ this, this.targets ].concat(handler.params));
}
},
/**
@ -706,20 +469,45 @@ var TweenChain = new Class({
*/
destroy: function ()
{
for (var i = 0; i < this.data.length; i++)
{
this.data[i].destroy();
}
BaseTween.prototype.destroy.call(this);
this.removeAllListeners();
this.callbacks = null;
this.data = null;
this.manager = null;
this.setDestroyedState();
console.log('TweenChain destroyed');
}
});
/**
* Creates a new Tween object.
*
* Note: This method will only be available if Tweens have been built into Phaser.
*
* @method Phaser.GameObjects.GameObjectFactory#tweenchain
* @since 3.0.0
*
* @param {Phaser.Types.Tweens.TweenBuilderConfig|object} config - The Tween configuration.
*
* @return {Phaser.Tweens.Tween} The Tween that was created.
*/
GameObjectFactory.register('tweenchain', function (config)
{
return this.scene.sys.tweens.add(config);
});
/**
* Creates a new Tween object and returns it.
*
* Note: This method will only be available if Tweens have been built into Phaser.
*
* @method Phaser.GameObjects.GameObjectCreator#tweenchain
* @since 3.0.0
*
* @param {Phaser.Types.Tweens.TweenBuilderConfig|object} config - The Tween configuration.
*
* @return {Phaser.Tweens.Tween} The Tween that was created.
*/
GameObjectCreator.register('tweenchain', function (config)
{
return this.scene.sys.tweens.create(config);
});
module.exports = TweenChain;