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.getTileAt(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.NodeRGBA.extend(/** @lends cc.TMXTiledMap# */{ 102 //the map's size property measured in tiles 103 _mapSize:null, 104 _tileSize:null, 105 _properties:null, 106 _objectGroups:null, 107 _mapOrientation:null, 108 //tile properties 109 //todo delete 110 _TMXLayers:null, 111 _tileProperties:null, 112 113 ctor:function(){ 114 cc.Node.prototype.ctor.call(this); 115 this._mapSize = cc.SizeZero(); 116 this._tileSize = cc.SizeZero(); 117 this._properties = null; 118 this._objectGroups = null; 119 this._mapOrientation = null; 120 this._TMXLayers = null; 121 122 this._tileProperties = []; 123 }, 124 125 /** 126 * @return {cc.Size} 127 */ 128 getMapSize:function () { 129 return cc.size(this._mapSize.width, this._mapSize.height); 130 }, 131 132 /** 133 * @param {cc.Size} Var 134 */ 135 setMapSize:function (Var) { 136 this._mapSize.width = Var.width; 137 this._mapSize.height = Var.height; 138 }, 139 140 /** 141 * @return {cc.Size} 142 */ 143 getTileSize:function () { 144 return cc.size(this._tileSize.width, this._tileSize.height); 145 }, 146 147 /** 148 * @param {cc.Size} Var 149 */ 150 setTileSize:function (Var) { 151 this._tileSize.width = Var.width; 152 this._tileSize.height = Var.height; 153 }, 154 155 /** 156 * map orientation 157 * @return {Number} 158 */ 159 getMapOrientation:function () { 160 return this._mapOrientation; 161 }, 162 163 /** 164 * @param {Number} Var 165 */ 166 setMapOrientation:function (Var) { 167 this._mapOrientation = Var; 168 }, 169 170 /** 171 * object groups 172 * @return {Array} 173 */ 174 getObjectGroups:function () { 175 return this._objectGroups; 176 }, 177 178 /** 179 * @param {Array} Var 180 */ 181 setObjectGroups:function (Var) { 182 this._objectGroups = Var; 183 }, 184 185 /** 186 * properties 187 * @return {object} 188 */ 189 getProperties:function () { 190 return this._properties; 191 }, 192 193 /** 194 * @param {object} Var 195 */ 196 setProperties:function (Var) { 197 this._properties = Var; 198 }, 199 200 /** 201 * @param {String} tmxFile 202 * @param {String} [resourcePath=] 203 * @return {Boolean} 204 * @example 205 * //example 206 * var map = new cc.TMXTiledMap() 207 * map.initWithTMXFile("hello.tmx"); 208 */ 209 initWithTMXFile:function (tmxFile,resourcePath) { 210 if(!tmxFile || tmxFile.length == 0) 211 throw "cc.TMXTiledMap.initWithTMXFile(): tmxFile should be non-null or non-empty string."; 212 this.setContentSize(cc.SizeZero()); 213 var mapInfo = cc.TMXMapInfo.create(tmxFile,resourcePath); 214 if (!mapInfo) 215 return false; 216 217 var locTilesets = mapInfo.getTilesets(); 218 if(!locTilesets || locTilesets.length === 0) 219 cc.log("cc.TMXTiledMap.initWithTMXFile(): Map not found. Please check the filename."); 220 this._buildWithMapInfo(mapInfo); 221 return true; 222 }, 223 224 initWithXML:function(tmxString, resourcePath){ 225 this.setContentSize(cc.SizeZero()); 226 227 var mapInfo = cc.TMXMapInfo.createWithXML(tmxString, resourcePath); 228 var locTilesets = mapInfo.getTilesets(); 229 if(!locTilesets || locTilesets.length === 0) 230 cc.log("cc.TMXTiledMap.initWithXML(): Map not found. Please check the filename."); 231 this._buildWithMapInfo(mapInfo); 232 return true; 233 }, 234 235 _buildWithMapInfo:function (mapInfo) { 236 this._mapSize = mapInfo.getMapSize(); 237 this._tileSize = mapInfo.getTileSize(); 238 this._mapOrientation = mapInfo.getOrientation(); 239 this._objectGroups = mapInfo.getObjectGroups(); 240 this._properties = mapInfo.getProperties(); 241 this._tileProperties = mapInfo.getTileProperties(); 242 243 var idx = 0; 244 var layers = mapInfo.getLayers(); 245 if (layers) { 246 var layerInfo = null; 247 for (var i = 0, len = layers.length; i < len; i++) { 248 layerInfo = layers[i]; 249 if (layerInfo && layerInfo.visible) { 250 var child = this._parseLayer(layerInfo, mapInfo); 251 this.addChild(child, idx, idx); 252 253 // update content size with the max size 254 var childSize = child.getContentSize(); 255 var currentSize = this.getContentSize(); 256 currentSize.width = Math.max(currentSize.width, childSize.width); 257 currentSize.height = Math.max(currentSize.height, childSize.height); 258 this.setContentSize(currentSize); 259 idx++; 260 } 261 } 262 } 263 }, 264 /** return the TMXLayer for the specific layer 265 * @param {String} layerName 266 * @return {cc.TMXLayer} 267 */ 268 getLayer:function (layerName) { 269 if(!layerName || layerName.length === 0) 270 throw "cc.TMXTiledMap.getLayer(): layerName should be non-null or non-empty string."; 271 272 for (var i = 0; i < this._children.length; i++) { 273 var layer = this._children[i]; 274 if (layer && layer.getLayerName() == layerName) 275 return layer; 276 } 277 278 // layer not found 279 return null; 280 }, 281 282 /** 283 * Return the TMXObjectGroup for the secific group 284 * @param {String} groupName 285 * @return {cc.TMXObjectGroup} 286 */ 287 getObjectGroup:function (groupName) { 288 if(!groupName || groupName.length === 0) 289 throw "cc.TMXTiledMap.getObjectGroup(): groupName should be non-null or non-empty string."; 290 if (this._objectGroups) { 291 for (var i = 0; i < this._objectGroups.length; i++) { 292 var objectGroup = this._objectGroups[i]; 293 if (objectGroup && objectGroup.getGroupName() == groupName) { 294 return objectGroup; 295 } 296 } 297 } 298 // objectGroup not found 299 return null; 300 }, 301 302 /** 303 * Return the value for the specific property name 304 * @param {String} propertyName 305 * @return {String} 306 */ 307 getProperty:function (propertyName) { 308 return this._properties[propertyName.toString()]; 309 }, 310 311 /** 312 * Return properties dictionary for tile GID 313 * @param {Number} GID 314 * @return {object} 315 */ 316 propertiesForGID:function (GID) { 317 return this._tileProperties[GID]; 318 }, 319 320 _parseLayer:function (layerInfo, mapInfo) { 321 var tileset = this._tilesetForLayer(layerInfo, mapInfo); 322 var layer = cc.TMXLayer.create(tileset, layerInfo, mapInfo); 323 // tell the layerinfo to release the ownership of the tiles map. 324 layerInfo.ownTiles = false; 325 layer.setupTiles(); 326 return layer; 327 }, 328 329 _tilesetForLayer:function (layerInfo, mapInfo) { 330 var size = layerInfo._layerSize; 331 var tilesets = mapInfo.getTilesets(); 332 if (tilesets) { 333 for (var i = tilesets.length - 1; i >= 0; i--) { 334 var tileset = tilesets[i]; 335 if (tileset) { 336 for (var y = 0; y < size.height; y++) { 337 for (var x = 0; x < size.width; x++) { 338 var pos = x + size.width * y; 339 var gid = layerInfo._tiles[pos]; 340 if (gid != 0) { 341 // Optimization: quick return 342 // if the layer is invalid (more than 1 tileset per layer) an cc.Assert will be thrown later 343 if (((gid & cc.TMX_TILE_FLIPPED_MASK)>>>0) >= tileset.firstGid) { 344 return tileset; 345 } 346 } 347 348 } 349 } 350 } 351 } 352 } 353 354 // If all the tiles are 0, return empty tileset 355 cc.log("cocos2d: Warning: TMX Layer " + layerInfo.name + " has no tiles"); 356 return null; 357 } 358 }); 359 360 /** 361 * Creates a TMX Tiled Map with a TMX file. 362 * Implementation cc.TMXTiledMap 363 * @param {String} tmxFile 364 * @param {String} resourcePath 365 * @return {cc.TMXTiledMap|undefined} 366 * @example 367 * //example 368 * var map = cc.TMXTiledMap.create("hello.tmx"); 369 */ 370 cc.TMXTiledMap.create = function (tmxFile, resourcePath) { 371 var ret = new cc.TMXTiledMap(); 372 if (ret.initWithTMXFile(tmxFile,resourcePath)) { 373 return ret; 374 } 375 return null; 376 }; 377 378 /** 379 * initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources 380 * @param {String} tmxString 381 * @param {String} resourcePath 382 * @return {cc.TMXTiledMap|undefined} 383 */ 384 cc.TMXTiledMap.createWithXML = function(tmxString, resourcePath){ 385 var tileMap = new cc.TMXTiledMap(); 386 if(tileMap.initWithXML(tmxString,resourcePath)) 387 return tileMap; 388 return null; 389 }; 390