Phaser.Game = function (width, height, renderer, parent, state, transparent, antialias, physicsConfig){ this.id = Phaser.GAMES.push(this) - 1; this.config = null ; this.physicsConfig = physicsConfig; this.parent = ''; this.width = 800; this.height = 600; this.resolution = 1; this._width = 800; this._height = 600; this.transparent = false ; this.antialias = true ; 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.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 transparent !== 'undefined') { this.transparent = transparent; } if (typeof antialias !== 'undefined') { this.antialias = antialias; } 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.transparent !== undefined) { this.transparent = config.transparent; } if (config.antialias !== undefined) { this.antialias = config.antialias; } 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 ; PIXI.game = this; this.math = Phaser.Math; this.scale = new Phaser.ScaleManager(this, this._width, this._height); this.stage = new Phaser.Stage(this); this.setUpRenderer(); this.world = new Phaser.World(this); this.add = new Phaser.GameObjectFactory(this); 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.world.boot(); this.scale.boot(); this.input.boot(); this.sound.boot(); this.state.boot(); if (this.config.enableDebug) { this.debug = new Phaser.Utils.Debug(this); this.debug.boot(); } else { 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 v = Phaser.VERSION; var r = 'Canvas'; var a = 'HTML Audio'; var c = 1; if (this.renderType === Phaser.WEBGL) { r = 'WebGL'; c++ ; } else if (this.renderType == Phaser.HEADLESS) { r = 'Headless'; } if (this.device.webAudio) { a = 'WebAudio'; c++ ; } if (this.device.chrome) { var args = ['%c %c %c Phaser v' + v + ' | Pixi.js ' + PIXI.VERSION + ' | ' + r + ' | ' + a + ' %c %c ' + '%c http://phaser.io %c\u2665%c\u2665%c\u2665', 'background: #9854d8', 'background: #6c2ca7', 'color: #ffffff; background: #450f78;', 'background: #6c2ca7', 'background: #9854d8', 'background: #ffffff'] ; for (var i = 0; i < 3; i++ ){ if (i < c) { args.push('color: #ff2424; background: #fff'); } else { args.push('color: #959595; background: #fff'); } } console.log.apply(console, args); } else if (window.console) { console.log('Phaser v' + v + ' | Pixi.js ' + PIXI.VERSION + ' | ' + r + ' | ' + a + ' | 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 PIXI.CanvasRenderer(this); this.context = this.renderer.context; } else { throw new Error('Phaser.Game - Cannot create Canvas or WebGL context, aborting.') } } else { this.renderType = Phaser.WEBGL; this.renderer = new PIXI.WebGLRenderer(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); 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.scale.preUpdate(); this.debug.preUpdate(); this.camera.preUpdate(); this.physics.preUpdate(); this.state.preUpdate(timeStep); this.plugins.preUpdate(timeStep); this.stage.preUpdate(); this.state.update(); this.stage.update(); this.tweens.update(); this.sound.update(); this.input.update(); this.physics.update(); this.particles.update(); this.plugins.update(); this.stage.postUpdate(); this.plugins.postUpdate(); } else { this.scale.pauseUpdate(); this.state.pauseUpdate(); this.debug.preUpdate(); } this.stage.updateTransform(); } , updateRender: function (elapsedTime){ if (this.lockRender) { return ; } this.state.preRender(elapsedTime); if (this.renderType !== Phaser.HEADLESS) { this.renderer.render(this.stage); this.plugins.render(elapsedTime); this.state.render(elapsedTime); } this.plugins.postRender(elapsedTime); } , 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 ; } } } );