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" />
|
2013-05-01 03:10:21 +00:00
|
|
|
/// <reference path="FXManager.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
/// <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" />
|
2013-05-14 02:37:38 +00:00
|
|
|
/// <reference path="system/input/MSPointer.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
/// <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-05-03 11:12:12 +00:00
|
|
|
* This is where the magic happens. The Game object is the heart of your game,
|
2013-04-23 23:47:11 +00:00
|
|
|
* 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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Game constructor
|
|
|
|
*
|
2013-05-16 01:36:58 +00:00
|
|
|
* Instantiate a new <code>Phaser.Game</code> object.
|
2013-05-03 11:12:12 +00:00
|
|
|
*
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param callbackContext Which context will the callbacks be called with.
|
|
|
|
* @param parent {string} ID of its parent DOM element.
|
|
|
|
* @param width {number} The width of your game in game pixels.
|
|
|
|
* @param height {number} The height of your game in game pixels.
|
|
|
|
* @param initCallback {function} Init callback invoked when init default screen.
|
|
|
|
* @param createCallback {function} Create callback invoked when create default screen.
|
|
|
|
* @param updateCallback {function} Update callback invoked when update default screen.
|
|
|
|
* @param renderCallback {function} Render callback invoked when render default screen.
|
2013-05-16 01:36:58 +00:00
|
|
|
* @param destroyCallback {function} Destroy callback invoked when state is destroyed.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-05-16 01:36:58 +00:00
|
|
|
constructor(callbackContext, parent?: string = '', width?: number = 800, height?: number = 600, initCallback = null, createCallback = null, updateCallback = null, renderCallback = null, destroyCallback = 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-05-16 01:36:58 +00:00
|
|
|
this.onDestroyCallback = destroyCallback;
|
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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Game loop trigger wrapper.
|
|
|
|
*/
|
2013-05-14 02:37:38 +00:00
|
|
|
public _raf: RequestAnimationFrame;
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Max allowable accumulation.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _maxAccumulation: number = 32;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Total number of milliseconds elapsed since last update loop.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _accumulator: number = 0;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Milliseconds of time per step of the game loop.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _step: number = 0;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Whether loader complete loading or not.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _loadComplete: bool = false;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Game is paused?
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _paused: bool = false;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* The state to be switched to in the next frame.
|
|
|
|
* @type {State}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _pendingState = null;
|
|
|
|
|
2013-05-16 01:36:58 +00:00
|
|
|
/**
|
|
|
|
* The current State object (defaults to null)
|
|
|
|
* @type {State}
|
|
|
|
*/
|
|
|
|
public state = null;
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Context for calling the callbacks.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public callbackContext;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* This will be called when init states. (loading assets...)
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public onInitCallback = null;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* This will be called when create states. (setup states...)
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public onCreateCallback = null;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* This will be called when update states.
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public onUpdateCallback = null;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* This will be called when render states.
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public onRenderCallback = null;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* This will be called when states paused.
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public onPausedCallback = null;
|
|
|
|
|
2013-05-16 01:36:58 +00:00
|
|
|
/**
|
|
|
|
* This will be called when the state is destroyed (i.e. swapping to a new state)
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
public onDestroyCallback = null;
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the assets cache.
|
|
|
|
* @type {Cache}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public cache: Cache;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the collision helper.
|
|
|
|
* @type {Collision}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public collision: Collision;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the input manager
|
|
|
|
* @type {Input}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public input: Input;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the assets loader.
|
|
|
|
* @type {Loader}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public loader: Loader;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the math helper.
|
|
|
|
* @type {GameMath}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public math: GameMath;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the motion helper.
|
|
|
|
* @type {Motion}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public motion: Motion;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the sound manager.
|
|
|
|
* @type {SoundManager}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public sound: SoundManager;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the stage.
|
|
|
|
* @type {Stage}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public stage: Stage;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to game clock.
|
|
|
|
* @type {Time}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public time: Time;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the tween manager.
|
|
|
|
* @type {TweenManager}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public tweens: TweenManager;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Reference to the world.
|
|
|
|
* @type {World}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public world: World;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Instance of repeatable random data generator helper.
|
|
|
|
* @type {RandomDataGenerator}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public rnd: RandomDataGenerator;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Device detector.
|
|
|
|
* @type {Device}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public device: Device;
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Whether the game engine is booted, aka available.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public isBooted: bool = false;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Is game running or paused?
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
public isRunning: bool = false;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Initialize engine sub modules and start the game.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param parent {string} ID of parent Dom element.
|
|
|
|
* @param width {number} Width of the game screen.
|
|
|
|
* @param height {number} Height of the game screen.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
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;
|
2013-04-29 01:41:19 +00:00
|
|
|
this.isBooted = true;
|
2013-05-16 01:36:58 +00:00
|
|
|
this.input.start();
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
// 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-05-14 02:37:38 +00:00
|
|
|
this._raf = new RequestAnimationFrame(this, this.bootLoop);
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-29 01:41:19 +00:00
|
|
|
this.isRunning = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
this._loadComplete = false;
|
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
this._raf = new RequestAnimationFrame(this, this.loop);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
if (this._pendingState)
|
|
|
|
{
|
|
|
|
this.switchState(this._pendingState, false, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.startState();
|
|
|
|
}
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Called when the loader has finished after init was run.
|
|
|
|
*/
|
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
|
|
|
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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Game loop method will be called when it's booting.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private bootLoop() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.tweens.update();
|
2013-04-29 01:41:19 +00:00
|
|
|
this.input.update();
|
|
|
|
this.stage.update();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-29 01:41:19 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Game loop method will be called when it's paused.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private pausedLoop() {
|
|
|
|
|
|
|
|
this.tweens.update();
|
|
|
|
this.input.update();
|
|
|
|
this.stage.update();
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-04-29 01:41:19 +00:00
|
|
|
if (this.onPausedCallback !== null)
|
|
|
|
{
|
|
|
|
this.onPausedCallback.call(this.callbackContext);
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-29 01:41:19 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Game loop method will be called when it's running.
|
|
|
|
*/
|
2013-04-29 01:41:19 +00:00
|
|
|
private loop() {
|
|
|
|
|
|
|
|
this.tweens.update();
|
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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Start current state.
|
|
|
|
*/
|
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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Set all state callbacks (init, create, update, render).
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param initCallback {function} Init callback invoked when init state.
|
|
|
|
* @param createCallback {function} Create callback invoked when create state.
|
|
|
|
* @param updateCallback {function} Update callback invoked when update state.
|
|
|
|
* @param renderCallback {function} Render callback invoked when render state.
|
2013-05-16 01:36:58 +00:00
|
|
|
* @param destroyCallback {function} Destroy callback invoked when state is destroyed.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-05-16 01:36:58 +00:00
|
|
|
public setCallbacks(initCallback = null, createCallback = null, updateCallback = null, renderCallback = null, destroyCallback = 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-05-16 01:36:58 +00:00
|
|
|
this.onDestroyCallback = destroyCallback;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Switch to a new State.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param state {State} The state you want to switch to.
|
2013-05-04 16:18:45 +00:00
|
|
|
* @param [clearWorld] {boolean} clear everything in the world? (Default to true)
|
|
|
|
* @param [clearCache] {boolean} clear asset cache? (Default to false and ONLY available when clearWorld=true)
|
2013-05-03 11:12:12 +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-05-16 01:36:58 +00:00
|
|
|
// Destroy current state?
|
|
|
|
if (this.onDestroyCallback !== null)
|
|
|
|
{
|
|
|
|
this.onDestroyCallback.call(this.callbackContext);
|
|
|
|
}
|
|
|
|
|
2013-05-18 02:05:28 +00:00
|
|
|
this.input.reset(true);
|
2013-05-16 01:36:58 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Prototype?
|
|
|
|
if (typeof state === 'function')
|
|
|
|
{
|
2013-05-16 01:36:58 +00:00
|
|
|
this.state = new state(this);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Ok, have we got the right functions?
|
2013-05-16 01:36:58 +00:00
|
|
|
if (this.state['create'] || this.state['update'])
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-16 01:36:58 +00:00
|
|
|
this.callbackContext = this.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-05-16 01:36:58 +00:00
|
|
|
this.onDestroyCallback = null;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Bingo, let's set them up
|
2013-05-16 01:36:58 +00:00
|
|
|
if (this.state['init'])
|
|
|
|
{
|
|
|
|
this.onInitCallback = this.state['init'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state['create'])
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-16 01:36:58 +00:00
|
|
|
this.onCreateCallback = this.state['create'];
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-16 01:36:58 +00:00
|
|
|
if (this.state['update'])
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-16 01:36:58 +00:00
|
|
|
this.onUpdateCallback = this.state['update'];
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-16 01:36:58 +00:00
|
|
|
if (this.state['render'])
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-16 01:36:58 +00:00
|
|
|
this.onRenderCallback = this.state['render'];
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-16 01:36:58 +00:00
|
|
|
if (this.state['paused'])
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-16 01:36:58 +00:00
|
|
|
this.onPausedCallback = this.state['paused'];
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-16 01:36:58 +00:00
|
|
|
if (this.state['destroy'])
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-16 01:36:58 +00:00
|
|
|
this.onDestroyCallback = this.state['destroy'];
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Nuke the whole game from orbit
|
|
|
|
*/
|
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.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-05-16 01:36:58 +00:00
|
|
|
this.onDestroyCallback = null;
|
2013-04-18 13:16:18 +00:00
|
|
|
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-05-14 02:37:38 +00:00
|
|
|
this._raf.callback = this.pausedLoop;
|
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;
|
2013-05-14 02:37:38 +00:00
|
|
|
//this.time.time = window.performance.now ? (performance.now() + performance.timing.navigationStart) : Date.now();
|
2013-04-18 13:16:18 +00:00
|
|
|
this.input.reset();
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-04-29 01:41:19 +00:00
|
|
|
if (this.isRunning == false)
|
|
|
|
{
|
2013-05-14 02:37:38 +00:00
|
|
|
this._raf.callback = this.bootLoop;
|
2013-04-29 01:41:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-14 02:37:38 +00:00
|
|
|
this._raf.callback = this.loop;
|
2013-04-29 01:41:19 +00:00
|
|
|
}
|
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-05-03 11:12:12 +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:12:12 +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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new GeomSprite with specific position.
|
|
|
|
*
|
2013-05-04 11:46:19 +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 this.world.createGeomSprite(x, y);
|
|
|
|
}
|
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:46:19 +00:00
|
|
|
* @param x {number} X position of the new sprite.
|
|
|
|
* @param y {number} Y position of the new sprite.
|
2013-05-14 02:37:38 +00:00
|
|
|
* @param key {string} Optional, key for the sprite sheet you want it to use.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @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 this.world.createSprite(x, y, key);
|
|
|
|
}
|
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:46:19 +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 this.world.createDynamicTexture(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
|
|
|
/**
|
|
|
|
* Create a new object container.
|
|
|
|
*
|
2013-05-17 05:49:43 +00:00
|
|
|
* @param maxSize {number} Optional, capacity of this group.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @returns {Group} The newly created group.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-05-17 05:49:43 +00:00
|
|
|
public createGroup(maxSize?: number = 0): Group {
|
|
|
|
return this.world.createGroup(maxSize);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new Particle.
|
|
|
|
*
|
2013-05-04 11:46:19 +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 this.world.createParticle();
|
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new Emitter.
|
|
|
|
*
|
2013-05-14 02:37:38 +00:00
|
|
|
* @param x {number} Optional, x position of the emitter.
|
|
|
|
* @param y {number} Optional, y position of the emitter.
|
|
|
|
* @param size {number} Optional, size of this emitter.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @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 this.world.createEmitter(x, y, size);
|
|
|
|
}
|
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:46:19 +00:00
|
|
|
* @param key {string} 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.
|
2013-05-14 02:37:38 +00:00
|
|
|
* @param height {number} Height of this object.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @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 this.world.createScrollZone(key, x, y, width, height);
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new Tilemap.
|
|
|
|
*
|
2013-05-04 11:46:19 +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)
|
2013-05-04 16:18:45 +00:00
|
|
|
* @param [resizeWorld] {boolean} resize the world to make same as tilemap?
|
|
|
|
* @param [tileWidth] {number} width of each tile.
|
|
|
|
* @param [tileHeight] {number} height of each tile.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @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 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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Create a tween object for a specific object.
|
|
|
|
*
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param obj Object you wish the tween will affect.
|
|
|
|
* @return {Phaser.Tween} The newly created tween object.
|
2013-05-03 11:12:12 +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-05-03 11:12:12 +00:00
|
|
|
/**
|
2013-05-14 02:37:38 +00:00
|
|
|
* Call this method to see if one object collides with another.
|
|
|
|
* @return {boolean} Whether the given objects or groups collides.
|
2013-05-03 11:12:12 +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-28 20:40:06 +00:00
|
|
|
public get camera(): Camera {
|
|
|
|
return this.world.cameras.current;
|
|
|
|
}
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|