phaser/Phaser/World.ts

169 lines
4.7 KiB
TypeScript
Raw Normal View History

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-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
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._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-18 13:16:18 +00:00
private _game: Game;
private _cameras: CameraManager;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public group: Group;
public bounds: Rectangle;
public worldDivisions: number;
2013-04-12 16:19:56 +00:00
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-18 13:16:18 +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-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
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-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-18 13:16:18 +00:00
this._cameras.destroy();
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-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-04-18 13:16:18 +00:00
public addExistingCamera(cam: Camera): Camera {
//return this._cameras.addCamera(x, y, width, height);
return cam;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public createCamera(x: number, y: number, width: number, height: number): Camera {
return this._cameras.addCamera(x, y, width, height);
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public removeCamera(id: number): bool {
return this._cameras.removeCamera(id);
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public getAllCameras(): Camera[] {
return this._cameras.getAll();
}
2013-04-12 16:19:56 +00:00
// Game Objects
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// Drop this?
public addExistingSprite(sprite: Sprite): Sprite {
return <Sprite> this.group.add(sprite);
2013-04-12 16:19:56 +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-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
}
public createDynamicTexture(width: number, height: number): DynamicTexture {
return new DynamicTexture(this._game, width, height);
2013-04-12 16:19:56 +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
public createScrollZone(key: string, x: number, y: number, width: number, height: number): ScrollZone {
return <ScrollZone> this.group.add(new ScrollZone(this._game, key, x, y, width, height));
2013-04-12 16:19:56 +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-04-18 13:16:18 +00:00
public createParticle(): Particle {
return new Particle(this._game);
2013-04-12 16:19:56 +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
}
}
}