1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 Orthogonal orientation 29 * @constant 30 * @type Number 31 */ 32 cc.TMX_ORIENTATION_ORTHO = 0; 33 34 /** 35 * Hexagonal orientation 36 * @constant 37 * @type Number 38 */ 39 40 cc.TMX_ORIENTATION_HEX = 1; 41 42 /** 43 * Isometric orientation 44 * @constant 45 * @type Number 46 */ 47 cc.TMX_ORIENTATION_ISO = 2; 48 49 /** 50 * <p>cc.TMXTiledMap knows how to parse and render a TMX map.</p> 51 * 52 * <p>It adds support for the TMX tiled map format used by http://www.mapeditor.org <br /> 53 * It supports isometric, hexagonal and orthogonal tiles.<br /> 54 * It also supports object groups, objects, and properties.</p> 55 * 56 * <p>Features: <br /> 57 * - Each tile will be treated as an cc.Sprite<br /> 58 * - The sprites are created on demand. They will be created only when you call "layer.tileAt(position)" <br /> 59 * - Each tile can be rotated / moved / scaled / tinted / "opacitied", since each tile is a cc.Sprite<br /> 60 * - Tiles can be added/removed in runtime<br /> 61 * - The z-order of the tiles can be modified in runtime<br /> 62 * - Each tile has an anchorPoint of (0,0) <br /> 63 * - The anchorPoint of the TMXTileMap is (0,0) <br /> 64 * - The TMX layers will be added as a child <br /> 65 * - The TMX layers will be aliased by default <br /> 66 * - The tileset image will be loaded using the cc.TextureCache <br /> 67 * - Each tile will have a unique tag<br /> 68 * - Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z<br /> 69 * - Each object group will be treated as an cc.MutableArray <br /> 70 * - Object class which will contain all the properties in a dictionary<br /> 71 * - Properties can be assigned to the Map, Layer, Object Group, and Object</p> 72 * 73 * <p>Limitations: <br /> 74 * - It only supports one tileset per layer. <br /> 75 * - Embeded images are not supported <br /> 76 * - It only supports the XML format (the JSON format is not supported)</p> 77 * 78 * <p>Technical description: <br /> 79 * Each layer is created using an cc.TMXLayer (subclass of cc.SpriteBatchNode). If you have 5 layers, then 5 cc.TMXLayer will be created, <br /> 80 * unless the layer visibility is off. In that case, the layer won't be created at all. <br /> 81 * You can obtain the layers (cc.TMXLayer objects) at runtime by: <br /> 82 * - map.getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...<br /> 83 * - map.getLayer(name_of_the_layer); </p> 84 * 85 * <p>Each object group is created using a cc.TMXObjectGroup which is a subclass of cc.MutableArray.<br /> 86 * You can obtain the object groups at runtime by: <br /> 87 * - map.getObjectGroup(name_of_the_object_group); </p> 88 * 89 * <p>Each object is a cc.TMXObject.</p> 90 * 91 * <p>Each property is stored as a key-value pair in an cc.MutableDictionary.<br /> 92 * You can obtain the properties at runtime by: </p> 93 * 94 * <p>map.getProperty(name_of_the_property); <br /> 95 * layer.getProperty(name_of_the_property); <br /> 96 * objectGroup.getProperty(name_of_the_property); <br /> 97 * object.getProperty(name_of_the_property);</p> 98 * @class 99 * @extends cc.Node 100 */ 101 cc.TMXTiledMap = cc.Node.extend(/** @lends cc.TMXTiledMap# */{ 102 //the map's size property measured in tiles 103 _mapSize:cc.SizeZero(), 104 _tileSize:cc.SizeZero(), 105 _properties:null, 106 _objectGroups:null, 107 _mapOrientation:null, 108 //tile properties 109 //todo delete 110 _TMXLayers:null, 111 _tileProperties:[], 112 113 /** 114 * @return {cc.Size} 115 */ 116 getMapSize:function () { 117 return this._mapSize; 118 }, 119 120 /** 121 * @param {cc.Size} Var 122 */ 123 setMapSize:function (Var) { 124 this._mapSize = Var; 125 }, 126 127 /** 128 * @return {cc.Size} 129 */ 130 getTileSize:function () { 131 return this._tileSize; 132 }, 133 134 /** 135 * @param {cc.Size} Var 136 */ 137 setTileSize:function (Var) { 138 this._tileSize = Var; 139 }, 140 141 /** 142 * map orientation 143 * @return {Number} 144 */ 145 getMapOrientation:function () { 146 return this._mapOrientation; 147 }, 148 149 /** 150 * @param {Number} Var 151 */ 152 setMapOrientation:function (Var) { 153 this._mapOrientation = Var; 154 }, 155 156 /** 157 * object groups 158 * @return {Array} 159 */ 160 getObjectGroups:function () { 161 return this._objectGroups; 162 }, 163 164 /** 165 * @param {Array} Var 166 */ 167 setObjectGroups:function (Var) { 168 this._objectGroups = Var; 169 }, 170 171 /** 172 * properties 173 * @return {object} 174 */ 175 getProperties:function () { 176 return this._properties; 177 }, 178 179 /** 180 * @param {object} Var 181 */ 182 setProperties:function (Var) { 183 this._properties = Var; 184 }, 185 186 /** 187 * @param {String} tmxFile 188 * @return {Boolean} 189 * @example 190 * //example 191 * var map = new cc.TMXTiledMap() 192 * map.initWithTMXFile("hello.tmx"); 193 */ 194 initWithTMXFile:function (tmxFile,resourcePath) { 195 cc.Assert(tmxFile != null && tmxFile.length > 0, "TMXTiledMap: tmx file should not be nil"); 196 this.setContentSize(cc.SizeZero()); 197 var mapInfo = cc.TMXMapInfo.create(tmxFile,resourcePath); 198 if (!mapInfo) { 199 return false; 200 } 201 cc.Assert(mapInfo.getTilesets().length != 0, "TMXTiledMap: Map not found. Please check the filename."); 202 this._buildWithMapInfo(mapInfo); 203 return true; 204 }, 205 206 _buildWithMapInfo:function (mapInfo) { 207 this._mapSize = mapInfo.getMapSize(); 208 this._tileSize = mapInfo.getTileSize(); 209 this._mapOrientation = mapInfo.getOrientation(); 210 this._objectGroups = mapInfo.getObjectGroups(); 211 this._properties = mapInfo.getProperties(); 212 this._tileProperties = mapInfo.getTileProperties(); 213 214 var idx = 0; 215 216 var layers = mapInfo.getLayers(); 217 if (layers) { 218 var layerInfo = null; 219 for (var i = 0, len = layers.length; i < len; i++) { 220 layerInfo = layers[i]; 221 if (layerInfo && layerInfo.visible) { 222 var child = this._parseLayer(layerInfo, mapInfo); 223 this.addChild(child, idx, idx); 224 225 // update content size with the max size 226 var childSize = child.getContentSize(); 227 var currentSize = this.getContentSize(); 228 currentSize.width = Math.max(currentSize.width, childSize.width); 229 currentSize.height = Math.max(currentSize.height, childSize.height); 230 this.setContentSize(currentSize); 231 232 idx++; 233 } 234 } 235 } 236 }, 237 /** return the TMXLayer for the specific layer 238 * @param {String} layerName 239 * @return {cc.TMXLayer} 240 */ 241 getLayer:function (layerName) { 242 cc.Assert(layerName != null && layerName.length > 0, "Invalid layer name!"); 243 244 for (var i = 0; i < this._children.length; i++) { 245 var layer = this._children[i]; 246 if (layer) { 247 if (layer.getLayerName() == layerName) { 248 return layer; 249 } 250 } 251 } 252 253 // layer not found 254 return null; 255 }, 256 257 /** 258 * Return the TMXObjectGroup for the secific group 259 * @param {String} groupName 260 * @return {cc.TMXObjectGroup} 261 */ 262 getObjectGroup:function (groupName) { 263 cc.Assert(groupName != null && groupName.length > 0, "Invalid group name!"); 264 if (this._objectGroups) { 265 for (var i = 0; i < this._objectGroups.length; i++) { 266 var objectGroup = this._objectGroups[i]; 267 if (objectGroup && objectGroup.getGroupName() == groupName) { 268 return objectGroup; 269 } 270 } 271 } 272 // objectGroup not found 273 return null; 274 }, 275 276 /** 277 * Return the value for the specific property name 278 * @param {String} propertyName 279 * @return {String} 280 */ 281 getProperty:function (propertyName) { 282 return this._properties[propertyName.toString()]; 283 }, 284 285 /** 286 * Return properties dictionary for tile GID 287 * @param {Number} GID 288 * @return {object} 289 */ 290 propertiesForGID:function (GID) { 291 return this._tileProperties[GID]; 292 }, 293 294 _parseLayer:function (layerInfo, mapInfo) { 295 var tileset = this._tilesetForLayer(layerInfo, mapInfo); 296 var layer = cc.TMXLayer.create(tileset, layerInfo, mapInfo); 297 // tell the layerinfo to release the ownership of the tiles map. 298 layerInfo.ownTiles = false; 299 layer.setupTiles(); 300 return layer; 301 }, 302 303 _tilesetForLayer:function (layerInfo, mapInfo) { 304 var size = layerInfo._layerSize; 305 var tilesets = mapInfo.getTilesets(); 306 if (tilesets) { 307 for (var i = tilesets.length - 1; i >= 0; i--) { 308 var tileset = tilesets[i]; 309 if (tileset) { 310 for (var y = 0; y < size.height; y++) { 311 for (var x = 0; x < size.width; x++) { 312 var pos = x + size.width * y; 313 var gid = layerInfo._tiles[pos]; 314 if (gid != 0) { 315 // Optimization: quick return 316 // if the layer is invalid (more than 1 tileset per layer) an cc.Assert will be thrown later 317 if (((gid & cc.TMX_TILE_ALL_FLAGS_MASK)>>>0) >= tileset.firstGid) { 318 return tileset; 319 } 320 } 321 322 } 323 } 324 } 325 } 326 } 327 328 // If all the tiles are 0, return empty tileset 329 cc.log("cocos2d: Warning: TMX Layer " + layerInfo.name + " has no tiles"); 330 return null; 331 } 332 }); 333 334 /** 335 * Creates a TMX Tiled Map with a TMX file. 336 * Implementation cc.TMXTiledMap 337 * @param {String} tmxString 338 * @param {String} resourcePath 339 * @return {cc.TMXTiledMap|null} 340 * @example 341 * //example 342 * var map = cc.TMXTiledMap.create("hello.tmx"); 343 */ 344 cc.TMXTiledMap.create = function (tmxFile, resourcePath) { 345 var ret = new cc.TMXTiledMap(); 346 if (ret.initWithTMXFile(tmxFile,resourcePath)) { 347 return ret; 348 } 349 return null; 350 }; 351