TileMapDefs2D.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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. /// \file
  23. #pragma once
  24. #include "../Container/RefCounted.h"
  25. #include "../Urho2D/Sprite2D.h"
  26. namespace Urho3D
  27. {
  28. class XMLElement;
  29. /// Orientation.
  30. enum Orientation2D
  31. {
  32. /// Orthogonal.
  33. O_ORTHOGONAL = 0,
  34. /// Isometric.
  35. O_ISOMETRIC,
  36. /// Staggered.
  37. O_STAGGERED,
  38. /// Hexagonal.
  39. O_HEXAGONAL
  40. };
  41. /// Tile map information.
  42. struct URHO3D_API TileMapInfo2D
  43. {
  44. /// Orientation.
  45. Orientation2D orientation_;
  46. /// Width.
  47. int width_;
  48. /// Height.
  49. int height_;
  50. /// Tile width.
  51. float tileWidth_;
  52. /// Tile height.
  53. float tileHeight_;
  54. /// Return map width.
  55. float GetMapWidth() const;
  56. /// return map height.
  57. float GetMapHeight() const;
  58. /// Convert tmx position to Urho position.
  59. Vector2 ConvertPosition(const Vector2& position) const;
  60. /// Convert tile index to position.
  61. Vector2 TileIndexToPosition(int x, int y) const;
  62. /// Convert position to tile index, if out of map return false.
  63. bool PositionToTileIndex(int& x, int& y, const Vector2& position) const;
  64. };
  65. /// Tile map layer type.
  66. enum TileMapLayerType2D
  67. {
  68. /// Tile layer.
  69. LT_TILE_LAYER = 0,
  70. /// Object group.
  71. LT_OBJECT_GROUP,
  72. /// Image layer.
  73. LT_IMAGE_LAYER,
  74. /// Invalid.
  75. LT_INVALID = 0xffff
  76. };
  77. /// Tile map object type.
  78. enum TileMapObjectType2D
  79. {
  80. /// Rectangle.
  81. OT_RECTANGLE = 0,
  82. /// Ellipse.
  83. OT_ELLIPSE,
  84. /// Polygon.
  85. OT_POLYGON,
  86. /// Polyline.
  87. OT_POLYLINE,
  88. /// Tile.
  89. OT_TILE,
  90. /// Invalid.
  91. OT_INVALID = 0xffff
  92. };
  93. /// Property set.
  94. class URHO3D_API PropertySet2D : public RefCounted
  95. {
  96. public:
  97. PropertySet2D();
  98. ~PropertySet2D() override;
  99. /// Load from XML element.
  100. void Load(const XMLElement& element);
  101. /// Return has property.
  102. bool HasProperty(const String& name) const;
  103. /// Return property value.
  104. const String& GetProperty(const String& name) const;
  105. protected:
  106. /// Property name to property value mapping.
  107. HashMap<String, String> nameToValueMapping_;
  108. };
  109. /// Tile flipping flags.
  110. static const unsigned FLIP_HORIZONTAL = 0x80000000u;
  111. static const unsigned FLIP_VERTICAL = 0x40000000u;
  112. static const unsigned FLIP_DIAGONAL = 0x20000000u;
  113. static const unsigned FLIP_RESERVED = 0x10000000u;
  114. static const unsigned FLIP_ALL = FLIP_HORIZONTAL | FLIP_VERTICAL | FLIP_DIAGONAL | FLIP_RESERVED;
  115. /// Tile define.
  116. class URHO3D_API Tile2D : public RefCounted
  117. {
  118. public:
  119. /// Construct.
  120. Tile2D();
  121. /// Return gid.
  122. unsigned GetGid() const { return gid_ & ~FLIP_ALL; }
  123. /// Return flip X.
  124. bool GetFlipX() const { return gid_ & FLIP_HORIZONTAL; }
  125. /// Return flip Y.
  126. bool GetFlipY() const { return gid_ & FLIP_VERTICAL; }
  127. /// Return swap X and Y.
  128. bool GetSwapXY() const { return gid_ & FLIP_DIAGONAL; }
  129. /// Return sprite.
  130. Sprite2D* GetSprite() const;
  131. /// Return has property.
  132. bool HasProperty(const String& name) const;
  133. /// Return property.
  134. const String& GetProperty(const String& name) const;
  135. private:
  136. friend class TmxTileLayer2D;
  137. /// Gid.
  138. unsigned gid_;
  139. /// Sprite.
  140. SharedPtr<Sprite2D> sprite_;
  141. /// Property set.
  142. SharedPtr<PropertySet2D> propertySet_;
  143. };
  144. /// Tile map object.
  145. class URHO3D_API TileMapObject2D : public RefCounted
  146. {
  147. public:
  148. TileMapObject2D();
  149. /// Return type.
  150. TileMapObjectType2D GetObjectType() const { return objectType_; }
  151. /// Return name.
  152. const String& GetName() const { return name_; }
  153. /// Return type.
  154. const String& GetType() const { return type_; }
  155. /// Return position.
  156. const Vector2& GetPosition() const { return position_; }
  157. /// Return size (for rectangle and ellipse).
  158. const Vector2& GetSize() const { return size_; }
  159. /// Return number of points (use for script).
  160. unsigned GetNumPoints() const;
  161. /// Return point at index (use for script).
  162. const Vector2& GetPoint(unsigned index) const;
  163. /// Return tile Gid.
  164. unsigned GetTileGid() const { return gid_ & ~FLIP_ALL; }
  165. /// Return tile flip X.
  166. bool GetTileFlipX() const { return gid_ & FLIP_HORIZONTAL; }
  167. /// Return tile flip Y.
  168. bool GetTileFlipY() const { return gid_ & FLIP_VERTICAL; }
  169. /// Return tile swap X and Y.
  170. bool GetTileSwapXY() const { return gid_ & FLIP_DIAGONAL; }
  171. /// Return tile sprite.
  172. Sprite2D* GetTileSprite() const;
  173. /// Return has property.
  174. bool HasProperty(const String& name) const;
  175. /// Return property value.
  176. const String& GetProperty(const String& name) const;
  177. private:
  178. friend class TmxObjectGroup2D;
  179. /// Object type.
  180. TileMapObjectType2D objectType_{};
  181. /// Name.
  182. String name_;
  183. /// Type.
  184. String type_;
  185. /// Position.
  186. Vector2 position_;
  187. /// Size (for rectangle and ellipse).
  188. Vector2 size_;
  189. /// Points(for polygon and polyline).
  190. Vector<Vector2> points_;
  191. /// Gid (for tile).
  192. unsigned gid_{};
  193. /// Sprite (for tile).
  194. SharedPtr<Sprite2D> sprite_;
  195. /// Property set.
  196. SharedPtr<PropertySet2D> propertySet_;
  197. };
  198. }