CCTMXLayer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /****************************************************************************
  2. Copyright (c) 2008-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_LAYER_H__
  25. #define __CCTMX_LAYER_H__
  26. #include "2d/CCSpriteBatchNode.h"
  27. #include "2d/CCTMXXMLParser.h"
  28. #include "base/ccCArray.h"
  29. NS_CC_BEGIN
  30. class TMXMapInfo;
  31. class TMXLayerInfo;
  32. class TMXTilesetInfo;
  33. struct _ccCArray;
  34. /**
  35. * @addtogroup _2d
  36. * @{
  37. */
  38. /** @brief TMXLayer represents the TMX layer.
  39. * It is a subclass of SpriteBatchNode. By default the tiles are rendered using a TextureAtlas.
  40. * If you modify a tile on runtime, then, that tile will become a Sprite, otherwise no Sprite objects are created.
  41. * The benefits of using Sprite objects as tiles are:
  42. * - tiles (Sprite) can be rotated/scaled/moved with a nice API.
  43. * If the layer contains a property named "cc_vertexz" with an integer (in can be positive or negative),
  44. * then all the tiles belonging to the layer will use that value as their OpenGL vertex Z for depth.
  45. * On the other hand, if the "cc_vertexz" property has the "automatic" value, then the tiles will use an automatic vertex Z value.
  46. * Also before drawing the tiles, GL_ALPHA_TEST will be enabled, and disabled after drawing them. The used alpha func will be:
  47. * glAlphaFunc( GL_GREATER, value ).
  48. * "value" by default is 0, but you can change it from Tiled by adding the "cc_alpha_func" property to the layer.
  49. * The value 0 should work for most cases, but if you have tiles that are semi-transparent, then you might want to use a different
  50. * value, like 0.5.
  51. * For further information, please see the programming guide:
  52. * http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps
  53. * @since v0.8.1
  54. * Tiles can have tile flags for additional properties. At the moment only flip horizontal and flip vertical are used. These bit flags are defined in TMXXMLParser.h.
  55. * @since 1.1
  56. */
  57. class CC_DLL TMXLayer : public SpriteBatchNode
  58. {
  59. public:
  60. /** Creates a TMXLayer with an tileset info, a layer info and a map info.
  61. *
  62. * @param tilesetInfo An tileset info.
  63. * @param layerInfo A layer info.
  64. * @param mapInfo A map info.
  65. * @return An autorelease object.
  66. */
  67. static TMXLayer * create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
  68. /**
  69. * @js ctor
  70. */
  71. TMXLayer();
  72. /**
  73. * @js NA
  74. * @lua NA
  75. */
  76. virtual ~TMXLayer();
  77. /** Initializes a TMXLayer with a tileset info, a layer info and a map info.
  78. *
  79. * @param tilesetInfo An tileset info.
  80. * @param layerInfo A layer info.
  81. * @param mapInfo A map info.
  82. * @return If initializes successfully, it will return true.
  83. */
  84. bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
  85. /** Dealloc the map that contains the tile position from memory.
  86. * Unless you want to know at runtime the tiles positions, you can safely call this method.
  87. * If you are going to call layer->tileGIDAt() then, don't release the map.
  88. */
  89. void releaseMap();
  90. /** Returns the tile (Sprite) at a given a tile coordinate.
  91. * The returned Sprite will be already added to the TMXLayer. Don't add it again.
  92. * The Sprite can be treated like any other Sprite: rotated, scaled, translated, opacity, color, etc.
  93. * You can remove either by calling:
  94. * - layer->removeChild(sprite, cleanup);
  95. * - or layer->removeTileAt(Vec2(x,y));
  96. *
  97. * @param tileCoordinate A tile coordinate.
  98. * @return Returns the tile (Sprite) at a given a tile coordinate.
  99. */
  100. Sprite* getTileAt(const Vec2& tileCoordinate);
  101. /**
  102. * @js NA
  103. */
  104. CC_DEPRECATED_ATTRIBUTE Sprite* tileAt(const Vec2& tileCoordinate) { return getTileAt(tileCoordinate); };
  105. /** Returns the tile gid at a given tile coordinate. It also returns the tile flags.
  106. * This method requires the tile map has not been previously released (eg. don't call [layer releaseMap]).
  107. *
  108. * @param tileCoordinate The tile coordinate.
  109. * @param flags Tile flags.
  110. * @return Returns the tile gid at a given tile coordinate. It also returns the tile flags.
  111. */
  112. uint32_t getTileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags = nullptr);
  113. /**
  114. * @js NA
  115. */
  116. CC_DEPRECATED_ATTRIBUTE uint32_t tileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags = nullptr){
  117. return getTileGIDAt(tileCoordinate, flags);
  118. }
  119. /** Sets the tile gid (gid = tile global id) at a given tile coordinate.
  120. * The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
  121. * If a tile is already placed at that position, then it will be removed.
  122. *
  123. * @param gid The tile gid.
  124. * @param tileCoordinate The tile coordinate.
  125. */
  126. void setTileGID(uint32_t gid, const Vec2& tileCoordinate);
  127. /** Sets the tile gid (gid = tile global id) at a given tile coordinate.
  128. * The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
  129. * If a tile is already placed at that position, then it will be removed.
  130. * Use withFlags if the tile flags need to be changed as well.
  131. *
  132. * @param gid The tile gid.
  133. * @param tileCoordinate The tile coordinate.
  134. * @param flags The tile flags.
  135. */
  136. void setTileGID(uint32_t gid, const Vec2& tileCoordinate, TMXTileFlags flags);
  137. /** Removes a tile at given tile coordinate.
  138. *
  139. * @param tileCoordinate The tile coordinate.
  140. */
  141. void removeTileAt(const Vec2& tileCoordinate);
  142. /** Returns the position in points of a given tile coordinate.
  143. *
  144. * @param tileCoordinate The tile coordinate.
  145. * @return The position in points of a given tile coordinate.
  146. */
  147. Vec2 getPositionAt(const Vec2& tileCoordinate);
  148. /**
  149. * @js NA
  150. */
  151. CC_DEPRECATED_ATTRIBUTE Vec2 positionAt(const Vec2& tileCoordinate) { return getPositionAt(tileCoordinate); };
  152. /** Return the value for the specific property name.
  153. *
  154. * @param propertyName The specific property name.
  155. * @return Return the value for the specific property name.
  156. */
  157. Value getProperty(const std::string& propertyName) const;
  158. /**
  159. * @js NA
  160. */
  161. CC_DEPRECATED_ATTRIBUTE Value propertyNamed(const std::string& propertyName) const { return getProperty(propertyName); };
  162. /** Creates the tiles. */
  163. void setupTiles();
  164. /** Get the layer name.
  165. *
  166. * @return The layer name.
  167. */
  168. const std::string& getLayerName() { return _layerName; }
  169. /** Set the layer name.
  170. *
  171. * @param layerName The layer name.
  172. */
  173. void setLayerName(const std::string& layerName) { _layerName = layerName; }
  174. /** Size of the layer in tiles.
  175. *
  176. * @return Size of the layer in tiles.
  177. */
  178. const Size& getLayerSize() const { return _layerSize; }
  179. /** Set size of the layer in tiles.
  180. *
  181. * @param size Size of the layer in tiles.
  182. */
  183. void setLayerSize(const Size& size) { _layerSize = size; }
  184. /** Size of the map's tile (could be different from the tile's size).
  185. *
  186. * @return The size of the map's tile.
  187. */
  188. const Size& getMapTileSize() const { return _mapTileSize; }
  189. /** Set the size of the map's tile.
  190. *
  191. * @param size The size of the map's tile.
  192. */
  193. void setMapTileSize(const Size& size) { _mapTileSize = size; }
  194. /** Pointer to the map of tiles.
  195. * @js NA
  196. * @lua NA
  197. * @return Pointer to the map of tiles.
  198. */
  199. uint32_t* getTiles() const { return _tiles; };
  200. /** Set a pointer to the map of tiles.
  201. *
  202. * @param tiles A pointer to the map of tiles.
  203. */
  204. void setTiles(uint32_t* tiles) { _tiles = tiles; };
  205. /** Tileset information for the layer.
  206. *
  207. * @return Tileset information for the layer.
  208. */
  209. TMXTilesetInfo* getTileSet() const { return _tileSet; }
  210. /** Set tileset information for the layer.
  211. *
  212. * @param info The tileset information for the layer.
  213. * @js NA
  214. */
  215. void setTileSet(TMXTilesetInfo* info) {
  216. CC_SAFE_RETAIN(info);
  217. CC_SAFE_RELEASE(_tileSet);
  218. _tileSet = info;
  219. }
  220. /** Layer orientation, which is the same as the map orientation.
  221. *
  222. * @return Layer orientation, which is the same as the map orientation.
  223. */
  224. int getLayerOrientation() const { return _layerOrientation; }
  225. /** Set layer orientation, which is the same as the map orientation.
  226. *
  227. * @param orientation Layer orientation,which is the same as the map orientation.
  228. */
  229. void setLayerOrientation(int orientation) { _layerOrientation = orientation; }
  230. /** Properties from the layer. They can be added using Tiled.
  231. *
  232. * @return Properties from the layer. They can be added using Tiled.
  233. */
  234. const ValueMap& getProperties() const { return _properties; }
  235. /** Properties from the layer. They can be added using Tiled.
  236. *
  237. * @return Properties from the layer. They can be added using Tiled.
  238. */
  239. ValueMap& getProperties() { return _properties; }
  240. /** Set an Properties from to layer.
  241. *
  242. * @param properties It is used to set the layer Properties.
  243. */
  244. void setProperties(const ValueMap& properties) {
  245. _properties = properties;
  246. }
  247. //
  248. // Override
  249. //
  250. /** TMXLayer doesn't support adding a Sprite manually.
  251. @warning addChild(z, tag); is not supported on TMXLayer. Instead of setTileGID.
  252. */
  253. using SpriteBatchNode::addChild;
  254. virtual void addChild(Node * child, int zOrder, int tag) override;
  255. // super method
  256. void removeChild(Node* child, bool cleanup) override;
  257. /**
  258. * @js NA
  259. */
  260. virtual std::string getDescription() const override;
  261. protected:
  262. Vec2 getPositionForIsoAt(const Vec2& pos);
  263. Vec2 getPositionForOrthoAt(const Vec2& pos);
  264. Vec2 getPositionForHexAt(const Vec2& pos);
  265. Vec2 getPositionForStaggeredAt(const Vec2& pos);
  266. Vec2 calculateLayerOffset(const Vec2& offset);
  267. /* optimization methods */
  268. Sprite* appendTileForGID(uint32_t gid, const Vec2& pos);
  269. Sprite* insertTileForGID(uint32_t gid, const Vec2& pos);
  270. Sprite* updateTileForGID(uint32_t gid, const Vec2& pos);
  271. intptr_t getZForPos(const Vec2& pos) const;
  272. /* The layer recognizes some special properties, like cc_vertexz */
  273. void parseInternalProperties();
  274. void setupTileSprite(Sprite* sprite, const Vec2& pos, uint32_t gid);
  275. Sprite* reusedTileWithRect(const Rect& rect);
  276. int getVertexZForPos(const Vec2& pos);
  277. // index
  278. ssize_t atlasIndexForExistantZ(int z);
  279. ssize_t atlasIndexForNewZ(int z);
  280. //! name of the layer
  281. std::string _layerName;
  282. //! TMX Layer supports opacity
  283. unsigned char _opacity;
  284. //! Only used when vertexZ is used
  285. int _vertexZvalue;
  286. bool _useAutomaticVertexZ;
  287. //! used for optimization
  288. Sprite *_reusedTile;
  289. ccCArray *_atlasIndexArray;
  290. // used for retina display
  291. float _contentScaleFactor;
  292. /** size of the layer in tiles */
  293. Size _layerSize;
  294. /** size of the map's tile (could be different from the tile's size) */
  295. Size _mapTileSize;
  296. /** pointer to the map of tiles */
  297. uint32_t* _tiles;
  298. /** Tileset information for the layer */
  299. TMXTilesetInfo* _tileSet;
  300. /** Layer orientation, which is the same as the map orientation */
  301. int _layerOrientation;
  302. /** Stagger Axis */
  303. int _staggerAxis;
  304. /** Stagger Index */
  305. int _staggerIndex;
  306. /** Hex side length*/
  307. int _hexSideLength;
  308. /** properties from the layer. They can be added using Tiled */
  309. ValueMap _properties;
  310. };
  311. // end of tilemap_parallax_nodes group
  312. /** @} */
  313. NS_CC_END
  314. #endif //__CCTMX_LAYER_H__