TileMapDefs2D.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "../Container/RefCounted.h"
  24. #include "../Urho2D/Sprite2D.h"
  25. namespace Urho3D
  26. {
  27. class XMLElement;
  28. /// Orientation.
  29. enum Orientation2D
  30. {
  31. /// Orthogonal.
  32. O_ORTHOGONAL = 0,
  33. /// Isometric.
  34. O_ISOMETRIC,
  35. /// Staggered.
  36. O_STAGGERED,
  37. /// Hexagonal
  38. O_HEXAGONAL
  39. };
  40. /// Tile map information.
  41. struct URHO3D_API TileMapInfo2D
  42. {
  43. /// Orientation.
  44. Orientation2D orientation_;
  45. /// Width.
  46. int width_;
  47. /// Height.
  48. int height_;
  49. /// Tile width.
  50. float tileWidth_;
  51. /// Tile height.
  52. float tileHeight_;
  53. /// Return map width.
  54. float GetMapWidth() const;
  55. /// return map height.
  56. float GetMapHeight() const;
  57. /// Convert tmx position to Urho position.
  58. Vector2 ConvertPosition(const Vector2& position) const;
  59. /// Convert tile index to position.
  60. Vector2 TileIndexToPosition(int x, int y) const;
  61. /// Convert position to tile index, if out of map return false.
  62. bool PositionToTileIndex(int& x, int& y, const Vector2& position) const;
  63. };
  64. /// Tile map layer type.
  65. enum TileMapLayerType2D
  66. {
  67. /// Tile layer.
  68. LT_TILE_LAYER = 0,
  69. /// Object group.
  70. LT_OBJECT_GROUP,
  71. /// Image layer.
  72. LT_IMAGE_LAYER,
  73. /// Invalid.
  74. LT_INVALID = 0xffff
  75. };
  76. /// Tile map object type.
  77. enum TileMapObjectType2D
  78. {
  79. /// Rectangle.
  80. OT_RECTANGLE = 0,
  81. /// Ellipse.
  82. OT_ELLIPSE,
  83. /// Polygon.
  84. OT_POLYGON,
  85. /// Polyline.
  86. OT_POLYLINE,
  87. /// Tile.
  88. OT_TILE,
  89. /// Invalid.
  90. OT_INVALID = 0xffff
  91. };
  92. /// Property set.
  93. class URHO3D_API PropertySet2D : public RefCounted
  94. {
  95. public:
  96. PropertySet2D();
  97. virtual ~PropertySet2D();
  98. /// Load from XML element.
  99. void Load(const XMLElement& element);
  100. /// Return has property.
  101. bool HasProperty(const String& name) const;
  102. /// Return property value.
  103. const String& GetProperty(const String& name) const;
  104. protected:
  105. /// Property name to property value mapping.
  106. HashMap<String, String> nameToValueMapping_;
  107. };
  108. /// Tile define.
  109. class URHO3D_API Tile2D : public RefCounted
  110. {
  111. public:
  112. /// Construct.
  113. Tile2D();
  114. /// Return gid.
  115. int GetGid() const { return gid_; }
  116. /// Return sprite.
  117. Sprite2D* GetSprite() const;
  118. /// Return has property.
  119. bool HasProperty(const String& name) const;
  120. /// Return property.
  121. const String& GetProperty(const String& name) const;
  122. private:
  123. friend class TmxTileLayer2D;
  124. /// Gid.
  125. int gid_;
  126. /// Sprite.
  127. SharedPtr<Sprite2D> sprite_;
  128. /// Property set.
  129. SharedPtr<PropertySet2D> propertySet_;
  130. };
  131. /// Tile map object.
  132. class URHO3D_API TileMapObject2D : public RefCounted
  133. {
  134. public:
  135. TileMapObject2D();
  136. /// Return type.
  137. TileMapObjectType2D GetObjectType() const { return objectType_; }
  138. /// Return name.
  139. const String& GetName() const { return name_; }
  140. /// Return type.
  141. const String& GetType() const { return type_; }
  142. /// Return position.
  143. const Vector2& GetPosition() const { return position_; }
  144. /// Return size (for rectangle and ellipse).
  145. const Vector2& GetSize() const { return size_; }
  146. /// Return number of points (use for script).
  147. unsigned GetNumPoints() const;
  148. /// Return point at index (use for script).
  149. const Vector2& GetPoint(unsigned index) const;
  150. /// Return tile Gid.
  151. int GetTileGid() const { return gid_; }
  152. /// Return tile sprite.
  153. Sprite2D* GetTileSprite() const;
  154. /// Return has property.
  155. bool HasProperty(const String& name) const;
  156. /// Return property value.
  157. const String& GetProperty(const String& name) const;
  158. private:
  159. friend class TmxObjectGroup2D;
  160. /// Object type.
  161. TileMapObjectType2D objectType_;
  162. /// Name.
  163. String name_;
  164. /// Type.
  165. String type_;
  166. /// Position.
  167. Vector2 position_;
  168. /// Size (for rectangle and ellipse).
  169. Vector2 size_;
  170. /// Points(for polygon and polyline).
  171. Vector<Vector2> points_;
  172. /// Gid (for tile).
  173. int gid_;
  174. /// Sprite (for tile).
  175. SharedPtr<Sprite2D> sprite_;
  176. /// Property set.
  177. SharedPtr<PropertySet2D> propertySet_;
  178. };
  179. }