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