Phaser.Game = function (width, height, renderer, parent, state, pixelArt){ this.id = Phaser.GAMES.push(this) - 1; this.config = null ; this.physicsConfig = null ; this.parent = ''; this.width = 800; this.height = 600; this.resolution = 1; this._width = 800; this._height = 600; this.antialias = true ; this.multiTexture = false ; this.preserveDrawingBuffer = false ; this.clearBeforeRender = true ; this.renderer = null ; this.renderType = Phaser.AUTO; this.state = null ; this.isBooted = false ; this.isRunning = false ; this.raf = null ; this.add = null ; this.make = null ; this.textures = null ; this.updates = null ; this.cache = null ; this.input = null ; this.load = null ; this.math = null ; this.net = null ; this.scale = null ; this.sound = null ; this.stage = null ; this.time = null ; this.tweens = null ; this.world = null ; this.physics = null ; this.plugins = null ; this.rnd = null ; this.device = Phaser.Device; this.camera = null ; this.canvas = null ; this.context = null ; this.debug = null ; this.particles = null ; this.create = null ; this.lockRender = false ; this.stepping = false ; this.pendingStep = false ; this.stepCount = 0; this.onPause = null ; this.onResume = null ; this.onBlur = null ; this.onFocus = null ; this._paused = false ; this._codePaused = false ; this.currentUpdateID = 0; this.updatesThisFrame = 1; this._deltaTime = 0; this._lastCount = 0; this._spiraling = 0; this._kickstart = true ; this.fpsProblemNotifier = new Phaser.Signal(); this.forceSingleUpdate = true ; this._nextFpsNotification = 0; if (_AN_Read_length('length', arguments) === 1 && typeof arguments[0] === 'object') { this.parseConfig(arguments[0]); } else { this.config = { enableDebug: true } ; if (typeof width !== 'undefined') { this._width = width; } if (typeof height !== 'undefined') { this._height = height; } if (typeof renderer !== 'undefined') { this.renderType = renderer; } if (typeof parent !== 'undefined') { this.parent = parent; } if (typeof pixelArt !== 'undefined') { this.antialias = !pixelArt; } this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()] ); this.state = new Phaser.StateManager(this, state); } this.device.whenReady(this.boot, this); return this; } ; Phaser.Game.prototype = { parseConfig: function (config){ this.config = config; if (config.enableDebug === undefined) { this.config.enableDebug = true ; } if (config.width) { this._width = config.width; } if (config.height) { this._height = config.height; } if (config.renderer) { this.renderType = config.renderer; } if (config.parent) { this.parent = config.parent; } if (config.antialias !== undefined) { this.antialias = config.antialias; } if (config.multiTexture !== undefined) { this.multiTexture = config.multiTexture; } if (config.resolution) { this.resolution = config.resolution; } if (config.preserveDrawingBuffer !== undefined) { this.preserveDrawingBuffer = config.preserveDrawingBuffer; } if (config.physicsConfig) { this.physicsConfig = config.physicsConfig; } var seed = [(Date.now() * Math.random()).toString()] ; if (config.seed) { seed = config.seed; } this.rnd = new Phaser.RandomDataGenerator(seed); var state = null ; if (config.state) { state = config.state; } this.state = new Phaser.StateManager(this, state); } , boot: function (){ if (this.isBooted) { return ; } this.onPause = new Phaser.Signal(); this.onResume = new Phaser.Signal(); this.onBlur = new Phaser.Signal(); this.onFocus = new Phaser.Signal(); this.isBooted = true ; this.math = Phaser.Math; this.camera = { _shake: { x: 0, y: 0} } ; this.updates = new Phaser.UpdateManager(this); this.scale = new Phaser.ScaleManager(this, this._width, this._height); this.stage = new Phaser.Stage(this); this.setUpRenderer(); this.textures = new Phaser.TextureManager(this); this.add = new Phaser.GameObject.Factory(this); this.add.boot(); this.make = new Phaser.GameObjectCreator(this); this.cache = new Phaser.Cache(this); this.load = new Phaser.Loader(this); this.time = new Phaser.Time(this); this.tweens = new Phaser.TweenManager(this); this.input = new Phaser.Input(this); this.sound = new Phaser.SoundManager(this); this.physics = new Phaser.Physics(this, this.physicsConfig); this.particles = new Phaser.Particles(this); this.create = new Phaser.Create(this); this.plugins = new Phaser.PluginManager(this); this.net = new Phaser.Net(this); this.time.boot(); this.stage.boot(); this.scale.boot(); this.input.boot(); this.sound.boot(); this.state.boot(); this.debug = { preUpdate: function (){ } , update: function (){ } , reset: function (){ } } ; this.showDebugHeader(); this.isRunning = true ; if (this.config && this.config.forceSetTimeOut) { this.raf = new Phaser.RequestAnimationFrame(this, this.config.forceSetTimeOut); } else { this.raf = new Phaser.RequestAnimationFrame(this, false ); } this._kickstart = true ; if (window.focus) { if (!window.PhaserGlobal || (window.PhaserGlobal && !window.PhaserGlobal.stopFocus)) { window.focus(); } } this.raf.start(); } , showDebugHeader: function (){ if (window.PhaserGlobal && window.PhaserGlobal.hideBanner) { return ; } var c = (this.renderType === Phaser.CANVAS)? 'Canvas': 'WebGL'; if (!this.device.ie) { var args = ['%c %c %c %c %c Phaser v' + Phaser.VERSION + ' / Pixi.js / ' + c + ' %c http://phaser.io', 'background: #ff0000', 'background: #ffff00', 'background: #00ff00', 'background: #00ffff', 'color: #ffffff; background: #000;', 'background: #fff'] ; console.log.apply(console, args); } else if (window.console) { console.log('Phaser v' + Phaser.VERSION + ' / Pixi.js / http://phaser.io'); } } , setUpRenderer: function (){ if (this.config.canvas) { this.canvas = this.config.canvas; } else { this.canvas = Phaser.Canvas.create(this, this.width, this.height, this.config.canvasID, true ); } if (this.config.canvasStyle) { this.canvas.style = this.config.canvasStyle; } else { this.canvas.style["-webkit-full-screen"] = 'width: 100%; height: 100%'; } if (this.renderType === Phaser.HEADLESS || this.renderType === Phaser.CANVAS || (this.renderType === Phaser.AUTO && !this.device.webGL)) { if (this.device.canvas) { this.renderType = Phaser.CANVAS; this.renderer = new Phaser.Renderer.Canvas(this); this.context = this.renderer.context; } else { throw new Error('Phaser.Game - Cannot create Canvas or WebGL context, aborting.') } } else { if (this.multiTexture || this.renderType === Phaser.WEBGL_MULTI) { PIXI.enableMultiTexture(); } this.renderType = Phaser.WEBGL; this.renderer = new Phaser.Renderer.WebGL(this); this.context = null ; this.canvas.addEventListener('webglcontextlost', this.contextLost.bind(this), false ); this.canvas.addEventListener('webglcontextrestored', this.contextRestored.bind(this), false ); } if (this.device.cocoonJS) { this.canvas.screencanvas = (this.renderType === Phaser.CANVAS)? true : false ; } if (this.renderType !== Phaser.HEADLESS) { this.stage.smoothed = this.antialias; Phaser.Canvas.addToDOM(this.canvas, this.parent, false ); Phaser.Canvas.setTouchAction(this.canvas); } } , contextLost: function (event){ event.preventDefault(); this.renderer.contextLost = true ; } , contextRestored: function (){ this.renderer.initContext(); this.cache.clearGLTextures(); this.renderer.contextLost = false ; } , update: function (time){ this.time.update(time); this.updateLogic(this.time.desiredFpsMult); this.updateRender(this.time.slowMotion * this.time.desiredFps); return ; if (this._kickstart) { this.updateLogic(this.time.desiredFpsMult); this.updateRender(this.time.slowMotion * this.time.desiredFps); this._kickstart = false ; return ; } if (this._spiraling > 1 && !this.forceSingleUpdate) { if (this.time.time > this._nextFpsNotification) { this._nextFpsNotification = this.time.time + 10000; this.fpsProblemNotifier.dispatch(); } this._deltaTime = 0; this._spiraling = 0; this.updateRender(this.time.slowMotion * this.time.desiredFps); } else { var slowStep = this.time.slowMotion * 1000 / this.time.desiredFps; this._deltaTime += Math.max(Math.min(slowStep * 3, this.time.elapsed), 0); var count = 0; this.updatesThisFrame = Math.floor(this._deltaTime / slowStep); if (this.forceSingleUpdate) { this.updatesThisFrame = Math.min(1, this.updatesThisFrame); } while (this._deltaTime >= slowStep){ this._deltaTime -= slowStep; this.currentUpdateID = count; this.updateLogic(this.time.desiredFpsMult); count++ ; if (this.forceSingleUpdate && count === 1) { break ; } else { _AN_Call_refresh('refresh', this.time); } } if (count > this._lastCount) { this._spiraling++ ; } else if (count < this._lastCount) { this._spiraling = 0; } this._lastCount = count; this.updateRender(this._deltaTime / slowStep); } } , updateLogic: function (timeStep){ if (!this._paused && !this.pendingStep) { if (this.stepping) { this.pendingStep = true ; } this.state.preUpdate(timeStep); this.stage.preUpdate(); this.tweens.update(); this.state.update(); this.stage.update(); this.stage.postUpdate(); this.plugins.postUpdate(); } else { this.scale.pauseUpdate(); this.state.pauseUpdate(); this.debug.preUpdate(); } } , updateRender: function (elapsedTime){ if (this.lockRender) { return ; } this.state.preRender(elapsedTime); this.updates.start(); this.renderer.render(this.stage); this.state.render(elapsedTime); this.updates.stop(); } , enableStep: function (){ this.stepping = true ; this.pendingStep = false ; this.stepCount = 0; } , disableStep: function (){ this.stepping = false ; this.pendingStep = false ; } , step: function (){ this.pendingStep = false ; this.stepCount++ ; } , destroy: function (){ this.raf.stop(); this.state.destroy(); this.sound.destroy(); this.scale.destroy(); this.stage.destroy(); this.input.destroy(); this.physics.destroy(); this.plugins.destroy(); this.state = null ; this.sound = null ; this.scale = null ; this.stage = null ; this.input = null ; this.physics = null ; this.plugins = null ; this.cache = null ; this.load = null ; this.time = null ; this.world = null ; this.isBooted = false ; this.renderer.destroy(false ); Phaser.Canvas.removeFromDOM(this.canvas); PIXI.defaultRenderer = null ; Phaser.GAMES[this.id] = null ; } , gamePaused: function (event){ if (!this._paused) { this._paused = true ; this.time.gamePaused(); if (this.sound.muteOnPause) { this.sound.setMute(); } this.onPause.dispatch(event); if (this.device.cordova && this.device.iOS) { this.lockRender = true ; } } } , gameResumed: function (event){ if (this._paused && !this._codePaused) { this._paused = false ; this.time.gameResumed(); this.input.reset(); if (this.sound.muteOnPause) { this.sound.unsetMute(); } this.onResume.dispatch(event); if (this.device.cordova && this.device.iOS) { this.lockRender = false ; } } } , focusLoss: function (event){ this.onBlur.dispatch(event); if (!this.stage.disableVisibilityChange) { this.gamePaused(event); } } , focusGain: function (event){ this.onFocus.dispatch(event); if (!this.stage.disableVisibilityChange) { this.gameResumed(event); } } } ; Phaser.Game.prototype.constructor = Phaser.Game; Object.defineProperty(Phaser.Game.prototype, "paused", { get: function (){ return this._paused; } , set: function (value){ if (value === true ) { if (this._paused === false ) { this._paused = true ; this.sound.setMute(); this.time.gamePaused(); this.onPause.dispatch(this); } this._codePaused = true ; } else { if (this._paused) { this._paused = false ; this.input.reset(); this.sound.unsetMute(); this.time.gameResumed(); this.onResume.dispatch(this); } this._codePaused = false ; } } } );