TileMapDefs2D.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "../Precompiled.h"
  23. #include "../Resource/XMLElement.h"
  24. #include "../Resource/JSONFile.h"
  25. #include "../Urho2D/TileMapDefs2D.h"
  26. #include "../DebugNew.h"
  27. namespace Urho3D
  28. {
  29. extern URHO3D_API const float PIXEL_SIZE;
  30. float TileMapInfo2D::GetMapWidth() const
  31. {
  32. return width_ * tileWidth_;
  33. }
  34. float TileMapInfo2D::GetMapHeight() const
  35. {
  36. if (orientation_ == O_STAGGERED)
  37. return (height_ + 1) * 0.5f * tileHeight_;
  38. else if (orientation_ == O_HEXAGONAL)
  39. return (height_) * 0.5f * (tileHeight_ + tileHeight_ * 0.5f);
  40. else
  41. return height_ * tileHeight_;
  42. }
  43. Vector2 TileMapInfo2D::ConvertPosition(const Vector2& position) const
  44. {
  45. switch (orientation_)
  46. {
  47. case O_ISOMETRIC:
  48. {
  49. Vector2 index = position * PIXEL_SIZE / tileHeight_;
  50. return Vector2((width_ + index.x_ - index.y_) * tileWidth_ * 0.5f,
  51. (height_ * 2.0f - index.x_ - index.y_) * tileHeight_ * 0.5f);
  52. }
  53. case O_STAGGERED:
  54. return Vector2(position.x_ * PIXEL_SIZE, GetMapHeight() - position.y_ * PIXEL_SIZE);
  55. case O_HEXAGONAL:
  56. case O_ORTHOGONAL:
  57. default:
  58. return Vector2(position.x_ * PIXEL_SIZE, GetMapHeight() - position.y_ * PIXEL_SIZE);
  59. }
  60. }
  61. Vector2 TileMapInfo2D::TileIndexToPosition(int x, int y) const
  62. {
  63. switch (orientation_)
  64. {
  65. case O_ISOMETRIC:
  66. return Vector2((width_ + x - y - 1) * tileWidth_ * 0.5f, (height_ * 2 - x - y - 2) * tileHeight_ * 0.5f);
  67. case O_STAGGERED:
  68. if (y % 2 == 0)
  69. return Vector2(x * tileWidth_, (height_ - 1 - y) * 0.5f * tileHeight_);
  70. else
  71. return Vector2((x + 0.5f) * tileWidth_, (height_ - 1 - y) * 0.5f * tileHeight_);
  72. case O_HEXAGONAL:
  73. if (y % 2 == 0)
  74. return Vector2(x * tileWidth_, (height_ - 1 - y) * 0.75f * tileHeight_);
  75. else
  76. return Vector2((x + 0.5f) * tileWidth_, (height_ - 1 - y) * 0.75f * tileHeight_);
  77. case O_ORTHOGONAL:
  78. default:
  79. return Vector2(x * tileWidth_, (height_ - 1 - y) * tileHeight_);
  80. }
  81. }
  82. bool TileMapInfo2D::PositionToTileIndex(int& x, int& y, const Vector2& position) const
  83. {
  84. switch (orientation_)
  85. {
  86. case O_ISOMETRIC:
  87. {
  88. float ox = position.x_ / tileWidth_ - height_ * 0.5f;
  89. float oy = position.y_ / tileHeight_;
  90. x = (int)(width_ - oy + ox);
  91. y = (int)(height_ - oy - ox);
  92. }
  93. break;
  94. case O_STAGGERED:
  95. y = (int)(height_ - 1 - position.y_ * 2.0f / tileHeight_);
  96. if (y % 2 == 0)
  97. x = (int)(position.x_ / tileWidth_);
  98. else
  99. x = (int)(position.x_ / tileWidth_ - 0.5f);
  100. break;
  101. case O_HEXAGONAL:
  102. y = (int)(height_ - 1 - position.y_ / 0.75f / tileHeight_);
  103. if (y % 2 == 0)
  104. x = (int)(position.x_ / tileWidth_);
  105. else
  106. x = (int)(position.x_ / tileWidth_ - 0.75f);
  107. break;
  108. case O_ORTHOGONAL:
  109. default:
  110. x = (int)(position.x_ / tileWidth_);
  111. y = height_ - 1 - int(position.y_ / tileHeight_);
  112. break;
  113. }
  114. return x >= 0 && x < width_ && y >= 0 && y < height_;
  115. }
  116. PropertySet2D::PropertySet2D() = default;
  117. PropertySet2D::~PropertySet2D() = default;
  118. void PropertySet2D::Load(const XMLElement& element)
  119. {
  120. assert(element.GetName() == "properties");
  121. for (XMLElement propertyElem = element.GetChild("property"); propertyElem; propertyElem = propertyElem.GetNext("property"))
  122. nameToValueMapping_[propertyElem.GetAttribute("name")] = propertyElem.GetAttribute("value");
  123. }
  124. bool PropertySet2D::HasProperty(const String& name) const
  125. {
  126. return nameToValueMapping_.Find(name) != nameToValueMapping_.End();
  127. }
  128. const String& PropertySet2D::GetProperty(const String& name) const
  129. {
  130. HashMap<String, String>::ConstIterator i = nameToValueMapping_.Find(name);
  131. if (i == nameToValueMapping_.End())
  132. return String::EMPTY;
  133. return i->second_;
  134. }
  135. Tile2D::Tile2D() :
  136. gid_(0)
  137. {
  138. }
  139. Sprite2D* Tile2D::GetSprite() const
  140. {
  141. return sprite_;
  142. }
  143. bool Tile2D::HasProperty(const String& name) const
  144. {
  145. if (!propertySet_)
  146. return false;
  147. return propertySet_->HasProperty(name);
  148. }
  149. const String& Tile2D::GetProperty(const String& name) const
  150. {
  151. if (!propertySet_)
  152. return String::EMPTY;
  153. return propertySet_->GetProperty(name);
  154. }
  155. TileMapObject2D::TileMapObject2D() = default;
  156. unsigned TileMapObject2D::GetNumPoints() const
  157. {
  158. return points_.Size();
  159. }
  160. const Vector2& TileMapObject2D::GetPoint(unsigned index) const
  161. {
  162. if (index >= points_.Size())
  163. return Vector2::ZERO;
  164. return points_[index];
  165. }
  166. Sprite2D* TileMapObject2D::GetTileSprite() const
  167. {
  168. return sprite_;
  169. }
  170. bool TileMapObject2D::HasProperty(const String& name) const
  171. {
  172. if (!propertySet_)
  173. return false;
  174. return propertySet_->HasProperty(name);
  175. }
  176. const String& TileMapObject2D::GetProperty(const String& name) const
  177. {
  178. if (!propertySet_)
  179. return String::EMPTY;
  180. return propertySet_->GetProperty(name);
  181. }
  182. }