2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @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');
|
2018-01-31 03:38:10 +00:00
|
|
|
var CanvasPool = require('../display/canvas/CanvasPool');
|
2017-06-30 14:47:51 +00:00
|
|
|
var Class = require('../utils/Class');
|
2016-11-24 01:35:02 +00:00
|
|
|
var Config = require('./Config');
|
2018-01-25 00:48:48 +00:00
|
|
|
var CreateRenderer = require('./CreateRenderer');
|
2018-01-31 03:38:10 +00:00
|
|
|
var DataManager = require('../data/DataManager');
|
2016-11-22 03:32:41 +00:00
|
|
|
var DebugHeader = require('./DebugHeader');
|
2016-12-13 16:12:25 +00:00
|
|
|
var Device = require('../device');
|
2016-11-25 02:08:33 +00:00
|
|
|
var DOMContentLoaded = require('../dom/DOMContentLoaded');
|
2018-01-12 17:09:09 +00:00
|
|
|
var EventEmitter = require('eventemitter3');
|
2019-01-15 15:11:19 +00:00
|
|
|
var Events = require('./events');
|
2018-01-16 16:14:21 +00:00
|
|
|
var InputManager = require('../input/InputManager');
|
2018-07-31 09:29:11 +00:00
|
|
|
var PluginCache = require('../plugins/PluginCache');
|
2018-05-10 16:14:33 +00:00
|
|
|
var PluginManager = require('../plugins/PluginManager');
|
2019-01-29 15:42:27 +00:00
|
|
|
var ScaleManager = require('../scale/ScaleManager');
|
2018-01-16 16:33:23 +00:00
|
|
|
var SceneManager = require('../scene/SceneManager');
|
2018-01-16 02:08:04 +00:00
|
|
|
var SoundManagerCreator = require('../sound/SoundManagerCreator');
|
2019-01-17 11:54:41 +00:00
|
|
|
var TextureEvents = require('../textures/events');
|
2017-01-16 22:43:46 +00:00
|
|
|
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
|
|
|
|
2018-08-23 17:10:20 +00:00
|
|
|
if (typeof EXPERIMENTAL)
|
|
|
|
{
|
2019-01-29 15:18:04 +00:00
|
|
|
var CreateDOMContainer = require('../dom/CreateDOMContainer');
|
2018-08-23 17:10:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 13:14:29 +00:00
|
|
|
if (typeof PLUGIN_FBINSTANT)
|
|
|
|
{
|
|
|
|
var FacebookInstantGamesPlugin = require('../../plugins/fbinstant/src/FacebookInstantGamesPlugin');
|
|
|
|
}
|
|
|
|
|
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
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser
|
2018-02-07 15:27:21 +00:00
|
|
|
* @constructor
|
2019-01-15 16:17:04 +00:00
|
|
|
* @fires Phaser.Core.Events#BLUR
|
|
|
|
* @fires Phaser.Core.Events#FOCUS
|
|
|
|
* @fires Phaser.Core.Events#HIDDEN
|
|
|
|
* @fires Phaser.Core.Events#VISIBLE
|
2018-02-07 15:27:21 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-27 11:30:00 +00:00
|
|
|
* @param {GameConfig} [GameConfig] - The configuration object for your Phaser Game instance.
|
2018-02-07 15:27:21 +00:00
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
var Game = new Class({
|
2016-11-24 01:35:02 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2016-11-24 17:01:52 +00:00
|
|
|
|
2017-06-30 14:47:51 +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-03-05 16:15:16 +00:00
|
|
|
*
|
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
|
2019-01-15 16:17:04 +00:00
|
|
|
* @type {Phaser.Core.Config}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-01-25 00:48:48 +00:00
|
|
|
* @since 3.0.0
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2017-06-30 14:47:51 +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
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.renderer = null;
|
2017-10-04 22:48:16 +00:00
|
|
|
|
2018-08-23 17:10:20 +00:00
|
|
|
if (typeof EXPERIMENTAL)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A reference to an HTML Div Element used as a DOM Element Container.
|
|
|
|
*
|
|
|
|
* Only set if `createDOMContainer` is `true` in the game config (by default it is `false`) and
|
|
|
|
* if you provide a parent element to insert the Phaser Game inside.
|
|
|
|
*
|
|
|
|
* See the DOM Element Game Object for more details.
|
|
|
|
*
|
|
|
|
* @name Phaser.Game#domContainer
|
|
|
|
* @type {HTMLDivElement}
|
|
|
|
* @since 3.12.0
|
|
|
|
*/
|
|
|
|
this.domContainer = null;
|
|
|
|
}
|
2018-07-18 16:22:52 +00:00
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
2018-05-10 11:25:33 +00:00
|
|
|
* A reference to the HTML Canvas Element that Phaser uses to render the game.
|
|
|
|
* This is created automatically by Phaser unless you provide a `canvas` property
|
|
|
|
* in your Game Config.
|
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
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.canvas = null;
|
2017-10-04 22:48:16 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-10 11:25:33 +00:00
|
|
|
* A reference to the Rendering Context belonging to the Canvas Element this game is rendering to.
|
|
|
|
* If the game is running under Canvas it will be a 2d Canvas Rendering Context.
|
|
|
|
* If the game is running under WebGL it will be a WebGL Rendering Context.
|
|
|
|
* This context is created automatically by Phaser unless you provide a `context` property
|
|
|
|
* in your Game Config.
|
2017-10-04 22:48:16 +00:00
|
|
|
*
|
2018-02-12 23:13:16 +00:00
|
|
|
* @name Phaser.Game#context
|
2018-05-17 14:10:23 +00:00
|
|
|
* @type {(CanvasRenderingContext2D|WebGLRenderingContext)}
|
2018-01-25 00:48:48 +00:00
|
|
|
* @since 3.0.0
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2017-06-30 14:47:51 +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-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-01-25 00:48:48 +00:00
|
|
|
* @since 3.0.0
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2017-06-30 14:47:51 +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-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-01-25 00:48:48 +00:00
|
|
|
* @since 3.0.0
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2017-06-30 14:47:51 +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
|
2018-03-29 12:12:07 +00:00
|
|
|
* @type {Phaser.Events.EventEmitter}
|
2018-01-25 00:48:48 +00:00
|
|
|
* @since 3.0.0
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2018-01-12 17:09:09 +00:00
|
|
|
this.events = new EventEmitter();
|
2017-06-30 14:47:51 +00:00
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
2018-01-25 00:48:48 +00:00
|
|
|
* An instance of the Animation Manager.
|
2018-03-05 16:15:16 +00:00
|
|
|
*
|
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
|
|
|
*/
|
2017-06-30 14:47:51 +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-03-05 16:15:16 +00:00
|
|
|
*
|
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
|
|
|
*/
|
2017-06-30 14:47:51 +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-03-05 16:15:16 +00:00
|
|
|
*
|
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-06-30 14:47:51 +00:00
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
2018-04-08 19:13:02 +00:00
|
|
|
* An instance of the Data Manager
|
2017-10-04 22:48:16 +00:00
|
|
|
*
|
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
|
|
|
*/
|
2018-01-31 03:38:10 +00:00
|
|
|
this.registry = new DataManager(this);
|
2017-06-30 14:47:51 +00:00
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
2018-01-25 00:48:48 +00:00
|
|
|
* An instance of the Input Manager.
|
2018-03-05 16:15:16 +00:00
|
|
|
*
|
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-06-30 14:47:51 +00:00
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
2018-01-25 00:48:48 +00:00
|
|
|
* An instance of the Scene Manager.
|
2018-03-05 16:15:16 +00:00
|
|
|
*
|
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-06-30 14:47:51 +00:00
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
2018-01-25 17:03:35 +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
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.device = Device;
|
|
|
|
|
2018-10-18 13:59:27 +00:00
|
|
|
/**
|
|
|
|
* An instance of the Scale Manager.
|
|
|
|
*
|
2019-01-29 23:21:24 +00:00
|
|
|
* The Scale Manager is a global system responsible for handling scaling of the game canvas.
|
2018-10-18 13:59:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Game#scale
|
2019-01-29 23:21:24 +00:00
|
|
|
* @type {Phaser.Scale.ScaleManager}
|
|
|
|
* @since 3.16.0
|
2018-10-18 13:59:27 +00:00
|
|
|
*/
|
|
|
|
this.scale = new ScaleManager(this, this.config);
|
|
|
|
|
2017-11-09 13:41:23 +00:00
|
|
|
/**
|
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.
|
2017-11-09 13:41:23 +00:00
|
|
|
*
|
2018-02-12 23:13:16 +00:00
|
|
|
* @name Phaser.Game#sound
|
2018-03-28 14:04:09 +00:00
|
|
|
* @type {Phaser.Sound.BaseSoundManager}
|
2018-01-25 00:48:48 +00:00
|
|
|
* @since 3.0.0
|
2017-11-09 13:41:23 +00:00
|
|
|
*/
|
2017-11-10 18:05:26 +00:00
|
|
|
this.sound = SoundManagerCreator.create(this);
|
2017-11-09 13:41:23 +00:00
|
|
|
|
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
|
2019-01-15 16:17:04 +00:00
|
|
|
* @type {Phaser.Core.TimeStep}
|
2018-01-25 00:48:48 +00:00
|
|
|
* @since 3.0.0
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.loop = new TimeStep(this, this.config.fps);
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
/**
|
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-01-16 02:08:04 +00:00
|
|
|
*
|
2018-02-12 23:13:16 +00:00
|
|
|
* @name Phaser.Game#plugins
|
2018-05-10 16:14:33 +00:00
|
|
|
* @type {Phaser.Plugins.PluginManager}
|
2018-01-25 00:48:48 +00:00
|
|
|
* @since 3.0.0
|
2018-01-16 02:08:04 +00:00
|
|
|
*/
|
|
|
|
this.plugins = new PluginManager(this, this.config);
|
|
|
|
|
2018-09-20 13:14:29 +00:00
|
|
|
if (typeof PLUGIN_FBINSTANT)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* An instance of the Facebook Instant Games Plugin.
|
|
|
|
*
|
|
|
|
* This will only be available if the plugin has been built into Phaser,
|
|
|
|
* or you're using the special Facebook Instant Games custom build.
|
|
|
|
*
|
|
|
|
* @name Phaser.Game#facebook
|
|
|
|
* @type {Phaser.FacebookInstantGamesPlugin}
|
|
|
|
* @since 3.13.0
|
|
|
|
*/
|
|
|
|
this.facebook = new FacebookInstantGamesPlugin(this);
|
|
|
|
}
|
|
|
|
|
2018-04-16 16:02:32 +00:00
|
|
|
/**
|
2018-04-16 16:14:12 +00:00
|
|
|
* Is this Game pending destruction at the start of the next frame?
|
2018-04-16 16:02:32 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Game#pendingDestroy
|
|
|
|
* @type {boolean}
|
|
|
|
* @private
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
this.pendingDestroy = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the Canvas once the destroy is over?
|
|
|
|
*
|
|
|
|
* @name Phaser.Game#removeCanvas
|
|
|
|
* @type {boolean}
|
|
|
|
* @private
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
this.removeCanvas = false;
|
|
|
|
|
2018-07-31 09:29:11 +00:00
|
|
|
/**
|
|
|
|
* Remove everything when the game is destroyed.
|
|
|
|
* You cannot create a new Phaser instance on the same web page after doing this.
|
|
|
|
*
|
|
|
|
* @name Phaser.Game#noReturn
|
|
|
|
* @type {boolean}
|
|
|
|
* @private
|
|
|
|
* @since 3.12.0
|
|
|
|
*/
|
|
|
|
this.noReturn = false;
|
|
|
|
|
2018-05-18 16:37:45 +00:00
|
|
|
/**
|
|
|
|
* Does the window the game is running in currently have focus or not?
|
|
|
|
* This is modified by the VisibilityHandler.
|
|
|
|
*
|
|
|
|
* @name Phaser.Game#hasFocus
|
|
|
|
* @type {boolean}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-05-18 16:37:45 +00:00
|
|
|
* @since 3.9.0
|
|
|
|
*/
|
|
|
|
this.hasFocus = false;
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
// Wait for the DOM Ready event, then call boot.
|
|
|
|
DOMContentLoaded(this.boot.bind(this));
|
|
|
|
},
|
2016-11-25 01:37:14 +00:00
|
|
|
|
2018-01-25 00:48:48 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2019-01-15 16:17:04 +00:00
|
|
|
* @fires Phaser.Core.Events#BOOT
|
2019-01-17 11:54:41 +00:00
|
|
|
* @listens Phaser.Textures.Events#READY
|
2017-10-04 22:48:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-11-25 02:08:33 +00:00
|
|
|
boot: function ()
|
|
|
|
{
|
2018-07-31 09:29:11 +00:00
|
|
|
if (!PluginCache.hasCore('EventEmitter'))
|
|
|
|
{
|
2018-10-10 09:46:47 +00:00
|
|
|
console.warn('Aborting. Core Plugins missing.');
|
2018-07-31 09:29:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
this.isBooted = true;
|
|
|
|
|
2018-03-05 16:09:12 +00:00
|
|
|
this.config.preBoot(this);
|
2016-11-28 16:55:13 +00:00
|
|
|
|
2018-10-18 13:59:27 +00:00
|
|
|
this.scale.preBoot();
|
|
|
|
|
2016-11-29 11:26:30 +00:00
|
|
|
CreateRenderer(this);
|
2016-11-25 02:08:33 +00:00
|
|
|
|
2018-08-23 17:10:20 +00:00
|
|
|
if (typeof EXPERIMENTAL)
|
|
|
|
{
|
|
|
|
CreateDOMContainer(this);
|
|
|
|
}
|
2018-07-18 16:22:52 +00:00
|
|
|
|
2017-09-13 14:27:43 +00:00
|
|
|
DebugHeader(this);
|
|
|
|
|
2016-12-07 03:42:41 +00:00
|
|
|
AddToDOM(this.canvas, this.config.parent);
|
|
|
|
|
2018-08-01 12:18:28 +00:00
|
|
|
// The Texture Manager has to wait on a couple of non-blocking events before it's fully ready.
|
|
|
|
// So it will emit this internal event when done:
|
2019-01-18 13:41:43 +00:00
|
|
|
this.textures.once(TextureEvents.READY, this.texturesReady, this);
|
|
|
|
|
|
|
|
this.events.emit(Events.BOOT);
|
2018-08-01 12:18:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called automatically when the Texture Manager has finished setting up and preparing the
|
|
|
|
* default textures.
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#texturesReady
|
|
|
|
* @private
|
|
|
|
* @fires Phaser.Game#ready
|
|
|
|
* @since 3.12.0
|
|
|
|
*/
|
|
|
|
texturesReady: function ()
|
|
|
|
{
|
|
|
|
// Start all the other systems
|
2019-01-15 16:27:32 +00:00
|
|
|
this.events.emit(Events.READY);
|
2018-08-01 12:18:28 +00:00
|
|
|
|
|
|
|
this.start();
|
2018-01-18 14:01:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
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.
|
2018-01-18 14:01:29 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Game#start
|
2018-01-25 00:48:48 +00:00
|
|
|
* @protected
|
2018-01-18 14:01:29 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
start: function ()
|
|
|
|
{
|
2016-11-29 15:25:14 +00:00
|
|
|
this.isRunning = true;
|
2016-11-26 01:28:53 +00:00
|
|
|
|
2018-03-05 16:09:12 +00:00
|
|
|
this.config.postBoot(this);
|
2016-11-25 02:08:33 +00:00
|
|
|
|
2018-02-28 21:57:32 +00:00
|
|
|
if (this.renderer)
|
|
|
|
{
|
|
|
|
this.loop.start(this.step.bind(this));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.loop.start(this.headlessStep.bind(this));
|
|
|
|
}
|
2017-05-09 00:24:46 +00:00
|
|
|
|
2018-05-18 16:37:45 +00:00
|
|
|
VisibilityHandler(this);
|
2017-05-09 00:24:46 +00:00
|
|
|
|
2018-05-11 00:50:16 +00:00
|
|
|
var eventEmitter = this.events;
|
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
eventEmitter.on(Events.HIDDEN, this.onHidden, this);
|
|
|
|
eventEmitter.on(Events.VISIBLE, this.onVisible, this);
|
|
|
|
eventEmitter.on(Events.BLUR, this.onBlur, this);
|
|
|
|
eventEmitter.on(Events.FOCUS, this.onFocus, this);
|
2017-04-27 02:11:56 +00:00
|
|
|
},
|
|
|
|
|
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
|
2019-01-15 16:17:04 +00:00
|
|
|
* @fires Phaser.Core.Events#PRE_STEP_EVENT
|
|
|
|
* @fires Phaser.Core.Events#STEP_EVENT
|
|
|
|
* @fires Phaser.Core.Events#POST_STEP_EVENT
|
|
|
|
* @fires Phaser.Core.Events#PRE_RENDER_EVENT
|
|
|
|
* @fires Phaser.Core.Events#POST_RENDER_EVENT
|
2017-10-04 22:48:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-13 07:09:44 +00:00
|
|
|
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
|
|
|
|
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2017-04-27 16:03:19 +00:00
|
|
|
step: function (time, delta)
|
2017-04-27 02:11:56 +00:00
|
|
|
{
|
2018-04-16 16:02:32 +00:00
|
|
|
if (this.pendingDestroy)
|
|
|
|
{
|
|
|
|
return this.runDestroy();
|
|
|
|
}
|
|
|
|
|
2018-05-11 00:50:16 +00:00
|
|
|
var eventEmitter = this.events;
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2018-05-11 00:50:16 +00:00
|
|
|
// Global Managers like Input and Sound update in the prestep
|
2018-05-10 16:14:33 +00:00
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.PRE_STEP, time, delta);
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2018-05-11 00:50:16 +00:00
|
|
|
// This is mostly meant for user-land code and plugins
|
2017-11-26 15:45:07 +00:00
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.STEP, time, delta);
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2018-05-11 00:50:16 +00:00
|
|
|
// Update the Scene Manager and all active Scenes
|
2017-07-20 10:34:01 +00:00
|
|
|
|
2018-01-20 04:47:03 +00:00
|
|
|
this.scene.update(time, delta);
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2018-05-11 00:50:16 +00:00
|
|
|
// Our final event before rendering starts
|
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.POST_STEP, time, delta);
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2018-01-20 04:47:03 +00:00
|
|
|
var renderer = this.renderer;
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2018-05-11 00:50:16 +00:00
|
|
|
// Run the Pre-render (clearing the canvas, setting background colors, etc)
|
|
|
|
|
2017-04-27 02:11:56 +00:00
|
|
|
renderer.preRender();
|
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.PRE_RENDER, renderer, time, delta);
|
2018-05-11 00:50:16 +00:00
|
|
|
|
|
|
|
// The main render loop. Iterates all Scenes and all Cameras in those scenes, rendering to the renderer instance.
|
2018-01-16 02:08:04 +00:00
|
|
|
|
2018-01-20 04:47:03 +00:00
|
|
|
this.scene.render(renderer);
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2018-05-11 00:50:16 +00:00
|
|
|
// The Post-Render call. Tidies up loose end, takes snapshots, etc.
|
|
|
|
|
2017-04-27 02:11:56 +00:00
|
|
|
renderer.postRender();
|
2018-01-16 02:08:04 +00:00
|
|
|
|
2018-05-11 00:50:16 +00:00
|
|
|
// The final event before the step repeats. Your last chance to do anything to the canvas before it all starts again.
|
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.POST_RENDER, renderer, time, delta);
|
2017-05-09 00:24:46 +00:00
|
|
|
},
|
|
|
|
|
2018-02-28 21:57:32 +00:00
|
|
|
/**
|
|
|
|
* A special version of the Game Step for the HEADLESS renderer only.
|
2018-03-05 16:15:16 +00:00
|
|
|
*
|
2018-02-28 21:57:32 +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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2018-09-13 07:09:44 +00:00
|
|
|
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
|
|
|
|
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
|
2018-02-28 21:57:32 +00:00
|
|
|
*/
|
|
|
|
headlessStep: function (time, delta)
|
|
|
|
{
|
2018-05-11 00:50:16 +00:00
|
|
|
var eventEmitter = this.events;
|
|
|
|
|
2018-02-28 21:57:32 +00:00
|
|
|
// Global Managers
|
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.PRE_STEP, time, delta);
|
2018-02-28 21:57:32 +00:00
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.STEP, time, delta);
|
2018-02-28 21:57:32 +00:00
|
|
|
|
|
|
|
// Scenes
|
|
|
|
|
|
|
|
this.scene.update(time, delta);
|
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.POST_STEP, time, delta);
|
2018-05-11 00:50:16 +00:00
|
|
|
|
2018-02-28 21:57:32 +00:00
|
|
|
// Render
|
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.PRE_RENDER);
|
2018-02-28 21:57:32 +00:00
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
eventEmitter.emit(Events.POST_RENDER);
|
2018-02-28 21:57:32 +00:00
|
|
|
},
|
|
|
|
|
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
|
2019-01-15 16:17:04 +00:00
|
|
|
* @fires Phaser.Core.Events#PAUSE
|
2017-10-04 22:48:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-05-09 00:24:46 +00:00
|
|
|
onHidden: function ()
|
|
|
|
{
|
|
|
|
this.loop.pause();
|
2017-06-28 21:19:41 +00:00
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
this.events.emit(Events.PAUSE);
|
2017-05-09 00:24:46 +00:00
|
|
|
},
|
|
|
|
|
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
|
2019-01-15 16:17:04 +00:00
|
|
|
* @fires Phaser.Core.Events#RESUME
|
2017-10-04 22:48:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-05-09 00:24:46 +00:00
|
|
|
onVisible: function ()
|
|
|
|
{
|
|
|
|
this.loop.resume();
|
2017-06-28 21:19:41 +00:00
|
|
|
|
2019-01-15 15:11:19 +00:00
|
|
|
this.events.emit(Events.RESUME);
|
2017-05-09 14:39:30 +00:00
|
|
|
},
|
2017-01-25 17:10:19 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2017-05-09 14:39:30 +00:00
|
|
|
onBlur: function ()
|
|
|
|
{
|
2018-05-18 16:37:45 +00:00
|
|
|
this.hasFocus = false;
|
|
|
|
|
2017-05-09 14:39:30 +00:00
|
|
|
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
|
|
|
|
*/
|
2017-05-09 14:39:30 +00:00
|
|
|
onFocus: function ()
|
|
|
|
{
|
2018-05-18 16:37:45 +00:00
|
|
|
this.hasFocus = true;
|
|
|
|
|
2017-05-09 14:39:30 +00:00
|
|
|
this.loop.focus();
|
2018-01-25 00:48:48 +00:00
|
|
|
},
|
|
|
|
|
2018-11-23 16:19:18 +00:00
|
|
|
/**
|
|
|
|
* Returns the current game frame.
|
|
|
|
* When the game starts running, the frame is incremented every time Request Animation Frame, or Set Timeout, fires.
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#getFrame
|
|
|
|
* @since 3.16.0
|
|
|
|
*
|
|
|
|
* @return {number} The current game frame.
|
|
|
|
*/
|
|
|
|
getFrame: function ()
|
|
|
|
{
|
|
|
|
return this.loop.frame;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current game timestamp.
|
|
|
|
* When the game starts running, the frame is incremented every time Request Animation Frame, or Set Timeout, fires.
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#getTime
|
|
|
|
* @since 3.16.0
|
|
|
|
*
|
|
|
|
* @return {number} The current game timestamp.
|
|
|
|
*/
|
|
|
|
getTime: function ()
|
|
|
|
{
|
|
|
|
return this.loop.frame.time;
|
|
|
|
},
|
|
|
|
|
2018-01-25 00:48:48 +00:00
|
|
|
/**
|
2018-04-16 16:14:12 +00:00
|
|
|
* Flags this Game instance as needing to be destroyed on the next frame.
|
|
|
|
* It will wait until the current frame has completed and then call `runDestroy` internally.
|
2018-07-31 09:29:11 +00:00
|
|
|
*
|
|
|
|
* If you **do not** need to run Phaser again on the same web page you can set the `noReturn` argument to `true` and it will free-up
|
|
|
|
* memory being held by the core Phaser plugins. If you do need to create another game instance on the same page, leave this as `false`.
|
2018-01-25 00:48:48 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Game#destroy
|
2019-01-15 16:17:04 +00:00
|
|
|
* @fires Phaser.Core.Events#DESTROY
|
2018-01-25 00:48:48 +00:00
|
|
|
* @since 3.0.0
|
2018-04-16 16:14:12 +00:00
|
|
|
*
|
|
|
|
* @param {boolean} removeCanvas - Set to `true` if you would like the parent canvas element removed from the DOM, or `false` to leave it in place.
|
2018-07-31 09:29:11 +00:00
|
|
|
* @param {boolean} [noReturn=false] - If `true` all the core Phaser plugins are destroyed. You cannot create another instance of Phaser on the same web page if you do this.
|
2018-01-25 00:48:48 +00:00
|
|
|
*/
|
2018-07-31 09:29:11 +00:00
|
|
|
destroy: function (removeCanvas, noReturn)
|
2018-01-25 00:48:48 +00:00
|
|
|
{
|
2018-07-31 09:29:11 +00:00
|
|
|
if (noReturn === undefined) { noReturn = false; }
|
|
|
|
|
2018-04-16 16:02:32 +00:00
|
|
|
this.pendingDestroy = true;
|
|
|
|
|
2018-04-16 16:14:12 +00:00
|
|
|
this.removeCanvas = removeCanvas;
|
2018-07-31 09:29:11 +00:00
|
|
|
this.noReturn = noReturn;
|
2018-04-16 16:02:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys this Phaser.Game instance, all global systems, all sub-systems and all Scenes.
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#runDestroy
|
|
|
|
* @private
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
runDestroy: function ()
|
|
|
|
{
|
2019-01-15 15:11:19 +00:00
|
|
|
this.events.emit(Events.DESTROY);
|
2018-04-16 16:02:32 +00:00
|
|
|
|
|
|
|
this.events.removeAllListeners();
|
2018-01-31 03:38:10 +00:00
|
|
|
|
|
|
|
this.scene.destroy();
|
|
|
|
|
2018-04-03 14:41:02 +00:00
|
|
|
if (this.renderer)
|
|
|
|
{
|
|
|
|
this.renderer.destroy();
|
|
|
|
}
|
2018-01-31 03:38:10 +00:00
|
|
|
|
2018-04-16 16:02:32 +00:00
|
|
|
if (this.removeCanvas && this.canvas)
|
2018-01-31 03:38:10 +00:00
|
|
|
{
|
|
|
|
CanvasPool.remove(this.canvas);
|
2018-04-19 13:23:55 +00:00
|
|
|
|
|
|
|
if (this.canvas.parentNode)
|
|
|
|
{
|
|
|
|
this.canvas.parentNode.removeChild(this.canvas);
|
|
|
|
}
|
2018-01-31 03:38:10 +00:00
|
|
|
}
|
2018-04-16 16:02:32 +00:00
|
|
|
|
2018-08-23 17:10:20 +00:00
|
|
|
if (typeof EXPERIMENTAL)
|
2018-07-18 16:22:52 +00:00
|
|
|
{
|
2018-08-23 17:10:20 +00:00
|
|
|
if (this.domContainer)
|
|
|
|
{
|
|
|
|
this.domContainer.parentNode.removeChild(this.domContainer);
|
|
|
|
}
|
2018-07-18 16:22:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 16:02:32 +00:00
|
|
|
this.loop.destroy();
|
2018-07-31 09:29:11 +00:00
|
|
|
|
2018-04-16 16:02:32 +00:00
|
|
|
this.pendingDestroy = false;
|
2017-05-09 14:39:30 +00:00
|
|
|
}
|
2016-10-17 20:22:55 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
});
|
2017-06-27 01:20:03 +00:00
|
|
|
|
2019-02-08 13:08:32 +00:00
|
|
|
module.exports = Game;
|
|
|
|
|
2018-10-26 18:38:08 +00:00
|
|
|
/**
|
|
|
|
* "Computers are good at following instructions, but not at reading your mind." - Donald Knuth
|
|
|
|
*/
|