CCTMXTiledMap.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /****************************************************************************
  2. Copyright (c) 2009-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #ifndef __CCTMX_TILE_MAP_H__
  25. #define __CCTMX_TILE_MAP_H__
  26. #include "2d/CCNode.h"
  27. #include "2d/CCTMXObjectGroup.h"
  28. #include "base/CCValue.h"
  29. NS_CC_BEGIN
  30. class TMXLayer;
  31. class TMXLayerInfo;
  32. class TMXTilesetInfo;
  33. class TMXMapInfo;
  34. /**
  35. * @addtogroup _2d
  36. * @{
  37. */
  38. /** Possible orientations of the TMX map. */
  39. enum
  40. {
  41. /** Orthogonal orientation. */
  42. TMXOrientationOrtho,
  43. /** Hexagonal orientation. */
  44. TMXOrientationHex,
  45. /** Isometric orientation. */
  46. TMXOrientationIso,
  47. /** Isometric staggered orientation. */
  48. TMXOrientationStaggered,
  49. };
  50. /** Possible stagger axis of the TMX map. */
  51. enum
  52. {
  53. /** Stagger Axis x. */
  54. TMXStaggerAxis_X,
  55. /** Stagger Axis y. */
  56. TMXStaggerAxis_Y,
  57. };
  58. /** Possible stagger index of the TMX map. */
  59. enum
  60. {
  61. /** Stagger Index: Odd */
  62. TMXStaggerIndex_Odd,
  63. /** Stagger Index: Even */
  64. TMXStaggerIndex_Even,
  65. };
  66. /** @brief TMXTiledMap knows how to parse and render a TMX map.
  67. * It adds support for the TMX tiled map format used by http://www.mapeditor.org
  68. * It supports isometric, hexagonal and orthogonal tiles.
  69. * It also supports object groups, objects, and properties.
  70. * Features:
  71. * - Each tile will be treated as an Sprite.
  72. * - The sprites are created on demand. They will be created only when you call "layer->tileAt(position)".
  73. * - Each tile can be rotated / moved / scaled / tinted / "opaqued", since each tile is a Sprite.
  74. * - Tiles can be added/removed in runtime.
  75. * - The z-order of the tiles can be modified in runtime.
  76. * - Each tile has an anchorPoint of (0,0).
  77. * - The anchorPoint of the TMXTileMap is (0,0).
  78. * - The TMX layers will be added as a child.
  79. * - The TMX layers will be aliased by default.
  80. * - The tileset image will be loaded using the TextureCache.
  81. * - Each tile will have a unique tag.
  82. * - Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z.
  83. * - Each object group will be treated as an MutableArray.
  84. * - Object class which will contain all the properties in a dictionary.
  85. * - Properties can be assigned to the Map, Layer, Object Group, and Object.
  86. * Limitations:
  87. * - It only supports one tileset per layer.
  88. * - Embedded images are not supported.
  89. * - It only supports the XML format (the JSON format is not supported).
  90. * Technical description:
  91. * Each layer is created using an TMXLayer (subclass of SpriteBatchNode). If you have 5 layers, then 5 TMXLayer will be created,
  92. * unless the layer visibility is off. In that case, the layer won't be created at all.
  93. * You can obtain the layers (TMXLayer objects) at runtime by:
  94. * - map->getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...
  95. * - map->getLayer(name_of_the_layer);
  96. * Each object group is created using a TMXObjectGroup which is a subclass of MutableArray.
  97. * You can obtain the object groups at runtime by:
  98. * - map->getObjectGroup(name_of_the_object_group);
  99. * Each object is a TMXObject.
  100. * Each property is stored as a key-value pair in an MutableDictionary.
  101. * You can obtain the properties at runtime by:
  102. * map->getProperty(name_of_the_property);
  103. * layer->getProperty(name_of_the_property);
  104. * objectGroup->getProperty(name_of_the_property);
  105. * object->getProperty(name_of_the_property);
  106. * @since v0.8.1
  107. */
  108. class CC_DLL TMXTiledMap : public Node
  109. {
  110. public:
  111. /** Creates a TMX Tiled Map with a TMX file.
  112. *
  113. * @param tmxFile A TMX file.
  114. * @return An autorelease object.
  115. */
  116. static TMXTiledMap* create(const std::string& tmxFile);
  117. /** Initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources.
  118. *
  119. * @param tmxString A TMX formatted XML string.
  120. * @param resourcePath The path to TMX resources.
  121. * @return An autorelease object.
  122. * @js NA
  123. */
  124. static TMXTiledMap* createWithXML(const std::string& tmxString, const std::string& resourcePath);
  125. /** Return the TMXLayer for the specific layer.
  126. *
  127. * @param layerName A specific layer.
  128. * @return The TMXLayer for the specific layer.
  129. */
  130. TMXLayer* getLayer(const std::string& layerName) const;
  131. /**
  132. * @js NA
  133. * @lua NA
  134. */
  135. CC_DEPRECATED_ATTRIBUTE TMXLayer* layerNamed(const std::string& layerName) const { return getLayer(layerName); };
  136. /** Return the TMXObjectGroup for the specific group.
  137. *
  138. * @param groupName The group Name.
  139. * @return A Type of TMXObjectGroup.
  140. */
  141. TMXObjectGroup* getObjectGroup(const std::string& groupName) const;
  142. /**
  143. * @js NA
  144. * @lua NA
  145. */
  146. CC_DEPRECATED_ATTRIBUTE TMXObjectGroup* objectGroupNamed(const std::string& groupName) const { return getObjectGroup(groupName); };
  147. /** Return the value for the specific property name.
  148. *
  149. * @param propertyName The specific property name.
  150. * @return Return the value for the specific property name.
  151. */
  152. Value getProperty(const std::string& propertyName) const;
  153. /**
  154. * @js NA
  155. * @lua NA
  156. */
  157. CC_DEPRECATED_ATTRIBUTE Value propertyNamed(const char *propertyName) const { return getProperty(propertyName); };
  158. /** Return properties dictionary for tile GID.
  159. *
  160. * @param GID The tile GID.
  161. * @return Return properties dictionary for tile GID.
  162. */
  163. Value getPropertiesForGID(int GID) const;
  164. CC_DEPRECATED_ATTRIBUTE Value propertiesForGID(int GID) const { return getPropertiesForGID(GID); };
  165. /** Assigns properties to argument value, returns true if it did found properties
  166. * for that GID and did assigned a value, else it returns false.
  167. *
  168. * @param GID The tile GID.
  169. * @param value Argument value.
  170. * @return Return true if it did found properties for that GID and did assigned a value, else it returns false.
  171. */
  172. bool getPropertiesForGID(int GID, Value** value);
  173. /** The map's size property measured in tiles.
  174. *
  175. * @return The map's size property measured in tiles.
  176. */
  177. const Size& getMapSize() const { return _mapSize; }
  178. /** Set the map's size property measured in tiles.
  179. *
  180. * @param mapSize The map's size property measured in tiles.
  181. */
  182. void setMapSize(const Size& mapSize) { _mapSize = mapSize; }
  183. /** The tiles's size property measured in pixels.
  184. *
  185. * @return The tiles's size property measured in pixels.
  186. */
  187. const Size& getTileSize() const { return _tileSize; }
  188. /** Set the tiles's size property measured in pixels.
  189. *
  190. * @param tileSize The tiles's size property measured in pixels.
  191. */
  192. void setTileSize(const Size& tileSize) { _tileSize = tileSize; }
  193. /** Map orientation.
  194. *
  195. * @return Map orientation.
  196. */
  197. int getMapOrientation() const { return _mapOrientation; }
  198. /** Set map orientation.
  199. *
  200. * @param mapOrientation The map orientation.
  201. */
  202. void setMapOrientation(int mapOrientation) { _mapOrientation = mapOrientation; }
  203. /** Get the Object groups.
  204. *
  205. * @return The object groups.
  206. */
  207. const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; }
  208. Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; }
  209. /** Set the object groups.
  210. *
  211. * @param groups The object groups.
  212. */
  213. void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
  214. _objectGroups = groups;
  215. }
  216. /** Properties.
  217. *
  218. * @return Properties.
  219. */
  220. ValueMap& getProperties() { return _properties; }
  221. /** Set the properties.
  222. *
  223. * @param properties A Type of ValueMap to set the properties.
  224. */
  225. void setProperties(const ValueMap& properties) {
  226. _properties = properties;
  227. }
  228. /** Get the description.
  229. * @js NA
  230. */
  231. virtual std::string getDescription() const override;
  232. int getLayerNum();
  233. const std::string& getResourceFile() const { return _tmxFile; }
  234. CC_CONSTRUCTOR_ACCESS:
  235. /**
  236. * @js ctor
  237. */
  238. TMXTiledMap();
  239. /**
  240. * @js NA
  241. * @lua NA
  242. */
  243. virtual ~TMXTiledMap();
  244. /** initializes a TMX Tiled Map with a TMX file */
  245. bool initWithTMXFile(const std::string& tmxFile);
  246. /** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
  247. bool initWithXML(const std::string& tmxString, const std::string& resourcePath);
  248. protected:
  249. TMXLayer * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
  250. TMXTilesetInfo * tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
  251. void buildWithMapInfo(TMXMapInfo* mapInfo);
  252. /** the map's size property measured in tiles */
  253. Size _mapSize;
  254. /** the tiles's size property measured in pixels */
  255. Size _tileSize;
  256. /** map orientation */
  257. int _mapOrientation;
  258. /** object groups */
  259. Vector<TMXObjectGroup*> _objectGroups;
  260. /** properties */
  261. ValueMap _properties;
  262. //! tile properties
  263. ValueMapIntKey _tileProperties;
  264. std::string _tmxFile;
  265. int _tmxLayerNum;
  266. static const int TMXLayerTag = 32768;
  267. private:
  268. CC_DISALLOW_COPY_AND_ASSIGN(TMXTiledMap);
  269. };
  270. // end of tilemap_parallax_nodes group
  271. /// @}
  272. NS_CC_END
  273. #endif //__CCTMX_TILE_MAP_H__