2013-04-12 16:19:56 +00:00
|
|
|
/// <reference path="Game.ts" />
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - World
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* World constructor
|
|
|
|
* Create a new <code>World</code> with specific width and height.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param width {number} Width of the world bound.
|
|
|
|
* @param height {number} Height of the world bound.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
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-28 20:40:06 +00:00
|
|
|
this._game.camera = this.cameras.current;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.group = new Group(this._game, 0);
|
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
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.worldDivisions = 6;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-14 01:31:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +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
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Camera manager of this world.
|
|
|
|
* @type {CameraManager}
|
|
|
|
*/
|
2013-04-28 20:40:06 +00:00
|
|
|
public cameras: CameraManager;
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Object container stores every object created with `create*` methods.
|
|
|
|
* @type {Group}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public group: Group;
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Bound of this world that objects can not escape from.
|
|
|
|
* @type {Rectangle}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public bounds: Rectangle;
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public worldDivisions: number;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* This is called automatically every frame, and is where main logic performs.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public update() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.group.preUpdate();
|
|
|
|
this.group.update();
|
|
|
|
this.group.postUpdate();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
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
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Render every thing to the screen, automatically called after update().
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public render() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Unlike in flixel our render process is camera driven, not group driven
|
2013-04-28 20:40:06 +00:00
|
|
|
this.cameras.render();
|
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:12:12 +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.group.destroy();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-28 20:40:06 +00:00
|
|
|
this.cameras.destroy();
|
2013-04-15 21:13:11 +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
|
|
|
// World methods
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Update size of this world with specific width and height.
|
|
|
|
* You can choose update camera bounds automatically or not.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param width {number} New width of the world.
|
|
|
|
* @param height {number} New height of the world.
|
|
|
|
* @param updateCameraBounds {boolean} Optinal, update camera bounds automatically or not. Default to true.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public setSize(width: number, height: number, updateCameraBounds: 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
|
|
|
|
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
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Cameras
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new camera with specific position and size.
|
|
|
|
*
|
2013-05-04 11:34:05 +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:12:12 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public createCamera(x: number, y: number, width: number, height: number): Camera {
|
2013-04-28 20:40:06 +00:00
|
|
|
return this.cameras.addCamera(x, y, width, height);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Remove a new camera with its id.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param id {number} ID of the camera you want to remove.
|
|
|
|
* @returns {boolean} True if successfully removed the camera, otherwise return false.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public removeCamera(id: number): bool {
|
2013-04-28 20:40:06 +00:00
|
|
|
return this.cameras.removeCamera(id);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +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
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
// Game Objects
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new Sprite with specific position and sprite sheet key.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param x {number} X position of the new sprite.
|
|
|
|
* @param y {number} Y position of the new sprite.
|
|
|
|
* @param key {string} Optinal, key for the sprite sheet you want it to use.
|
|
|
|
* @returns {Sprite} The newly created sprite object.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public createSprite(x: number, y: number, key?: string = ''): Sprite {
|
|
|
|
return <Sprite> this.group.add(new Sprite(this._game, x, y, key));
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new GeomSprite with specific position.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param x {number} X position of the new geom sprite.
|
|
|
|
* @param y {number} Y position of the new geom sprite.
|
|
|
|
* @returns {GeomSprite} The newly created geom sprite object.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public createGeomSprite(x: number, y: number): GeomSprite {
|
|
|
|
return <GeomSprite> this.group.add(new GeomSprite(this._game, x, y));
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new DynamicTexture with specific size.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param width {number} Width of the texture.
|
|
|
|
* @param height {number} Height of the texture.
|
|
|
|
* @returns {DynamicTexture} The newly created dynamic texture object.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-22 00:53:24 +00:00
|
|
|
public createDynamicTexture(width: number, height: number): DynamicTexture {
|
|
|
|
return new DynamicTexture(this._game, width, height);
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new object container.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param MaxSize {number} Optinal, capacity of this group.
|
|
|
|
* @returns {Group} The newly created group.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public createGroup(MaxSize?: number = 0): Group {
|
|
|
|
return <Group> this.group.add(new Group(this._game, MaxSize));
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new ScrollZone object with image key, position and size.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param key {number} Key to a image you wish this object to use.
|
|
|
|
* @param x {number} X position of this object.
|
|
|
|
* @param y {number} Y position of this object.
|
|
|
|
* @param width {number} Width of this object.
|
|
|
|
* @param height {number} Heigth of this object.
|
|
|
|
* @returns {ScrollZone} The newly created scroll zone object.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-23 14:15:34 +00:00
|
|
|
public createScrollZone(key: string, x?: number = 0, y?: number = 0, width?: number = 0, height?: number = 0): ScrollZone {
|
2013-04-22 00:53:24 +00:00
|
|
|
return <ScrollZone> this.group.add(new ScrollZone(this._game, key, x, y, width, height));
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new Tilemap.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param key {string} Key for tileset image.
|
|
|
|
* @param mapData {string} Data of this tilemap.
|
|
|
|
* @param format {number} Format of map data. (Tilemap.FORMAT_CSV or Tilemap.FORMAT_TILED_JSON)
|
|
|
|
* @param resizeWorld {boolean} Optinal, resize the world to make same as tilemap?
|
|
|
|
* @param tileWidth {number} Optinal, width of each tile.
|
|
|
|
* @param tileHeight {number} Optinal, height of each tile.
|
|
|
|
* @return {Tilemap} The newly created tilemap object.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-22 00:53:24 +00:00
|
|
|
public createTilemap(key: string, mapData: string, format: number, resizeWorld: bool = true, tileWidth?: number = 0, tileHeight?: number = 0): Tilemap {
|
|
|
|
return <Tilemap> this.group.add(new Tilemap(this._game, key, mapData, format, resizeWorld, tileWidth, tileHeight));
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new Particle.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @return {Particle} The newly created particle object.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public createParticle(): Particle {
|
|
|
|
return new Particle(this._game);
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new Emitter.
|
|
|
|
*
|
2013-05-04 11:34:05 +00:00
|
|
|
* @param x {number} Optinal, x position of the emitter.
|
|
|
|
* @param y {number} Optinal, y position of the emitter.
|
|
|
|
* @param size {number} Optinal, size of this emitter.
|
|
|
|
* @return {Emitter} The newly created emitter object.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public createEmitter(x?: number = 0, y?: number = 0, size?: number = 0): Emitter {
|
|
|
|
return <Emitter> this.group.add(new Emitter(this._game, x, y, size));
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|