phaser/v3/src/boot/Game.js

311 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 EventDispatcher = require('../events/EventDispatcher');
2017-06-28 16:17:31 +00:00
var VisibilityHandler = require('./VisibilityHandler');
2017-10-13 10:38:34 +00:00
var AnimationManager = require('../animations/manager/AnimationManager');
var CreateRenderer = require('./CreateRenderer');
var Data = require('../scene/plugins/Data');
var GlobalCache = require('../cache/GlobalCache');
var GlobalInputManager = require('../input/global/GlobalInputManager');
var GlobalSceneManager = require('../scene/global/GlobalSceneManager');
var TextureManager = require('../textures/TextureManager');
2017-06-28 16:17:31 +00:00
var TimeStep = require('./TimeStep');
var SoundManagerCreator = require('../sound/SoundManagerCreator');
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 EventDispatcher();
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]
*
* @property {Phaser.Cache.GlobalCache} cache
*/
this.cache = new GlobalCache(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]
*
* @property {Phaser.Input.GlobalInputManager} input
*/
this.input = new GlobalInputManager(this, this.config);
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @property {Phaser.Scenes.GlobalSceneManager} scene
*/
this.scene = new GlobalSceneManager(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);
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.scene.boot();
this.input.boot();
this.isRunning = true;
this.config.postBoot();
this.loop.start(this.step.bind(this));
VisibilityHandler(this.events);
this.events.on('HIDDEN', this.onHidden.bind(this));
this.events.on('VISIBLE', this.onVisible.bind(this));
this.events.on('ON_BLUR', this.onBlur.bind(this));
this.events.on('ON_FOCUS', this.onFocus.bind(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 (Time, Input, etc)
this.input.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 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();
},
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @method Phaser.Game#onHidden
* @protected
* @since 3.0.0
*/
onHidden: function ()
{
this.loop.pause();
// var active = this.scene.active;
// for (var i = 0; i < active.length; i++)
// {
// active[i].scene.sys.pause();
// }
},
2017-10-04 22:48:16 +00:00
/**
* [description]
*
* @method Phaser.Game#onVisible
* @protected
* @since 3.0.0
*/
onVisible: function ()
{
this.loop.resume();
// var active = this.scene.active;
// for (var i = 0; i < active.length; i++)
// {
// active[i].scene.sys.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;