TileMapDefs2D.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Copyright (c) 2008-2014 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 "../Atomic2D/Sprite2D.h"
  25. namespace Atomic
  26. {
  27. class XMLElement;
  28. class TmxObjectGroup2D;
  29. class CollisionShape2D;
  30. class Node;
  31. /// Orientation.
  32. enum Orientation2D
  33. {
  34. /// Orthogonal.
  35. O_ORTHOGONAL = 0,
  36. /// Isometric.
  37. O_ISOMETRIC,
  38. /// Staggered.
  39. O_STAGGERED
  40. };
  41. /// Tile map infomation.
  42. struct ATOMIC_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& positon) 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 ATOMIC_API PropertySet2D : public RefCounted
  95. {
  96. public:
  97. PropertySet2D();
  98. virtual ~PropertySet2D();
  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 define.
  110. class ATOMIC_API Tile2D : public RefCounted
  111. {
  112. public:
  113. /// Construct.
  114. Tile2D();
  115. /// Return gid.
  116. int GetGid() const { return gid_; }
  117. /// Return sprite.
  118. Sprite2D* GetSprite() const;
  119. /// Return Object Group.
  120. TmxObjectGroup2D* GetObjectGroup() const;
  121. /// Return has property.
  122. bool HasProperty(const String& name) const;
  123. /// Return property.
  124. const String& GetProperty(const String& name) const;
  125. private:
  126. friend class TmxTileLayer2D;
  127. /// Gid.
  128. int gid_;
  129. /// Sprite.
  130. SharedPtr<Sprite2D> sprite_;
  131. /// Property set.
  132. SharedPtr<PropertySet2D> propertySet_;
  133. /// Object group (collision)
  134. SharedPtr<TmxObjectGroup2D> objectGroup_;
  135. };
  136. /// Tile map object.
  137. class ATOMIC_API TileMapObject2D : public RefCounted
  138. {
  139. public:
  140. TileMapObject2D();
  141. /// Return type.
  142. TileMapObjectType2D GetObjectType() const { return objectType_; }
  143. /// Return name.
  144. const String& GetName() const { return name_; }
  145. /// Return type.
  146. const String& GetType() const { return type_; }
  147. /// Return position.
  148. const Vector2& GetPosition() const { return position_; }
  149. /// Return size (for rectangle and ellipse).
  150. const Vector2& GetSize() const { return size_; }
  151. /// Return number of points (use for script).
  152. unsigned GetNumPoints() const;
  153. /// Return point at index (use for script).
  154. const Vector2& GetPoint(unsigned index) const;
  155. /// Return tile Gid.
  156. int GetTileGid() const { return gid_; }
  157. /// Return tile sprite.
  158. Sprite2D* GetTileSprite() const;
  159. /// Return has property.
  160. bool HasProperty(const String& name) const;
  161. /// Return property value.
  162. const String& GetProperty(const String& name) const;
  163. bool ValidCollisionShape() const;
  164. CollisionShape2D* CreateCollisionShape(Node* node) const;
  165. private:
  166. friend class TmxObjectGroup2D;
  167. /// Object type.
  168. TileMapObjectType2D objectType_;
  169. /// Name.
  170. String name_;
  171. /// Type.
  172. String type_;
  173. /// Position.
  174. Vector2 position_;
  175. /// Size (for rectangle and ellipse).
  176. Vector2 size_;
  177. /// Points(for polygon and polyline).
  178. Vector<Vector2> points_;
  179. /// Gid (for tile).
  180. int gid_;
  181. /// Sprite (for tile).
  182. SharedPtr<Sprite2D> sprite_;
  183. /// Property set.
  184. SharedPtr<PropertySet2D> propertySet_;
  185. };
  186. }