mirror of
https://github.com/wassname/phaser.git
synced 2026-07-04 17:20:31 +08:00
Added the TweenManager
This commit is contained in:
+1
-1
@@ -184,7 +184,7 @@ Phaser.Game.prototype = {
|
||||
this.cache = new Phaser.Cache(this);
|
||||
this.load = new Phaser.Loader(this);
|
||||
this.time = new Phaser.Time(this);
|
||||
// this.tweens = new Phaser.TweenManager(this);
|
||||
this.tweens = new Phaser.TweenManager(this);
|
||||
// this.input = new Phaser.InputManager(this);
|
||||
// this.sound = new Phaser.SoundManager(this);
|
||||
this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]);
|
||||
|
||||
@@ -0,0 +1,278 @@
|
||||
Phaser.Easing = {
|
||||
|
||||
Linear: {
|
||||
|
||||
None: function ( k ) {
|
||||
|
||||
return k;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Quadratic: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
return k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
return k * ( 2 - k );
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
if ( ( k *= 2 ) < 1 ) return 0.5 * k * k;
|
||||
return - 0.5 * ( --k * ( k - 2 ) - 1 );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Cubic: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
return k * k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
return --k * k * k + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k;
|
||||
return 0.5 * ( ( k -= 2 ) * k * k + 2 );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Quartic: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
return k * k * k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
return 1 - ( --k * k * k * k );
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
if ( ( k *= 2 ) < 1) return 0.5 * k * k * k * k;
|
||||
return - 0.5 * ( ( k -= 2 ) * k * k * k - 2 );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Quintic: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
return k * k * k * k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
return --k * k * k * k * k + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k * k * k;
|
||||
return 0.5 * ( ( k -= 2 ) * k * k * k * k + 2 );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Sinusoidal: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
return 1 - Math.cos( k * Math.PI / 2 );
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
return Math.sin( k * Math.PI / 2 );
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
return 0.5 * ( 1 - Math.cos( Math.PI * k ) );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Exponential: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
return k === 0 ? 0 : Math.pow( 1024, k - 1 );
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
return k === 1 ? 1 : 1 - Math.pow( 2, - 10 * k );
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
if ( k === 0 ) return 0;
|
||||
if ( k === 1 ) return 1;
|
||||
if ( ( k *= 2 ) < 1 ) return 0.5 * Math.pow( 1024, k - 1 );
|
||||
return 0.5 * ( - Math.pow( 2, - 10 * ( k - 1 ) ) + 2 );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Circular: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
return 1 - Math.sqrt( 1 - k * k );
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
return Math.sqrt( 1 - ( --k * k ) );
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
if ( ( k *= 2 ) < 1) return - 0.5 * ( Math.sqrt( 1 - k * k) - 1);
|
||||
return 0.5 * ( Math.sqrt( 1 - ( k -= 2) * k) + 1);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Elastic: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
var s, a = 0.1, p = 0.4;
|
||||
if ( k === 0 ) return 0;
|
||||
if ( k === 1 ) return 1;
|
||||
if ( !a || a < 1 ) { a = 1; s = p / 4; }
|
||||
else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
|
||||
return - ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
var s, a = 0.1, p = 0.4;
|
||||
if ( k === 0 ) return 0;
|
||||
if ( k === 1 ) return 1;
|
||||
if ( !a || a < 1 ) { a = 1; s = p / 4; }
|
||||
else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
|
||||
return ( a * Math.pow( 2, - 10 * k) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) + 1 );
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
var s, a = 0.1, p = 0.4;
|
||||
if ( k === 0 ) return 0;
|
||||
if ( k === 1 ) return 1;
|
||||
if ( !a || a < 1 ) { a = 1; s = p / 4; }
|
||||
else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
|
||||
if ( ( k *= 2 ) < 1 ) return - 0.5 * ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
|
||||
return a * Math.pow( 2, -10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) * 0.5 + 1;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Back: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
var s = 1.70158;
|
||||
return k * k * ( ( s + 1 ) * k - s );
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
var s = 1.70158;
|
||||
return --k * k * ( ( s + 1 ) * k + s ) + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
var s = 1.70158 * 1.525;
|
||||
if ( ( k *= 2 ) < 1 ) return 0.5 * ( k * k * ( ( s + 1 ) * k - s ) );
|
||||
return 0.5 * ( ( k -= 2 ) * k * ( ( s + 1 ) * k + s ) + 2 );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Bounce: {
|
||||
|
||||
In: function ( k ) {
|
||||
|
||||
return 1 - TWEEN.Easing.Bounce.Out( 1 - k );
|
||||
|
||||
},
|
||||
|
||||
Out: function ( k ) {
|
||||
|
||||
if ( k < ( 1 / 2.75 ) ) {
|
||||
|
||||
return 7.5625 * k * k;
|
||||
|
||||
} else if ( k < ( 2 / 2.75 ) ) {
|
||||
|
||||
return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
|
||||
|
||||
} else if ( k < ( 2.5 / 2.75 ) ) {
|
||||
|
||||
return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
|
||||
|
||||
} else {
|
||||
|
||||
return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
InOut: function ( k ) {
|
||||
|
||||
if ( k < 0.5 ) return TWEEN.Easing.Bounce.In( k * 2 ) * 0.5;
|
||||
return TWEEN.Easing.Bounce.Out( k * 2 - 1 ) * 0.5 + 0.5;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
@@ -0,0 +1,345 @@
|
||||
/**
|
||||
* Tween constructor
|
||||
* Create a new <code>Tween</code>.
|
||||
*
|
||||
* @param object {object} Target object will be affected by this tween.
|
||||
* @param game {Phaser.Game} Current game instance.
|
||||
*/
|
||||
|
||||
Phaser.Tween = function (object, game) {
|
||||
|
||||
/**
|
||||
* Reference to the target object.
|
||||
* @type {object}
|
||||
*/
|
||||
this._object = object;
|
||||
|
||||
this.game = game;
|
||||
this._manager = this.game.tweens;
|
||||
|
||||
this._valuesStart = {};
|
||||
this._valuesEnd = {};
|
||||
this._valuesStartRepeat = {};
|
||||
this._duration = 1000;
|
||||
this._repeat = 0;
|
||||
this._yoyo = false;
|
||||
this._reversed = false;
|
||||
this._delayTime = 0;
|
||||
this._startTime = null;
|
||||
this._easingFunction = Phaser.Easing.Linear.None;
|
||||
this._interpolationFunction = Phaser.Math.linearInterpolation;
|
||||
this._chainedTweens = [];
|
||||
this._onStartCallback = null;
|
||||
this._onStartCallbackFired = false;
|
||||
this._onUpdateCallback = null;
|
||||
this._onCompleteCallback = null;
|
||||
|
||||
this._pausedTime = 0;
|
||||
|
||||
// Set all starting values present on the target object
|
||||
for ( var field in object ) {
|
||||
this._valuesStart[ field ] = parseFloat(object[field], 10);
|
||||
}
|
||||
|
||||
// this.onStart = new Phaser.Signal();
|
||||
// this.onUpdate = new Phaser.Signal();
|
||||
this.onComplete = new Phaser.Signal();
|
||||
|
||||
this.isRunning = false;
|
||||
|
||||
};
|
||||
|
||||
Phaser.Tween.prototype = {
|
||||
|
||||
/**
|
||||
* Configure the Tween
|
||||
* @param properties {object} Propertis you want to tween.
|
||||
* @param [duration] {number} duration of this tween.
|
||||
* @param [ease] {any} Easing function.
|
||||
* @param [autoStart] {bool} Whether this tween will start automatically or not.
|
||||
* @param [delay] {number} delay before this tween will start, defaults to 0 (no delay)
|
||||
* @param [loop] {bool} Should the tween automatically restart once complete? (ignores any chained tweens)
|
||||
* @return {Tween} Itself.
|
||||
*/
|
||||
to: function ( properties, duration, ease, autoStart, delay, repeat, yoyo ) {
|
||||
|
||||
if (typeof duration === "undefined") { duration = 1000; }
|
||||
if (typeof ease === "undefined") { ease = null; }
|
||||
if (typeof autoStart === "undefined") { autoStart = false; }
|
||||
if (typeof delay === "undefined") { delay = 0; }
|
||||
if (typeof repeat === "undefined") { repeat = 0; }
|
||||
if (typeof yoyo === "undefined") { yoyo = false; }
|
||||
|
||||
this._duration = duration;
|
||||
this._valuesEnd = properties;
|
||||
|
||||
if (ease !== null) {
|
||||
this._easingFunction = ease;
|
||||
}
|
||||
|
||||
if (delay > 0) {
|
||||
this._delayTime = delay;
|
||||
}
|
||||
|
||||
this._yoyo = yoyo;
|
||||
|
||||
if (autoStart) {
|
||||
return this.start();
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
start: function ( time ) {
|
||||
|
||||
//TWEEN.add( this );
|
||||
|
||||
if (this.game === null || this._object === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.isRunning = true;
|
||||
|
||||
this._onStartCallbackFired = false;
|
||||
|
||||
// this._startTime = time !== undefined ? time : ( typeof window !== 'undefined' && window.performance !== undefined && window.performance.now !== undefined ? window.performance.now() : Date.now() );
|
||||
// this._startTime += _delayTime;
|
||||
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;
|
||||
|
||||
},
|
||||
|
||||
stop: function () {
|
||||
|
||||
//TWEEN.remove( this );
|
||||
if (this._manager !== null) {
|
||||
this._manager.remove(this);
|
||||
}
|
||||
|
||||
this.isRunning = false;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
delay: function ( amount ) {
|
||||
|
||||
this._delayTime = amount;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
repeat: function ( times ) {
|
||||
|
||||
this._repeat = times;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
yoyo: function( yoyo ) {
|
||||
|
||||
this._yoyo = yoyo;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
|
||||
easing: function ( easing ) {
|
||||
|
||||
this._easingFunction = easing;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
interpolation: function ( interpolation ) {
|
||||
|
||||
this._interpolationFunction = interpolation;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
chain: function () {
|
||||
|
||||
this._chainedTweens = arguments;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
onStart: function ( callback ) {
|
||||
|
||||
this._onStartCallback = callback;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
onUpdate: function ( callback ) {
|
||||
|
||||
this._onUpdateCallback = callback;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
onComplete: function ( callback ) {
|
||||
|
||||
this._onCompleteCallback = callback;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
pause: function () {
|
||||
this._paused = true;
|
||||
},
|
||||
|
||||
resume: function () {
|
||||
this._paused = false;
|
||||
this._startTime += this.game.time.pauseDuration;
|
||||
},
|
||||
|
||||
update: function ( time ) {
|
||||
|
||||
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;
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
if ( this._onCompleteCallback !== null ) {
|
||||
|
||||
this.onComplete.dispatch(this._object);
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -96,17 +96,15 @@ Phaser.TweenManager.prototype = {
|
||||
*
|
||||
* @return {bool} Return false if there's no tween to update, otherwise return true.
|
||||
*/
|
||||
update: function ( time ) {
|
||||
update: function () {
|
||||
|
||||
if ( _tweens.length === 0 ) return false;
|
||||
|
||||
var i = 0, numTweens = _tweens.length;
|
||||
|
||||
time = time !== undefined ? time : ( typeof window !== 'undefined' && window.performance !== undefined && window.performance.now !== undefined ? window.performance.now() : Date.now() );
|
||||
|
||||
while ( i < numTweens ) {
|
||||
|
||||
if ( _tweens[ i ].update( time ) ) {
|
||||
if ( _tweens[ i ].update( this.game.time.now ) ) {
|
||||
|
||||
i ++;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user