phaser/Phaser/CameraManager.ts

138 lines
3.5 KiB
TypeScript
Raw Normal View History

2013-04-12 16:19:56 +00:00
/// <reference path="Game.ts" />
/// <reference path="system/Camera.ts" />
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
2013-04-18 13:16:18 +00:00
this.current = this.addCamera(x, y, width, 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
/**
* 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
/**
* Currently used camera.
*/
2013-04-18 13:16:18 +00:00
public current: Camera;
2013-04-12 16:19:56 +00:00
/**
* 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() {
this._cameras.forEach((camera) => camera.update());
}
2013-04-12 16:19:56 +00:00
/**
* Render cameras.
*/
2013-04-18 13:16:18 +00:00
public render() {
this._cameras.forEach((camera) => camera.render());
}
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-04-18 13:16:18 +00:00
public addCamera(x: number, y: number, width: number, height: number): 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
}
/**
* 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
}