1 /** 2 * The Render Engine 3 * LevelLoader 4 * 5 * @fileoverview Loads 2D tilemapped levels. 6 * 7 * @author: Brett Fattori (brettf@renderengine.com) 8 * @author: $Author: bfattori $ 9 * @version: $Revision: 1555 $ 10 * 11 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a copy 14 * of this software and associated documentation files (the "Software"), to deal 15 * in the Software without restriction, including without limitation the rights 16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 * copies of the Software, and to permit persons to whom the Software is 18 * furnished to do so, subject to the following conditions: 19 * 20 * The above copyright notice and this permission notice shall be included in 21 * all copies or substantial portions of the Software. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 * THE SOFTWARE. 30 * 31 */ 32 33 // The class this file defines and its required classes 34 R.Engine.define({ 35 "class":"R.resources.loaders.LevelLoader", 36 "requires":[ 37 "R.math.Math2D", 38 "R.resources.loaders.ObjectLoader", 39 "R.resources.types.Level" 40 ] 41 }); 42 43 /** 44 * @class Loads 2D tilemapped levels for use in games. Levels are comprised of multiple layers 45 * which describe backgrounds, playfield (actors and fixtures), and foregrounds. 46 * The playfield is where the player and other interactive elements will exist. 47 * 48 * @constructor 49 * @param name {String=LevelLoader} The name of the resource loader 50 * @extends R.resources.loaders.ObjectLoader 51 */ 52 R.resources.loaders.LevelLoader = function () { 53 return R.resources.loaders.ObjectLoader.extend(/** @scope R.resources.loaders.LevelLoader.prototype */{ 54 55 tileLoader:null, 56 queuedLevels:0, 57 levels:{}, 58 59 /** @private */ 60 constructor:function (name) { 61 this.base(name || "LevelLoader"); 62 this.queuedLevels = 0; 63 this.levels = {}; 64 }, 65 66 /** 67 * Load a level object from a URL. 68 * 69 * @param name {String} The name of the level 70 * @param url {String} The URL where the resource is located 71 */ 72 load:function (name, url /*, obj */) { 73 this.base(name, url, arguments[2]); 74 if (arguments[2] === undefined) { 75 this.queuedLevels++; 76 } 77 }, 78 79 /** 80 * @private 81 */ 82 afterLoad:function (name, obj) { 83 // We need to mark this as "not ready" since we'll be loading tiles 84 // and other things before this object is actually ready 85 this.setReady(name, false); 86 87 // Levels actually deserialize themselves, so we just wire to the level's "loaded" event 88 var self = this; 89 this.levels[name] = R.resources.types.Level.deserialize(obj); 90 this.levels[name].addEvent(this, "loaded", function () { 91 self.queuedLevels--; 92 self.setReady(name, true); 93 }); 94 }, 95 96 /** 97 * Creates a {@link R.resources.types.Level} object representing the named level. 98 * 99 * @param level {String} A loaded level name 100 * @returns {R.resources.types.Level} A {@link R.resources.types.Level} object 101 */ 102 getLevel:function (level) { 103 return this.levels[level]; 104 }, 105 106 /** 107 * Export all of the levels, as a JavaScript object, with the 108 * level name as the key and the corresponding {@link R.resources.types.Level} as the value. 109 * @param [levelNames] {Array} An optional array of levels to export, by name, 110 * or <code>null</tt> to export all levels 111 */ 112 exportAll:function (levelNames) { 113 var o = {}; 114 var levels = this.getResources(); 115 for (var i in levels) { 116 if (!levelNames || R.engine.Support.indexOf(levelNames, levels[i]) != -1) { 117 o[levels[i]] = this.getLevel(i); 118 } 119 } 120 return o; 121 }, 122 123 /** 124 * The name of the resource this loader will get. 125 * @returns {String} The string "level" 126 */ 127 getResourceType:function () { 128 return "level"; 129 } 130 131 }, /** @scope R.resources.loaders.LevelLoader.prototype */ { 132 /** 133 * Get the class name of this object. 134 * @return {String} The string "R.resources.loaders.LevelLoader" 135 */ 136 getClassName:function () { 137 return "R.resources.loaders.LevelLoader"; 138 } 139 }); 140 141 }