phaser/src/tween/Tween.js

608 lines
13 KiB
JavaScript
Raw Normal View History

2013-10-01 12:54:29 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2013-08-28 23:09:12 +00:00
/**
* Tween constructor
* Create a new <code>Tween</code>.
*
2013-10-01 12:54:29 +00:00
* @class Phaser.Tween
* @constructor
* @param {object} object - Target object will be affected by this tween.
* @param {Phaser.Game} game - Current game instance.
2013-08-28 23:09:12 +00:00
*/
Phaser.Tween = function (object, game) {
/**
* Reference to the target object.
2013-10-01 12:54:29 +00:00
* @property {object} _object
* @private
2013-08-28 23:09:12 +00:00
*/
this._object = object;
2013-10-01 12:54:29 +00:00
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
*/
2013-08-28 23:09:12 +00:00
this.game = game;
2013-10-01 12:54:29 +00:00
/**
* @property {object} _manager - Description.
* @private
*/
2013-08-28 23:09:12 +00:00
this._manager = this.game.tweens;
2013-10-01 12:54:29 +00:00
/**
* @property {object} _valuesStart - Description.
* @private
*/
this._valuesStart = {};
/**
* @property {object} _valuesEnd - Description.
* @private
*/
this._valuesEnd = {};
/**
* @property {object} _valuesStartRepeat - Description.
* @private
*/
this._valuesStartRepeat = {};
/**
* @property {number} _duration - Description.
* @private
2013-10-01 12:54:29 +00:00
* @default
*/
this._duration = 1000;
/**
* @property {number} _repeat - Description.
* @private
* @default
*/
this._repeat = 0;
/**
2013-10-01 15:39:39 +00:00
* @property {boolean} _yoyo - Description.
2013-10-01 12:54:29 +00:00
* @private
* @default
*/
this._yoyo = false;
/**
2013-10-01 15:39:39 +00:00
* @property {boolean} _reversed - Description.
2013-10-01 12:54:29 +00:00
* @private
* @default
*/
this._reversed = false;
/**
* @property {number} _delayTime - Description.
* @private
* @default
*/
this._delayTime = 0;
/**
* @property {Description} _startTime - Description.
* @private
* @default null
*/
this._startTime = null;
/**
* @property {Description} _easingFunction - Description.
* @private
*/
this._easingFunction = Phaser.Easing.Linear.None;
/**
* @property {Description} _interpolationFunction - Description.
* @private
*/
this._interpolationFunction = Phaser.Math.linearInterpolation;
/**
* @property {Description} _chainedTweens - Description.
* @private
*/
this._chainedTweens = [];
/**
* @property {Description} _onStartCallback - Description.
* @private
* @default
*/
this._onStartCallback = null;
/**
2013-10-01 15:39:39 +00:00
* @property {boolean} _onStartCallbackFired - Description.
2013-10-01 12:54:29 +00:00
* @private
* @default
*/
this._onStartCallbackFired = false;
2013-08-28 23:09:12 +00:00
2013-10-01 12:54:29 +00:00
/**
* @property {Description} _onUpdateCallback - Description.
* @private
* @default null
*/
this._onUpdateCallback = null;
/**
* @property {Description} _onCompleteCallback - Description.
* @private
* @default null
*/
this._onCompleteCallback = null;
/**
* @property {number} _pausedTime - Description.
* @private
* @default
*/
2013-08-28 23:09:12 +00:00
this._pausedTime = 0;
2013-10-01 12:54:29 +00:00
/**
* @property {boolean} pendingDelete - If this tween is ready to be deleted by the TweenManager.
2013-10-01 12:54:29 +00:00
* @default
*/
this.pendingDelete = false;
2013-10-01 12:54:29 +00:00
// Set all starting values present on the target object
for ( var field in object ) {
this._valuesStart[ field ] = parseFloat(object[field], 10);
}
/**
* @property {Phaser.Signal} onStart - Description.
*/
this.onStart = new Phaser.Signal();
2013-10-01 12:54:29 +00:00
/**
* @property {Phaser.Signal} onComplete - Description.
*/
2013-08-28 23:09:12 +00:00
this.onComplete = new Phaser.Signal();
2013-10-01 12:54:29 +00:00
/**
2013-10-01 15:39:39 +00:00
* @property {boolean} isRunning - Description.
2013-10-01 12:54:29 +00:00
* @default
*/
2013-08-28 23:09:12 +00:00
this.isRunning = false;
};
Phaser.Tween.prototype = {
/**
* Configure the Tween
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#to
2013-10-01 12:54:29 +00:00
* @param {object} properties - Properties you want to tween.
* @param {number} duration - Duration of this tween.
* @param {function} ease - Easing function.
2013-10-01 15:39:39 +00:00
* @param {boolean} autoStart - Whether this tween will start automatically or not.
2013-10-01 12:54:29 +00:00
* @param {number} delay - Delay before this tween will start, defaults to 0 (no delay).
2013-10-01 15:39:39 +00:00
* @param {boolean} repeat - Should the tween automatically restart once complete? (ignores any chained tweens).
2013-10-01 12:54:29 +00:00
* @param {Phaser.Tween} yoyo - Description.
* @return {Phaser.Tween} Itself.
2013-08-28 23:09:12 +00:00
*/
to: function ( properties, duration, ease, autoStart, delay, repeat, yoyo ) {
duration = duration || 1000;
ease = ease || null;
autoStart = autoStart || false;
delay = delay || 0;
repeat = repeat || 0;
yoyo = yoyo || false;
var self;
if (this._parent)
{
self = this._manager.create(this._object);
2013-09-21 01:47:58 +00:00
self._parent = this._parent;
this.chain(self);
}
else
{
self = this;
self._parent = self;
}
self._repeat = repeat;
self._duration = duration;
self._valuesEnd = properties;
2013-08-28 23:09:12 +00:00
if (ease !== null)
{
self._easingFunction = ease;
2013-08-28 23:09:12 +00:00
}
if (delay > 0)
{
self._delayTime = delay;
2013-08-28 23:09:12 +00:00
}
self._yoyo = yoyo;
2013-08-28 23:09:12 +00:00
if (autoStart) {
return self.start();
2013-08-28 23:09:12 +00:00
} else {
return self;
2013-08-28 23:09:12 +00:00
}
},
2013-10-01 12:54:29 +00:00
/**
* Starts the tween running. Can also be called by the autoStart parameter of Tween.to.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#start
2013-10-01 12:54:29 +00:00
* @param {number} time - Description.
* @return {Phaser.Tween} Itself.
*/
2013-08-28 23:09:12 +00:00
start: function ( time ) {
if (this.game === null || this._object === null) {
return;
}
this._manager.add(this);
this.onStart.dispatch(this._object);
2013-08-28 23:09:12 +00:00
this.isRunning = true;
this._onStartCallbackFired = false;
this._startTime = this.game.time.now + this._delayTime;
for ( var property in this._valuesEnd ) {
// check if an Array was provided as property value
if ( this._valuesEnd[ property ] instanceof Array ) {
if ( this._valuesEnd[ property ].length === 0 ) {
continue;
}
// create a local copy of the Array with the start value at the front
this._valuesEnd[ property ] = [ this._object[ property ] ].concat( this._valuesEnd[ property ] );
}
this._valuesStart[ property ] = this._object[ property ];
if ( ( this._valuesStart[ property ] instanceof Array ) === false ) {
this._valuesStart[ property ] *= 1.0; // Ensures we're using numbers, not strings
}
this._valuesStartRepeat[ property ] = this._valuesStart[ property ] || 0;
}
return this;
},
2013-10-01 12:54:29 +00:00
/**
* Stops the tween if running and removes it from the TweenManager. If there are any onComplete callbacks or events they are not dispatched.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#stop
2013-10-01 12:54:29 +00:00
* @return {Phaser.Tween} Itself.
*/
2013-08-28 23:09:12 +00:00
stop: function () {
this._manager.remove(this);
2013-08-28 23:09:12 +00:00
this.isRunning = false;
return this;
},
2013-10-01 12:54:29 +00:00
/**
* Sets a delay time before this tween will start.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#delay
* @param {number} amount - The amount of the delay in ms.
2013-10-01 12:54:29 +00:00
* @return {Phaser.Tween} Itself.
*/
2013-08-28 23:09:12 +00:00
delay: function ( amount ) {
this._delayTime = amount;
return this;
},
2013-10-01 12:54:29 +00:00
/**
* Sets the number of times this tween will repeat.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#repeat
2013-10-01 12:54:29 +00:00
* @param {number} times - How many times to repeat.
* @return {Phaser.Tween} Itself.
*/
2013-08-28 23:09:12 +00:00
repeat: function ( times ) {
this._repeat = times;
return this;
},
2013-10-01 12:54:29 +00:00
/**
* A tween that has yoyo set to true will run through from start to finish, then reverse from finish to start.
* Used in combination with repeat you can create endless loops.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#yoyo
* @param {boolean} yoyo - Set to true to yoyo this tween.
2013-10-01 12:54:29 +00:00
* @return {Phaser.Tween} Itself.
*/
2013-08-28 23:09:12 +00:00
yoyo: function( yoyo ) {
this._yoyo = yoyo;
return this;
},
2013-10-01 12:54:29 +00:00
/**
* Set easing function this tween will use, i.e. Phaser.Easing.Linear.None.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#easing
* @param {function} easing - The easing function this tween will use, i.e. Phaser.Easing.Linear.None.
2013-10-01 12:54:29 +00:00
* @return {Phaser.Tween} Itself.
*/
2013-08-28 23:09:12 +00:00
easing: function ( easing ) {
this._easingFunction = easing;
return this;
},
2013-10-01 12:54:29 +00:00
/**
* Set interpolation function the tween will use, by default it uses Phaser.Math.linearInterpolation.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#interpolation
* @param {function} interpolation - The interpolation function to use (Phaser.Math.linearInterpolation by default)
2013-10-01 12:54:29 +00:00
* @return {Phaser.Tween} Itself.
*/
2013-08-28 23:09:12 +00:00
interpolation: function ( interpolation ) {
this._interpolationFunction = interpolation;
return this;
},
2013-10-01 12:54:29 +00:00
/**
* You can chain tweens together by passing a reference to the chain function. This enables one tween to call another on completion.
* You can pass as many tweens as you like to this function, they will each be chained in sequence.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#chain
2013-10-01 12:54:29 +00:00
* @return {Phaser.Tween} Itself.
*/
2013-08-28 23:09:12 +00:00
chain: function () {
this._chainedTweens = arguments;
return this;
},
2013-09-21 01:47:58 +00:00
/**
* Loop a chain of tweens
*
* Usage:
* game.add.tween(p).to({ x: 700 }, 1000, Phaser.Easing.Linear.None, true)
* .to({ y: 300 }, 1000, Phaser.Easing.Linear.None)
* .to({ x: 0 }, 1000, Phaser.Easing.Linear.None)
* .to({ y: 0 }, 1000, Phaser.Easing.Linear.None)
* .loop();
* @method Phaser.Tween#loop
2013-09-21 01:47:58 +00:00
* @return {Tween} Itself.
*/
loop: function() {
2013-09-21 01:47:58 +00:00
if (this._parent) this.chain(this._parent);
return this;
2013-09-21 01:47:58 +00:00
},
2013-10-01 12:54:29 +00:00
/**
* Sets a callback to be fired when the tween starts. Note: callback will be called in the context of the global scope.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#onStartCallback
* @param {function} callback - The callback to invoke on start.
2013-10-01 12:54:29 +00:00
* @return {Phaser.Tween} Itself.
*/
onStartCallback: function ( callback ) {
2013-08-28 23:09:12 +00:00
this._onStartCallback = callback;
return this;
},
2013-10-01 12:54:29 +00:00
/**
* Sets a callback to be fired each time this tween updates. Note: callback will be called in the context of the global scope.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#onUpdateCallback
* @param {function} callback - The callback to invoke each time this tween is updated.
2013-10-01 12:54:29 +00:00
* @return {Phaser.Tween} Itself.
*/
onUpdateCallback: function ( callback ) {
2013-08-28 23:09:12 +00:00
this._onUpdateCallback = callback;
return this;
},
2013-10-01 12:54:29 +00:00
/**
* Sets a callback to be fired when the tween completes. Note: callback will be called in the context of the global scope.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#onCompleteCallback
* @param {function} callback - The callback to invoke on completion.
2013-10-01 12:54:29 +00:00
* @return {Phaser.Tween} Itself.
*/
onCompleteCallback: function ( callback ) {
2013-08-28 23:09:12 +00:00
this._onCompleteCallback = callback;
return this;
},
2013-10-01 12:54:29 +00:00
/**
* Pauses the tween.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#pause
2013-10-01 12:54:29 +00:00
*/
pause: function () {
this._paused = true;
},
2013-10-01 12:54:29 +00:00
/**
* Resumes a paused tween.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#resume
2013-10-01 12:54:29 +00:00
*/
resume: function () {
this._paused = false;
this._startTime += this.game.time.pauseDuration;
},
2013-08-28 23:09:12 +00:00
2013-10-01 12:54:29 +00:00
/**
* Core tween update function called by the TweenManager. Does not need to be invoked directly.
2013-10-01 12:54:29 +00:00
*
* @method Phaser.Tween#update
* @param {number} time - A timestamp passed in by the TweenManager.
* @return {boolean} false if the tween has completed and should be deleted from the manager, otherwise true (still active).
2013-10-01 12:54:29 +00:00
*/
2013-08-28 23:09:12 +00:00
update: function ( time ) {
if (this.pendingDelete)
{
return false;
}
2013-08-28 23:09:12 +00:00
if (this._paused || time < this._startTime) {
return true;
}
var property;
if ( time < this._startTime ) {
return true;
}
if ( this._onStartCallbackFired === false ) {
if ( this._onStartCallback !== null ) {
this._onStartCallback.call( this._object );
}
this._onStartCallbackFired = true;
}
var elapsed = ( time - this._startTime ) / this._duration;
elapsed = elapsed > 1 ? 1 : elapsed;
var value = this._easingFunction( elapsed );
for ( property in this._valuesEnd ) {
var start = this._valuesStart[ property ] || 0;
var end = this._valuesEnd[ property ];
if ( end instanceof Array ) {
this._object[ property ] = this._interpolationFunction( end, value );
} else {
// Parses relative end values with start as base (e.g.: +10, -3)
if ( typeof(end) === "string" ) {
end = start + parseFloat(end, 10);
}
// protect against non numeric properties.
if ( typeof(end) === "number" ) {
this._object[ property ] = start + ( end - start ) * value;
}
}
}
if ( this._onUpdateCallback !== null ) {
this._onUpdateCallback.call( this._object, value );
}
if ( elapsed == 1 ) {
if ( this._repeat > 0 ) {
if ( isFinite( this._repeat ) ) {
this._repeat--;
}
// reassign starting values, restart by making startTime = now
for ( property in this._valuesStartRepeat ) {
if ( typeof( this._valuesEnd[ property ] ) === "string" ) {
this._valuesStartRepeat[ property ] = this._valuesStartRepeat[ property ] + parseFloat(this._valuesEnd[ property ], 10);
}
if (this._yoyo) {
var tmp = this._valuesStartRepeat[ property ];
this._valuesStartRepeat[ property ] = this._valuesEnd[ property ];
this._valuesEnd[ property ] = tmp;
this._reversed = !this._reversed;
}
this._valuesStart[ property ] = this._valuesStartRepeat[ property ];
}
this._startTime = time + this._delayTime;
this.onComplete.dispatch(this._object);
if ( this._onCompleteCallback !== null ) {
this._onCompleteCallback.call( this._object );
}
2013-08-28 23:09:12 +00:00
return true;
} else {
this.onComplete.dispatch(this._object);
2013-08-28 23:09:12 +00:00
if ( this._onCompleteCallback !== null ) {
2013-08-28 23:09:12 +00:00
this._onCompleteCallback.call( this._object );
}
for ( var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i ++ ) {
this._chainedTweens[ i ].start( time );
}
return false;
}
}
return true;
}
};