2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2013-08-28 20:29:25 +00:00
|
|
|
/**
|
|
|
|
* Phaser.Game has a single instance of the TweenManager through which all Tween objects are created and updated.
|
|
|
|
* Tweens are hooked into the game clock and pause system, adjusting based on the game state.
|
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* TweenManager is based heavily on tween.js by http://soledadpenades.com.
|
2013-08-28 20:29:25 +00:00
|
|
|
* The difference being that tweens belong to a games instance of TweenManager, rather than to a global TWEEN object.
|
|
|
|
* It also has callbacks swapped for Signals and a few issues patched with regard to properties and completion errors.
|
2013-10-03 01:38:35 +00:00
|
|
|
* Please see https://github.com/sole/tween.js for a full list of contributors.
|
2016-11-08 01:50:57 +00:00
|
|
|
*
|
2014-09-16 18:44:04 +00:00
|
|
|
* @class Phaser.TweenManager
|
2013-10-01 12:54:29 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2013-08-28 20:29:25 +00:00
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
Phaser.TweenManager = function (state)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @property {Phaser.State} game - Local reference to game.
|
|
|
|
*/
|
|
|
|
this.state = state;
|
2013-08-28 20:29:25 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
this.game = state.game;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2015-08-20 11:59:06 +00:00
|
|
|
/**
|
|
|
|
* Are all newly created Tweens frame or time based? A frame based tween will use the physics elapsed timer when updating. This means
|
|
|
|
* it will retain the same consistent frame rate, regardless of the speed of the device. The duration value given should
|
|
|
|
* be given in frames.
|
2016-11-08 01:50:57 +00:00
|
|
|
*
|
2015-08-20 11:59:06 +00:00
|
|
|
* If the Tween uses a time based update (which is the default) then the duration is given in milliseconds.
|
|
|
|
* In this situation a 2000ms tween will last exactly 2 seconds, regardless of the device and how many visual updates the tween
|
|
|
|
* has actually been through. For very short tweens you may wish to experiment with a frame based update instead.
|
|
|
|
* @property {boolean} frameBased
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.frameBased = false;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-02-05 02:39:03 +00:00
|
|
|
* @property {array<Phaser.Tween>} _tweens - All of the currently running tweens.
|
2013-11-25 04:40:04 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tweens = [];
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-02-05 02:39:03 +00:00
|
|
|
* @property {array<Phaser.Tween>} _add - All of the tweens queued to be added in the next update.
|
2013-11-25 04:40:04 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._add = [];
|
|
|
|
|
2014-11-19 21:11:07 +00:00
|
|
|
this.easeMap = {
|
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
Power0: Phaser.Easing.Power0,
|
|
|
|
Power1: Phaser.Easing.Power1,
|
|
|
|
Power2: Phaser.Easing.Power2,
|
|
|
|
Power3: Phaser.Easing.Power3,
|
|
|
|
Power4: Phaser.Easing.Power4,
|
|
|
|
|
|
|
|
Linear: Phaser.Easing.Linear.None,
|
|
|
|
Quad: Phaser.Easing.Quadratic.Out,
|
|
|
|
Cubic: Phaser.Easing.Cubic.Out,
|
|
|
|
Quart: Phaser.Easing.Quartic.Out,
|
|
|
|
Quint: Phaser.Easing.Quintic.Out,
|
|
|
|
Sine: Phaser.Easing.Sinusoidal.Out,
|
|
|
|
Expo: Phaser.Easing.Exponential.Out,
|
|
|
|
Circ: Phaser.Easing.Circular.Out,
|
|
|
|
Elastic: Phaser.Easing.Elastic.Out,
|
|
|
|
Back: Phaser.Easing.Back.Out,
|
|
|
|
Bounce: Phaser.Easing.Bounce.Out,
|
|
|
|
|
|
|
|
'Quad.easeIn': Phaser.Easing.Quadratic.In,
|
|
|
|
'Cubic.easeIn': Phaser.Easing.Cubic.In,
|
|
|
|
'Quart.easeIn': Phaser.Easing.Quartic.In,
|
|
|
|
'Quint.easeIn': Phaser.Easing.Quintic.In,
|
|
|
|
'Sine.easeIn': Phaser.Easing.Sinusoidal.In,
|
|
|
|
'Expo.easeIn': Phaser.Easing.Exponential.In,
|
|
|
|
'Circ.easeIn': Phaser.Easing.Circular.In,
|
|
|
|
'Elastic.easeIn': Phaser.Easing.Elastic.In,
|
|
|
|
'Back.easeIn': Phaser.Easing.Back.In,
|
|
|
|
'Bounce.easeIn': Phaser.Easing.Bounce.In,
|
|
|
|
|
|
|
|
'Quad.easeOut': Phaser.Easing.Quadratic.Out,
|
|
|
|
'Cubic.easeOut': Phaser.Easing.Cubic.Out,
|
|
|
|
'Quart.easeOut': Phaser.Easing.Quartic.Out,
|
|
|
|
'Quint.easeOut': Phaser.Easing.Quintic.Out,
|
|
|
|
'Sine.easeOut': Phaser.Easing.Sinusoidal.Out,
|
|
|
|
'Expo.easeOut': Phaser.Easing.Exponential.Out,
|
|
|
|
'Circ.easeOut': Phaser.Easing.Circular.Out,
|
|
|
|
'Elastic.easeOut': Phaser.Easing.Elastic.Out,
|
|
|
|
'Back.easeOut': Phaser.Easing.Back.Out,
|
|
|
|
'Bounce.easeOut': Phaser.Easing.Bounce.Out,
|
|
|
|
|
|
|
|
'Quad.easeInOut': Phaser.Easing.Quadratic.InOut,
|
|
|
|
'Cubic.easeInOut': Phaser.Easing.Cubic.InOut,
|
|
|
|
'Quart.easeInOut': Phaser.Easing.Quartic.InOut,
|
|
|
|
'Quint.easeInOut': Phaser.Easing.Quintic.InOut,
|
|
|
|
'Sine.easeInOut': Phaser.Easing.Sinusoidal.InOut,
|
|
|
|
'Expo.easeInOut': Phaser.Easing.Exponential.InOut,
|
|
|
|
'Circ.easeInOut': Phaser.Easing.Circular.InOut,
|
|
|
|
'Elastic.easeInOut': Phaser.Easing.Elastic.InOut,
|
|
|
|
'Back.easeInOut': Phaser.Easing.Back.InOut,
|
|
|
|
'Bounce.easeInOut': Phaser.Easing.Bounce.InOut
|
2014-11-19 21:11:07 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
// this.game.onPause.add(this._pauseAll, this);
|
|
|
|
// this.game.onResume.add(this._resumeAll, this);
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2013-08-28 20:29:25 +00:00
|
|
|
};
|
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
Phaser.TweenManager.prototype.constructor = Phaser.TweenManager;
|
|
|
|
|
2013-08-28 20:29:25 +00:00
|
|
|
Phaser.TweenManager.prototype = {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Get all the tween objects in an array.
|
|
|
|
* @method Phaser.TweenManager#getAll
|
2016-10-08 15:17:46 +00:00
|
|
|
* @return {Phaser.Tween[]} Array with all tween objects.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
getAll: function ()
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
return this._tweens;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-05 02:39:03 +00:00
|
|
|
* Remove all tweens running and in the queue. Doesn't call any of the tween onComplete events.
|
2013-11-25 04:40:04 +00:00
|
|
|
* @method Phaser.TweenManager#removeAll
|
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
removeAll: function ()
|
|
|
|
{
|
2014-02-05 02:39:03 +00:00
|
|
|
for (var i = 0; i < this._tweens.length; i++)
|
|
|
|
{
|
|
|
|
this._tweens[i].pendingDelete = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._add = [];
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2014-10-31 11:52:05 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-17 21:10:31 +00:00
|
|
|
* Remove all tweens from a specific object, array of objects or Group.
|
|
|
|
*
|
2014-10-31 11:52:05 +00:00
|
|
|
* @method Phaser.TweenManager#removeFrom
|
|
|
|
* @param {object|object[]|Phaser.Group} obj - The object you want to remove the tweens from.
|
2014-11-17 21:10:31 +00:00
|
|
|
* @param {boolean} [children=true] - If passing a group, setting this to true will remove the tweens from all of its children instead of the group itself.
|
2014-10-31 11:52:05 +00:00
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
removeFrom: function (obj, children)
|
|
|
|
{
|
2015-07-22 09:37:15 +00:00
|
|
|
if (children === undefined) { children = true; }
|
2014-11-17 21:10:31 +00:00
|
|
|
|
|
|
|
var i;
|
|
|
|
var len;
|
|
|
|
|
2014-10-31 11:55:32 +00:00
|
|
|
if (Array.isArray(obj))
|
2014-10-31 11:52:05 +00:00
|
|
|
{
|
2014-11-17 21:10:31 +00:00
|
|
|
for (i = 0, len = obj.length; i < len; i++)
|
2014-10-31 11:52:05 +00:00
|
|
|
{
|
2014-11-17 21:10:31 +00:00
|
|
|
this.removeFrom(obj[i]);
|
2014-10-31 11:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (obj.type === Phaser.GROUP && children)
|
|
|
|
{
|
2016-11-08 01:50:57 +00:00
|
|
|
for (i = 0, len = obj.children.length; i < len; i++)
|
2014-10-31 11:52:05 +00:00
|
|
|
{
|
2014-11-17 21:10:31 +00:00
|
|
|
this.removeFrom(obj.children[i]);
|
2014-10-31 11:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-17 21:10:31 +00:00
|
|
|
for (i = 0, len = this._tweens.length; i < len; i++)
|
2014-10-31 11:52:05 +00:00
|
|
|
{
|
2014-12-03 18:20:29 +00:00
|
|
|
if (obj === this._tweens[i].target)
|
2014-10-31 11:52:05 +00:00
|
|
|
{
|
2014-11-17 21:10:31 +00:00
|
|
|
this.remove(this._tweens[i]);
|
2014-10-31 11:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-17 21:10:31 +00:00
|
|
|
|
|
|
|
for (i = 0, len = this._add.length; i < len; i++)
|
2014-11-14 16:41:14 +00:00
|
|
|
{
|
2014-12-03 18:20:29 +00:00
|
|
|
if (obj === this._add[i].target)
|
2014-11-14 16:41:14 +00:00
|
|
|
{
|
2014-11-17 21:10:31 +00:00
|
|
|
this.remove(this._add[i]);
|
2014-11-14 16:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-31 11:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new tween into the TweenManager.
|
|
|
|
*
|
|
|
|
* @method Phaser.TweenManager#add
|
|
|
|
* @param {Phaser.Tween} tween - The tween object you want to add.
|
2016-10-08 15:17:46 +00:00
|
|
|
* @return {Phaser.Tween} The tween object you added to the manager.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
add: function (tween)
|
|
|
|
{
|
2014-03-07 01:26:09 +00:00
|
|
|
tween._manager = this;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
this._add.push(tween);
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-03-23 07:59:28 +00:00
|
|
|
* Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.TweenManager#create
|
2014-11-30 11:10:52 +00:00
|
|
|
* @param {object} object - Object the tween will be run on.
|
2016-10-08 15:17:46 +00:00
|
|
|
* @return {Phaser.Tween} The newly created tween object.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
create: function (object)
|
|
|
|
{
|
|
|
|
return new Phaser.Tween(object, this);
|
2013-08-28 20:29:25 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Remove a tween from this manager.
|
|
|
|
*
|
|
|
|
* @method Phaser.TweenManager#remove
|
|
|
|
* @param {Phaser.Tween} tween - The tween object you want to remove.
|
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
remove: function (tween)
|
|
|
|
{
|
2014-01-04 02:53:32 +00:00
|
|
|
var i = this._tweens.indexOf(tween);
|
2013-08-28 20:29:25 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
if (i !== -1)
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
this._tweens[i].pendingDelete = true;
|
|
|
|
}
|
2014-07-16 18:50:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
i = this._add.indexOf(tween);
|
|
|
|
|
|
|
|
if (i !== -1)
|
|
|
|
{
|
|
|
|
this._add[i].pendingDelete = true;
|
|
|
|
}
|
|
|
|
}
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-08-28 20:29:25 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Update all the tween objects you added to this manager.
|
|
|
|
*
|
|
|
|
* @method Phaser.TweenManager#update
|
2016-10-08 15:17:46 +00:00
|
|
|
* @return {boolean} Return false if there's no tween to update, otherwise return true.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2016-11-09 12:25:26 +00:00
|
|
|
update: function (frameDelta)
|
2016-11-08 01:50:57 +00:00
|
|
|
{
|
2014-07-16 18:50:58 +00:00
|
|
|
var addTweens = this._add.length;
|
|
|
|
var numTweens = this._tweens.length;
|
|
|
|
|
|
|
|
if (numTweens === 0 && addTweens === 0)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-28 20:29:25 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
var i = 0;
|
2013-08-28 20:29:25 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
while (i < numTweens)
|
|
|
|
{
|
2016-11-09 12:25:26 +00:00
|
|
|
if (this._tweens[i].update(frameDelta))
|
2014-01-04 02:53:32 +00:00
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
i++;
|
2014-01-04 02:53:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._tweens.splice(i, 1);
|
2013-08-28 20:29:25 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
numTweens--;
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 20:29:25 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// If there are any new tweens to be added, do so now - otherwise they can be spliced out of the array before ever running
|
2014-07-16 18:50:58 +00:00
|
|
|
if (addTweens > 0)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
|
|
|
this._tweens = this._tweens.concat(this._add);
|
|
|
|
this._add.length = 0;
|
|
|
|
}
|
2013-09-19 03:45:08 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return true;
|
|
|
|
},
|
2013-08-28 20:29:25 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if a particular Sprite is currently being tweened.
|
|
|
|
*
|
|
|
|
* @method Phaser.TweenManager#isTweening
|
|
|
|
* @param {object} object - The object to check for tweens against.
|
2016-10-08 15:17:46 +00:00
|
|
|
* @return {boolean} Returns true if the object is currently being tweened, false if not.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
isTweening: function (object)
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
return this._tweens.some(function(tween) {
|
2014-12-03 18:20:29 +00:00
|
|
|
return tween.target === object;
|
2013-11-25 04:40:04 +00:00
|
|
|
});
|
|
|
|
},
|
2013-11-01 17:28:09 +00:00
|
|
|
|
2014-02-26 23:27:22 +00:00
|
|
|
/**
|
|
|
|
* Private. Called by game focus loss. Pauses all currently running tweens.
|
|
|
|
*
|
|
|
|
* @method Phaser.TweenManager#_pauseAll
|
|
|
|
* @private
|
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
_pauseAll: function ()
|
|
|
|
{
|
2014-02-26 23:27:22 +00:00
|
|
|
for (var i = this._tweens.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
this._tweens[i]._pause();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private. Called by game focus loss. Resumes all currently paused tweens.
|
|
|
|
*
|
|
|
|
* @method Phaser.TweenManager#_resumeAll
|
|
|
|
* @private
|
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
_resumeAll: function ()
|
|
|
|
{
|
2014-02-26 23:27:22 +00:00
|
|
|
for (var i = this._tweens.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
this._tweens[i]._resume();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Pauses all currently running tweens.
|
|
|
|
*
|
2014-02-20 03:44:44 +00:00
|
|
|
* @method Phaser.TweenManager#pauseAll
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
pauseAll: function ()
|
|
|
|
{
|
2014-01-04 02:53:32 +00:00
|
|
|
for (var i = this._tweens.length - 1; i >= 0; i--)
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
this._tweens[i].pause();
|
|
|
|
}
|
2013-08-28 20:29:25 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-02-26 23:27:22 +00:00
|
|
|
* Resumes all currently paused tweens.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.TweenManager#resumeAll
|
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
resumeAll: function ()
|
|
|
|
{
|
2014-01-04 02:53:32 +00:00
|
|
|
for (var i = this._tweens.length - 1; i >= 0; i--)
|
|
|
|
{
|
2014-02-25 03:33:47 +00:00
|
|
|
this._tweens[i].resume(true);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 20:29:25 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|