CCTMXXMLParser.js 26 KB

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