2016-10-17 20:22:55 +00:00
|
|
|
|
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');
|
2017-05-09 00:24:46 +00:00
|
|
|
var EventDispatcher = require('../events/EventDispatcher');
|
2017-06-28 16:17:31 +00:00
|
|
|
var VisibilityHandler = require('./VisibilityHandler');
|
2016-12-13 16:12:25 +00:00
|
|
|
|
2017-06-28 16:17:31 +00:00
|
|
|
var AnimationManager = require('../animation/manager/AnimationManager');
|
2016-12-13 16:12:25 +00:00
|
|
|
var CreateRenderer = require('./CreateRenderer');
|
2017-07-04 00:59:31 +00:00
|
|
|
var Data = require('../plugins/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-07-14 13:30:20 +00:00
|
|
|
var GlobalSceneManager = require('../scene/GlobalSceneManager');
|
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-06-30 14:47:51 +00:00
|
|
|
function Game (config)
|
|
|
|
{
|
|
|
|
this.config = new Config(config);
|
|
|
|
|
|
|
|
this.renderer = null;
|
|
|
|
this.canvas = null;
|
|
|
|
this.context = null;
|
|
|
|
|
|
|
|
this.isBooted = false;
|
|
|
|
this.isRunning = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {EventDispatcher} events - Global / Global Game System Events
|
|
|
|
*/
|
|
|
|
this.events = new EventDispatcher();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.AnimationManager} anims - Reference to the Phaser Animation Manager.
|
|
|
|
*/
|
|
|
|
this.anims = new AnimationManager(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.TextureManager} textures - Reference to the Phaser Texture Manager.
|
|
|
|
*/
|
|
|
|
this.textures = new TextureManager(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Cache} cache - Reference to the assets cache.
|
|
|
|
*/
|
|
|
|
this.cache = new GlobalCache(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Data} registry - Game wide data store.
|
|
|
|
*/
|
|
|
|
this.registry = new Data(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Input} input - Reference to the input manager
|
|
|
|
*/
|
|
|
|
this.input = new GlobalInputManager(this, this.config);
|
|
|
|
|
|
|
|
/**
|
2017-07-14 13:30:20 +00:00
|
|
|
* @property {Phaser.GlobalSceneManager} scene - The SceneManager. Phaser instance specific.
|
2017-06-30 14:47:51 +00:00
|
|
|
*/
|
2017-07-14 13:30:20 +00:00
|
|
|
this.scene = new GlobalSceneManager(this, this.config.sceneConfig);
|
2017-06-30 14:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Device} device - Contains device information and capabilities (singleton)
|
|
|
|
*/
|
|
|
|
this.device = Device;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.MainLoop} mainloop - Main Loop handler.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
this.loop = new TimeStep(this, this.config.fps);
|
|
|
|
|
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
|
|
|
|
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-25 02:08:33 +00:00
|
|
|
DebugHeader(this);
|
|
|
|
|
2016-11-29 11:26:30 +00:00
|
|
|
CreateRenderer(this);
|
2016-11-25 02:08:33 +00:00
|
|
|
|
2016-12-07 03:42:41 +00:00
|
|
|
AddToDOM(this.canvas, this.config.parent);
|
|
|
|
|
2017-04-04 22:58:45 +00:00
|
|
|
this.anims.boot(this.textures);
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
this.scene.boot();
|
2016-11-29 10:46:35 +00:00
|
|
|
|
2017-02-21 01:04:11 +00:00
|
|
|
this.input.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);
|
|
|
|
|
2017-05-09 14:39:30 +00:00
|
|
|
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-04-27 02:11:56 +00:00
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Global Managers (Time, Input, etc)
|
|
|
|
|
2017-04-27 16:03:19 +00:00
|
|
|
this.input.update(time, delta);
|
2017-04-27 02:11:56 +00:00
|
|
|
|
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();
|
|
|
|
|
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();
|
2017-05-09 00:24:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onHidden: function ()
|
|
|
|
{
|
|
|
|
this.loop.pause();
|
2017-06-28 21:19:41 +00:00
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
// var active = this.scene.active;
|
2017-06-28 21:19:41 +00:00
|
|
|
|
|
|
|
// for (var i = 0; i < active.length; i++)
|
|
|
|
// {
|
2017-07-14 13:30:20 +00:00
|
|
|
// active[i].scene.sys.pause();
|
2017-06-28 21:19:41 +00:00
|
|
|
// }
|
2017-05-09 00:24:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onVisible: function ()
|
|
|
|
{
|
|
|
|
this.loop.resume();
|
2017-06-28 21:19:41 +00:00
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
// var active = this.scene.active;
|
2017-06-28 21:19:41 +00:00
|
|
|
|
|
|
|
// for (var i = 0; i < active.length; i++)
|
|
|
|
// {
|
2017-07-14 13:30:20 +00:00
|
|
|
// active[i].scene.sys.resume();
|
2017-06-28 21:19:41 +00:00
|
|
|
// }
|
2017-05-09 14:39:30 +00:00
|
|
|
},
|
2017-01-25 17:10:19 +00:00
|
|
|
|
2017-05-09 14:39:30 +00:00
|
|
|
onBlur: function ()
|
|
|
|
{
|
|
|
|
this.loop.blur();
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|