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');
|
2016-11-22 03:32:41 +00:00
|
|
|
var DebugHeader = require('./DebugHeader');
|
2016-12-13 16:12:25 +00:00
|
|
|
var Device = require('../device');
|
2017-07-20 10:34:01 +00:00
|
|
|
var NOOP = require('../utils/NOOP');
|
2016-12-13 16:12:25 +00:00
|
|
|
|
|
|
|
var AddToDOM = require('../dom/AddToDOM');
|
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');
|
2017-06-28 16:17:31 +00:00
|
|
|
var VisibilityHandler = require('./VisibilityHandler');
|
2016-12-13 16:12:25 +00:00
|
|
|
|
2017-10-13 10:38:34 +00:00
|
|
|
var AnimationManager = require('../animations/manager/AnimationManager');
|
2016-12-13 16:12:25 +00:00
|
|
|
var CreateRenderer = require('./CreateRenderer');
|
2018-01-10 16:29:37 +00:00
|
|
|
var Data = require('../data/Data');
|
2017-06-30 14:47:51 +00:00
|
|
|
var GlobalCache = require('../cache/GlobalCache');
|
2017-07-20 16:10:12 +00:00
|
|
|
var GlobalInputManager = require('../input/global/GlobalInputManager');
|
2017-08-18 00:42:14 +00:00
|
|
|
var GlobalSceneManager = require('../scene/global/GlobalSceneManager');
|
2018-01-16 02:08:04 +00:00
|
|
|
var PluginManager = require('../plugins/PluginManager');
|
|
|
|
var SoundManagerCreator = require('../sound/SoundManagerCreator');
|
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');
|
2016-11-22 03:11:33 +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-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class Game
|
|
|
|
* @memberOf Phaser
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
2017-11-09 13:41:23 +00:00
|
|
|
*
|
2017-10-04 22:48:16 +00:00
|
|
|
* @param {object} [GameConfig] - The configuration object for your Phaser Game instance.
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
function Game (config)
|
|
|
|
{
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Boot.Config} config
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.config = new Config(config);
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Renderer.CanvasRenderer|Phaser.Renderer.WebGLRenderer} renderer
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.renderer = null;
|
2017-10-04 22:48:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {HTMLCanvasElement} canvas
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.canvas = null;
|
2017-10-04 22:48:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {CanvasRenderingContext2D} context
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.context = null;
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {boolean} isBooted
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.isBooted = false;
|
2017-10-04 22:48:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {boolean} isRunning
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.isRunning = false;
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Events.EventDispatcher} events
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Animations.AnimationManager} anims
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.anims = new AnimationManager(this);
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Textures.TextureManager} textures
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.textures = new TextureManager(this);
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Cache.GlobalCache} cache
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.cache = new GlobalCache(this);
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {[type]} registry
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.registry = new Data(this);
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Input.GlobalInputManager} input
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.input = new GlobalInputManager(this, this.config);
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Scenes.GlobalSceneManager} scene
|
|
|
|
*/
|
2017-07-14 13:30:20 +00:00
|
|
|
this.scene = new GlobalSceneManager(this, this.config.sceneConfig);
|
2017-06-30 14:47:51 +00:00
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Device} device
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.device = Device;
|
|
|
|
|
2017-11-09 13:41:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2017-11-10 12:05:29 +00:00
|
|
|
* @property {Phaser.BaseSoundManager} sound
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Boot.TimeStep} loop
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.loop = new TimeStep(this, this.config.fps);
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Input.GlobalInputManager} input
|
|
|
|
*/
|
|
|
|
this.plugins = new PluginManager(this, this.config);
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {function} onStepCallback
|
|
|
|
*/
|
2017-07-20 10:34:01 +00:00
|
|
|
this.onStepCallback = NOOP;
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
// Wait for the DOM Ready event, then call boot.
|
|
|
|
DOMContentLoaded(this.boot.bind(this));
|
|
|
|
|
|
|
|
// For debugging only
|
|
|
|
window.game = this;
|
|
|
|
},
|
2016-11-25 01:37:14 +00:00
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#boot
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-11-25 02:08:33 +00:00
|
|
|
boot: function ()
|
|
|
|
{
|
2016-11-29 15:25:14 +00:00
|
|
|
this.isBooted = true;
|
|
|
|
|
2016-11-28 16:55:13 +00:00
|
|
|
this.config.preBoot();
|
|
|
|
|
2016-11-29 11:26:30 +00:00
|
|
|
CreateRenderer(this);
|
2016-11-25 02:08:33 +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);
|
|
|
|
|
2017-09-16 20:07:42 +00:00
|
|
|
this.textures.boot();
|
|
|
|
|
2017-04-04 22:58:45 +00:00
|
|
|
this.anims.boot(this.textures);
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
this.plugins.boot();
|
2016-11-29 10:46:35 +00:00
|
|
|
|
2017-02-21 01:04:11 +00:00
|
|
|
this.input.boot();
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
this.scene.boot();
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
this.isRunning = true;
|
2016-11-26 01:28:53 +00:00
|
|
|
|
2016-11-28 16:55:13 +00:00
|
|
|
this.config.postBoot();
|
2016-11-25 02:08:33 +00:00
|
|
|
|
2017-05-03 16:07:15 +00:00
|
|
|
this.loop.start(this.step.bind(this));
|
2017-05-09 00:24:46 +00:00
|
|
|
|
|
|
|
VisibilityHandler(this.events);
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
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-04-27 02:11:56 +00:00
|
|
|
},
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#step
|
|
|
|
* @since 3.0.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.
|
|
|
|
*/
|
2017-04-27 16:03:19 +00:00
|
|
|
step: function (time, delta)
|
2017-04-27 02:11:56 +00:00
|
|
|
{
|
2017-07-14 13:30:20 +00:00
|
|
|
var active = this.scene.active;
|
2017-04-27 02:11:56 +00:00
|
|
|
var renderer = this.renderer;
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
// Global Managers
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2017-04-27 16:03:19 +00:00
|
|
|
this.input.update(time, delta);
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2017-11-26 15:45:07 +00:00
|
|
|
this.sound.update(time, delta);
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
// Scenes
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2017-07-20 10:34:01 +00:00
|
|
|
this.onStepCallback();
|
|
|
|
|
2017-04-27 02:11:56 +00:00
|
|
|
for (var i = 0; i < active.length; i++)
|
|
|
|
{
|
2017-07-14 13:30:20 +00:00
|
|
|
active[i].scene.sys.step(time, delta);
|
2017-04-27 02:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render
|
|
|
|
|
|
|
|
// var interpolation = this.frameDelta / this.timestep;
|
|
|
|
|
|
|
|
renderer.preRender();
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
this.events.emit('prerender');
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
// This uses active.length, in case scene.update removed the scene from the active list
|
2017-04-27 02:11:56 +00:00
|
|
|
for (i = 0; i < active.length; i++)
|
|
|
|
{
|
2017-07-14 13:30:20 +00:00
|
|
|
active[i].scene.sys.render(0, renderer);
|
2017-04-27 02:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderer.postRender();
|
2018-01-16 02:08:04 +00:00
|
|
|
|
|
|
|
this.events.emit('postrender');
|
2017-05-09 00:24:46 +00:00
|
|
|
},
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#onHidden
|
|
|
|
* @protected
|
|
|
|
* @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
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
this.events.emit('pause');
|
2017-05-09 00:24:46 +00:00
|
|
|
},
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#onVisible
|
|
|
|
* @protected
|
|
|
|
* @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
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
this.events.emit('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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#onBlur
|
|
|
|
* @protected
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-05-09 14:39:30 +00:00
|
|
|
onBlur: function ()
|
|
|
|
{
|
|
|
|
this.loop.blur();
|
|
|
|
},
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#onFocus
|
|
|
|
* @protected
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-05-09 14:39:30 +00:00
|
|
|
onFocus: function ()
|
|
|
|
{
|
|
|
|
this.loop.focus();
|
|
|
|
}
|
2016-10-17 20:22:55 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
});
|
2017-06-27 01:20:03 +00:00
|
|
|
|
2016-11-22 03:11:33 +00:00
|
|
|
module.exports = Game;
|