TmxFile2D.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "Resource.h"
  24. #include "TileMapDefs2D.h"
  25. namespace Urho3D
  26. {
  27. class Sprite2D;
  28. class Texture2D;
  29. class TmxFile2D;
  30. class XMLElement;
  31. class XMLFile;
  32. /// Tmx layer.
  33. class TmxLayer2D : public RefCounted
  34. {
  35. public:
  36. TmxLayer2D(TmxFile2D* tmxFile, TileMapLayerType2D type);
  37. virtual ~TmxLayer2D();
  38. /// Return tmx file.
  39. TmxFile2D* GetTmxFile() const;
  40. /// Return type.
  41. TileMapLayerType2D GetType() const { return type_; }
  42. /// Return name.
  43. const String& GetName() const { return name_; }
  44. /// Return width.
  45. int GetWidth() const { return width_; }
  46. /// Return height.
  47. int GetHeight() const { return height_; }
  48. /// Return is visible.
  49. bool IsVisible() const { return visible_; }
  50. /// Return has property (use for script).
  51. bool HasProperty(const String& name) const;
  52. /// Return property value (use for script).
  53. const String& GetProperty(const String& name) const;
  54. protected:
  55. /// Load layer info.
  56. void LoadInfo(const XMLElement& element);
  57. /// Load property set.
  58. void LoadPropertySet(const XMLElement& element);
  59. /// Tmx file.
  60. WeakPtr<TmxFile2D> tmxFile_;
  61. /// Layer type.
  62. TileMapLayerType2D type_;
  63. /// Name.
  64. String name_;
  65. /// Width.
  66. int width_;
  67. /// Height.
  68. int height_;
  69. /// Visible.
  70. bool visible_;
  71. /// Property set.
  72. SharedPtr<PropertySet2D> propertySet_;
  73. };
  74. /// Tmx tile layer.
  75. class TmxTileLayer2D : public TmxLayer2D
  76. {
  77. public:
  78. TmxTileLayer2D(TmxFile2D* tmxFile);
  79. /// Load from XML element.
  80. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  81. /// Return tile.
  82. Tile2D* GetTile(int x, int y) const;
  83. protected:
  84. /// Tile.
  85. Vector<SharedPtr<Tile2D> > tiles_;
  86. };
  87. /// Tmx image layer.
  88. class TmxObjectGroup2D : public TmxLayer2D
  89. {
  90. public:
  91. TmxObjectGroup2D(TmxFile2D* tmxFile);
  92. /// Load from XML element.
  93. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  94. /// Return number of objects.
  95. unsigned GetNumObjects() const { return objects_.Size(); }
  96. /// Return tile map object at index.
  97. TileMapObject2D* GetObject(unsigned index) const;
  98. private:
  99. /// Objects.
  100. Vector<SharedPtr<TileMapObject2D> > objects_;
  101. };
  102. /// Tmx image layer.
  103. class TmxImageLayer2D : public TmxLayer2D
  104. {
  105. public:
  106. TmxImageLayer2D(TmxFile2D* tmxFile);
  107. /// Load from XML element.
  108. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  109. /// Return position.
  110. const Vector2& GetPosition() const { return position_; }
  111. /// Return source.
  112. const String& GetSource() const { return source_; }
  113. /// Return sprite.
  114. Sprite2D* GetSprite() const;
  115. private:
  116. /// Position.
  117. Vector2 position_;
  118. /// Source.
  119. String source_;
  120. /// Sprite.
  121. SharedPtr<Sprite2D> sprite_;
  122. };
  123. /// Tile map file.
  124. class URHO3D_API TmxFile2D : public Resource
  125. {
  126. OBJECT(TmxFile2D);
  127. public:
  128. /// Construct.
  129. TmxFile2D(Context* context);
  130. /// Destruct.
  131. virtual ~TmxFile2D();
  132. /// Register object factory.
  133. static void RegisterObject(Context* context);
  134. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  135. virtual bool BeginLoad(Deserializer& source);
  136. /// Finish resource loading. Always called from the main thread. Return true if successful.
  137. virtual bool EndLoad();
  138. /// Return information.
  139. const TileMapInfo2D& GetInfo() const { return info_; }
  140. /// Return tile sprite by gid, if not exist return 0.
  141. Sprite2D* GetTileSprite(int gid) const;
  142. /// Return tile property set by gid, if not exist return 0.
  143. PropertySet2D* GetTilePropertySet(int gid) const;
  144. /// Return number of layers.
  145. unsigned GetNumLayers() const { return layers_.Size(); }
  146. /// Return layer at index.
  147. const TmxLayer2D* GetLayer(unsigned index) const;
  148. private:
  149. /// Load TSX file.
  150. SharedPtr<XMLFile> LoadTSXFile(const String& source);
  151. /// Load tile set.
  152. bool LoadTileSet(const XMLElement& element);
  153. /// XML file used during loading.
  154. SharedPtr<XMLFile> loadXMLFile_;
  155. /// TSX name to XML file mapping.
  156. HashMap<String, SharedPtr<XMLFile> > tsxXMLFiles_;
  157. /// Tile map information.
  158. TileMapInfo2D info_;
  159. /// Tile set textures.
  160. Vector<SharedPtr<Texture2D> > tileSetTextures_;
  161. /// Gid to tile sprite mapping.
  162. HashMap<int, SharedPtr<Sprite2D> > gidToSpriteMapping_;
  163. /// Gid to tile property set mapping.
  164. HashMap<int, SharedPtr<PropertySet2D> > gidToPropertySetMapping_;
  165. /// Layers.
  166. Vector<TmxLayer2D*> layers_;
  167. };
  168. }