CCTMXTiledMap.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. Orthogonal orientation
  24. * @constant
  25. * @type Number
  26. */
  27. cc.TMX_ORIENTATION_ORTHO = 0;
  28. /**
  29. * Hexagonal orientation
  30. * @constant
  31. * @type Number
  32. */
  33. cc.TMX_ORIENTATION_HEX = 1;
  34. /**
  35. * Isometric orientation
  36. * @constant
  37. * @type Number
  38. */
  39. cc.TMX_ORIENTATION_ISO = 2;
  40. /**
  41. * <p>cc.TMXTiledMap knows how to parse and render a TMX map.</p>
  42. *
  43. * <p>It adds support for the TMX tiled map format used by http://www.mapeditor.org <br />
  44. * It supports isometric, hexagonal and orthogonal tiles.<br />
  45. * It also supports object groups, objects, and properties.</p>
  46. *
  47. * <p>Features: <br />
  48. * - Each tile will be treated as an cc.Sprite<br />
  49. * - The sprites are created on demand. They will be created only when you call "layer.getTileAt(position)" <br />
  50. * - Each tile can be rotated / moved / scaled / tinted / "opacitied", since each tile is a cc.Sprite<br />
  51. * - Tiles can be added/removed in runtime<br />
  52. * - The z-order of the tiles can be modified in runtime<br />
  53. * - Each tile has an anchorPoint of (0,0) <br />
  54. * - The anchorPoint of the TMXTileMap is (0,0) <br />
  55. * - The TMX layers will be added as a child <br />
  56. * - The TMX layers will be aliased by default <br />
  57. * - The tileset image will be loaded using the cc.TextureCache <br />
  58. * - Each tile will have a unique tag<br />
  59. * - Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z<br />
  60. * - Each object group will be treated as an cc.MutableArray <br />
  61. * - Object class which will contain all the properties in a dictionary<br />
  62. * - Properties can be assigned to the Map, Layer, Object Group, and Object</p>
  63. *
  64. * <p>Limitations: <br />
  65. * - It only supports one tileset per layer. <br />
  66. * - Embeded images are not supported <br />
  67. * - It only supports the XML format (the JSON format is not supported)</p>
  68. *
  69. * <p>Technical description: <br />
  70. * 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 />
  71. * unless the layer visibility is off. In that case, the layer won't be created at all. <br />
  72. * You can obtain the layers (cc.TMXLayer objects) at runtime by: <br />
  73. * - map.getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...<br />
  74. * - map.getLayer(name_of_the_layer); </p>
  75. *
  76. * <p>Each object group is created using a cc.TMXObjectGroup which is a subclass of cc.MutableArray.<br />
  77. * You can obtain the object groups at runtime by: <br />
  78. * - map.getObjectGroup(name_of_the_object_group); </p>
  79. *
  80. * <p>Each object is a cc.TMXObject.</p>
  81. *
  82. * <p>Each property is stored as a key-value pair in an cc.MutableDictionary.<br />
  83. * You can obtain the properties at runtime by: </p>
  84. *
  85. * <p>map.getProperty(name_of_the_property); <br />
  86. * layer.getProperty(name_of_the_property); <br />
  87. * objectGroup.getProperty(name_of_the_property); <br />
  88. * object.getProperty(name_of_the_property);</p>
  89. * @class
  90. * @extends cc.Node
  91. * @param {String} tmxFile tmxFile fileName or content string
  92. * @param {String} resourcePath If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
  93. *
  94. * @property {Array} properties - Properties from the map. They can be added using tilemap editors
  95. * @property {Number} mapOrientation - Map orientation
  96. * @property {Array} objectGroups - Object groups of the map
  97. * @property {Number} mapWidth - Width of the map
  98. * @property {Number} mapHeight - Height of the map
  99. * @property {Number} tileWidth - Width of a tile
  100. * @property {Number} tileHeight - Height of a tile
  101. *
  102. * @example
  103. * //example
  104. * 1.
  105. * //create a TMXTiledMap with file name
  106. * var tmxTiledMap = new cc.TMXTiledMap("res/orthogonal-test1.tmx");
  107. * 2.
  108. * //create a TMXTiledMap with content string and resource path
  109. * var resources = "res/TileMaps";
  110. * var filePath = "res/TileMaps/orthogonal-test1.tmx";
  111. * var xmlStr = cc.loader.getRes(filePath);
  112. * var tmxTiledMap = new cc.TMXTiledMap(xmlStr, resources);
  113. */
  114. cc.TMXTiledMap = cc.Node.extend(/** @lends cc.TMXTiledMap# */{
  115. properties: null,
  116. mapOrientation: null,
  117. objectGroups: null,
  118. //the map's size property measured in tiles
  119. _mapSize: null,
  120. _tileSize: null,
  121. //tile properties
  122. _tileProperties: null,
  123. _className: "TMXTiledMap",
  124. /**
  125. * Creates a TMX Tiled Map with a TMX file or content string. <br/>
  126. * Constructor of cc.TMXTiledMap
  127. * @param {String} tmxFile tmxFile fileName or content string
  128. * @param {String} resourcePath If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
  129. */
  130. ctor:function(tmxFile,resourcePath){
  131. cc.Node.prototype.ctor.call(this);
  132. this._mapSize = cc.size(0, 0);
  133. this._tileSize = cc.size(0, 0);
  134. if(resourcePath !== undefined){
  135. this.initWithXML(tmxFile,resourcePath);
  136. }else if(tmxFile !== undefined){
  137. this.initWithTMXFile(tmxFile);
  138. }
  139. },
  140. /**
  141. * Gets the map size.
  142. * @return {cc.Size}
  143. */
  144. getMapSize:function () {
  145. return cc.size(this._mapSize.width, this._mapSize.height);
  146. },
  147. /**
  148. * Set the map size.
  149. * @param {cc.Size} Var
  150. */
  151. setMapSize:function (Var) {
  152. this._mapSize.width = Var.width;
  153. this._mapSize.height = Var.height;
  154. },
  155. _getMapWidth: function () {
  156. return this._mapSize.width;
  157. },
  158. _setMapWidth: function (width) {
  159. this._mapSize.width = width;
  160. },
  161. _getMapHeight: function () {
  162. return this._mapSize.height;
  163. },
  164. _setMapHeight: function (height) {
  165. this._mapSize.height = height;
  166. },
  167. /**
  168. * Gets the tile size.
  169. * @return {cc.Size}
  170. */
  171. getTileSize:function () {
  172. return cc.size(this._tileSize.width, this._tileSize.height);
  173. },
  174. /**
  175. * Set the tile size
  176. * @param {cc.Size} Var
  177. */
  178. setTileSize:function (Var) {
  179. this._tileSize.width = Var.width;
  180. this._tileSize.height = Var.height;
  181. },
  182. _getTileWidth: function () {
  183. return this._tileSize.width;
  184. },
  185. _setTileWidth: function (width) {
  186. this._tileSize.width = width;
  187. },
  188. _getTileHeight: function () {
  189. return this._tileSize.height;
  190. },
  191. _setTileHeight: function (height) {
  192. this._tileSize.height = height;
  193. },
  194. /**
  195. * map orientation
  196. * @return {Number}
  197. */
  198. getMapOrientation:function () {
  199. return this.mapOrientation;
  200. },
  201. /**
  202. * map orientation
  203. * @param {Number} Var
  204. */
  205. setMapOrientation:function (Var) {
  206. this.mapOrientation = Var;
  207. },
  208. /**
  209. * object groups
  210. * @return {Array}
  211. */
  212. getObjectGroups:function () {
  213. return this.objectGroups;
  214. },
  215. /**
  216. * object groups
  217. * @param {Array} Var
  218. */
  219. setObjectGroups:function (Var) {
  220. this.objectGroups = Var;
  221. },
  222. /**
  223. * Gets the properties
  224. * @return {object}
  225. */
  226. getProperties:function () {
  227. return this.properties;
  228. },
  229. /**
  230. * Set the properties
  231. * @param {object} Var
  232. */
  233. setProperties:function (Var) {
  234. this.properties = Var;
  235. },
  236. /**
  237. * Initializes the instance of cc.TMXTiledMap with tmxFile
  238. * @param {String} tmxFile
  239. * @return {Boolean} Whether the initialization was successful.
  240. * @example
  241. * //example
  242. * var map = new cc.TMXTiledMap()
  243. * map.initWithTMXFile("hello.tmx");
  244. */
  245. initWithTMXFile:function (tmxFile) {
  246. if(!tmxFile || tmxFile.length == 0)
  247. throw "cc.TMXTiledMap.initWithTMXFile(): tmxFile should be non-null or non-empty string.";
  248. this.width = 0;
  249. this.height = 0;
  250. var mapInfo = cc.TMXMapInfo.create(tmxFile);
  251. if (!mapInfo)
  252. return false;
  253. var locTilesets = mapInfo.getTilesets();
  254. if(!locTilesets || locTilesets.length === 0)
  255. cc.log("cc.TMXTiledMap.initWithTMXFile(): Map not found. Please check the filename.");
  256. this._buildWithMapInfo(mapInfo);
  257. return true;
  258. },
  259. /**
  260. * Initializes the instance of cc.TMXTiledMap with tmxString
  261. * @param {String} tmxString
  262. * @param {String} resourcePath
  263. * @return {Boolean} Whether the initialization was successful.
  264. */
  265. initWithXML:function(tmxString, resourcePath){
  266. this.width = 0;
  267. this.height = 0;
  268. var mapInfo = cc.TMXMapInfo.create(tmxString, resourcePath);
  269. var locTilesets = mapInfo.getTilesets();
  270. if(!locTilesets || locTilesets.length === 0)
  271. cc.log("cc.TMXTiledMap.initWithXML(): Map not found. Please check the filename.");
  272. this._buildWithMapInfo(mapInfo);
  273. return true;
  274. },
  275. _buildWithMapInfo:function (mapInfo) {
  276. this._mapSize = mapInfo.getMapSize();
  277. this._tileSize = mapInfo.getTileSize();
  278. this.mapOrientation = mapInfo.orientation;
  279. this.objectGroups = mapInfo.getObjectGroups();
  280. this.properties = mapInfo.properties;
  281. this._tileProperties = mapInfo.getTileProperties();
  282. var idx = 0;
  283. var layers = mapInfo.getLayers();
  284. if (layers) {
  285. var layerInfo = null;
  286. for (var i = 0, len = layers.length; i < len; i++) {
  287. layerInfo = layers[i];
  288. if (layerInfo && layerInfo.visible) {
  289. var child = this._parseLayer(layerInfo, mapInfo);
  290. this.addChild(child, idx, idx);
  291. // update content size with the max size
  292. this.width = Math.max(this.width, child.width);
  293. this.height = Math.max(this.height, child.height);
  294. idx++;
  295. }
  296. }
  297. }
  298. },
  299. /**
  300. * Return All layers array.
  301. * @returns {Array}
  302. */
  303. allLayers: function () {
  304. var retArr = [], locChildren = this._children;
  305. for(var i = 0, len = locChildren.length;i< len;i++){
  306. var layer = locChildren[i];
  307. if(layer && layer instanceof cc.TMXLayer)
  308. retArr.push(layer);
  309. }
  310. return retArr;
  311. },
  312. /**
  313. * return the TMXLayer for the specific layer
  314. * @param {String} layerName
  315. * @return {cc.TMXLayer}
  316. */
  317. getLayer:function (layerName) {
  318. if(!layerName || layerName.length === 0)
  319. throw "cc.TMXTiledMap.getLayer(): layerName should be non-null or non-empty string.";
  320. var locChildren = this._children;
  321. for (var i = 0; i < locChildren.length; i++) {
  322. var layer = locChildren[i];
  323. if (layer && layer.layerName == layerName)
  324. return layer;
  325. }
  326. // layer not found
  327. return null;
  328. },
  329. /**
  330. * Return the TMXObjectGroup for the specific group
  331. * @param {String} groupName
  332. * @return {cc.TMXObjectGroup}
  333. */
  334. getObjectGroup:function (groupName) {
  335. if(!groupName || groupName.length === 0)
  336. throw "cc.TMXTiledMap.getObjectGroup(): groupName should be non-null or non-empty string.";
  337. if (this.objectGroups) {
  338. for (var i = 0; i < this.objectGroups.length; i++) {
  339. var objectGroup = this.objectGroups[i];
  340. if (objectGroup && objectGroup.groupName == groupName) {
  341. return objectGroup;
  342. }
  343. }
  344. }
  345. // objectGroup not found
  346. return null;
  347. },
  348. /**
  349. * Return the value for the specific property name
  350. * @param {String} propertyName
  351. * @return {String}
  352. */
  353. getProperty:function (propertyName) {
  354. return this.properties[propertyName.toString()];
  355. },
  356. /**
  357. * Return properties dictionary for tile GID
  358. * @param {Number} GID
  359. * @return {object}
  360. * @deprecated
  361. */
  362. propertiesForGID:function (GID) {
  363. cc.log("propertiesForGID is deprecated. Please use getPropertiesForGID instead.");
  364. return this.getPropertiesForGID[GID];
  365. },
  366. /**
  367. * Return properties dictionary for tile GID
  368. * @param {Number} GID
  369. * @return {object}
  370. */
  371. getPropertiesForGID: function(GID) {
  372. return this._tileProperties[GID];
  373. },
  374. _parseLayer:function (layerInfo, mapInfo) {
  375. var tileset = this._tilesetForLayer(layerInfo, mapInfo);
  376. var layer = cc.TMXLayer.create(tileset, layerInfo, mapInfo);
  377. // tell the layerinfo to release the ownership of the tiles map.
  378. layerInfo.ownTiles = false;
  379. layer.setupTiles();
  380. return layer;
  381. },
  382. _tilesetForLayer:function (layerInfo, mapInfo) {
  383. var size = layerInfo._layerSize;
  384. var tilesets = mapInfo.getTilesets();
  385. if (tilesets) {
  386. for (var i = tilesets.length - 1; i >= 0; i--) {
  387. var tileset = tilesets[i];
  388. if (tileset) {
  389. for (var y = 0; y < size.height; y++) {
  390. for (var x = 0; x < size.width; x++) {
  391. var pos = x + size.width * y;
  392. var gid = layerInfo._tiles[pos];
  393. if (gid != 0) {
  394. // Optimization: quick return
  395. // if the layer is invalid (more than 1 tileset per layer) an cc.assert will be thrown later
  396. if (((gid & cc.TMX_TILE_FLIPPED_MASK)>>>0) >= tileset.firstGid) {
  397. return tileset;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. }
  404. }
  405. // If all the tiles are 0, return empty tileset
  406. cc.log("cocos2d: Warning: TMX Layer " + layerInfo.name + " has no tiles");
  407. return null;
  408. }
  409. });
  410. var _p = cc.TMXTiledMap.prototype;
  411. // Extended properties
  412. /** @expose */
  413. _p.mapWidth;
  414. cc.defineGetterSetter(_p, "mapWidth", _p._getMapWidth, _p._setMapWidth);
  415. /** @expose */
  416. _p.mapHeight;
  417. cc.defineGetterSetter(_p, "mapHeight", _p._getMapHeight, _p._setMapHeight);
  418. /** @expose */
  419. _p.tileWidth;
  420. cc.defineGetterSetter(_p, "tileWidth", _p._getTileWidth, _p._setTileWidth);
  421. /** @expose */
  422. _p.tileHeight;
  423. cc.defineGetterSetter(_p, "tileHeight", _p._getTileHeight, _p._setTileHeight);
  424. /**
  425. * Creates a TMX Tiled Map with a TMX file or content string.
  426. * Implementation cc.TMXTiledMap
  427. * @deprecated since v3.0 please use new cc.TMXTiledMap(tmxFile,resourcePath) instead.
  428. * @param {String} tmxFile tmxFile fileName or content string
  429. * @param {String} resourcePath If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
  430. * @return {cc.TMXTiledMap|undefined}
  431. * @example
  432. * //example
  433. * 1.
  434. * //create a TMXTiledMap with file name
  435. * var tmxTiledMap = cc.TMXTiledMap.create("res/orthogonal-test1.tmx");
  436. * 2.
  437. * //create a TMXTiledMap with content string and resource path
  438. * var resources = "res/TileMaps";
  439. * var filePath = "res/TileMaps/orthogonal-test1.tmx";
  440. * var xmlStr = cc.loader.getRes(filePath);
  441. * var tmxTiledMap = cc.TMXTiledMap.create(xmlStr, resources);
  442. */
  443. cc.TMXTiledMap.create = function (tmxFile,resourcePath) {
  444. return new cc.TMXTiledMap(tmxFile,resourcePath);
  445. };