2013-08-28 06:02:55 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
2013-08-28 06:02:55 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
Phaser.Game = function (width, height, renderType, parent, stateConfig)
|
2016-11-08 01:50:57 +00:00
|
|
|
{
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2015-02-26 10:52:30 +00:00
|
|
|
this.resolution = 1;
|
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
this.renderType = renderType;
|
2014-04-07 11:01:51 +00:00
|
|
|
this.renderer = null;
|
2016-11-08 01:50:57 +00:00
|
|
|
this.canvas = null;
|
|
|
|
this.context = null;
|
2013-12-24 03:18:55 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-08 01:50:57 +00:00
|
|
|
* @property {string|HTMLElement} parent - The Games DOM parent.
|
|
|
|
* @default
|
2013-12-24 03:18:55 +00:00
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
this.parent = parent;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
|
|
|
this.isBooted = false;
|
|
|
|
this.isRunning = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.RequestAnimationFrame} raf - Automatically handles the core game loop via requestAnimationFrame or setTimeout
|
2014-11-29 19:40:35 +00:00
|
|
|
* @protected
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2016-07-13 01:44:35 +00:00
|
|
|
this.raf = null;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2016-10-10 22:39:52 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.TextureManager} textures - Reference to the Phaser Texture Manager.
|
|
|
|
*/
|
|
|
|
this.textures = null;
|
|
|
|
|
2016-10-14 03:09:07 +00:00
|
|
|
/**
|
2016-10-14 07:58:12 +00:00
|
|
|
* @property {Phaser.UpdateManager} updates - Reference to the Phaser Update Manager.
|
2016-10-14 03:09:07 +00:00
|
|
|
*/
|
2016-10-14 07:58:12 +00:00
|
|
|
this.updates = null;
|
2016-10-14 03:09:07 +00:00
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {Phaser.Cache} cache - Reference to the assets cache.
|
|
|
|
*/
|
2013-09-10 22:51:35 +00:00
|
|
|
this.cache = null;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2016-11-10 00:10:39 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Input} input - Reference to the input manager
|
|
|
|
*/
|
|
|
|
this.input = null;
|
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
/**
|
2016-11-08 01:50:57 +00:00
|
|
|
* @property {Phaser.StateManager} state - The StateManager.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2016-11-09 14:52:53 +00:00
|
|
|
this.state = new Phaser.StateManager(this, stateConfig);
|
2013-08-29 02:52:59 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {Phaser.Device} device - Contains device information and capabilities.
|
|
|
|
*/
|
2014-11-14 02:06:45 +00:00
|
|
|
this.device = Phaser.Device;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
this.rnd = new Phaser.RandomDataGenerator([ (Date.now() * Math.random()).toString() ]);
|
2013-12-24 03:18:55 +00:00
|
|
|
|
2014-11-14 02:06:45 +00:00
|
|
|
this.device.whenReady(this.boot, this);
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
Phaser.Game.prototype.constructor = Phaser.Game;
|
2014-03-21 09:51:33 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
Phaser.Game.prototype = {
|
2013-12-24 03:18:55 +00:00
|
|
|
|
2016-10-14 05:30:07 +00:00
|
|
|
boot: function ()
|
|
|
|
{
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this.isBooted)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-04 05:41:00 +00:00
|
|
|
// Inject any new Factory helpers that exist in the build
|
|
|
|
for (var gameobject in Phaser.GameObject)
|
|
|
|
{
|
|
|
|
if (Phaser.GameObject[gameobject].hasOwnProperty('FACTORY_KEY'))
|
|
|
|
{
|
|
|
|
var key = Phaser.GameObject[gameobject]['FACTORY_KEY'];
|
|
|
|
|
|
|
|
Phaser.GameObject.Factory.prototype[key] = Phaser.GameObject[gameobject]['FACTORY_ADD'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 02:06:45 +00:00
|
|
|
this.isBooted = true;
|
|
|
|
|
|
|
|
this.setUpRenderer();
|
|
|
|
this.showDebugHeader();
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
// Global
|
|
|
|
// this.scale = new Phaser.ScaleManager(this, this.width, this.height);
|
2016-11-10 00:10:39 +00:00
|
|
|
|
|
|
|
this.scale = { offset: { x: 0, y: 0 } };
|
|
|
|
this.time = { time: function () { return Date.now(); } };
|
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
this.textures = new Phaser.TextureManager(this);
|
|
|
|
this.cache = new Phaser.Cache(this);
|
2016-11-10 00:10:39 +00:00
|
|
|
this.input = new Phaser.Input(this);
|
|
|
|
|
|
|
|
this.input.boot();
|
2014-11-14 02:06:45 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
this.state.boot();
|
2015-02-05 05:12:59 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
this.isRunning = true;
|
2015-03-24 13:27:27 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
this.rafHandle = window.requestAnimationFrame(this.step.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
// timestamp = DOMHighResTimeStamp
|
|
|
|
step: function (timestamp)
|
|
|
|
{
|
2016-11-10 00:10:39 +00:00
|
|
|
this.input.update();
|
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
this.state.step(timestamp);
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
this.rafHandle = window.requestAnimationFrame(this.step.bind(this));
|
2013-11-25 03:13:04 +00:00
|
|
|
},
|
2013-10-23 12:15:56 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-11-01 04:58:08 +00:00
|
|
|
* Displays a Phaser version debug header in the console.
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#showDebugHeader
|
|
|
|
* @protected
|
|
|
|
*/
|
2016-11-08 01:50:57 +00:00
|
|
|
showDebugHeader: function ()
|
|
|
|
{
|
|
|
|
if (Phaser.hideBanner)
|
2015-02-09 20:00:10 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-03 11:44:54 +00:00
|
|
|
var c = (this.renderType === Phaser.CANVAS) ? 'Canvas' : 'WebGL';
|
|
|
|
|
2016-09-29 01:48:48 +00:00
|
|
|
if (!this.device.ie)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
|
|
|
var args = [
|
2016-11-08 01:50:57 +00:00
|
|
|
'%c %c %c %c %c Phaser v' + Phaser.VERSION + ' / ' + c + ' %c http://phaser.io',
|
2016-09-29 01:48:48 +00:00
|
|
|
'background: #ff0000',
|
|
|
|
'background: #ffff00',
|
|
|
|
'background: #00ff00',
|
|
|
|
'background: #00ffff',
|
|
|
|
'color: #ffffff; background: #000;',
|
|
|
|
'background: #fff'
|
2013-11-25 03:13:04 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
console.log.apply(console, args);
|
|
|
|
}
|
2014-04-22 22:31:07 +00:00
|
|
|
else if (window['console'])
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2016-11-08 01:50:57 +00:00
|
|
|
console.log('Phaser v' + Phaser.VERSION + ' / http://phaser.io');
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the device is capable of using the requested renderer and sets it up or an alternative if not.
|
|
|
|
*
|
|
|
|
* @method Phaser.Game#setUpRenderer
|
|
|
|
* @protected
|
|
|
|
*/
|
2016-11-07 17:05:14 +00:00
|
|
|
setUpRenderer: function ()
|
|
|
|
{
|
2016-11-08 01:50:57 +00:00
|
|
|
// if (this.config['canvas'])
|
|
|
|
// {
|
|
|
|
// this.canvas = this.config['canvas'];
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
this.canvas = Phaser.Canvas.create(this, this.width, this.height, '', true);
|
|
|
|
// }
|
2014-05-30 01:01:33 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
// if (this.config['canvasStyle'])
|
|
|
|
// {
|
|
|
|
// this.canvas.style = this.config['canvasStyle'];
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// this.canvas.style['-webkit-full-screen'] = 'width: 100%; height: 100%';
|
|
|
|
// }
|
2014-05-30 01:01:33 +00:00
|
|
|
|
2015-09-15 11:19:31 +00:00
|
|
|
if (this.renderType === Phaser.HEADLESS || this.renderType === Phaser.CANVAS || (this.renderType === Phaser.AUTO && !this.device.webGL))
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
|
|
|
if (this.device.canvas)
|
|
|
|
{
|
2015-09-15 11:19:31 +00:00
|
|
|
// They requested Canvas and their browser supports it
|
|
|
|
this.renderType = Phaser.CANVAS;
|
|
|
|
|
2016-10-03 11:44:54 +00:00
|
|
|
this.renderer = new Phaser.Renderer.Canvas(this);
|
2013-11-25 14:53:30 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
this.context = this.renderer.context;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-15 11:19:31 +00:00
|
|
|
throw new Error('Phaser.Game - Cannot create Canvas or WebGL context, aborting.');
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-10 09:28:23 +00:00
|
|
|
// They requested WebGL and their browser supports it
|
2016-09-19 22:31:20 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
this.renderType = Phaser.WEBGL;
|
2014-10-11 03:18:42 +00:00
|
|
|
|
2016-10-04 14:39:54 +00:00
|
|
|
this.renderer = new Phaser.Renderer.WebGL(this);
|
2015-09-15 11:19:31 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
this.context = null;
|
2015-07-22 11:59:32 +00:00
|
|
|
|
2016-10-04 14:39:54 +00:00
|
|
|
// Move to renderer class
|
2016-11-08 01:50:57 +00:00
|
|
|
// this.canvas.addEventListener('webglcontextlost', this.contextLost.bind(this), false);
|
|
|
|
// this.canvas.addEventListener('webglcontextrestored', this.contextRestored.bind(this), false);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2013-08-29 16:20:59 +00:00
|
|
|
|
2014-03-12 13:45:30 +00:00
|
|
|
if (this.renderType !== Phaser.HEADLESS)
|
2014-03-11 16:14:01 +00:00
|
|
|
{
|
2014-06-05 01:33:13 +00:00
|
|
|
Phaser.Canvas.addToDOM(this.canvas, this.parent, false);
|
2014-03-11 16:14:01 +00:00
|
|
|
Phaser.Canvas.setTouchAction(this.canvas);
|
|
|
|
}
|
2013-08-29 16:20:59 +00:00
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|