phaser/Phaser/World.ts

190 lines
4.9 KiB
TypeScript
Raw Normal View History

2013-04-12 16:19:56 +00:00
/// <reference path="Game.ts" />
2013-05-28 20:38:37 +00:00
/// <reference path="cameras/CameraManager.ts" />
/// <reference path="core/Group.ts" />
/// <reference path="geom/Rectangle.ts" />
/// <reference path="physics/PhysicsManager.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 - World
*
2013-05-20 05:21:12 +00:00
* "This world is but a canvas to our imagination." - Henry David Thoreau
*
2013-04-18 15:49:08 +00:00
* A game has only one world. The world is an abstract place in which all game objects live. It is not bound
* by stage limits and can be any size or dimension. You look into the world via cameras and all game objects
* live within the world at world-based coordinates. By default a world is created the same size as your Stage.
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 13:16:18 +00:00
export class World {
2013-04-12 16:19:56 +00:00
/**
* World constructor
* Create a new <code>World</code> with specific width and height.
*
* @param width {number} Width of the world bound.
* @param height {number} Height of the world bound.
*/
2013-04-18 13:16:18 +00:00
constructor(game: Game, 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-28 20:40:06 +00:00
this.cameras = new CameraManager(this._game, 0, 0, width, height);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.bounds = new Rectangle(0, 0, width, height);
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-04-12 16:19:56 +00:00
/**
* Camera manager of this world.
* @type {CameraManager}
*/
2013-04-28 20:40:06 +00:00
public cameras: CameraManager;
/**
* Object container stores every object created with `create*` methods.
* @type {Group}
*/
2013-04-18 13:16:18 +00:00
public group: Group;
/**
* Bound of this world that objects can not escape from.
* @type {Rectangle}
*/
2013-04-18 13:16:18 +00:00
public bounds: Rectangle;
/**
* Reference to the physics manager.
* @type {Physics.PhysicsManager}
*/
public physics: Physics.PhysicsManager;
/**
* Object container stores every object created with `create*` methods.
* @type {Group}
*/
private _groupCounter: number = 0;
public getNextGroupID(): number {
return this._groupCounter++;
}
/**
* Called once by Game during the boot process.
*/
public boot() {
this.group = new Group(this._game, 0);
this.physics = new Physics.PhysicsManager(this._game, this.width, this.height);
}
2013-04-12 16:19:56 +00:00
/**
2013-05-28 20:38:37 +00:00
* This is called automatically every frame, and is where main logic happens.
*/
2013-04-18 13:16:18 +00:00
public update() {
2013-04-12 16:19:56 +00:00
//this.physics.update();
2013-04-18 13:16:18 +00:00
this.group.update();
2013-04-28 20:40:06 +00:00
this.cameras.update();
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* This is called automatically every frame, and is where main logic happens.
*/
public postUpdate() {
//this.physics.postUpdate();
this.group.postUpdate();
this.cameras.postUpdate();
}
/**
* Clean up memory.
*/
2013-04-18 13:16:18 +00:00
public destroy() {
2013-04-12 16:19:56 +00:00
//this.physics.destroy();
2013-04-18 13:16:18 +00:00
this.group.destroy();
2013-04-28 20:40:06 +00:00
this.cameras.destroy();
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
2013-05-28 20:38:37 +00:00
* Updates the size of this world.
*
* @param width {number} New width of the world.
* @param height {number} New height of the world.
2013-05-28 20:38:37 +00:00
* @param [updateCameraBounds] {boolean} Update camera bounds automatically or not. Default to true.
*/
public setSize(width: number, height: number, updateCameraBounds: bool = true, updatePhysicsBounds: bool = true) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.bounds.width = width;
this.bounds.height = height;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (updateCameraBounds == true)
{
this._game.camera.setBounds(0, 0, width, height);
}
2013-04-12 16:19:56 +00:00
if (updatePhysicsBounds == true)
{
//this.physics.bounds.copyFrom(this.bounds);
}
2013-05-28 20:38:37 +00:00
// dispatch world resize event
2013-05-22 23:01:58 +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
public get width(): number {
return this.bounds.width;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public set width(value: number) {
this.bounds.width = value;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public get height(): number {
return this.bounds.height;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public set height(value: number) {
this.bounds.height = value;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public get centerX(): number {
return this.bounds.halfWidth;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public get centerY(): number {
return this.bounds.halfHeight;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public get randomX(): number {
return Math.round(Math.random() * this.bounds.width);
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public get randomY(): number {
return Math.round(Math.random() * this.bounds.height);
}
2013-04-12 16:19:56 +00:00
/**
* Get all the cameras.
*
* @returns {array} An array contains all the cameras.
*/
2013-04-18 13:16:18 +00:00
public getAllCameras(): Camera[] {
2013-04-28 20:40:06 +00:00
return this.cameras.getAll();
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
}
}