1 /** 2 * The Render Engine 3 * Game 4 * 5 * @fileoverview The game object represents an instance of a game. It is 6 * the controlling entity for all of a game and is responsible 7 * for setup and teardown of the game. 8 * 9 * @author: Brett Fattori (brettf@renderengine.com) 10 * @author: $Author: bfattori $ 11 * @version: $Revision: 1555 $ 12 * 13 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining a copy 16 * of this software and associated documentation files (the "Software"), to deal 17 * in the Software without restriction, including without limitation the rights 18 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 19 * copies of the Software, and to permit persons to whom the Software is 20 * furnished to do so, subject to the following conditions: 21 * 22 * The above copyright notice and this permission notice shall be included in 23 * all copies or substantial portions of the Software. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 30 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 31 * THE SOFTWARE 32 */ 33 "use strict"; 34 35 // The class this file defines and its required classes 36 R.Engine.define({ 37 "class":"R.engine.Game", 38 "requires":[] 39 }); 40 41 /** 42 * @class The game object represents an instance of a game. It is 43 * the controlling entity for the game constructs and is responsible 44 * for setup and teardown of the game. All games must extend from this class 45 * to be executed by the engine. 46 */ 47 R.engine.Game = Base.extend(/** @scope R.engine.Game.prototype */{ 48 49 scriptsToLoad:[], 50 51 /** @private */ 52 constructor:null, 53 54 /** 55 * [ABSTRACT] Initialize the game. This method will be called automatically by the 56 * engine when all dependencies for the game have been resolved. 57 * @memberOf R.engine.Game 58 */ 59 setup:function () { 60 }, 61 62 /** 63 * [ABSTRACT] Shut down the game. This method will be called if the engine is shut down 64 * giving a game time to clean up before it is destroyed. 65 * @memberOf R.engine.Game 66 */ 67 tearDown:function () { 68 }, 69 70 /** 71 * Get the display name of the game. 72 * @memberOf R.engine.Game 73 */ 74 getName:function () { 75 return "Game"; 76 }, 77 78 /** 79 * Load a script relative to this game. Scripts cannot be specified 80 * with an absolute URL. 81 * 82 * @param scriptSource {String} The relative path to the script to load. 83 * @return {String} An Id for the script which is used in the call to {@link #scriptLoaded} 84 * when the script has completed loading (or errored out) 85 * @memberOf R.engine.Game 86 */ 87 load:function (scriptSource) { 88 Assert((scriptSource.indexOf("http") == -1), "Game scripts can only be loaded relative to the game's path"); 89 R.engine.Script.loadScript((scriptSource.charAt(0) != "/" ? "./" : ".") + scriptSource); 90 var self = this; 91 this.setQueueCallback(function () { 92 self.scriptLoaded(scriptSource); 93 }); 94 }, 95 96 /** 97 * Load a script, relative to the game engine. Scripts cannot be loaded 98 * with an absolute URL. 99 * 100 * @param scriptPath {String} The relative path of the script to load. 101 * @memberOf R.engine.Game 102 */ 103 loadEngineScript:function (scriptPath) { 104 R.engine.Script.loadNow(scriptPath, Game.scriptLoaded); 105 }, 106 107 /** 108 * [ABSTRACT] Will be called with the path of a loaded script. You can 109 * be guaranteed that the script either loaded and is ready or failed to load. 110 * @param scriptPath {String} The script path 111 * @memberOf R.engine.Game 112 */ 113 scriptLoaded:function (scriptPath) { 114 }, 115 116 /** 117 * Allows a game to inject a function call into the scriping 118 * queue to be processed when the queue has an available slot. 119 * @param cb {Function} The callback to execute 120 * @memberOf R.engine.Game 121 */ 122 setQueueCallback:function (cb) { 123 R.engine.Script.setQueueCallback(cb); 124 }, 125 126 /** 127 * Get the number of players the game supports. 128 * 129 * @return {Number} 130 * @memberOf R.engine.Game 131 */ 132 getPlayers:function () { 133 return 1; 134 }, 135 136 /** 137 * Get the path where your game class exists. 138 * @return {String} 139 * @memberOf R.engine.Game 140 */ 141 getGamePath:function () { 142 var loc = window.location; 143 var path = loc.protocol + "//" + loc.host + loc.pathname.substring(0, loc.pathname.lastIndexOf("/")); 144 path += (path.charAt(path.length - 1) == "/" ? "" : "/"); 145 return path; 146 }, 147 148 /** 149 * Get the path of the specified file, relative to your game class. 150 * @param fileName {String} The path to the file 151 * @return {String} 152 * @memberOf R.engine.Game 153 */ 154 getFilePath:function (fileName) { 155 var loc = window.location; 156 if (fileName.indexOf(loc.protocol) != -1 && fileName.indexOf(loc.host) == -1) { 157 throw new Error("File cannot be located on another server!"); 158 } 159 160 if (fileName.indexOf(loc.protocol) == -1) { 161 return this.getGamePath() + fileName; 162 } else { 163 return fileName; 164 } 165 }, 166 167 /** 168 * This method is called just before the next frame is generated. 169 * @param time {Number} The current world time 170 * @param dt {Number} The delta between the last frame time and the world time 171 */ 172 tick: function(time, dt) { 173 } 174 }); 175