2013-04-18 14:48:06 +00:00
|
|
|
/// <reference path="AnimationManager.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="Basic.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
/// <reference path="Cache.ts" />
|
2013-04-18 14:48:06 +00:00
|
|
|
/// <reference path="CameraManager.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="Collision.ts" />
|
|
|
|
/// <reference path="DynamicTexture.ts" />
|
|
|
|
/// <reference path="GameMath.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
/// <reference path="Group.ts" />
|
|
|
|
/// <reference path="Loader.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="Motion.ts" />
|
|
|
|
/// <reference path="Signal.ts" />
|
|
|
|
/// <reference path="SignalBinding.ts" />
|
2013-04-18 15:49:08 +00:00
|
|
|
/// <reference path="SoundManager.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
/// <reference path="Stage.ts" />
|
|
|
|
/// <reference path="Time.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="TweenManager.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
/// <reference path="World.ts" />
|
2013-04-13 03:25:03 +00:00
|
|
|
/// <reference path="system/Device.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="system/RandomDataGenerator.ts" />
|
|
|
|
/// <reference path="system/RequestAnimationFrame.ts" />
|
|
|
|
/// <reference path="system/input/Input.ts" />
|
|
|
|
/// <reference path="system/input/Keyboard.ts" />
|
|
|
|
/// <reference path="system/input/Mouse.ts" />
|
|
|
|
/// <reference path="system/input/Touch.ts" />
|
|
|
|
/// <reference path="gameobjects/Emitter.ts" />
|
|
|
|
/// <reference path="gameobjects/GameObject.ts" />
|
|
|
|
/// <reference path="gameobjects/GeomSprite.ts" />
|
|
|
|
/// <reference path="gameobjects/Particle.ts" />
|
|
|
|
/// <reference path="gameobjects/Sprite.ts" />
|
|
|
|
/// <reference path="gameobjects/Tilemap.ts" />
|
2013-04-22 00:53:24 +00:00
|
|
|
/// <reference path="gameobjects/ScrollZone.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - Game
|
2013-04-12 16:19:56 +00:00
|
|
|
*
|
2013-04-23 23:47:11 +00:00
|
|
|
* This is where the magic happens. The Game object is the heart of your game,
|
|
|
|
* providing quick access to common functions and handling the boot process.
|
2013-04-25 00:55:56 +00:00
|
|
|
*
|
|
|
|
* "Hell, there are no rules here - we're trying to accomplish something."
|
|
|
|
* Thomas A. Edison
|
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 Game {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
constructor(callbackContext, parent?: string = '', width?: number = 800, height?: number = 600, initCallback = null, createCallback = null, updateCallback = null, renderCallback = null) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.callbackContext = callbackContext;
|
|
|
|
this.onInitCallback = initCallback;
|
|
|
|
this.onCreateCallback = createCallback;
|
|
|
|
this.onUpdateCallback = updateCallback;
|
|
|
|
this.onRenderCallback = renderCallback;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (document.readyState === 'complete' || document.readyState === 'interactive')
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-24 08:37:29 +00:00
|
|
|
setTimeout(() => this.boot(parent, width, height));
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
document.addEventListener('DOMContentLoaded', () => this.boot(parent, width, height), false);
|
2013-04-23 23:47:11 +00:00
|
|
|
window.addEventListener('load', () => this.boot(parent, width, height), false);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
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 _raf: RequestAnimationFrame;
|
|
|
|
private _maxAccumulation: number = 32;
|
|
|
|
private _accumulator: number = 0;
|
|
|
|
private _step: number = 0;
|
|
|
|
private _loadComplete: bool = false;
|
|
|
|
private _paused: bool = false;
|
|
|
|
private _pendingState = null;
|
|
|
|
|
|
|
|
// Event callbacks
|
|
|
|
public callbackContext;
|
|
|
|
public onInitCallback = null;
|
|
|
|
public onCreateCallback = null;
|
|
|
|
public onUpdateCallback = null;
|
|
|
|
public onRenderCallback = null;
|
|
|
|
public onPausedCallback = null;
|
|
|
|
|
|
|
|
public camera: Camera; // quick reference to the default created camera, access the rest via .world
|
|
|
|
public cache: Cache;
|
|
|
|
public collision: Collision;
|
|
|
|
public input: Input;
|
|
|
|
public loader: Loader;
|
|
|
|
public math: GameMath;
|
|
|
|
public motion: Motion;
|
|
|
|
public sound: SoundManager;
|
|
|
|
public stage: Stage;
|
|
|
|
public time: Time;
|
|
|
|
public tweens: TweenManager;
|
|
|
|
public world: World;
|
|
|
|
public rnd: RandomDataGenerator;
|
|
|
|
public device: Device;
|
|
|
|
|
|
|
|
public isBooted: bool = false;
|
|
|
|
|
|
|
|
private boot(parent: string, width: number, height: number) {
|
|
|
|
|
2013-04-23 23:47:11 +00:00
|
|
|
if (this.isBooted == true)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (!document.body)
|
|
|
|
{
|
|
|
|
window.setTimeout(() => this.boot(parent, width, height), 13);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.device = new Device();
|
|
|
|
this.motion = new Motion(this);
|
|
|
|
this.math = new GameMath(this);
|
|
|
|
this.stage = new Stage(this, parent, width, height);
|
|
|
|
this.world = new World(this, width, height);
|
|
|
|
this.sound = new SoundManager(this);
|
|
|
|
this.cache = new Cache(this);
|
|
|
|
this.collision = new Collision(this);
|
|
|
|
this.loader = new Loader(this, this.loadComplete);
|
|
|
|
this.time = new Time(this);
|
|
|
|
this.tweens = new TweenManager(this);
|
|
|
|
this.input = new Input(this);
|
|
|
|
this.rnd = new RandomDataGenerator([(Date.now() * Math.random()).toString()]);
|
|
|
|
|
|
|
|
this.framerate = 60;
|
|
|
|
|
|
|
|
// Display the default game screen?
|
|
|
|
if (this.onInitCallback == null && this.onCreateCallback == null && this.onUpdateCallback == null && this.onRenderCallback == null && this._pendingState == null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
this.isBooted = false;
|
|
|
|
this.stage.drawInitScreen();
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
this.isBooted = true;
|
|
|
|
this._loadComplete = false;
|
|
|
|
|
|
|
|
this._raf = new RequestAnimationFrame(this.loop, this);
|
|
|
|
|
|
|
|
if (this._pendingState)
|
|
|
|
{
|
|
|
|
this.switchState(this._pendingState, false, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.startState();
|
|
|
|
}
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
private loadComplete() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Called when the loader has finished after init was run
|
|
|
|
this._loadComplete = true;
|
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 loop() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.time.update();
|
|
|
|
this.tweens.update();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._paused == true)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this.onPausedCallback !== null)
|
|
|
|
{
|
|
|
|
this.onPausedCallback.call(this.callbackContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.input.update();
|
|
|
|
this.stage.update();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._accumulator += this.time.delta;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._accumulator > this._maxAccumulation)
|
|
|
|
{
|
|
|
|
this._accumulator = this._maxAccumulation;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
while (this._accumulator >= this._step)
|
|
|
|
{
|
|
|
|
this.time.elapsed = this.time.timeScale * (this._step / 1000);
|
|
|
|
this.world.update();
|
|
|
|
this._accumulator = this._accumulator - this._step;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._loadComplete && this.onUpdateCallback)
|
|
|
|
{
|
|
|
|
this.onUpdateCallback.call(this.callbackContext);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.world.render();
|
|
|
|
|
|
|
|
if (this._loadComplete && this.onRenderCallback)
|
|
|
|
{
|
|
|
|
this.onRenderCallback.call(this.callbackContext);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
private startState() {
|
|
|
|
|
|
|
|
if (this.onInitCallback !== null)
|
|
|
|
{
|
2013-04-20 02:40:17 +00:00
|
|
|
this.loader.reset();
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.onInitCallback.call(this.callbackContext);
|
2013-04-20 02:40:17 +00:00
|
|
|
|
|
|
|
// Is the loader empty?
|
|
|
|
if (this.loader.queueSize == 0)
|
|
|
|
{
|
|
|
|
if (this.onCreateCallback !== null)
|
|
|
|
{
|
|
|
|
this.onCreateCallback.call(this.callbackContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._loadComplete = true;
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No init? Then there was nothing to load either
|
|
|
|
if (this.onCreateCallback !== null)
|
|
|
|
{
|
|
|
|
this.onCreateCallback.call(this.callbackContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._loadComplete = true;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public setCallbacks(initCallback = null, createCallback = null, updateCallback = null, renderCallback = null) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.onInitCallback = initCallback;
|
|
|
|
this.onCreateCallback = createCallback;
|
|
|
|
this.onUpdateCallback = updateCallback;
|
|
|
|
this.onRenderCallback = renderCallback;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
public switchState(state, clearWorld: bool = true, clearCache: bool = false) {
|
|
|
|
|
|
|
|
if (this.isBooted == false)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
this._pendingState = state;
|
|
|
|
return;
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Prototype?
|
|
|
|
if (typeof state === 'function')
|
|
|
|
{
|
|
|
|
state = new state(this);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Ok, have we got the right functions?
|
|
|
|
if (state['create'] || state['update'])
|
|
|
|
{
|
|
|
|
this.callbackContext = state;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.onInitCallback = null;
|
|
|
|
this.onCreateCallback = null;
|
|
|
|
this.onUpdateCallback = null;
|
|
|
|
this.onRenderCallback = null;
|
|
|
|
this.onPausedCallback = null;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Bingo, let's set them up
|
|
|
|
if (state['init'])
|
|
|
|
{
|
|
|
|
this.onInitCallback = state['init'];
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (state['create'])
|
|
|
|
{
|
|
|
|
this.onCreateCallback = state['create'];
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (state['update'])
|
|
|
|
{
|
|
|
|
this.onUpdateCallback = state['update'];
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (state['render'])
|
|
|
|
{
|
|
|
|
this.onRenderCallback = state['render'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state['paused'])
|
|
|
|
{
|
|
|
|
this.onPausedCallback = state['paused'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clearWorld)
|
|
|
|
{
|
|
|
|
this.world.destroy();
|
|
|
|
|
|
|
|
if (clearCache == true)
|
|
|
|
{
|
|
|
|
this.cache.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._loadComplete = false;
|
|
|
|
|
|
|
|
this.startState();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-25 19:05:56 +00:00
|
|
|
throw new Error("Invalid State object given. Must contain at least a create or update function.");
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Nuke the whole game from orbit
|
|
|
|
public destroy() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.callbackContext = null;
|
2013-04-12 16:19:56 +00:00
|
|
|
this.onInitCallback = null;
|
|
|
|
this.onCreateCallback = null;
|
|
|
|
this.onUpdateCallback = null;
|
|
|
|
this.onRenderCallback = null;
|
|
|
|
this.onPausedCallback = null;
|
2013-04-18 13:16:18 +00:00
|
|
|
this.camera = null;
|
|
|
|
this.cache = null;
|
|
|
|
this.input = null;
|
|
|
|
this.loader = null;
|
|
|
|
this.sound = null;
|
|
|
|
this.stage = null;
|
|
|
|
this.time = null;
|
|
|
|
this.world = null;
|
|
|
|
this.isBooted = false;
|
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 paused(): bool {
|
|
|
|
return this._paused;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public set paused(value: bool) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (value == true && this._paused == false)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
this._paused = true;
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
else if (value == false && this._paused == true)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
this._paused = false;
|
|
|
|
this.time.time = Date.now();
|
|
|
|
this.input.reset();
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get framerate(): number {
|
|
|
|
return 1000 / this._step;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public set framerate(value: number) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._step = 1000 / value;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._maxAccumulation < this._step)
|
|
|
|
{
|
|
|
|
this._maxAccumulation = this._step;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Handy Proxy methods
|
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.world.createCamera(x, y, width, height);
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public createGeomSprite(x: number, y: number): GeomSprite {
|
|
|
|
return this.world.createGeomSprite(x, y);
|
|
|
|
}
|
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 this.world.createSprite(x, y, key);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-22 00:53:24 +00:00
|
|
|
public createDynamicTexture(width: number, height: number): DynamicTexture {
|
|
|
|
return this.world.createDynamicTexture(width, height);
|
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 createGroup(MaxSize?: number = 0): Group {
|
|
|
|
return this.world.createGroup(MaxSize);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public createParticle(): Particle {
|
|
|
|
return this.world.createParticle();
|
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public createEmitter(x?: number = 0, y?: number = 0, size?: number = 0): Emitter {
|
|
|
|
return this.world.createEmitter(x, y, size);
|
|
|
|
}
|
2013-04-12 16:19:56 +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 this.world.createScrollZone(key, x, y, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
public createTilemap(key: string, mapData: string, format: number, resizeWorld: bool = true, tileWidth?: number = 0, tileHeight?: number = 0): Tilemap {
|
|
|
|
return this.world.createTilemap(key, mapData, format, resizeWorld, tileWidth, tileHeight);
|
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 createTween(obj): Tween {
|
|
|
|
return this.tweens.create(obj);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-25 19:05:56 +00:00
|
|
|
public collide(objectOrGroup1: Basic = null, objectOrGroup2: Basic = null, notifyCallback = null): bool {
|
|
|
|
return this.collision.overlap(objectOrGroup1, objectOrGroup2, notifyCallback, Collision.separate);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|