phaser/src/boot/Game.js

558 lines
16 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2018-01-25 00:48:48 +00:00
var AddToDOM = require('../dom/AddToDOM');
var AnimationManager = require('../animations/AnimationManager');
var CacheManager = require('../cache/CacheManager');
var CanvasPool = require('../display/canvas/CanvasPool');
var Class = require('../utils/Class');
var Config = require('./Config');
2018-01-25 00:48:48 +00:00
var CreateRenderer = require('./CreateRenderer');
var DataManager = require('../data/DataManager');
2016-11-22 03:32:41 +00:00
var DebugHeader = require('./DebugHeader');
var Device = require('../device');
var DOMContentLoaded = require('../dom/DOMContentLoaded');
var EventEmitter = require('eventemitter3');
2018-01-16 16:14:21 +00:00
var InputManager = require('../input/InputManager');
2018-01-25 00:48:48 +00:00
var NOOP = require('../utils/NOOP');
2018-02-12 23:13:16 +00:00
var PluginManager = require('./PluginManager');
2018-01-16 16:33:23 +00:00
var SceneManager = require('../scene/SceneManager');
var SoundManagerCreator = require('../sound/SoundManagerCreator');
var TextureManager = require('../textures/TextureManager');
2017-06-28 16:17:31 +00:00
var TimeStep = require('./TimeStep');
2018-01-25 00:48:48 +00:00
var VisibilityHandler = require('./VisibilityHandler');
2016-11-22 03:11:33 +00:00
/**
* @callback GameStepCallback
*/
2018-02-07 15:27:21 +00:00
/**
* @classdesc
* The Phaser.Game instance is the main controller for the entire Phaser game. It is responsible
* for handling the boot process, parsing the configuration values, creating the renderer,
* and setting-up all of the global Phaser systems, such as sound and input.
* Once that is complete it will start the Scene Manager and then begin the main game loop.
*
* You should generally avoid accessing any of the systems created by Game, and instead use those
* made available to you via the Phaser.Scene Systems class instead.
*
* @class Game
* @memberOf Phaser
* @constructor
* @since 3.0.0
*
* @param {object} [GameConfig] - The configuration object for your Phaser Game instance.
*/
var Game = new Class({
initialize:
2016-11-24 17:01:52 +00:00
function Game (config)
{
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* The parsed Game Configuration object.
*
2018-01-25 00:48:48 +00:00
* The values stored within this object are read-only and should not be changed at run-time.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#config
* @type {Phaser.Boot.Config}
2018-01-25 00:48:48 +00:00
* @readOnly
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.config = new Config(config);
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* A reference to either the Canvas or WebGL Renderer that this Game is using.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#renderer
2018-03-20 14:58:02 +00:00
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.renderer = null;
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* A reference to the HTML Canvas Element on which the renderer is drawing.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#canvas
* @type {HTMLCanvasElement}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.canvas = null;
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* A reference to the Canvas Rendering Context belonging to the Canvas Element this game is rendering to.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#context
* @type {CanvasRenderingContext2D}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.context = null;
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* A flag indicating when this Game instance has finished its boot process.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#isBooted
* @type {boolean}
2018-01-25 00:48:48 +00:00
* @readOnly
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.isBooted = false;
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* A flag indicating if this Game is currently running its game step or not.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#isRunning
* @type {boolean}
2018-01-25 00:48:48 +00:00
* @readOnly
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.isRunning = false;
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* An Event Emitter which is used to broadcast game-level events from the global systems.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#events
* @type {EventEmitter}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.events = new EventEmitter();
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* An instance of the Animation Manager.
*
2018-01-25 00:48:48 +00:00
* The Animation Manager is a global system responsible for managing all animations used within your game.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#anims
* @type {Phaser.Animations.AnimationManager}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.anims = new AnimationManager(this);
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* An instance of the Texture Manager.
*
2018-01-25 00:48:48 +00:00
* The Texture Manager is a global system responsible for managing all textures being used by your game.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#textures
* @type {Phaser.Textures.TextureManager}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.textures = new TextureManager(this);
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* An instance of the Cache Manager.
*
2018-01-25 00:48:48 +00:00
* The Cache Manager is a global system responsible for caching, accessing and releasing external game assets.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#cache
* @type {Phaser.Cache.CacheManager}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
2018-01-16 16:30:11 +00:00
this.cache = new CacheManager(this);
2017-10-04 22:48:16 +00:00
/**
* [description]
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#registry
* @type {Phaser.Data.DataManager}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.registry = new DataManager(this);
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* An instance of the Input Manager.
*
2018-01-25 00:48:48 +00:00
* The Input Manager is a global system responsible for the capture of browser-level input events.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#input
* @type {Phaser.Input.InputManager}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
2018-01-16 16:14:21 +00:00
this.input = new InputManager(this, this.config);
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* An instance of the Scene Manager.
*
2018-01-25 00:48:48 +00:00
* The Scene Manager is a global system responsible for creating, modifying and updating the Scenes in your game.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#scene
* @type {Phaser.Scenes.SceneManager}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
2018-01-16 16:33:23 +00:00
this.scene = new SceneManager(this, this.config.sceneConfig);
2017-10-04 22:48:16 +00:00
/**
* A reference to the Device inspector.
2018-01-25 00:48:48 +00:00
*
* Contains information about the device running this game, such as OS, browser vendor and feature support.
* Used by various systems to determine capabilities and code paths.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#device
2018-03-21 14:40:30 +00:00
* @type {Phaser.DeviceConf}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.device = Device;
/**
2018-01-25 00:48:48 +00:00
* An instance of the base Sound Manager.
*
* The Sound Manager is a global system responsible for the playback and updating of all audio in your game.
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#sound
* @type {Phaser.BaseSoundManager}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
*/
this.sound = SoundManagerCreator.create(this);
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* An instance of the Time Step.
*
* The Time Step is a global system responsible for setting-up and responding to the browser frame events, processing
* them and calculating delta values. It then automatically calls the game step.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#loop
* @type {Phaser.Boot.TimeStep}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
this.loop = new TimeStep(this, this.config.fps);
/**
2018-01-25 00:48:48 +00:00
* An instance of the Plugin Manager.
*
* The Plugin Manager is a global system that allows plugins to register themselves with it, and can then install
* those plugins into Scenes as required.
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#plugins
* @type {Phaser.Boot.PluginManager}
2018-01-25 00:48:48 +00:00
* @since 3.0.0
*/
this.plugins = new PluginManager(this, this.config);
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* The `onStepCallback` is a callback that is fired each time the Time Step ticks.
* It is set automatically when the Game boot process has completed.
2017-10-04 22:48:16 +00:00
*
2018-02-12 23:13:16 +00:00
* @name Phaser.Game#onStepCallback
* @type {GameStepCallback}
2018-02-12 23:13:16 +00:00
* @private
2018-01-25 00:48:48 +00:00
* @since 3.0.0
2017-10-04 22:48:16 +00:00
*/
2017-07-20 10:34:01 +00:00
this.onStepCallback = NOOP;
// Wait for the DOM Ready event, then call boot.
DOMContentLoaded(this.boot.bind(this));
},
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* Game boot event.
*
* This is an internal event dispatched when the game has finished booting, but before it is ready to start running.
* The global systems use this event to know when to set themselves up, dispatching their own `ready` events as required.
*
* @event Phaser.Game#boot
*/
/**
* This method is called automatically when the DOM is ready. It is responsible for creating the renderer,
* displaying the Debug Header, adding the game canvas to the DOM and emitting the 'boot' event.
* It listens for a 'ready' event from the base systems and once received it will call `Game.start`.
2017-10-04 22:48:16 +00:00
*
* @method Phaser.Game#boot
2018-01-25 00:48:48 +00:00
* @protected
* @fires Phaser.Game#boot
2017-10-04 22:48:16 +00:00
* @since 3.0.0
*/
boot: function ()
{
this.isBooted = true;
this.config.preBoot(this);
2016-11-29 11:26:30 +00:00
CreateRenderer(this);
DebugHeader(this);
2016-12-07 03:42:41 +00:00
AddToDOM(this.canvas, this.config.parent);
this.events.emit('boot');
// The Texture Manager has to wait on a couple of non-blocking events before it's fully ready, so it will emit this event
this.events.once('ready', this.start, this);
},
/**
2018-01-25 00:48:48 +00:00
* Called automatically by Game.boot once all of the global systems have finished setting themselves up.
* By this point the Game is now ready to start the main loop running.
* It will also enable the Visibility Handler.
*
* @method Phaser.Game#start
2018-01-25 00:48:48 +00:00
* @protected
* @since 3.0.0
*/
start: function ()
{
this.isRunning = true;
this.config.postBoot(this);
if (this.renderer)
{
this.loop.start(this.step.bind(this));
}
else
{
this.loop.start(this.headlessStep.bind(this));
}
VisibilityHandler(this.events);
this.events.on('hidden', this.onHidden, this);
this.events.on('visible', this.onVisible, this);
this.events.on('blur', this.onBlur, this);
this.events.on('focus', this.onFocus, this);
},
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* Game Pre-Render event.
*
* This event is dispatched immediately before any of the Scenes have started to render.
* The renderer will already have been initialized this frame, clearing itself and preparing to receive
* the Scenes for rendering, but it won't have actually drawn anything yet.
*
2018-02-12 23:51:47 +00:00
* @event Phaser.Game#prerenderEvent
2018-03-20 14:58:02 +00:00
* @param {(Phaser.Renderer.CanvasRenderer|Phaser.Renderer.WebGLRenderer)} renderer - A reference to the current renderer.
2018-01-25 00:48:48 +00:00
*/
/**
* Game Post-Render event.
*
* This event is dispatched right at the end of the render process.
* Every Scene will have rendered and drawn to the canvas.
*
2018-02-12 23:51:47 +00:00
* @event Phaser.Game#postrenderEvent
2018-03-20 14:58:02 +00:00
* @param {(Phaser.Renderer.CanvasRenderer|Phaser.Renderer.WebGLRenderer)} renderer - A reference to the current renderer.
2018-01-25 00:48:48 +00:00
*/
/**
* The main Game Step. Called automatically by the Time Step, once per browser frame (typically as a result of
* Request Animation Frame, or Set Timeout on very old browsers.)
*
* The step will update the global managers first, then proceed to update each Scene in turn, via the Scene Manager.
*
* It will then render each Scene in turn, via the Renderer. This process emits `prerender` and `postrender` events.
2017-10-04 22:48:16 +00:00
*
* @method Phaser.Game#step
2018-02-12 23:51:47 +00:00
* @fires Phaser.Game#prerenderEvent
* @fires Phaser.Game#postrenderEvent
2017-10-04 22:48:16 +00:00
* @since 3.0.0
*
* @param {integer} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
2018-03-18 13:43:37 +00:00
* @param {number} delta - The delta time, in ms, elapsed since the last frame.
2017-10-04 22:48:16 +00:00
*/
step: function (time, delta)
{
// Global Managers
this.input.update(time, delta);
2017-11-26 15:45:07 +00:00
this.sound.update(time, delta);
// Scenes
2017-07-20 10:34:01 +00:00
this.onStepCallback();
this.scene.update(time, delta);
// Render
var renderer = this.renderer;
renderer.preRender();
this.events.emit('prerender', renderer);
this.scene.render(renderer);
renderer.postRender();
this.events.emit('postrender', renderer);
},
/**
* A special version of the Game Step for the HEADLESS renderer only.
*
* The main Game Step. Called automatically by the Time Step, once per browser frame (typically as a result of
* Request Animation Frame, or Set Timeout on very old browsers.)
*
* The step will update the global managers first, then proceed to update each Scene in turn, via the Scene Manager.
*
* This process emits `prerender` and `postrender` events, even though nothing actually displays.
*
* @method Phaser.Game#headlessStep
* @fires Phaser.Game#prerenderEvent
* @fires Phaser.Game#postrenderEvent
* @since 3.2.0
*
* @param {integer} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
* @param {number} delta - The delta time elapsed since the last frame.
*/
headlessStep: function (time, delta)
{
// Global Managers
this.input.update(time, delta);
this.sound.update(time, delta);
// Scenes
this.onStepCallback();
this.scene.update(time, delta);
// Render
this.events.emit('prerender');
this.events.emit('postrender');
},
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* Game Pause event.
*
* This event is dispatched when the game loop enters a paused state, usually as a result of the Visibility Handler.
*
2018-02-12 23:51:47 +00:00
* @event Phaser.Game#pauseEvent
2018-01-25 00:48:48 +00:00
*/
/**
* Called automatically by the Visibility Handler.
* This will pause the main loop and then emit a pause event.
2017-10-04 22:48:16 +00:00
*
* @method Phaser.Game#onHidden
* @protected
2018-02-12 23:51:47 +00:00
* @fires Phaser.Game#pauseEvent
2017-10-04 22:48:16 +00:00
* @since 3.0.0
*/
onHidden: function ()
{
this.loop.pause();
this.events.emit('pause');
},
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* Game Resume event.
*
* This event is dispatched when the game loop leaves a paused state and resumes running.
*
2018-02-12 23:51:47 +00:00
* @event Phaser.Game#resumeEvent
2018-01-25 00:48:48 +00:00
*/
/**
* Called automatically by the Visibility Handler.
* This will resume the main loop and then emit a resume event.
2017-10-04 22:48:16 +00:00
*
* @method Phaser.Game#onVisible
* @protected
2018-02-12 23:51:47 +00:00
* @fires Phaser.Game#resumeEvent
2017-10-04 22:48:16 +00:00
* @since 3.0.0
*/
onVisible: function ()
{
this.loop.resume();
this.events.emit('resume');
},
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* Called automatically by the Visibility Handler.
* This will set the main loop into a 'blurred' state, which pauses it.
2017-10-04 22:48:16 +00:00
*
* @method Phaser.Game#onBlur
* @protected
* @since 3.0.0
*/
onBlur: function ()
{
this.loop.blur();
},
2017-10-04 22:48:16 +00:00
/**
2018-01-25 00:48:48 +00:00
* Called automatically by the Visibility Handler.
* This will set the main loop into a 'focused' state, which resumes it.
2017-10-04 22:48:16 +00:00
*
* @method Phaser.Game#onFocus
* @protected
* @since 3.0.0
*/
onFocus: function ()
{
this.loop.focus();
2018-01-25 00:48:48 +00:00
},
/**
* Game Resize event.
*
* @event Phaser.Game#resizeEvent
* @param {number} width - The new width of the Game.
* @param {number} height - The new height of the Game.
*/
/**
* Updates the Game Config with the new width and height values given.
* Then resizes the Renderer and Input Manager scale.
*
* @method Phaser.Game#resize
* @since 3.2.0
*
* @param {number} width - The new width of the game.
* @param {number} height - The new height of the game.
*/
resize: function (width, height)
{
this.config.width = width;
this.config.height = height;
this.renderer.resize(width, height);
this.input.resize();
this.scene.resize(width, height);
this.events.emit('resize', width, height);
},
2018-01-25 00:48:48 +00:00
/**
* Destroys this Phaser.Game instance, all global systems, all sub-systems and all Scenes.
*
* @method Phaser.Game#destroy
* @since 3.0.0
*/
destroy: function (removeCanvas)
2018-01-25 00:48:48 +00:00
{
this.loop.destroy();
this.scene.destroy();
this.renderer.destroy();
this.events.emit('destroy');
this.events.removeAllListeners();
this.onStepCallback = null;
if (removeCanvas)
{
CanvasPool.remove(this.canvas);
}
}
2016-10-17 20:22:55 +00:00
});
2017-06-27 01:20:03 +00:00
2016-11-22 03:11:33 +00:00
module.exports = Game;