CCFastTMXTiledMap.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __CC_FAST_TMX_TILEMAP_H__
  24. #define __CC_FAST_TMX_TILEMAP_H__
  25. #include "2d/CCNode.h"
  26. #include "2d/CCTMXObjectGroup.h"
  27. NS_CC_BEGIN
  28. class TMXLayerInfo;
  29. class TMXTilesetInfo;
  30. class TMXMapInfo;
  31. namespace experimental {
  32. class TMXLayer;
  33. /**
  34. * @addtogroup _2d
  35. * @{
  36. */
  37. /** @brief FastTMXTiledMap knows how to parse and render a TMX map.
  38. * It adds support for the TMX tiled map format used by http://www.mapeditor.org.
  39. * It supports isometric, hexagonal and orthogonal tiles.
  40. * It also supports object groups, objects, and properties.
  41. * Features:
  42. * - Each tile will be treated as an Sprite.
  43. * - The sprites are created on demand. They will be created only when you call "layer->tileAt(position)".
  44. * - Each tile can be rotated / moved / scaled / tinted / "opaqued", since each tile is a Sprite.
  45. * - Tiles can be added/removed in runtime.
  46. * - The z-order of the tiles can be modified in runtime.
  47. * - Each tile has an anchorPoint of (0,0).
  48. * - The anchorPoint of the TMXTileMap is (0,0).
  49. * - The TMX layers will be added as a child.
  50. * - The TMX layers will be aliased by default.
  51. * - The tileset image will be loaded using the TextureCache.
  52. * - Each tile will have a unique tag.
  53. * - Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z.
  54. * - Each object group will be treated as an MutableArray.
  55. * - Object class which will contain all the properties in a dictionary.
  56. * - Properties can be assigned to the Map, Layer, Object Group, and Object.
  57. * Limitations:
  58. * - It only supports one tileset per layer.
  59. * - Embedded images are not supported.
  60. * - It only supports the XML format (the JSON format is not supported).
  61. * Technical description:
  62. * Each layer is created using an FastTMXLayer (subclass of SpriteBatchNode). If you have 5 layers, then 5 FastTMXLayer will be created,
  63. * unless the layer visibility is off. In that case, the layer won't be created at all.
  64. * You can obtain the layers (FastTMXLayer objects) at runtime by:
  65. * - map->getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...
  66. * - map->getLayer(name_of_the_layer);
  67. * Each object group is created using a TMXObjectGroup which is a subclass of MutableArray.
  68. * You can obtain the object groups at runtime by:
  69. * - map->getObjectGroup(name_of_the_object_group);
  70. * Each object is a TMXObject.
  71. * Each property is stored as a key-value pair in an MutableDictionary.
  72. * You can obtain the properties at runtime by:
  73. * map->getProperty(name_of_the_property);
  74. * layer->getProperty(name_of_the_property);
  75. * objectGroup->getProperty(name_of_the_property);
  76. * object->getProperty(name_of_the_property);
  77. * @since v3.2
  78. * @js NA
  79. */
  80. class CC_DLL TMXTiledMap : public Node
  81. {
  82. public:
  83. /** Creates a TMX Tiled Map with a TMX file.
  84. *
  85. * @return An autorelease object.
  86. */
  87. static TMXTiledMap* create(const std::string& tmxFile);
  88. /** Initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources.
  89. *
  90. * @param tmxString A TMX formatted XML string.
  91. * @param resourcePath A path to TMX resources.
  92. * @return An autorelease object.
  93. */
  94. static TMXTiledMap* createWithXML(const std::string& tmxString, const std::string& resourcePath);
  95. /** Return the FastTMXLayer for the specific layer.
  96. *
  97. * @return Return the FastTMXLayer for the specific layer.
  98. */
  99. TMXLayer* getLayer(const std::string& layerName) const;
  100. /** Return the TMXObjectGroup for the specific group.
  101. *
  102. * @return Return the TMXObjectGroup for the specific group.
  103. */
  104. TMXObjectGroup* getObjectGroup(const std::string& groupName) const;
  105. /** Return the value for the specific property name.
  106. *
  107. * @return Return the value for the specific property name.
  108. */
  109. Value getProperty(const std::string& propertyName) const;
  110. /** Return properties dictionary for tile GID.
  111. *
  112. * @return Return properties dictionary for tile GID.
  113. */
  114. Value getPropertiesForGID(int GID) const;
  115. /** The map's size property measured in tiles.
  116. *
  117. * @return The map's size property measured in tiles.
  118. */
  119. const Size& getMapSize() const { return _mapSize; }
  120. /** Set the map's size property measured in tiles.
  121. *
  122. * @param mapSize The map's size property measured in tiles.
  123. */
  124. void setMapSize(const Size& mapSize) { _mapSize = mapSize; }
  125. /** The tiles's size property measured in pixels.
  126. *
  127. * @return The tiles's size property measured in pixels.
  128. */
  129. const Size& getTileSize() const { return _tileSize; }
  130. /** Set the tiles's size property measured in pixels.
  131. *
  132. * @param tileSize The tiles's size property measured in pixels.
  133. */
  134. void setTileSize(const Size& tileSize) { _tileSize = tileSize; }
  135. /** Get map orientation.
  136. *
  137. * @return The map orientation.
  138. */
  139. int getMapOrientation() const { return _mapOrientation; }
  140. /** Set map orientation.
  141. *
  142. * @param mapOrientation The map orientation.
  143. */
  144. void setMapOrientation(int mapOrientation) { _mapOrientation = mapOrientation; }
  145. /** Get object groups.
  146. *
  147. * @return Object groups.
  148. */
  149. const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; }
  150. Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; }
  151. /** Set object groups.
  152. *
  153. * @param groups An object groups.
  154. */
  155. void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
  156. _objectGroups = groups;
  157. }
  158. /** Get properties.
  159. *
  160. * @return Properties.
  161. */
  162. const ValueMap& getProperties() const { return _properties; }
  163. /** Set properties.
  164. *
  165. * @param properties An ValueMap Properties.
  166. */
  167. void setProperties(const ValueMap& properties) {
  168. _properties = properties;
  169. }
  170. virtual std::string getDescription() const override;
  171. protected:
  172. /**
  173. * @js ctor
  174. */
  175. TMXTiledMap();
  176. /**
  177. * @js NA
  178. * @lua NA
  179. */
  180. virtual ~TMXTiledMap();
  181. /** initializes a TMX Tiled Map with a TMX file */
  182. bool initWithTMXFile(const std::string& tmxFile);
  183. /** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
  184. bool initWithXML(const std::string& tmxString, const std::string& resourcePath);
  185. TMXLayer * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
  186. TMXTilesetInfo * tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
  187. void buildWithMapInfo(TMXMapInfo* mapInfo);
  188. /** the map's size property measured in tiles */
  189. Size _mapSize;
  190. /** the tiles's size property measured in pixels */
  191. Size _tileSize;
  192. /** map orientation */
  193. int _mapOrientation;
  194. /** object groups */
  195. Vector<TMXObjectGroup*> _objectGroups;
  196. /** properties */
  197. ValueMap _properties;
  198. //! tile properties
  199. ValueMapIntKey _tileProperties;
  200. private:
  201. CC_DISALLOW_COPY_AND_ASSIGN(TMXTiledMap);
  202. };
  203. // end of tilemap_parallax_nodes group
  204. /** @} */
  205. } //end of namespace experimental
  206. NS_CC_END
  207. #endif //__CCTMX_TILE_MAP2_H__