2013-05-28 20:38:37 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
|
|
|
/// <reference path="Tween.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - TweenManager
|
|
|
|
*
|
|
|
|
* The 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.
|
|
|
|
* TweenManager is based heavily on tween.js by sole (http://soledadpenades.com).
|
2013-05-03 11:22:04 +00:00
|
|
|
* I converted it to TypeScript, swapped the callbacks for signals and patched a few issues with regard
|
2013-04-18 15:49:08 +00:00
|
|
|
* to properties and completion errors. Please see https://github.com/sole/tween.js for a full list of contributors.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
module Phaser {
|
|
|
|
|
|
|
|
export class TweenManager {
|
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* TweenManager constructor
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param game {Game} A reference to the current Game.
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
constructor(game: Phaser.Game) {
|
|
|
|
|
2013-08-08 03:35:13 +00:00
|
|
|
this.game = game;
|
2013-04-18 13:16:18 +00:00
|
|
|
this._tweens = [];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
2013-08-08 03:35:13 +00:00
|
|
|
* Local reference to Game
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-08-08 03:35:13 +00:00
|
|
|
public game: Phaser.Game;
|
2013-05-22 23:01:58 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Local private array which is the container of all tween objects.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _tweens: Phaser.Tween[];
|
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Get all the tween objects in an array.
|
2013-05-04 11:34:05 +00:00
|
|
|
* @return {Phaser.Tween[]} Array with all tween objects.
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public getAll() {
|
|
|
|
|
|
|
|
return this._tweens;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Remove all tween objects.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public removeAll() {
|
|
|
|
|
|
|
|
this._tweens.length = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
2013-07-13 11:38:59 +00:00
|
|
|
* Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite.
|
2013-05-03 11:22:04 +00:00
|
|
|
*
|
2013-07-13 11:38:59 +00:00
|
|
|
* @param obj {object} Object the tween will be run on.
|
|
|
|
* @param [localReference] {bool} If true the tween will be stored in the object.tween property so long as it exists. If already set it'll be over-written.
|
2013-05-04 11:34:05 +00:00
|
|
|
* @return {Phaser.Tween} The newly created tween object.
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-07-13 11:38:59 +00:00
|
|
|
public create(object, localReference?:bool = false): Phaser.Tween {
|
|
|
|
|
|
|
|
if (localReference)
|
|
|
|
{
|
2013-08-08 03:35:13 +00:00
|
|
|
object['tween'] = new Phaser.Tween(object, this.game);
|
2013-07-13 11:38:59 +00:00
|
|
|
return object['tween'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-08 03:35:13 +00:00
|
|
|
return new Phaser.Tween(object, this.game);
|
2013-07-13 11:38:59 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
2013-07-13 11:38:59 +00:00
|
|
|
* Add a new tween into the TweenManager.
|
2013-05-03 11:22:04 +00:00
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param tween {Phaser.Tween} The tween object you want to add.
|
2013-05-03 11:22:04 +00:00
|
|
|
* @return {Phaser.Tween} The tween object you added to the manager.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public add(tween: Phaser.Tween) {
|
|
|
|
|
2013-08-08 03:35:13 +00:00
|
|
|
tween.parent = this.game;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
this._tweens.push(tween);
|
|
|
|
|
|
|
|
return tween;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Remove a tween from this manager.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param tween {Phaser.Tween} The tween object you want to remove.
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public remove(tween: Phaser.Tween) {
|
|
|
|
|
|
|
|
var i = this._tweens.indexOf(tween);
|
|
|
|
|
|
|
|
if (i !== -1)
|
|
|
|
{
|
|
|
|
this._tweens.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Update all the tween objects you added to this manager.
|
|
|
|
*
|
|
|
|
* @return {boolean} Return false if there's no tween to update, otherwise return true.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public update() {
|
|
|
|
|
|
|
|
if (this._tweens.length === 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
var numTweens = this._tweens.length;
|
|
|
|
|
|
|
|
while (i < numTweens)
|
|
|
|
{
|
2013-08-08 03:35:13 +00:00
|
|
|
if (this._tweens[i].update(this.game.time.now))
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._tweens.splice(i, 1);
|
|
|
|
numTweens--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|