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
|
|
|
|
2013-04-18 14:48:06 +00:00
|
|
|
export class CameraManager {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* CameraManager constructor
|
|
|
|
* This will create a new <code>Camera</code> with position and size.
|
|
|
|
*
|
2013-05-04 11:46:19 +00:00
|
|
|
* @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-05-03 11:22:04 +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-08-08 03:35:13 +00:00
|
|
|
this.game = game;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._cameras = [];
|
2013-08-08 02:05:59 +00:00
|
|
|
this._cameraLength = 0;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-07-19 01:59:23 +00:00
|
|
|
this.defaultCamera = this.addCamera(x, y, width, height);
|
2013-08-08 00:07:22 +00:00
|
|
|
|
2013-07-19 01:59:23 +00:00
|
|
|
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
|
|
|
|
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: Game;
|
2013-05-16 20:34:24 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
2013-05-16 20:34:24 +00:00
|
|
|
* Local container for storing cameras.
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _cameras: Camera[];
|
2013-05-16 20:34:24 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
2013-08-08 02:05:59 +00:00
|
|
|
* Local container for storing cameras array length.
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-08-08 02:05:59 +00:00
|
|
|
private _cameraLength: number;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
/**
|
|
|
|
* Helper for sort.
|
|
|
|
*/
|
|
|
|
private _sortIndex: string = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper for sort.
|
|
|
|
*/
|
|
|
|
private _sortOrder: number;
|
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Currently used camera.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public current: Camera;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-06-03 02:08:24 +00:00
|
|
|
/**
|
|
|
|
* The default created camera.
|
|
|
|
*/
|
2013-07-19 01:59:23 +00:00
|
|
|
public defaultCamera: Camera;
|
2013-06-03 02:08:24 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Get all the cameras.
|
|
|
|
*
|
2013-05-04 11:46:19 +00:00
|
|
|
* @returns {Camera[]} An array contains all the cameras.
|
2013-05-03 11:22:04 +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-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Update cameras.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public update() {
|
2013-06-07 06:35:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Create a new camera with specific position and size.
|
|
|
|
*
|
2013-05-04 11:46:19 +00:00
|
|
|
* @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-03 11:22:04 +00:00
|
|
|
*/
|
2013-08-08 00:07:22 +00:00
|
|
|
public addCamera(x: number, y: number, width: number, height: number): Camera {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-08-08 03:35:13 +00:00
|
|
|
var newCam: Camera = new Camera(this.game, this._cameraLength, x, y, width, height);
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-08-08 02:05:59 +00:00
|
|
|
this._cameraLength = this._cameras.push(newCam);
|
2013-04-24 01:48:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return newCam;
|
|
|
|
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Remove a new camera with its id.
|
|
|
|
*
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param id {number} ID of the camera you want to remove.
|
2013-05-03 11:22:04 +00:00
|
|
|
* @returns {boolean} True if successfully removed the camera, otherwise return false.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public removeCamera(id: number): bool {
|
|
|
|
|
2013-04-24 01:48:03 +00:00
|
|
|
for (var c = 0; c < this._cameras.length; c++)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-24 01:48:03 +00:00
|
|
|
if (this._cameras[c].ID == id)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-04-24 01:48:03 +00:00
|
|
|
if (this.current.ID === this._cameras[c].ID)
|
|
|
|
{
|
|
|
|
this.current = null;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-24 01:48:03 +00:00
|
|
|
this._cameras.splice(c, 1);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-24 01:48:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-24 01:48:03 +00:00
|
|
|
return false;
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 01:47:08 +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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-08 02:05:59 +00:00
|
|
|
public getCameraUnderPoint(x: number, y: number): Camera {
|
|
|
|
|
|
|
|
// Work through the cameras in reverse as they are rendered in array order
|
|
|
|
// Return the first camera we find matching the criteria
|
|
|
|
for (var c = this._cameraLength - 1; c >= 0; c--)
|
|
|
|
{
|
|
|
|
if (this._cameras[c].visible && Phaser.RectangleUtils.contains(this._cameras[c].screenView, x, y))
|
|
|
|
{
|
|
|
|
return this._cameras[c];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:22:04 +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-08-08 03:35:13 +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
|
|
|
}
|