phaser/Phaser/CameraManager.ts

89 lines
2.1 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.
*
* TODO: If the Camera is larger than the Stage size then the rotation offset isn't correct
* TODO: Texture Repeat doesn't scroll, because it's part of the camera not the world, need to think about this more
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
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
2013-04-18 13:16:18 +00:00
private _game: Game;
private _cameras: Camera[];
private _cameraInstance: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public current: Camera;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public getAll(): Camera[] {
return this._cameras;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public update() {
this._cameras.forEach((camera) => camera.update());
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public render() {
this._cameras.forEach((camera) => camera.render());
}
2013-04-12 16:19:56 +00:00
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
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
}
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
}