phaser/TS Source/tweens/TweenManager.ts

184 lines
4.5 KiB
TypeScript
Raw Normal View History

/// <reference path="../_definitions.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).
* 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 {
/**
* TweenManager constructor
* @param game {Game} A reference to the current Game.
*/
2013-04-18 13:16:18 +00:00
constructor(game: Phaser.Game) {
this.game = game;
2013-04-18 13:16:18 +00:00
this._tweens = [];
this.game.onPause.add(this.pauseAll, this);
this.game.onResume.add(this.resumeAll, this);
2013-04-18 13:16:18 +00:00
}
/**
* Local reference to Game
*/
public game: Phaser.Game;
2013-05-22 23:01:58 +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[];
/**
* Get all the tween objects in an array.
* @return {Phaser.Tween[]} Array with all tween objects.
*/
2013-04-18 13:16:18 +00:00
public getAll() {
return this._tweens;
}
/**
* Remove all tween objects.
*/
2013-04-18 13:16:18 +00:00
public removeAll() {
this._tweens.length = 0;
}
/**
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-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.
* @return {Phaser.Tween} The newly created tween object.
*/
public create(object, localReference:bool = false): Phaser.Tween {
2013-07-13 11:38:59 +00:00
if (localReference)
{
object['tween'] = new Phaser.Tween(object, this.game);
2013-07-13 11:38:59 +00:00
return object['tween'];
}
else
{
return new Phaser.Tween(object, this.game);
2013-07-13 11:38:59 +00:00
}
2013-04-18 13:16:18 +00:00
}
/**
2013-07-13 11:38:59 +00:00
* Add a new tween into the TweenManager.
*
* @param tween {Phaser.Tween} The tween object you want to add.
* @return {Phaser.Tween} The tween object you added to the manager.
*/
2013-04-18 13:16:18 +00:00
public add(tween: Phaser.Tween) {
tween.parent = this.game;
2013-04-18 13:16:18 +00:00
this._tweens.push(tween);
return tween;
}
/**
* Remove a tween from this manager.
*
* @param tween {Phaser.Tween} The tween object you want to remove.
*/
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);
}
}
/**
* Update all the tween objects you added to this manager.
*
* @return {bool} 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)
{
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;
}
public pauseAll() {
if (this._tweens.length === 0)
{
return false;
}
var i = 0;
var numTweens = this._tweens.length;
while (i < numTweens)
{
this._tweens[i].pause();
i++;
}
}
public resumeAll() {
if (this._tweens.length === 0)
{
return false;
}
var i = 0;
var numTweens = this._tweens.length;
while (i < numTweens)
{
this._tweens[i].resume();
i++;
}
}
2013-04-18 13:16:18 +00:00
}
}