phaser/Phaser/cameras/CameraManager.ts

226 lines
6 KiB
TypeScript
Raw Normal View History

2013-05-28 20:38:37 +00:00
/// <reference path="../Game.ts" />
/// <reference path="Camera.ts" />
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
2013-04-18 15:49:08 +00:00
* Phaser - CameraManager
*
* Your game only has one CameraManager instance and it's responsible for looking after, creating and destroying
* all of the cameras in the world.
2013-04-18 13:16:18 +00:00
*/
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
module Phaser {
2013-04-12 16:19:56 +00:00
export class CameraManager {
2013-04-12 16:19:56 +00:00
/**
* CameraManager constructor
* This will create a new <code>Camera</code> with position and size.
*
* @param x {number} X Position of the created camera.
* @param y {number} y Position of the created camera.
* @param width {number} Width of the created camera.
* @param height {number} Height of the created camera.
*/
2013-04-18 13:16:18 +00:00
constructor(game: Game, x: number, y: number, width: number, height: number) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game = game;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._cameras = [];
2013-04-12 16:19:56 +00:00
this.defaultCamera = this.addCamera(x, y, width, height);
this.current = this.defaultCamera;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* Local private reference to Game.
*/
2013-04-18 13:16:18 +00:00
private _game: Game;
2013-05-16 20:34:24 +00:00
/**
2013-05-16 20:34:24 +00:00
* Local container for storing cameras.
*/
2013-04-18 13:16:18 +00:00
private _cameras: Camera[];
2013-05-16 20:34:24 +00:00
/**
* Local helper stores index of next created camera.
*/
private _cameraInstance: number = 0;
2013-04-12 16:19:56 +00:00
/**
* Helper for sort.
*/
private _sortIndex: string = '';
/**
* Helper for sort.
*/
private _sortOrder: number;
2013-05-28 20:38:37 +00:00
public static CAMERA_TYPE_ORTHOGRAPHIC: number = 0;
public static CAMERA_TYPE_ISOMETRIC: number = 1;
/**
* Currently used camera.
*/
2013-04-18 13:16:18 +00:00
public current: Camera;
2013-04-12 16:19:56 +00:00
/**
* The default created camera.
*/
public defaultCamera: Camera;
/**
* Get all the cameras.
*
* @returns {Camera[]} An array contains all the cameras.
*/
2013-04-18 13:16:18 +00:00
public getAll(): Camera[] {
return this._cameras;
}
2013-04-12 16:19:56 +00:00
/**
* Update cameras.
*/
2013-04-18 13:16:18 +00:00
public update() {
for (var i = 0; i < this._cameras.length; i++)
{
this._cameras[i].update();
}
}
/**
* postUpdate cameras.
*/
public postUpdate() {
for (var i = 0; i < this._cameras.length; i++)
{
this._cameras[i].postUpdate();
}
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* Create a new camera with specific position and size.
*
* @param x {number} X position of the new camera.
* @param y {number} Y position of the new camera.
* @param width {number} Width of the new camera.
* @param height {number} Height of the new camera.
* @returns {Camera} The newly created camera object.
*/
2013-05-28 20:38:37 +00:00
public addCamera(x: number, y: number, width: number, height: number, type: number = CameraManager.CAMERA_TYPE_ORTHOGRAPHIC): Camera {
2013-04-12 16:19:56 +00:00
var newCam: Camera = new Camera(this._game, this._cameraInstance, x, y, width, height);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._cameras.push(newCam);
2013-04-12 16:19:56 +00:00
this._cameraInstance++;
2013-04-18 13:16:18 +00:00
return newCam;
}
2013-04-12 16:19:56 +00:00
/**
* Remove a new camera with its id.
*
* @param id {number} ID of the camera you want to remove.
* @returns {boolean} True if successfully removed the camera, otherwise return false.
*/
2013-04-18 13:16:18 +00:00
public removeCamera(id: number): bool {
for (var c = 0; c < this._cameras.length; c++)
2013-04-12 16:19:56 +00:00
{
if (this._cameras[c].ID == id)
2013-04-18 13:16:18 +00:00
{
if (this.current.ID === this._cameras[c].ID)
{
this.current = null;
}
2013-04-12 16:19:56 +00:00
this._cameras.splice(c, 1);
2013-04-18 13:16:18 +00:00
return true;
}
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
return false;
2013-04-12 16:19:56 +00:00
}
public swap(camera1: Camera, camera2: Camera, sort?: bool = true): bool {
if (camera1.ID == camera2.ID)
{
return false;
}
var tempZ: number = camera1.z;
camera1.z = camera2.z;
camera2.z = tempZ;
if (sort)
{
this.sort();
}
return true;
}
/**
* Call this function to sort the Cameras according to a particular value and order (default is their Z value).
* The order in which they are sorted determines the render order. If sorted on z then Cameras with a lower z-index value render first.
*
* @param {string} index The <code>string</code> name of the Camera variable you want to sort on. Default value is "z".
* @param {number} order A <code>Group</code> constant that defines the sort order. Possible values are <code>Group.ASCENDING</code> and <code>Group.DESCENDING</code>. Default value is <code>Group.ASCENDING</code>.
*/
public sort(index: string = 'z', order: number = Group.ASCENDING) {
this._sortIndex = index;
this._sortOrder = order;
this._cameras.sort((a, b) => this.sortHandler(a, b));
}
/**
* Helper function for the sort process.
*
* @param {Basic} Obj1 The first object being sorted.
* @param {Basic} Obj2 The second object being sorted.
*
* @return {number} An integer value: -1 (Obj1 before Obj2), 0 (same), or 1 (Obj1 after Obj2).
*/
public sortHandler(obj1, obj2): number {
if (obj1[this._sortIndex] < obj2[this._sortIndex])
{
return this._sortOrder;
}
else if (obj1[this._sortIndex] > obj2[this._sortIndex])
{
return -this._sortOrder;
}
return 0;
}
/**
* Clean up memory.
*/
2013-04-18 13:16:18 +00:00
public destroy() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._cameras.length = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.current = this.addCamera(0, 0, this._game.stage.width, this._game.stage.height);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
}