CCTMXTiledMap.js 13 KB

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