phaser/src/boot/Game.js

317 lines
6.9 KiB
JavaScript
Raw Normal View History

var Class = require('../utils/Class');
var Config = require('./Config');
2016-11-22 03:32:41 +00:00
var DebugHeader = require('./DebugHeader');
var Device = require('../device');
2017-07-20 10:34:01 +00:00
var NOOP = require('../utils/NOOP');
var AddToDOM = require('../dom/AddToDOM');
var DOMContentLoaded = require('../dom/DOMContentLoaded');
var EventEmitter = require('eventemitter3');
2017-06-28 16:17:31 +00:00
var VisibilityHandler = require('./VisibilityHandler');
2018-01-16 13:04:35 +00:00
var AnimationManager = require('../animations/AnimationManager');
2018-01-16 16:33:23 +00:00
var CacheManager = require('../cache/CacheManager');
var CreateRenderer = require('./CreateRenderer');
var Data = require('../data/Data');
2018-01-16 16:14:21 +00:00
var InputManager = require('../input/InputManager');
var PluginManager = require('../plugins/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');
2016-11-22 03:11:33 +00:00
var Game = new Class({
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-10-04 22:48:16 +00:00
* @param {object} [GameConfig] - The configuration object for your Phaser Game instance.
*/
function Game (config)
{
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {Phaser.Boot.Config} config
*/
this.config = new Config(config);
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {Phaser.Renderer.CanvasRenderer|Phaser.Renderer.WebGLRenderer} renderer
*/
this.renderer = null;
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {HTMLCanvasElement} canvas
*/
this.canvas = null;
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {CanvasRenderingContext2D} context
*/
this.context = null;
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {boolean} isBooted
*/
this.isBooted = false;
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {boolean} isRunning
*/
this.isRunning = false;
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {Phaser.Events.EventDispatcher} events
*/
this.events = new EventEmitter();
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {Phaser.Animations.AnimationManager} anims
*/
this.anims = new AnimationManager(this);
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {Phaser.Textures.TextureManager} textures
*/
this.textures = new TextureManager(this);
2017-10-04 22:48:16 +00:00
/**
* [description]
*
2018-01-16 16:30:11 +00:00
* @property {Phaser.Cache.CacheManager} cache
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]
*
* @property {[type]} registry
*/
this.registry = new Data(this);
2017-10-04 22:48:16 +00:00
/**
* [description]
*
2018-01-16 16:14:21 +00:00
* @property {Phaser.Input.InputManager} input
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
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @property {Phaser.Scenes.SceneManager} scene
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
/**
* [description]
*
* @property {Phaser.Device} device
*/
this.device = Device;
/**
* [description]
*
* @property {Phaser.BaseSoundManager} sound
*/
this.sound = SoundManagerCreator.create(this);
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {Phaser.Boot.TimeStep} loop
*/
this.loop = new TimeStep(this, this.config.fps);
/**
* [description]
*
2018-01-16 16:14:21 +00:00
* @property {Phaser.Plugins.PluginManager} plugins
*/
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;
// Wait for the DOM Ready event, then call boot.
DOMContentLoaded(this.boot.bind(this));
// For debugging only
window.game = this;
},
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @method Phaser.Game#boot
* @since 3.0.0
*/
boot: function ()
{
this.isBooted = true;
this.config.preBoot();
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.textures.boot();
2017-04-04 22:58:45 +00:00
this.anims.boot(this.textures);
this.plugins.boot();
this.input.boot();
this.scene.boot();
this.isRunning = true;
this.config.postBoot();
this.loop.start(this.step.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
/**
* [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.
*/
step: function (time, delta)
{
var active = this.scene.active;
var renderer = this.renderer;
// 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();
for (var i = 0; i < active.length; i++)
{
active[i].scene.sys.step(time, delta);
}
// Render
// var interpolation = this.frameDelta / this.timestep;
renderer.preRender();
this.events.emit('prerender');
// This uses active.length, in case scene.update removed the scene from the active list
for (i = 0; i < active.length; i++)
{
active[i].scene.sys.render(0, renderer);
}
renderer.postRender();
this.events.emit('postrender');
},
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @method Phaser.Game#onHidden
* @protected
* @since 3.0.0
*/
onHidden: function ()
{
this.loop.pause();
this.events.emit('pause');
},
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @method Phaser.Game#onVisible
* @protected
* @since 3.0.0
*/
onVisible: function ()
{
this.loop.resume();
this.events.emit('resume');
},
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @method Phaser.Game#onBlur
* @protected
* @since 3.0.0
*/
onBlur: function ()
{
this.loop.blur();
},
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @method Phaser.Game#onFocus
* @protected
* @since 3.0.0
*/
onFocus: function ()
{
this.loop.focus();
}
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;