CCTMXXMLParser.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. * @constant
  24. * @type Number
  25. */
  26. cc.TMX_PROPERTY_NONE = 0;
  27. /**
  28. * @constant
  29. * @type Number
  30. */
  31. cc.TMX_PROPERTY_MAP = 1;
  32. /**
  33. * @constant
  34. * @type Number
  35. */
  36. cc.TMX_PROPERTY_LAYER = 2;
  37. /**
  38. * @constant
  39. * @type Number
  40. */
  41. cc.TMX_PROPERTY_OBJECTGROUP = 3;
  42. /**
  43. * @constant
  44. * @type Number
  45. */
  46. cc.TMX_PROPERTY_OBJECT = 4;
  47. /**
  48. * @constant
  49. * @type Number
  50. */
  51. cc.TMX_PROPERTY_TILE = 5;
  52. /**
  53. * @constant
  54. * @type Number
  55. */
  56. cc.TMX_TILE_HORIZONTAL_FLAG = 0x80000000;
  57. /**
  58. * @constant
  59. * @type Number
  60. */
  61. cc.TMX_TILE_VERTICAL_FLAG = 0x40000000;
  62. /**
  63. * @constant
  64. * @type Number
  65. */
  66. cc.TMX_TILE_DIAGONAL_FLAG = 0x20000000;
  67. /**
  68. * @constant
  69. * @type Number
  70. */
  71. cc.TMX_TILE_FLIPPED_ALL = (cc.TMX_TILE_HORIZONTAL_FLAG | cc.TMX_TILE_VERTICAL_FLAG | cc.TMX_TILE_DIAGONAL_FLAG) >>> 0;
  72. /**
  73. * @constant
  74. * @type Number
  75. */
  76. cc.TMX_TILE_FLIPPED_MASK = (~(cc.TMX_TILE_FLIPPED_ALL)) >>> 0;
  77. // Bits on the far end of the 32-bit global tile ID (GID's) are used for tile flags
  78. /**
  79. * <p>cc.TMXLayerInfo contains the information about the layers like: <br />
  80. * - Layer name<br />
  81. * - Layer size <br />
  82. * - Layer opacity at creation time (it can be modified at runtime) <br />
  83. * - Whether the layer is visible (if it's not visible, then the CocosNode won't be created) <br />
  84. * <br />
  85. * This information is obtained from the TMX file.</p>
  86. * @class
  87. * @extends cc.Class
  88. *
  89. * @property {Array} properties - Properties of the layer info.
  90. */
  91. cc.TMXLayerInfo = cc.Class.extend(/** @lends cc.TMXLayerInfo# */{
  92. properties:null,
  93. name:"",
  94. _layerSize:null,
  95. _tiles:null,
  96. visible:null,
  97. _opacity:null,
  98. ownTiles:true,
  99. _minGID:100000,
  100. _maxGID:0,
  101. offset:null,
  102. ctor:function () {
  103. this.properties = [];
  104. this.name = "";
  105. this._layerSize = null;
  106. this._tiles = [];
  107. this.visible = true;
  108. this._opacity = 0;
  109. this.ownTiles = true;
  110. this._minGID = 100000;
  111. this._maxGID = 0;
  112. this.offset = cc.p(0,0);
  113. },
  114. /**
  115. * Gets the Properties.
  116. * @return {Array}
  117. */
  118. getProperties:function () {
  119. return this.properties;
  120. },
  121. /**
  122. * Set the Properties.
  123. * @param {object} value
  124. */
  125. setProperties:function (value) {
  126. this.properties = value;
  127. }
  128. });
  129. /**
  130. * <p>cc.TMXTilesetInfo contains the information about the tilesets like: <br />
  131. * - Tileset name<br />
  132. * - Tileset spacing<br />
  133. * - Tileset margin<br />
  134. * - size of the tiles<br />
  135. * - Image used for the tiles<br />
  136. * - Image size<br />
  137. *
  138. * This information is obtained from the TMX file. </p>
  139. * @class
  140. * @extends cc.Class
  141. *
  142. * @property {string} name - Tileset name
  143. * @property {number} firstGid - First grid
  144. * @property {number} spacing - Spacing
  145. * @property {number} margin - Margin
  146. * @property {string} sourceImage - Filename containing the tiles (should be sprite sheet / texture atlas)
  147. * @property {cc.Size|null} imageSize - Size in pixels of the image
  148. */
  149. cc.TMXTilesetInfo = cc.Class.extend(/** @lends cc.TMXTilesetInfo# */{
  150. //Tileset name
  151. name:"",
  152. //First grid
  153. firstGid:0,
  154. _tileSize:null,
  155. //Spacing
  156. spacing:0,
  157. //Margin
  158. margin:0,
  159. //Filename containing the tiles (should be sprite sheet / texture atlas)
  160. sourceImage:"",
  161. //Size in pixels of the image
  162. imageSize:null,
  163. ctor:function () {
  164. this._tileSize = cc.size(0, 0);
  165. this.imageSize = cc.size(0, 0);
  166. },
  167. /**
  168. * Return rect
  169. * @param {Number} gid
  170. * @return {cc.Rect}
  171. */
  172. rectForGID:function (gid) {
  173. var rect = cc.rect(0, 0, 0, 0);
  174. rect.width = this._tileSize.width;
  175. rect.height = this._tileSize.height;
  176. gid &= cc.TMX_TILE_FLIPPED_MASK;
  177. gid = gid - parseInt(this.firstGid, 10);
  178. var max_x = parseInt((this.imageSize.width - this.margin * 2 + this.spacing) / (this._tileSize.width + this.spacing), 10);
  179. rect.x = parseInt((gid % max_x) * (this._tileSize.width + this.spacing) + this.margin, 10);
  180. rect.y = parseInt(parseInt(gid / max_x, 10) * (this._tileSize.height + this.spacing) + this.margin, 10);
  181. return rect;
  182. }
  183. });
  184. /**
  185. * <p>cc.TMXMapInfo contains the information about the map like: <br/>
  186. *- Map orientation (hexagonal, isometric or orthogonal)<br/>
  187. *- Tile size<br/>
  188. *- Map size</p>
  189. *
  190. * <p>And it also contains: <br/>
  191. * - Layers (an array of TMXLayerInfo objects)<br/>
  192. * - Tilesets (an array of TMXTilesetInfo objects) <br/>
  193. * - ObjectGroups (an array of TMXObjectGroupInfo objects) </p>
  194. *
  195. * <p>This information is obtained from the TMX file. </p>
  196. * @class
  197. * @extends cc.saxParser
  198. *
  199. * @property {Array} properties - Properties of the map info.
  200. * @property {Number} orientation - Map orientation.
  201. * @property {Object} parentElement - Parent element.
  202. * @property {Number} parentGID - Parent GID.
  203. * @property {Object} layerAttrs - Layer attributes.
  204. * @property {Boolean} storingCharacters - Is reading storing characters stream.
  205. * @property {String} tmxFileName - TMX file name.
  206. * @property {String} currentString - Current string stored from characters stream.
  207. * @property {Number} mapWidth - Width of the map
  208. * @property {Number} mapHeight - Height of the map
  209. * @property {Number} tileWidth - Width of a tile
  210. * @property {Number} tileHeight - Height of a tile
  211. *
  212. * @param {String} tmxFile fileName or content string
  213. * @param {String} resourcePath If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
  214. * @example
  215. * 1.
  216. * //create a TMXMapInfo with file name
  217. * var tmxMapInfo = cc.TMXMapInfo.create("res/orthogonal-test1.tmx");
  218. * 2.
  219. * //create a TMXMapInfo with content string and resource path
  220. * var resources = "res/TileMaps";
  221. * var filePath = "res/TileMaps/orthogonal-test1.tmx";
  222. * var xmlStr = cc.loader.getRes(filePath);
  223. * var tmxMapInfo = cc.TMXMapInfo.create(xmlStr, resources);
  224. */
  225. cc.TMXMapInfo = cc.SAXParser.extend(/** @lends cc.TMXMapInfo# */{
  226. properties:null,
  227. orientation:null,
  228. parentElement:null,
  229. parentGID:null,
  230. layerAttrs:0,
  231. storingCharacters:false,
  232. tmxFileName:null,
  233. currentString:null,
  234. _objectGroups:null,
  235. _mapSize:null,
  236. _tileSize:null,
  237. _layers:null,
  238. _tilesets:null,
  239. // tile properties
  240. _tileProperties:null,
  241. _resources:"",
  242. _currentFirstGID:0,
  243. /**
  244. * Creates a TMX Format with a tmx file or content string <br/>
  245. * Constructor of cc.TMXMapInfo
  246. * @param {String} tmxFile fileName or content string
  247. * @param {String} resourcePath If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
  248. */
  249. ctor:function (tmxFile, resourcePath) {
  250. cc.SAXParser.prototype.ctor.apply(this);
  251. this._mapSize = cc.size(0, 0);
  252. this._tileSize = cc.size(0, 0);
  253. this._layers = [];
  254. this._tilesets = [];
  255. this._objectGroups = [];
  256. this.properties = [];
  257. this._tileProperties = {};
  258. this._currentFirstGID = 0;
  259. if (resourcePath !== undefined) {
  260. this.initWithXML(tmxFile,resourcePath);
  261. } else if(tmxFile !== undefined){
  262. this.initWithTMXFile(tmxFile);
  263. }
  264. },
  265. /**
  266. * Gets Map orientation.
  267. * @return {Number}
  268. */
  269. getOrientation:function () {
  270. return this.orientation;
  271. },
  272. /**
  273. * Set the Map orientation.
  274. * @param {Number} value
  275. */
  276. setOrientation:function (value) {
  277. this.orientation = value;
  278. },
  279. /**
  280. * Map width & height
  281. * @return {cc.Size}
  282. */
  283. getMapSize:function () {
  284. return cc.size(this._mapSize.width,this._mapSize.height);
  285. },
  286. /**
  287. * Map width & height
  288. * @param {cc.Size} value
  289. */
  290. setMapSize:function (value) {
  291. this._mapSize.width = value.width;
  292. this._mapSize.height = value.height;
  293. },
  294. _getMapWidth: function () {
  295. return this._mapSize.width;
  296. },
  297. _setMapWidth: function (width) {
  298. this._mapSize.width = width;
  299. },
  300. _getMapHeight: function () {
  301. return this._mapSize.height;
  302. },
  303. _setMapHeight: function (height) {
  304. this._mapSize.height = height;
  305. },
  306. /**
  307. * Tiles width & height
  308. * @return {cc.Size}
  309. */
  310. getTileSize:function () {
  311. return cc.size(this._tileSize.width, this._tileSize.height);
  312. },
  313. /**
  314. * Tiles width & height
  315. * @param {cc.Size} value
  316. */
  317. setTileSize:function (value) {
  318. this._tileSize.width = value.width;
  319. this._tileSize.height = value.height;
  320. },
  321. _getTileWidth: function () {
  322. return this._tileSize.width;
  323. },
  324. _setTileWidth: function (width) {
  325. this._tileSize.width = width;
  326. },
  327. _getTileHeight: function () {
  328. return this._tileSize.height;
  329. },
  330. _setTileHeight: function (height) {
  331. this._tileSize.height = height;
  332. },
  333. /**
  334. * Layers
  335. * @return {Array}
  336. */
  337. getLayers:function () {
  338. return this._layers;
  339. },
  340. /**
  341. * Layers
  342. * @param {cc.TMXLayerInfo} value
  343. */
  344. setLayers:function (value) {
  345. this._layers.push(value);
  346. },
  347. /**
  348. * tilesets
  349. * @return {Array}
  350. */
  351. getTilesets:function () {
  352. return this._tilesets;
  353. },
  354. /**
  355. * tilesets
  356. * @param {cc.TMXTilesetInfo} value
  357. */
  358. setTilesets:function (value) {
  359. this._tilesets.push(value);
  360. },
  361. /**
  362. * ObjectGroups
  363. * @return {Array}
  364. */
  365. getObjectGroups:function () {
  366. return this._objectGroups;
  367. },
  368. /**
  369. * ObjectGroups
  370. * @param {cc.TMXObjectGroup} value
  371. */
  372. setObjectGroups:function (value) {
  373. this._objectGroups.push(value);
  374. },
  375. /**
  376. * parent element
  377. * @return {Object}
  378. */
  379. getParentElement:function () {
  380. return this.parentElement;
  381. },
  382. /**
  383. * parent element
  384. * @param {Object} value
  385. */
  386. setParentElement:function (value) {
  387. this.parentElement = value;
  388. },
  389. /**
  390. * parent GID
  391. * @return {Number}
  392. */
  393. getParentGID:function () {
  394. return this.parentGID;
  395. },
  396. /**
  397. * parent GID
  398. * @param {Number} value
  399. */
  400. setParentGID:function (value) {
  401. this.parentGID = value;
  402. },
  403. /**
  404. * Layer attribute
  405. * @return {Object}
  406. */
  407. getLayerAttribs:function () {
  408. return this.layerAttrs;
  409. },
  410. /**
  411. * Layer attribute
  412. * @param {Object} value
  413. */
  414. setLayerAttribs:function (value) {
  415. this.layerAttrs = value;
  416. },
  417. /**
  418. * Is reading storing characters stream
  419. * @return {Boolean}
  420. */
  421. getStoringCharacters:function () {
  422. return this.storingCharacters;
  423. },
  424. /**
  425. * Is reading storing characters stream
  426. * @param {Boolean} value
  427. */
  428. setStoringCharacters:function (value) {
  429. this.storingCharacters = value;
  430. },
  431. /**
  432. * Properties
  433. * @return {Array}
  434. */
  435. getProperties:function () {
  436. return this.properties;
  437. },
  438. /**
  439. * Properties
  440. * @param {object} value
  441. */
  442. setProperties:function (value) {
  443. this.properties = value;
  444. },
  445. /**
  446. * Initializes a TMX format with a tmx file
  447. * @param {String} tmxFile
  448. * @return {Element}
  449. */
  450. initWithTMXFile:function (tmxFile) {
  451. this._internalInit(tmxFile, null);
  452. return this.parseXMLFile(tmxFile);
  453. },
  454. /**
  455. * initializes a TMX format with an XML string and a TMX resource path
  456. * @param {String} tmxString
  457. * @param {String} resourcePath
  458. * @return {Boolean}
  459. */
  460. initWithXML:function (tmxString, resourcePath) {
  461. this._internalInit(null, resourcePath);
  462. return this.parseXMLString(tmxString);
  463. },
  464. /** Initalises parsing of an XML file, either a tmx (Map) file or tsx (Tileset) file
  465. * @param {String} tmxFile
  466. * @param {boolean} [isXmlString=false]
  467. * @return {Element}
  468. */
  469. parseXMLFile:function (tmxFile, isXmlString) {
  470. isXmlString = isXmlString || false;
  471. var xmlStr = isXmlString ? tmxFile : cc.loader.getRes(tmxFile);
  472. if(!xmlStr) throw "Please load the resource first : " + tmxFile;
  473. var mapXML = this._parseXML(xmlStr);
  474. var i, j;
  475. // PARSE <map>
  476. var map = mapXML.documentElement;
  477. var version = map.getAttribute('version');
  478. var orientationStr = map.getAttribute('orientation');
  479. if (map.nodeName == "map") {
  480. if (version != "1.0" && version !== null)
  481. cc.log("cocos2d: TMXFormat: Unsupported TMX version:" + version);
  482. if (orientationStr == "orthogonal")
  483. this.orientation = cc.TMX_ORIENTATION_ORTHO;
  484. else if (orientationStr == "isometric")
  485. this.orientation = cc.TMX_ORIENTATION_ISO;
  486. else if (orientationStr == "hexagonal")
  487. this.orientation = cc.TMX_ORIENTATION_HEX;
  488. else if (orientationStr !== null)
  489. cc.log("cocos2d: TMXFomat: Unsupported orientation:" + orientationStr);
  490. var mapSize = cc.size(0, 0);
  491. mapSize.width = parseFloat(map.getAttribute('width'));
  492. mapSize.height = parseFloat(map.getAttribute('height'));
  493. this.setMapSize(mapSize);
  494. mapSize = cc.size(0, 0);
  495. mapSize.width = parseFloat(map.getAttribute('tilewidth'));
  496. mapSize.height = parseFloat(map.getAttribute('tileheight'));
  497. this.setTileSize(mapSize);
  498. // The parent element is the map
  499. var propertyArr = map.querySelectorAll("map > properties > property");
  500. if (propertyArr) {
  501. var aPropertyDict = {};
  502. for (i = 0; i < propertyArr.length; i++) {
  503. aPropertyDict[propertyArr[i].getAttribute('name')] = propertyArr[i].getAttribute('value');
  504. }
  505. this.properties = aPropertyDict;
  506. }
  507. }
  508. // PARSE <tileset>
  509. var tilesets = map.getElementsByTagName('tileset');
  510. if (map.nodeName !== "map") {
  511. tilesets = [];
  512. tilesets.push(map);
  513. }
  514. for (i = 0; i < tilesets.length; i++) {
  515. var selTileset = tilesets[i];
  516. // If this is an external tileset then start parsing that
  517. var tsxName = selTileset.getAttribute('source');
  518. if (tsxName) {
  519. //this._currentFirstGID = parseInt(selTileset.getAttribute('firstgid'));
  520. var tsxPath = isXmlString ? cc.path.join(this._resources, tsxName) : cc.path.changeBasename(tmxFile, tsxName);
  521. this.parseXMLFile(tsxPath);
  522. } else {
  523. var tileset = new cc.TMXTilesetInfo();
  524. tileset.name = selTileset.getAttribute('name') || "";
  525. //TODO need fix
  526. //if(this._currentFirstGID === 0){
  527. tileset.firstGid = parseInt(selTileset.getAttribute('firstgid')) || 0;
  528. //}else{
  529. // tileset.firstGid = this._currentFirstGID;
  530. // this._currentFirstGID = 0;
  531. //}
  532. tileset.spacing = parseInt(selTileset.getAttribute('spacing')) || 0;
  533. tileset.margin = parseInt(selTileset.getAttribute('margin')) || 0;
  534. var tilesetSize = cc.size(0, 0);
  535. tilesetSize.width = parseFloat(selTileset.getAttribute('tilewidth'));
  536. tilesetSize.height = parseFloat(selTileset.getAttribute('tileheight'));
  537. tileset._tileSize = tilesetSize;
  538. var image = selTileset.getElementsByTagName('image')[0];
  539. var imagename = image.getAttribute('source');
  540. var num = -1;
  541. if(this.tmxFileName)
  542. num = this.tmxFileName.lastIndexOf("/");
  543. if (num !== -1) {
  544. var dir = this.tmxFileName.substr(0, num + 1);
  545. tileset.sourceImage = dir + imagename;
  546. } else {
  547. tileset.sourceImage = this._resources + (this._resources ? "/" : "") + imagename;
  548. }
  549. this.setTilesets(tileset);
  550. // PARSE <tile>
  551. var tiles = selTileset.getElementsByTagName('tile');
  552. if (tiles) {
  553. for (var tIdx = 0; tIdx < tiles.length; tIdx++) {
  554. var t = tiles[tIdx];
  555. this.parentGID = parseInt(tileset.firstGid) + parseInt(t.getAttribute('id') || 0);
  556. var tp = t.querySelectorAll("properties > property");
  557. if (tp) {
  558. var dict = {};
  559. for (j = 0; j < tp.length; j++) {
  560. var name = tp[j].getAttribute('name');
  561. dict[name] = tp[j].getAttribute('value');
  562. }
  563. this._tileProperties[this.parentGID] = dict;
  564. }
  565. }
  566. }
  567. }
  568. }
  569. // PARSE <layer>
  570. var layers = map.getElementsByTagName('layer');
  571. if (layers) {
  572. for (i = 0; i < layers.length; i++) {
  573. var selLayer = layers[i];
  574. var data = selLayer.getElementsByTagName('data')[0];
  575. var layer = new cc.TMXLayerInfo();
  576. layer.name = selLayer.getAttribute('name');
  577. var layerSize = cc.size(0, 0);
  578. layerSize.width = parseFloat(selLayer.getAttribute('width'));
  579. layerSize.height = parseFloat(selLayer.getAttribute('height'));
  580. layer._layerSize = layerSize;
  581. var visible = selLayer.getAttribute('visible');
  582. layer.visible = !(visible == "0");
  583. var opacity = selLayer.getAttribute('opacity') || 1;
  584. if (opacity)
  585. layer._opacity = parseInt(255 * parseFloat(opacity));
  586. else
  587. layer._opacity = 255;
  588. layer.offset = cc.p(parseFloat(selLayer.getAttribute('x')) || 0, parseFloat(selLayer.getAttribute('y')) || 0);
  589. var nodeValue = '';
  590. for (j = 0; j < data.childNodes.length; j++) {
  591. nodeValue += data.childNodes[j].nodeValue
  592. }
  593. nodeValue = nodeValue.trim();
  594. // Unpack the tilemap data
  595. var compression = data.getAttribute('compression');
  596. var encoding = data.getAttribute('encoding');
  597. if(compression && compression !== "gzip" && compression !== "zlib"){
  598. cc.log("cc.TMXMapInfo.parseXMLFile(): unsupported compression method");
  599. return null;
  600. }
  601. switch (compression) {
  602. case 'gzip':
  603. layer._tiles = cc.unzipBase64AsArray(nodeValue, 4);
  604. break;
  605. case 'zlib':
  606. var inflator = new Zlib.Inflate(cc.Codec.Base64.decodeAsArray(nodeValue, 1));
  607. layer._tiles = cc.uint8ArrayToUint32Array(inflator.decompress());
  608. break;
  609. case null:
  610. case '':
  611. // Uncompressed
  612. if (encoding == "base64")
  613. layer._tiles = cc.Codec.Base64.decodeAsArray(nodeValue, 4);
  614. else if (encoding === "csv") {
  615. layer._tiles = [];
  616. var csvTiles = nodeValue.split(',');
  617. for (var csvIdx = 0; csvIdx < csvTiles.length; csvIdx++)
  618. layer._tiles.push(parseInt(csvTiles[csvIdx]));
  619. } else {
  620. //XML format
  621. var selDataTiles = data.getElementsByTagName("tile");
  622. layer._tiles = [];
  623. for (var xmlIdx = 0; xmlIdx < selDataTiles.length; xmlIdx++)
  624. layer._tiles.push(parseInt(selDataTiles[xmlIdx].getAttribute("gid")));
  625. }
  626. break;
  627. default:
  628. if(this.layerAttrs == cc.TMXLayerInfo.ATTRIB_NONE)
  629. cc.log("cc.TMXMapInfo.parseXMLFile(): Only base64 and/or gzip/zlib maps are supported");
  630. break;
  631. }
  632. // The parent element is the last layer
  633. var layerProps = selLayer.querySelectorAll("properties > property");
  634. if (layerProps) {
  635. var layerProp = {};
  636. for (j = 0; j < layerProps.length; j++) {
  637. layerProp[layerProps[j].getAttribute('name')] = layerProps[j].getAttribute('value');
  638. }
  639. layer.properties = layerProp;
  640. }
  641. this.setLayers(layer);
  642. }
  643. }
  644. // PARSE <objectgroup>
  645. var objectGroups = map.getElementsByTagName('objectgroup');
  646. if (objectGroups) {
  647. for (i = 0; i < objectGroups.length; i++) {
  648. var selGroup = objectGroups[i];
  649. var objectGroup = new cc.TMXObjectGroup();
  650. objectGroup.groupName = selGroup.getAttribute('name');
  651. objectGroup.setPositionOffset(cc.p(parseFloat(selGroup.getAttribute('x')) * this.getTileSize().width || 0,
  652. parseFloat(selGroup.getAttribute('y')) * this.getTileSize().height || 0));
  653. var groupProps = selGroup.querySelectorAll("objectgroup > properties > property");
  654. if (groupProps) {
  655. for (j = 0; j < groupProps.length; j++) {
  656. var groupProp = {};
  657. groupProp[groupProps[j].getAttribute('name')] = groupProps[j].getAttribute('value');
  658. // Add the property to the layer
  659. objectGroup.properties = groupProp;
  660. }
  661. }
  662. var objects = selGroup.querySelectorAll('object');
  663. if (objects) {
  664. for (j = 0; j < objects.length; j++) {
  665. var selObj = objects[j];
  666. // The value for "type" was blank or not a valid class name
  667. // Create an instance of TMXObjectInfo to store the object and its properties
  668. var objectProp = {};
  669. // Set the name of the object to the value for "name"
  670. objectProp["name"] = selObj.getAttribute('name') || "";
  671. // Assign all the attributes as key/name pairs in the properties dictionary
  672. objectProp["type"] = selObj.getAttribute('type') || "";
  673. objectProp["x"] = parseInt(selObj.getAttribute('x') || 0) + objectGroup.getPositionOffset().x;
  674. var y = parseInt(selObj.getAttribute('y') || 0) + objectGroup.getPositionOffset().y;
  675. objectProp["width"] = parseInt(selObj.getAttribute('width')) || 0;
  676. objectProp["height"] = parseInt(selObj.getAttribute('height')) || 0;
  677. // Correct y position. (Tiled uses Flipped, cocos2d uses Standard)
  678. objectProp["y"] = parseInt(this.getMapSize().height * this.getTileSize().height) - y - objectProp["height"];
  679. var docObjProps = selObj.querySelectorAll("properties > property");
  680. if (docObjProps) {
  681. for (var k = 0; k < docObjProps.length; k++)
  682. objectProp[docObjProps[k].getAttribute('name')] = docObjProps[k].getAttribute('value');
  683. }
  684. //polygon
  685. var polygonProps = selObj.querySelectorAll("polygon");
  686. if(polygonProps && polygonProps.length > 0) {
  687. var selPgPointStr = polygonProps[0].getAttribute('points');
  688. if(selPgPointStr)
  689. objectProp["polygonPoints"] = this._parsePointsString(selPgPointStr);
  690. }
  691. //polyline
  692. var polylineProps = selObj.querySelectorAll("polyline");
  693. if(polylineProps && polylineProps.length > 0) {
  694. var selPlPointStr = polylineProps[0].getAttribute('points');
  695. if(selPlPointStr)
  696. objectProp["polylinePoints"] = this._parsePointsString(selPlPointStr);
  697. }
  698. // Add the object to the objectGroup
  699. objectGroup.setObjects(objectProp);
  700. }
  701. }
  702. this.setObjectGroups(objectGroup);
  703. }
  704. }
  705. return map;
  706. },
  707. _parsePointsString:function(pointsString){
  708. if(!pointsString)
  709. return null;
  710. var points = [];
  711. var pointsStr = pointsString.split(' ');
  712. for(var i = 0; i < pointsStr.length; i++){
  713. var selPointStr = pointsStr[i].split(',');
  714. points.push({'x':selPointStr[0], 'y':selPointStr[1]});
  715. }
  716. return points;
  717. },
  718. /**
  719. * initializes parsing of an XML string, either a tmx (Map) string or tsx (Tileset) string
  720. * @param {String} xmlString
  721. * @return {Boolean}
  722. */
  723. parseXMLString:function (xmlString) {
  724. return this.parseXMLFile(xmlString, true);
  725. },
  726. /**
  727. * Gets the tile properties.
  728. * @return {object}
  729. */
  730. getTileProperties:function () {
  731. return this._tileProperties;
  732. },
  733. /**
  734. * Set the tile properties.
  735. * @param {object} tileProperties
  736. */
  737. setTileProperties:function (tileProperties) {
  738. this._tileProperties.push(tileProperties);
  739. },
  740. /**
  741. * Gets the currentString
  742. * @return {String}
  743. */
  744. getCurrentString:function () {
  745. return this.currentString;
  746. },
  747. /**
  748. * Set the currentString
  749. * @param {String} currentString
  750. */
  751. setCurrentString:function (currentString) {
  752. this.currentString = currentString;
  753. },
  754. /**
  755. * Gets the tmxFileName
  756. * @return {String}
  757. */
  758. getTMXFileName:function () {
  759. return this.tmxFileName;
  760. },
  761. /**
  762. * Set the tmxFileName
  763. * @param {String} fileName
  764. */
  765. setTMXFileName:function (fileName) {
  766. this.tmxFileName = fileName;
  767. },
  768. _internalInit:function (tmxFileName, resourcePath) {
  769. this._tilesets.length = 0;
  770. this._layers.length = 0;
  771. this.tmxFileName = tmxFileName;
  772. if (resourcePath)
  773. this._resources = resourcePath;
  774. this._objectGroups.length = 0;
  775. this.properties.length = 0;
  776. this._tileProperties.length = 0;
  777. // tmp vars
  778. this.currentString = "";
  779. this.storingCharacters = false;
  780. this.layerAttrs = cc.TMXLayerInfo.ATTRIB_NONE;
  781. this.parentElement = cc.TMX_PROPERTY_NONE;
  782. this._currentFirstGID = 0;
  783. }
  784. });
  785. var _p = cc.TMXMapInfo.prototype;
  786. // Extended properties
  787. /** @expose */
  788. _p.mapWidth;
  789. cc.defineGetterSetter(_p, "mapWidth", _p._getMapWidth, _p._setMapWidth);
  790. /** @expose */
  791. _p.mapHeight;
  792. cc.defineGetterSetter(_p, "mapHeight", _p._getMapHeight, _p._setMapHeight);
  793. /** @expose */
  794. _p.tileWidth;
  795. cc.defineGetterSetter(_p, "tileWidth", _p._getTileWidth, _p._setTileWidth);
  796. /** @expose */
  797. _p.tileHeight;
  798. cc.defineGetterSetter(_p, "tileHeight", _p._getTileHeight, _p._setTileHeight);
  799. /**
  800. * Creates a TMX Format with a tmx file or content string
  801. * @deprecated since v3.0 please use new cc.TMXMapInfo(tmxFile, resourcePath) instead.
  802. * @param {String} tmxFile fileName or content string
  803. * @param {String} resourcePath If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
  804. * @return {cc.TMXMapInfo}
  805. * @example
  806. * 1.
  807. * //create a TMXMapInfo with file name
  808. * var tmxMapInfo = cc.TMXMapInfo.create("res/orthogonal-test1.tmx");
  809. * 2.
  810. * //create a TMXMapInfo with content string and resource path
  811. * var resources = "res/TileMaps";
  812. * var filePath = "res/TileMaps/orthogonal-test1.tmx";
  813. * var xmlStr = cc.loader.getRes(filePath);
  814. * var tmxMapInfo = cc.TMXMapInfo.create(xmlStr, resources);
  815. */
  816. cc.TMXMapInfo.create = function (tmxFile, resourcePath) {
  817. return new cc.TMXMapInfo(tmxFile, resourcePath);
  818. };
  819. cc.loader.register(["tmx", "tsx"], cc._txtLoader);
  820. /**
  821. * @constant
  822. * @type Number
  823. */
  824. cc.TMXLayerInfo.ATTRIB_NONE = 1 << 0;
  825. /**
  826. * @constant
  827. * @type Number
  828. */
  829. cc.TMXLayerInfo.ATTRIB_BASE64 = 1 << 1;
  830. /**
  831. * @constant
  832. * @type Number
  833. */
  834. cc.TMXLayerInfo.ATTRIB_GZIP = 1 << 2;
  835. /**
  836. * @constant
  837. * @type Number
  838. */
  839. cc.TMXLayerInfo.ATTRIB_ZLIB = 1 << 3;