phaser/Phaser/Game.ts

699 lines
21 KiB
TypeScript
Raw Normal View History

/// <reference path="_definitions.ts" />
2013-04-12 16:19:56 +00:00
/**
* Game
*
* 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.
*
* "Hell, there are no rules here - we're trying to accomplish something."
* Thomas A. Edison
*
* @package Phaser.Game
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
*/
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
/**
* Game constructor
*
2013-05-16 01:36:58 +00:00
* Instantiate a new <code>Phaser.Game</code> object.
*
* @constructor
* @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 preloadCallback {function} Preload 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-08-08 10:34:33 +00:00
constructor(callbackContext, parent: string = '', width: number = 800, height: number = 600, preloadCallback = null, createCallback = null, updateCallback = null, renderCallback = null, destroyCallback = null) {
2013-04-12 16:19:56 +00:00
2013-08-15 23:14:57 +00:00
// Single instance check
if (window['PhaserGlobal'] && window['PhaserGlobal'].singleInstance)
2013-08-15 23:14:57 +00:00
{
if (Phaser.GAMES.length > 0)
{
2013-08-16 22:44:06 +00:00
console.log('Phaser detected an instance of this game already running, aborting');
2013-08-15 23:14:57 +00:00
return;
}
}
this.id = Phaser.GAMES.push(this) - 1;
2013-04-18 13:16:18 +00:00
this.callbackContext = callbackContext;
this.onPreloadCallback = preloadCallback;
2013-04-18 13:16:18 +00:00
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
{
setTimeout(() => Phaser.GAMES[this.id].boot(parent, width, height));
2013-04-12 16:19:56 +00:00
}
else
{
document.addEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot(parent, width, height), false);
window.addEventListener('load', Phaser.GAMES[this.id].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
public id: number;
/**
* Game loop trigger wrapper.
*/
2013-08-08 10:34:33 +00:00
public _raf: Phaser.RequestAnimationFrame;
/**
* Whether load complete loading or not.
* @type {bool}
*/
private _loadComplete: bool = false;
/**
* Game is paused?
* @type {bool}
*/
private _paused: bool = false;
/**
* The state to be switched to in the next frame.
* @type {State}
*/
2013-04-18 13:16:18 +00:00
private _pendingState = null;
/**
2013-08-06 02:14:48 +00:00
* The PluginManager for the Game
* @type {PluginManager}
*/
2013-08-08 10:34:33 +00:00
public plugins: Phaser.PluginManager;
2013-05-16 01:36:58 +00:00
/**
* The current State object (defaults to null)
* @type {State}
*/
public state = null;
/**
* Context for calling the callbacks.
*/
2013-04-18 13:16:18 +00:00
public callbackContext;
/**
* This will be called when init states. (loading assets...)
* @type {function}
*/
public onPreloadCallback = null;
/**
* This will be called when create states. (setup states...)
* @type {function}
*/
2013-04-18 13:16:18 +00:00
public onCreateCallback = null;
/**
* This will be called when State is updated, this doesn't happen during load (see onLoadUpdateCallback)
* @type {function}
*/
2013-04-18 13:16:18 +00:00
public onUpdateCallback = null;
/**
* This will be called when the State is rendered, this doesn't happen during load (see onLoadRenderCallback)
* @type {function}
*/
2013-04-18 13:16:18 +00:00
public onRenderCallback = null;
/**
* This will be called before the State is rendered and before the stage is cleared
* @type {function}
*/
public onPreRenderCallback = null;
/**
* This will be called when the State is updated but only during the load process
* @type {function}
*/
public onLoadUpdateCallback = null;
/**
* This will be called when the State is rendered but only during the load process
* @type {function}
*/
public onLoadRenderCallback = null;
/**
* 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;
/**
* This Signal is dispatched whenever the game pauses.
* @type {Phaser.Signal}
*/
public onPause: Phaser.Signal;
/**
* This Signal is dispatched whenever the game resumes from a paused state.
* @type {Phaser.Signal}
*/
public onResume: Phaser.Signal;
2013-05-22 23:01:58 +00:00
/**
* Reference to the GameObject Factory.
* @type {GameObjectFactory}
*/
2013-08-08 10:34:33 +00:00
public add: Phaser.GameObjectFactory;
2013-05-22 23:01:58 +00:00
/**
* Reference to the assets cache.
* @type {Cache}
*/
2013-08-08 10:34:33 +00:00
public cache: Phaser.Cache;
/**
* Reference to the input manager
* @type {Input}
*/
2013-08-08 10:34:33 +00:00
public input: Phaser.InputManager;
/**
* Reference to the assets loader.
* @type {Loader}
*/
2013-08-08 10:34:33 +00:00
public load: Phaser.Loader;
/**
* Reference to the math helper.
* @type {GameMath}
*/
2013-08-08 10:34:33 +00:00
public math: Phaser.GameMath;
/**
* Reference to the network class.
* @type {Net}
*/
2013-08-08 10:34:33 +00:00
public net: Phaser.Net;
/**
* Reference to the sound manager.
* @type {SoundManager}
*/
2013-08-08 10:34:33 +00:00
public sound: Phaser.SoundManager;
/**
* Reference to the stage.
* @type {Stage}
*/
2013-08-08 10:34:33 +00:00
public stage: Phaser.Stage;
/**
* Reference to game clock.
* @type {Time}
*/
2013-08-08 10:34:33 +00:00
public time: Phaser.TimeManager;
/**
* Reference to the tween manager.
* @type {TweenManager}
*/
2013-08-08 10:34:33 +00:00
public tweens: Phaser.TweenManager;
/**
* Reference to the world.
* @type {World}
*/
2013-08-08 10:34:33 +00:00
public world: Phaser.World;
2013-06-26 04:44:56 +00:00
/**
* Reference to the physics manager.
* @type {Physics.PhysicsManager}
2013-06-26 04:44:56 +00:00
*/
public physics: Phaser.Physics.PhysicsManager;
2013-06-26 04:44:56 +00:00
/**
* Instance of repeatable random data generator helper.
* @type {RandomDataGenerator}
*/
2013-08-08 10:34:33 +00:00
public rnd: Phaser.RandomDataGenerator;
/**
2013-05-22 23:01:58 +00:00
* Contains device information and capabilities.
* @type {Device}
*/
2013-08-08 10:34:33 +00:00
public device: Phaser.Device;
2013-05-28 20:38:37 +00:00
/**
* Reference to the render manager
* @type {RenderManager}
*/
2013-08-08 10:34:33 +00:00
public renderer: Phaser.IRenderer;
2013-05-28 20:38:37 +00:00
/**
* Whether the game engine is booted, aka available.
* @type {bool}
*/
public isBooted: bool = false;
/**
* Is game running or paused?
* @type {bool}
*/
public isRunning: bool = false;
2013-04-18 13:16:18 +00:00
/**
* Initialize engine sub modules and start the game.
* @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-04-18 13:16:18 +00:00
private boot(parent: string, width: number, height: number) {
if (this.isBooted == true)
{
return;
}
2013-04-18 13:16:18 +00:00
if (!document.body)
{
setTimeout(() => Phaser.GAMES[this.id].boot(parent, width, height), 13);
2013-04-18 13:16:18 +00:00
}
else
{
document.removeEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot);
window.removeEventListener('load', Phaser.GAMES[this.id].boot);
this.onPause = new Phaser.Signal;
this.onResume = new Phaser.Signal;
2013-08-08 10:34:33 +00:00
this.device = new Phaser.Device();
this.net = new Phaser.Net(this);
this.math = new Phaser.GameMath(this);
this.stage = new Phaser.Stage(this, parent, width, height);
this.world = new Phaser.World(this, width, height);
this.add = new Phaser.GameObjectFactory(this);
this.cache = new Phaser.Cache(this);
this.load = new Phaser.Loader(this);
this.time = new Phaser.TimeManager(this);
this.tweens = new Phaser.TweenManager(this);
this.input = new Phaser.InputManager(this);
this.sound = new Phaser.SoundManager(this);
this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]);
this.physics = new Phaser.Physics.PhysicsManager(this);
2013-08-08 10:34:33 +00:00
this.plugins = new Phaser.PluginManager(this, this);
2013-05-28 20:38:37 +00:00
2013-08-09 15:56:38 +00:00
this.load.onLoadComplete.add(this.loadComplete, this);
2013-05-28 20:38:37 +00:00
this.setRenderer(Phaser.Types.RENDERER_CANVAS);
2013-04-18 13:16:18 +00:00
this.world.boot();
this.stage.boot();
this.input.boot();
this.isBooted = true;
2013-04-18 13:16:18 +00:00
// Set-up some static helper references
2013-08-08 10:34:33 +00:00
Phaser.DebugUtils.game = this;
Phaser.ColorUtils.game = this;
Phaser.DebugUtils.context = this.stage.context;
2013-04-18 13:16:18 +00:00
// Display the default game screen?
if (this.onPreloadCallback == null && this.onCreateCallback == null && this.onUpdateCallback == null && this.onRenderCallback == null && this._pendingState == null)
2013-04-12 16:19:56 +00:00
{
this._raf = new RequestAnimationFrame(this, this.bootLoop);
2013-04-12 16:19:56 +00:00
}
else
{
this.isRunning = true;
2013-04-18 13:16:18 +00:00
this._loadComplete = false;
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
}
}
}
/**
* Called when the load has finished after preload was run.
*/
2013-04-18 13:16:18 +00:00
private loadComplete() {
this._loadComplete = true;
this.onCreateCallback.call(this.callbackContext);
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* The bootLoop is called while the game is still booting (waiting for the DOM and resources to be available)
*/
private bootLoop() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.tweens.update();
this.input.update();
this.stage.update();
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
/**
* The pausedLoop is called when the game is paused.
*/
private pausedLoop() {
this.tweens.update();
this.input.update();
this.stage.update();
this.sound.update();
2013-04-18 13:16:18 +00:00
if (this.onPausedCallback !== null)
{
this.onPausedCallback.call(this.callbackContext);
2013-04-12 16:19:56 +00:00
}
}
/**
* Game loop method will be called when it's running.
*/
private loop() {
2013-08-06 02:14:48 +00:00
this.plugins.preUpdate();
this.tweens.update();
2013-04-18 13:16:18 +00:00
this.input.update();
this.stage.update();
this.sound.update();
this.physics.update();
2013-06-26 04:44:56 +00:00
this.world.update();
2013-08-06 02:14:48 +00:00
this.plugins.update();
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);
}
else if (this._loadComplete == false && this.onLoadUpdateCallback)
{
this.onLoadUpdateCallback.call(this.callbackContext);
}
2013-04-12 16:19:56 +00:00
this.world.postUpdate();
2013-08-06 02:14:48 +00:00
this.plugins.postUpdate();
this.plugins.preRender();
if (this._loadComplete && this.onPreRenderCallback)
{
this.onPreRenderCallback.call(this.callbackContext);
}
2013-05-28 20:38:37 +00:00
this.renderer.render();
2013-08-06 02:14:48 +00:00
this.plugins.render();
2013-04-18 13:16:18 +00:00
if (this._loadComplete && this.onRenderCallback)
{
this.onRenderCallback.call(this.callbackContext);
}
else if (this._loadComplete == false && this.onLoadRenderCallback)
{
this.onLoadRenderCallback.call(this.callbackContext);
}
2013-04-12 16:19:56 +00:00
2013-08-06 02:14:48 +00:00
this.plugins.postRender();
2013-04-12 16:19:56 +00:00
}
/**
* Start current state.
*/
2013-04-18 13:16:18 +00:00
private startState() {
if (this.onPreloadCallback !== null)
2013-04-18 13:16:18 +00:00
{
this.load.reset();
this.onPreloadCallback.call(this.callbackContext);
// Is the loader empty?
if (this.load.queueSize == 0)
{
if (this.onCreateCallback !== null)
{
this.onCreateCallback.call(this.callbackContext);
}
this._loadComplete = true;
}
else
{
// Start the loader going as we have something in the queue
2013-08-09 15:56:38 +00:00
this.load.onLoadComplete.add(this.loadComplete, this);
this.load.start();
}
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-08-08 10:34:33 +00:00
public setRenderer(renderer: number) {
2013-08-08 10:34:33 +00:00
switch (renderer)
{
case Phaser.Types.RENDERER_AUTO_DETECT:
this.renderer = new Phaser.Renderer.Headless.HeadlessRenderer(this);
break;
case Phaser.Types.RENDERER_AUTO_DETECT:
case Phaser.Types.RENDERER_CANVAS:
this.renderer = new Phaser.Renderer.Canvas.CanvasRenderer(this);
break;
// WebGL coming soon :)
}
}
/**
* Set the most common state callbacks (init, create, update, render).
* @param preloadCallback {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.
*/
public setCallbacks(preloadCallback = null, createCallback = null, updateCallback = null, renderCallback = null, destroyCallback = null) {
2013-04-12 16:19:56 +00:00
this.onPreloadCallback = preloadCallback;
2013-04-18 13:16:18 +00:00
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
/**
* Switch to a new State.
* @param state {State} The state you want to switch to.
* @param [clearWorld] {bool} clear everything in the world? (Default to true)
* @param [clearCache] {bool} clear asset cache? (Default to false and ONLY available when clearWorld=true)
*/
public switchState(state, clearWorld: bool = true, clearCache: bool = false) {
2013-04-18 13:16:18 +00:00
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);
}
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
this.onPreloadCallback = null;
this.onLoadRenderCallback = null;
this.onLoadUpdateCallback = null;
2013-04-18 13:16:18 +00:00
this.onCreateCallback = null;
this.onUpdateCallback = null;
this.onRenderCallback = null;
this.onPreRenderCallback = null;
2013-04-18 13:16:18 +00:00
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
if (this.state['preload'])
2013-05-16 01:36:58 +00:00
{
this.onPreloadCallback = this.state['preload'];
2013-05-16 01:36:58 +00:00
}
if (this.state['loadRender'])
{
this.onLoadRenderCallback = this.state['loadRender'];
}
if (this.state['loadUpdate'])
{
this.onLoadUpdateCallback = this.state['loadUpdate'];
}
2013-05-16 01:36:58 +00:00
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
if (this.state['preRender'])
{
this.onPreRenderCallback = this.state['preRender'];
}
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
{
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
}
/**
* Nuke the entire 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;
this.onPreloadCallback = null;
this.onLoadRenderCallback = null;
this.onLoadUpdateCallback = null;
2013-04-12 16:19:56 +00:00
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.load = null;
2013-04-18 13:16:18 +00:00
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
public get paused(): bool {
2013-04-18 13:16:18 +00:00
return this._paused;
}
2013-04-12 16:19:56 +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;
this.onPause.dispatch();
this.sound.pauseAll();
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;
this.onResume.dispatch();
2013-04-18 13:16:18 +00:00
this.input.reset();
this.sound.resumeAll();
if (this.isRunning == false)
{
this._raf.callback = this.bootLoop;
}
else
{
this._raf.callback = this.loop;
}
2013-04-12 16:19:56 +00:00
}
}
public get camera(): Phaser.Camera {
2013-04-28 20:40:06 +00:00
return this.world.cameras.current;
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
}