From 4bbcc380c5f27b405de16b901b9d523aefd35ff9 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Thu, 29 Aug 2013 07:06:16 +0100 Subject: [PATCH] State handling done. Refactored the Game constructor a LOT. Now works from within closures, outside of them, with Phaser.State objects or normal Objects with the right functions inside. Also fixed some small bugs in PluginManager and various scope issues with RAF. --- Phaser.sublime-workspace | 473 ++++++---------------------- examples/flow.php | 53 ++++ examples/flow2.php | 55 ++++ examples/flow3.php | 50 +++ examples/js.php | 67 ++++ src/Game.js | 210 +++++++----- src/Phaser.js | 10 +- src/core/Plugin.js | 2 +- src/core/PluginManager.js | 2 +- src/core/State.js | 73 +++++ src/system/RequestAnimationFrame.js | 26 +- src/tween/TweenManager.js | 12 +- 12 files changed, 558 insertions(+), 475 deletions(-) create mode 100644 examples/flow.php create mode 100644 examples/flow2.php create mode 100644 examples/flow3.php create mode 100644 examples/js.php create mode 100644 src/core/State.js diff --git a/Phaser.sublime-workspace b/Phaser.sublime-workspace index cf201740..f0b37b13 100644 --- a/Phaser.sublime-workspace +++ b/Phaser.sublime-workspace @@ -3,6 +3,34 @@ { "selected_items": [ + [ + "remem", + "rememberMe" + ], + [ + "start", + "startState" + ], + [ + "swit", + "switchState" + ], + [ + "onpause", + "onPausedCallback" + ], + [ + "onload", + "onLoadUpdateCallback" + ], + [ + "onp", + "onPreloadCallback (variable)" + ], + [ + "upda", + "updateSetTimeout" + ], [ "inter", "intersectsRaw" @@ -195,10 +223,6 @@ "jukeb", "jukeboxPP" ], - [ - "start", - "startTheEngines" - ], [ "star", "startPause" @@ -490,30 +514,6 @@ [ "die", "diveSpeed" - ], - [ - "wave", - "waveY" - ], - [ - "dive", - "diveCanvas" - ], - [ - "wa", - "waveY" - ], - [ - "di", - "diveContext" - ], - [ - "divec", - "diveContext" - ], - [ - "wavet", - "waveTween1" ] ] }, @@ -523,26 +523,9 @@ "file": "src/Game.js", "settings": { - "buffer_size": 5482, + "buffer_size": 15461, "line_ending": "Windows" } - }, - { - "file": "todo/Tween.js", - "settings": - { - "buffer_size": 12434, - "line_ending": "Windows" - } - }, - { - "file": "src/tween/TweenManager.js", - "settings": - { - "buffer_size": 3317, - "line_ending": "Windows", - "name": "Phaser.TweenManager = function () {" - } } ], "build_system": "", @@ -560,10 +543,7 @@ }, "console": { - "height": 146.0, - "history": - [ - ] + "height": 146.0 }, "distraction_free": { @@ -576,6 +556,38 @@ }, "file_history": [ + "/D/wamp/www/phaser/examples/flow.php", + "/D/wamp/www/phaser/examples/flow3.php", + "/D/wamp/www/phaser/src/core/State.js", + "/D/wamp/www/phaser/examples/flow2.php", + "/D/wamp/www/phaser/TS Tests/tilemaps/csv tilemap.js", + "/D/wamp/www/phaser/src/system/RequestAnimationFrame.js", + "/D/wamp/www/phaser/TS Source/system/RequestAnimationFrame.js", + "/D/wamp/www/phaser/TS Source/Game.js", + "/D/wamp/www/phaser/TS Source/State.js", + "/D/wamp/www/phaser/examples/js.php", + "/D/wamp/www/phaser/src/Phaser.js", + "/D/wamp/www/phaser/TS Tests/audio/audio sprites 1.ts", + "/D/wamp/www/phaser/examples/loader 2.html", + "/D/wamp/www/phaser/src/tween/TweenManager.js", + "/D/wamp/www/phaser/src/core/Plugin.js", + "/D/wamp/www/phaser/src/core/PluginManager.js", + "/D/wamp/www/index.php", + "/D/wamp/www/phaser/src/loader/Loader.js", + "/D/wamp/www/phaser/TS Source/core/Plugin.js", + "/D/wamp/www/phaser/TS Source/core/PluginManager.js", + "/D/wamp/www/phaser/TS Source/Game.ts", + "/D/wamp/www/phaser/examples/pixi 1.html", + "/D/wamp/www/pixi.js/examples/example 1 - Basics/index.html", + "/D/wamp/www/phaser/src/pixi/textures/Texture.js", + "/D/wamp/www/phaser/src/pixi/textures/BaseTexture.js", + "/D/wamp/www/phaser/examples/loader.html", + "/D/wamp/www/phaser/examples/math sincos.html", + "/D/wamp/www/phaser/examples/raf.html", + "/D/wamp/www/phaser/src/time/Time.js", + "/D/wamp/www/phaser/src/pixi/utils/Utils.js", + "/D/wamp/www/phaser/TS Source/system/RequestAnimationFrame.ts", + "/D/wamp/www/phaser/todo/Tween.js", "/D/wamp/www/phaser/TS Source/tweens/TweenManager.js", "/D/wamp/www/phaser/src/geom/Rectangle.js", "/D/wamp/www/phaser/TS Source/utils/RectangleUtils.js", @@ -590,17 +602,14 @@ "/D/wamp/www/phaser/TS Source/time/TimeManager.js", "/D/wamp/www/phaser/TS Source/time/TimeManager.ts", "/D/wamp/www/phaser/TS Source/math/GameMath.js", - "/D/wamp/www/phaser/examples/math sincos.html", "/D/wamp/www/phaser/examples/circle.html", "/D/wamp/www/phaser/TS Source/utils/CircleUtils.js", "/D/wamp/www/phaser/TS Source/geom/Circle.js", - "/D/wamp/www/phaser/src/time/Time.js", "/D/wamp/www/phaser/src/net/Net.js", "/D/wamp/www/phaser/examples/net.html", "/D/wamp/www/phaser/TS Source/net/Net.js", "/D/wamp/www/phaser/src/animation/Parser.js", "/D/wamp/www/phaser/src/loader/Cache.js", - "/D/wamp/www/phaser/src/loader/Loader.js", "/D/wamp/www/phaser/examples/loader audio.html", "/D/wamp/www/phaser/examples/loader spritesheet.html", "/D/wamp/www/phaser/examples/loader atlas xml.html", @@ -616,14 +625,10 @@ "/D/wamp/www/phaser/TS Source/animation/FrameData.js", "/D/wamp/www/phaser/TS Source/animation/Frame.ts", "/D/wamp/www/phaser/TS Source/animation/Frame.js", - "/D/wamp/www/phaser/examples/loader 2.html", "/D/wamp/www/bbc-wolfblood/game.js", - "/D/wamp/www/phaser/examples/loader.html", "/D/wamp/www/phaser/TS Source/loader/Loader.js", - "/D/wamp/www/phaser/TS Source/Game.js", "/D/wamp/www/phaser/TS Source/loader/Cache.js", "/D/wamp/www/phaser/TS Source/loader/Cache.ts", - "/D/wamp/www/phaser/src/Phaser.js", "/D/wamp/www/phaser/TS Source/loader/Loader.ts", "/D/wamp/www/phaser/src/math/RandomDataGenerator.js", "/D/wamp/www/phaser/src/core/SignalBinding.js", @@ -633,7 +638,6 @@ "/D/wamp/www/phaser/TS Source/core/Signal.js", "/D/wamp/www/js-signals/src/Signal.js", "/D/wamp/www/js-signals/src/wrapper.js", - "/D/wamp/www/pixi.js/examples/example 1 - Basics/index.html", "/D/wamp/www/awasenghi/lib/Core.js", "/D/wamp/www/phaser/TS Source/math/RandomDataGenerator.js", "/D/wamp/www/Nonsense/Nonsense.js", @@ -679,31 +683,7 @@ "/D/wamp/www/Proton/src/zone/Zone.js", "/D/wamp/www/Proton/src/zone/PointZone.js", "/D/wamp/www/Proton/src/plus/ease.js", - "/D/wamp/www/Proton/src/behaviour/RandomDrift.js", - "/D/wamp/www/Proton/src/math/ColorSpan.js", - "/D/wamp/www/Proton/src/math/NumericalIntegration.js", - "/D/wamp/www/Proton/src/math/MathUtils.js", - "/D/wamp/www/Proton/src/behaviour/Behaviour.js", - "/D/wamp/www/Proton/src/initialize/Velocity.js", - "/D/wamp/www/Proton/src/math/Polar2D.js", - "/D/wamp/www/Proton/src/behaviour/Scale.js", - "/D/wamp/www/Proton/src/behaviour/Repulsion.js", - "/D/wamp/www/Proton/src/initialize/Rate.js", - "/D/wamp/www/Proton/src/core/Proton.js", - "/D/wamp/www/Proton/src/core/Particle.js", - "/D/wamp/www/Proton/src/core/ParticlePool.js", - "/D/wamp/www/Proton/src/initialize/Life.js", - "/D/wamp/www/Proton/src/initialize/Radius.js", - "/D/wamp/www/Proton/src/utils/Util.js", - "/D/wamp/www/Proton/src/math/Span.js", - "/D/wamp/www/Proton/src/initialize/Initialize.js", - "/D/wamp/www/Proton/src/initialize/Mass.js", - "/D/wamp/www/bbc-jigsaw/sarahandduck/frame.html", - "/D/wamp/www/phaser/Tests/phaser.css", - "/D/wamp/www/phaser/Docs/tags.txt", - "/D/wamp/www/phaser/Docs/docs_server.bat", - "/D/wamp/www/phaser/Docs/docs_build.bat", - "/D/wamp/www/phaser/Phaser/Phaser.csproj" + "/D/wamp/www/Proton/src/behaviour/RandomDrift.js" ], "find": { @@ -711,7 +691,7 @@ }, "find_in_files": { - "height": 90.0, + "height": 0.0, "where_history": [ "D:\\wamp\\www\\phaser\\src\\pixi", @@ -736,6 +716,22 @@ "case_sensitive": false, "find_history": [ + "game", + "State.prototype.", + "preload", + "Callback", + "Plugin.prototype.", + "this._parent", + "PluginManager.prototype.", + "public ", + "startSt", + "_this", + "Phaser.Math", + "_raf", + "raf", + "delta", + "_raf", + "RequestAnimationFrame.prototype.", "TweenManager.prototype.", "Phaser.Rectangle.", "a.", @@ -847,23 +843,7 @@ "solveVelocity", "velocitySolver", "initSolve", - "step", - "contactSolverArr", - "add", - "findContact", - "contactSolvers", - "add", - "norma", - "saveSceneToServer", - "console", - "saveSceneToServer", - "drawBox", - "drawBodyShape", - "scale", - "damping", - "gravity", - "timeDelta", - " for (var i = 0; i <= 10; i++) {\n var body = new Body(Body.DYNAMIC, new vec2(-6 + i * 1.2, 8));\n var shape = new ShapeCircle(0, 0, 0.4);\n shape.e = i / 10;\n shape.u = 1.0;\n shape.density = 1;\n body.addShape(shape);\n body.resetMassData();\n space.addBody(body);\n }\n" + "step" ], "highlight": true, "in_selection": false, @@ -871,6 +851,7 @@ "regex": false, "replace_history": [ + "elapsed", "queueSize", "{boolean}", "queueSize", @@ -911,24 +892,23 @@ "groups": [ { - "selected": 2, + "selected": 0, "sheets": [ { "buffer": 0, "file": "src/Game.js", - "semi_transient": false, "settings": { - "buffer_size": 5482, + "buffer_size": 15461, "regions": { }, "selection": [ [ - 1281, - 1281 + 12999, + 12999 ] ], "settings": @@ -1040,269 +1020,7 @@ "translate_tabs_to_spaces": false }, "translation.x": 0.0, - "translation.y": 0.0, - "zoom_level": 1.0 - }, - "type": "text" - }, - { - "buffer": 1, - "file": "todo/Tween.js", - "semi_transient": false, - "settings": - { - "buffer_size": 12434, - "regions": - { - }, - "selection": - [ - [ - 726, - 1538 - ] - ], - "settings": - { - "codeintel": true, - "codeintel_config": - { - "JavaScript": - { - "codeintel_max_recursive_dir_depth": 2, - "codeintel_scan_files_in_project": false, - "javascriptExtraPaths": - [ - ] - }, - "PHP": - { - "codeintel_max_recursive_dir_depth": 5, - "codeintel_scan_files_in_project": false, - "phpExtraPaths": - [ - ] - }, - "Python": - { - "env": - { - } - } - }, - "codeintel_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_live": true, - "codeintel_live_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_max_recursive_dir_depth": 10, - "codeintel_scan_exclude_dir": - { - "JavaScript": - [ - "/build/", - "/min/" - ] - }, - "codeintel_scan_files_in_project": true, - "codeintel_selected_catalogs": - [ - "PyWin32", - "jQuery", - "Rails" - ], - "codeintel_snippets": true, - "codeintel_syntax_map": - { - "Python Django": "Python" - }, - "sublime_auto_complete": true, - "syntax": "Packages/JavaScript/JavaScript.tmLanguage", - "translate_tabs_to_spaces": false - }, - "translation.x": 0.0, - "translation.y": 798.0, - "zoom_level": 1.0 - }, - "type": "text" - }, - { - "buffer": 2, - "file": "src/tween/TweenManager.js", - "semi_transient": false, - "settings": - { - "buffer_size": 3317, - "regions": - { - }, - "selection": - [ - [ - 720, - 720 - ] - ], - "settings": - { - "auto_name": "Phaser.TweenManager = function () {", - "codeintel": true, - "codeintel_config": - { - "JavaScript": - { - "codeintel_max_recursive_dir_depth": 2, - "codeintel_scan_files_in_project": false, - "javascriptExtraPaths": - [ - ] - }, - "PHP": - { - "codeintel_max_recursive_dir_depth": 5, - "codeintel_scan_files_in_project": false, - "phpExtraPaths": - [ - ] - }, - "Python": - { - "env": - { - } - } - }, - "codeintel_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_live": true, - "codeintel_live_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_max_recursive_dir_depth": 10, - "codeintel_scan_exclude_dir": - { - "JavaScript": - [ - "/build/", - "/min/" - ] - }, - "codeintel_scan_files_in_project": true, - "codeintel_selected_catalogs": - [ - "PyWin32", - "jQuery", - "Rails" - ], - "codeintel_snippets": true, - "codeintel_syntax_map": - { - "Python Django": "Python" - }, - "sublime_auto_complete": true, - "syntax": "Packages/JavaScript/JavaScript.tmLanguage" - }, - "translation.x": 0.0, - "translation.y": 0.0, + "translation.y": 8910.0, "zoom_level": 1.0 }, "type": "text" @@ -1341,7 +1059,6 @@ ] }, "menu_visible": true, - "project": "Phaser.sublime-project", "replace": { "height": 62.0 @@ -1363,24 +1080,10 @@ ], "width": 0.0 }, - "select_symbol": - { - "height": 0.0, - "selected_items": - [ - ], - "width": 0.0 - }, - "settings": - { - }, "show_minimap": true, "show_open_files": false, "show_tabs": true, "side_bar_visible": true, "side_bar_width": 284.0, - "status_bar_visible": true, - "template_settings": - { - } + "status_bar_visible": true } diff --git a/examples/flow.php b/examples/flow.php new file mode 100644 index 00000000..f535e32d --- /dev/null +++ b/examples/flow.php @@ -0,0 +1,53 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/examples/flow2.php b/examples/flow2.php new file mode 100644 index 00000000..e6a2fd36 --- /dev/null +++ b/examples/flow2.php @@ -0,0 +1,55 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/examples/flow3.php b/examples/flow3.php new file mode 100644 index 00000000..ee23ecbc --- /dev/null +++ b/examples/flow3.php @@ -0,0 +1,50 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/examples/js.php b/examples/js.php new file mode 100644 index 00000000..8a9b743a --- /dev/null +++ b/examples/js.php @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Game.js b/src/Game.js index 15bdf7d7..172b43f4 100644 --- a/src/Game.js +++ b/src/Game.js @@ -13,25 +13,28 @@ * @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License */ -Phaser.Game = function (callbackContext, parent, width, height, preloadCallback, createCallback, updateCallback, renderCallback, destroyCallback) { +/** +* Game constructor +* +* Instantiate a new Phaser.Game object. +* +* @constructor +* @param width {number} The width of your game in game pixels. +* @param height {number} The height of your game in game pixels. +* @param renderer {number} Which renderer to use (canvas or webgl) +* @param parent {string} ID of its parent DOM element. +*/ +Phaser.Game = function (width, height, renderer, parent) { - if (typeof parent === "undefined") { parent = ''; } if (typeof width === "undefined") { width = 800; } if (typeof height === "undefined") { height = 600; } - if (typeof preloadCallback === "undefined") { preloadCallback = null; } - if (typeof createCallback === "undefined") { createCallback = null; } - if (typeof updateCallback === "undefined") { updateCallback = null; } - if (typeof renderCallback === "undefined") { renderCallback = null; } - if (typeof destroyCallback === "undefined") { destroyCallback = null; } + if (typeof renderer === "undefined") { renderer = Phaser.RENDERER_AUTO; } + if (typeof parent === "undefined") { parent = ''; } this.id = Phaser.GAMES.push(this) - 1; + this.renderer = renderer; - this.callbackContext = callbackContext; - this.onPreloadCallback = preloadCallback; - this.onCreateCallback = createCallback; - this.onUpdateCallback = updateCallback; - this.onRenderCallback = renderCallback; - this.onDestroyCallback = destroyCallback; + //var _this = this; if (document.readyState === 'complete' || document.readyState === 'interactive') { @@ -43,6 +46,8 @@ Phaser.Game = function (callbackContext, parent, width, height, preloadCallback, window.addEventListener('load', Phaser.GAMES[this.id].boot(parent, width, height), false); } + return this; + }; Phaser.Game.prototype = { @@ -53,6 +58,12 @@ Phaser.Game.prototype = { */ id: 0, + /** + * The Renderer this Phaser.Game will use. Either Phaser.RENDERER_AUTO, Phaser.RENDERER_CANVAS or Phaser.RENDERER_WEBGL + * @type {number} + */ + renderer: 0, + /** * Whether load complete loading or not. * @type {bool} @@ -244,13 +255,18 @@ Phaser.Game.prototype = { return; } + console.log('Phaser.Game.Boot'); + console.log(this); + + var _this = this; + if (!document.body) { - setTimeout(Phaser.GAMES[this.id].boot(parent, width, height), 13); + setTimeout(Phaser.GAMES[_this.id].boot(parent, width, height), 13); } else { - document.removeEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot); - window.removeEventListener('load', Phaser.GAMES[this.id].boot); + document.removeEventListener('DOMContentLoaded', Phaser.GAMES[_this.id].boot); + window.removeEventListener('load', Phaser.GAMES[_this.id].boot); this.onPause = new Phaser.Signal(); this.onResume = new Phaser.Signal(); @@ -270,49 +286,99 @@ Phaser.Game.prototype = { this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]); // this.physics = new Phaser.Physics.PhysicsManager(this); this.plugins = new Phaser.PluginManager(this, this); - + this.load.onLoadComplete.add(this.loadComplete, this); - // this.setRenderer(Phaser.Types.RENDERER_CANVAS); // this.world.boot(); // this.stage.boot(); // this.input.boot(); + console.log('Phaser', Phaser.VERSION, 'initialized'); + this.isBooted = true; + this.isRunning = true; + this._loadComplete = false; - if (this.onPreloadCallback == null && this.onCreateCallback == null && this.onUpdateCallback == null && this.onRenderCallback == null && this._pendingState == null) { - console.warn("Phaser update loop cannot start: No preload, create, update or render functions given and no pending State found"); - } - else - { - console.log('Phaser', Phaser.VERSION, 'alive'); - this.isRunning = true; - this._loadComplete = false; - - this.raf = new Phaser.RequestAnimationFrame(this); - this.raf.start(); - - if (this._pendingState) - { - this.switchState(this._pendingState, false, false); - } - else - { - this.startState(); - } - } + this.raf = new Phaser.RequestAnimationFrame(this); + this.raf.start(); + if (this.onPreloadCallback || this.onCreateCallback || this.onUpdateCallback || this.onRenderCallback || this._pendingState) + { + if (this._pendingState) + { + console.log('boot has found a pending state'); + this.switchState(this._pendingState, false, false); + } + else + { + console.log('boot has found enough callbacks to start the state'); + this.startState(); + } + } } }, + /** + * Launch the game + * @param callbackContext Which context will the callbacks be called with. + * @param preloadCallback {function} Preload callback invoked when init default screen. + * @param createCallback {function} Create callback invoked when create default screen. + * @param updateCallback {function} Update callback invoked when update default screen. + * @param renderCallback {function} Render callback invoked when render default screen. + */ + launch: function (context, preload, create, update, render) { + + this.callbackContext = context; + + this.onPreloadCallback = preload || null; + this.onCreateCallback = create || null; + this.onUpdateCallback = update || null; + this.onRenderCallback = render || null; + + if (this.onPreloadCallback == null && this.onCreateCallback == null && this.onUpdateCallback == null && this.onRenderCallback == null && this._pendingState == null) + { + console.warn("Phaser cannot start: No preload, create, update or render functions given and no pending State found"); + } + else + { + if (this.isBooted) + { + console.log('launch has set the callbacks and dom is booted so lets rock'); + + this.startState(); + + // if (this._pendingState) + // { + // this.switchState(this._pendingState, false, false); + // } + // else + // { + // this.startState(); + // } + + } + else + { + console.log('launch has set the callbacks but cant start because the DOM isnt booted yet'); + } + } + + }, + /** * Called when the load has finished after preload was run. */ loadComplete: function () { + console.log('loadComplete', this); + this._loadComplete = true; - this.onCreateCallback.call(this.callbackContext); + + if (this.onCreateCallback) { + this.onCreateCallback.call(this.callbackContext); + // this.onCreateCallback.call(this); + } }, @@ -321,10 +387,15 @@ Phaser.Game.prototype = { */ startState: function () { + console.log('startState'); + // console.log(this); + // console.log(this.callbackContext); + if (this.onPreloadCallback !== null) { this.load.reset(); this.onPreloadCallback.call(this.callbackContext); + // this.onPreloadCallback.call(this.onPreloadCallback); // Is the loader empty? if (this.load.queueSize == 0) @@ -332,10 +403,10 @@ Phaser.Game.prototype = { if (this.onCreateCallback !== null) { this.onCreateCallback.call(this.callbackContext); + // this.onCreateCallback.call(this.onCreateCallback); } this._loadComplete = true; - } else { @@ -349,36 +420,12 @@ Phaser.Game.prototype = { // No init? Then there was nothing to load either if (this.onCreateCallback !== null) { this.onCreateCallback.call(this.callbackContext); + // this.onCreateCallback.call(this.onCreateCallback); } this._loadComplete = true; - } - }, - - /** - * Set the most common state callbacks (init, create, update, render). - * @param preloadCallback {function} Init callback invoked when init state. - * @param createCallback {function} Create callback invoked when create state. - * @param updateCallback {function} Update callback invoked when update state. - * @param renderCallback {function} Render callback invoked when render state. - * @param destroyCallback {function} Destroy callback invoked when state is destroyed. - */ - setCallbacks: function (preloadCallback, createCallback, updateCallback, renderCallback, destroyCallback) { - - if (typeof preloadCallback === "undefined") { preloadCallback = null; } - if (typeof createCallback === "undefined") { createCallback = null; } - if (typeof updateCallback === "undefined") { updateCallback = null; } - if (typeof renderCallback === "undefined") { renderCallback = null; } - if (typeof destroyCallback === "undefined") { destroyCallback = null; } - - this.onPreloadCallback = preloadCallback; - this.onCreateCallback = createCallback; - this.onUpdateCallback = updateCallback; - this.onRenderCallback = renderCallback; - this.onDestroyCallback = destroyCallback; - }, update: function (time) { @@ -388,11 +435,11 @@ Phaser.Game.prototype = { this.plugins.preUpdate(); this.tweens.update(); - this.input.update(); - this.stage.update(); - this.sound.update(); - this.physics.update(); - this.world.update(); + // this.input.update(); + // this.stage.update(); + // this.sound.update(); + // this.physics.update(); + // this.world.update(); this.plugins.update(); @@ -403,7 +450,7 @@ Phaser.Game.prototype = { this.onUpdateCallback.call(this.callbackContext); } - this.world.postUpdate(); + // this.world.postUpdate(); this.plugins.postUpdate(); this.plugins.preRender(); @@ -412,7 +459,7 @@ Phaser.Game.prototype = { this.onPreRenderCallback.call(this.callbackContext); } - this.renderer.render(); + // this.renderer.render(); this.plugins.render(); if (this.onRenderCallback) @@ -430,10 +477,10 @@ Phaser.Game.prototype = { this.onLoadUpdateCallback.call(this.callbackContext); } - this.world.postUpdate(); + // this.world.postUpdate(); this.plugins.postUpdate(); this.plugins.preRender(); - this.renderer.render(); + // this.renderer.render(); this.plugins.render(); if (this.onLoadRenderCallback) @@ -454,9 +501,17 @@ Phaser.Game.prototype = { */ switchState: function (state, clearWorld, clearCache) { + // console.log('switchState', state, this.isBooted); + // console.log(typeof state); + // console.log(state instanceof Phaser.State); + if (typeof clearWorld === "undefined") { clearWorld = true; } if (typeof clearCache === "undefined") { clearCache = false; } + if (state instanceof Phaser.State) { + state.link(this); + } + if (this.isBooted == false) { this._pendingState = state; return; @@ -467,7 +522,9 @@ Phaser.Game.prototype = { this.onDestroyCallback.call(this.callbackContext); } - this.input.reset(true); + if (this.input) { + this.input.reset(true); + } // Prototype? if (typeof state === 'function') @@ -507,7 +564,6 @@ Phaser.Game.prototype = { this._loadComplete = false; this.startState(); - } else { diff --git a/src/Phaser.js b/src/Phaser.js index 78eb26a0..78c1c4ff 100644 --- a/src/Phaser.js +++ b/src/Phaser.js @@ -23,4 +23,12 @@ /** * @module Phaser */ -var Phaser = Phaser || { VERSION: '1.0.0', GAMES: [] }; +var Phaser = Phaser || { + + VERSION: '1.0.0', + GAMES: [], + RENDERER_AUTO: 0, + RENDERER_CANVAS: 1, + RENDERER_WEBGL: 2 + + }; diff --git a/src/core/Plugin.js b/src/core/Plugin.js index 568a8375..c83f89e8 100644 --- a/src/core/Plugin.js +++ b/src/core/Plugin.js @@ -20,7 +20,7 @@ Phaser.Plugin = function (game, parent) { }; -Phaser.Plugin.prototype = {, +Phaser.Plugin.prototype = { /** * Pre-update is called at the start of the update cycle, before any other updates have taken place. diff --git a/src/core/PluginManager.js b/src/core/PluginManager.js index 4509c289..2cf2cc9c 100644 --- a/src/core/PluginManager.js +++ b/src/core/PluginManager.js @@ -13,7 +13,7 @@ Phaser.PluginManager = function(game, parent) { }; -Phaser.PluginManager.prototype = {, +Phaser.PluginManager.prototype = { /** * Add a new Plugin to the PluginManager. diff --git a/src/core/State.js b/src/core/State.js new file mode 100644 index 00000000..3287e72f --- /dev/null +++ b/src/core/State.js @@ -0,0 +1,73 @@ +/** +* State +* +* This is a base State class which can be extended if you are creating your game with TypeScript. +* It provides quick access to common functions such as the camera, cache, input, match, sound and more. +* +* @package Phaser.State +* @author Richard Davey +* @copyright 2013 Photon Storm Ltd. +* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License +*/ + +Phaser.State = function () { +}; + +Phaser.State.prototype = { + + link: function (game) { + + this.game = game; + // this.add = game.add; + // this.camera = game.camera; + this.cache = game.cache; + // this.input = game.input; + this.load = game.load; + this.math = game.math; + // this.sound = game.sound; + // this.stage = game.stage; + this.time = game.time; + this.tweens = game.tweens; + // this.world = game.world; + + }, + + /** + * Override this method to add some load operations. + * If you need to use the loader, you may need to use them here. + */ + preload: function () { + }, + + /** + * This method is called after the game engine successfully switches states. + * Feel free to add any setup code here.(Do not load anything here, override init() instead) + */ + create: function () { + }, + + /** + * Put update logic here. + */ + update: function () { + }, + + /** + * Put render operations here. + */ + render: function () { + }, + + /** + * This method will be called when game paused. + */ + paused: function () { + }, + + /** + * This method will be called when the state is destroyed + */ + destroy: function () { + } + +}; diff --git a/src/system/RequestAnimationFrame.js b/src/system/RequestAnimationFrame.js index de24c67e..b12ea73e 100644 --- a/src/system/RequestAnimationFrame.js +++ b/src/system/RequestAnimationFrame.js @@ -26,6 +26,12 @@ Phaser.RequestAnimationFrame = function(game) { Phaser.RequestAnimationFrame.prototype = { + /** + * The function called by the update + * @private + **/ + _onLoop: null, + /** * Starts the requestAnimatioFrame running or setTimeout if unavailable in browser * @method start @@ -34,15 +40,27 @@ Phaser.RequestAnimationFrame.prototype = { this.isRunning = true; + var _this = this; + if (!window.requestAnimationFrame) { this._isSetTimeOut = true; - this._timeOutID = window.setTimeout(Phaser.GAMES[this.game.id].raf.updateSetTimeout, 0); + + this._onLoop = function () { + return _this.updateSetTimeout(); + }; + + this._timeOutID = window.setTimeout(this._onLoop, 0); } else { this._isSetTimeOut = false; - window.requestAnimationFrame(Phaser.GAMES[this.game.id].raf.updateRAF); + + this._onLoop = function () { + return _this.updateRAF(); + }; + + window.requestAnimationFrame(this._onLoop); } }, @@ -55,7 +73,7 @@ Phaser.RequestAnimationFrame.prototype = { this.game.update(time); - window.requestAnimationFrame(Phaser.GAMES[this.game.id].raf.updateRAF); + window.requestAnimationFrame(this._onLoop); }, @@ -67,7 +85,7 @@ Phaser.RequestAnimationFrame.prototype = { this.game.update(Date.now()); - this._timeOutID = window.setTimeout(Phaser.GAMES[this.game.id].raf.updateSetTimeout, this.game.time.timeToCall); + this._timeOutID = window.setTimeout(this._onLoop, this.game.time.timeToCall); }, diff --git a/src/tween/TweenManager.js b/src/tween/TweenManager.js index 3b161bb5..ad437c26 100644 --- a/src/tween/TweenManager.js +++ b/src/tween/TweenManager.js @@ -98,21 +98,21 @@ Phaser.TweenManager.prototype = { */ update: function () { - if ( _tweens.length === 0 ) return false; + if ( this._tweens.length === 0 ) return false; - var i = 0, numTweens = _tweens.length; + var i = 0, numTweens = this._tweens.length; while ( i < numTweens ) { - if ( _tweens[ i ].update( this.game.time.now ) ) { + if ( this._tweens[ i ].update( this.game.time.now ) ) { - i ++; + i++; } else { - _tweens.splice( i, 1 ); + this._tweens.splice( i, 1 ); - numTweens --; + numTweens--; }