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(0, 0); 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(0, 0); 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 // update content size with the max size 253 var childSize = child.getContentSize(); 254 var currentSize = this.getContentSize(); 255 this.setContentSize(cc.size(Math.max(currentSize.width, childSize.width), Math.max(currentSize.height, childSize.height))); 256 idx++; 257 } 258 } 259 } 260 }, 261 /** return the TMXLayer for the specific layer 262 * @param {String} layerName 263 * @return {cc.TMXLayer} 264 */ 265 getLayer:function (layerName) { 266 if(!layerName || layerName.length === 0) 267 throw "cc.TMXTiledMap.getLayer(): layerName should be non-null or non-empty string."; 268 269 for (var i = 0; i < this._children.length; i++) { 270 var layer = this._children[i]; 271 if (layer && layer.getLayerName() == layerName) 272 return layer; 273 } 274 275 // layer not found 276 return null; 277 }, 278 279 /** 280 * Return the TMXObjectGroup for the secific group 281 * @param {String} groupName 282 * @return {cc.TMXObjectGroup} 283 */ 284 getObjectGroup:function (groupName) { 285 if(!groupName || groupName.length === 0) 286 throw "cc.TMXTiledMap.getObjectGroup(): groupName should be non-null or non-empty string."; 287 if (this._objectGroups) { 288 for (var i = 0; i < this._objectGroups.length; i++) { 289 var objectGroup = this._objectGroups[i]; 290 if (objectGroup && objectGroup.getGroupName() == groupName) { 291 return objectGroup; 292 } 293 } 294 } 295 // objectGroup not found 296 return null; 297 }, 298 299 /** 300 * Return the value for the specific property name 301 * @param {String} propertyName 302 * @return {String} 303 */ 304 getProperty:function (propertyName) { 305 return this._properties[propertyName.toString()]; 306 }, 307 308 /** 309 * Return properties dictionary for tile GID 310 * @param {Number} GID 311 * @return {object} 312 */ 313 propertiesForGID:function (GID) { 314 return this._tileProperties[GID]; 315 }, 316 317 _parseLayer:function (layerInfo, mapInfo) { 318 var tileset = this._tilesetForLayer(layerInfo, mapInfo); 319 var layer = cc.TMXLayer.create(tileset, layerInfo, mapInfo); 320 // tell the layerinfo to release the ownership of the tiles map. 321 layerInfo.ownTiles = false; 322 layer.setupTiles(); 323 return layer; 324 }, 325 326 _tilesetForLayer:function (layerInfo, mapInfo) { 327 var size = layerInfo._layerSize; 328 var tilesets = mapInfo.getTilesets(); 329 if (tilesets) { 330 for (var i = tilesets.length - 1; i >= 0; i--) { 331 var tileset = tilesets[i]; 332 if (tileset) { 333 for (var y = 0; y < size.height; y++) { 334 for (var x = 0; x < size.width; x++) { 335 var pos = x + size.width * y; 336 var gid = layerInfo._tiles[pos]; 337 if (gid != 0) { 338 // Optimization: quick return 339 // if the layer is invalid (more than 1 tileset per layer) an cc.Assert will be thrown later 340 if (((gid & cc.TMX_TILE_FLIPPED_MASK)>>>0) >= tileset.firstGid) { 341 return tileset; 342 } 343 } 344 345 } 346 } 347 } 348 } 349 } 350 351 // If all the tiles are 0, return empty tileset 352 cc.log("cocos2d: Warning: TMX Layer " + layerInfo.name + " has no tiles"); 353 return null; 354 } 355 }); 356 357 /** 358 * Creates a TMX Tiled Map with a TMX file. 359 * Implementation cc.TMXTiledMap 360 * @param {String} tmxFile 361 * @param {String} resourcePath 362 * @return {cc.TMXTiledMap|undefined} 363 * @example 364 * //example 365 * var map = cc.TMXTiledMap.create("hello.tmx"); 366 */ 367 cc.TMXTiledMap.create = function (tmxFile, resourcePath) { 368 var ret = new cc.TMXTiledMap(); 369 if (ret.initWithTMXFile(tmxFile,resourcePath)) { 370 return ret; 371 } 372 return null; 373 }; 374 375 /** 376 * initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources 377 * @param {String} tmxString 378 * @param {String} resourcePath 379 * @return {cc.TMXTiledMap|undefined} 380 */ 381 cc.TMXTiledMap.createWithXML = function(tmxString, resourcePath){ 382 var tileMap = new cc.TMXTiledMap(); 383 if(tileMap.initWithXML(tmxString,resourcePath)) 384 return tileMap; 385 return null; 386 }; 387