TileMapDefs2D.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "../Atomic2D/TileMapDefs2D.h"
  24. #include "../Atomic2D/TmxFile2D.h"
  25. #include "../Resource/XMLElement.h"
  26. #include "../Atomic2D/CollisionBox2D.h"
  27. #include "../Scene/Node.h"
  28. #include "../DebugNew.h"
  29. namespace Atomic
  30. {
  31. extern ATOMIC_API const float PIXEL_SIZE;
  32. float TileMapInfo2D::GetMapWidth() const
  33. {
  34. return width_ * tileWidth_;
  35. }
  36. float TileMapInfo2D::GetMapHeight() const
  37. {
  38. if (orientation_ == O_STAGGERED)
  39. return (height_ + 1) * 0.5f * tileHeight_;
  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, (height_ * 2.0f - index.x_ - index.y_) * tileHeight_ * 0.5f);
  51. }
  52. case O_STAGGERED:
  53. return Vector2(position.x_ * PIXEL_SIZE, GetMapHeight() - position.y_ * PIXEL_SIZE);
  54. case O_ORTHOGONAL:
  55. default:
  56. return Vector2(position.x_ * PIXEL_SIZE, GetMapHeight() - position.y_ * PIXEL_SIZE);
  57. }
  58. return Vector2::ZERO;
  59. }
  60. Vector2 TileMapInfo2D::TileIndexToPosition(int x, int y) const
  61. {
  62. switch (orientation_)
  63. {
  64. case O_ISOMETRIC:
  65. return Vector2((width_ + x - y - 1) * tileWidth_ * 0.5f, (height_ * 2 - x - y - 2) * tileHeight_ * 0.5f);
  66. case O_STAGGERED:
  67. if (y % 2 == 0)
  68. return Vector2(x * tileWidth_, (height_ - 1 - y) * 0.5f * tileHeight_);
  69. else
  70. return Vector2((x + 0.5f) * tileWidth_, (height_ - 1 - y) * 0.5f * tileHeight_);
  71. case O_ORTHOGONAL:
  72. default:
  73. return Vector2(x * tileWidth_, (height_ - 1 - y) * tileHeight_);
  74. }
  75. return Vector2::ZERO;
  76. }
  77. bool TileMapInfo2D::PositionToTileIndex(int& x, int& y, const Vector2& position) const
  78. {
  79. switch (orientation_)
  80. {
  81. case O_ISOMETRIC:
  82. {
  83. int x_sub_y = (int)(position.x_ * 2.0f / tileWidth_ + 1 - width_);
  84. int x_add_y = (int)(height_ * 2.0f - position.y_ * 2.0f / tileHeight_ - 2.0f);
  85. x = (x_sub_y - x_add_y) / 2;
  86. y = (x_sub_y - x_add_y) / 2;
  87. }
  88. break;
  89. case O_STAGGERED:
  90. y = (int)(height_ - 1 - position.y_ * 2.0f / tileHeight_);
  91. if (y % 2 == 0)
  92. x = (int)(position.x_ / tileWidth_);
  93. else
  94. x = (int)(position.x_ / tileWidth_ - 0.5f);
  95. break;
  96. case O_ORTHOGONAL:
  97. default:
  98. x = (int)(position.x_ / tileWidth_);
  99. y = height_ - 1 - int(position.y_ / tileHeight_);
  100. break;
  101. }
  102. return x >= 0 && x < width_ && y >= 0 && y < height_;
  103. }
  104. PropertySet2D::PropertySet2D()
  105. {
  106. }
  107. PropertySet2D::~PropertySet2D()
  108. {
  109. }
  110. void PropertySet2D::Load(const XMLElement& element)
  111. {
  112. assert(element.GetName() == "properties");
  113. for (XMLElement propertyElem = element.GetChild("property"); propertyElem; propertyElem = propertyElem.GetNext("property"))
  114. nameToValueMapping_[propertyElem.GetAttribute("name")] = propertyElem.GetAttribute("value");
  115. }
  116. bool PropertySet2D::HasProperty(const String& name) const
  117. {
  118. return nameToValueMapping_.Find(name) != nameToValueMapping_.End();
  119. }
  120. const String& PropertySet2D::GetProperty(const String& name) const
  121. {
  122. HashMap<String, String>::ConstIterator i = nameToValueMapping_.Find(name);
  123. if (i == nameToValueMapping_.End())
  124. return String::EMPTY;
  125. return i->second_;
  126. }
  127. Tile2D::Tile2D() :
  128. gid_(0)
  129. {
  130. }
  131. Sprite2D* Tile2D::GetSprite() const
  132. {
  133. return sprite_;
  134. }
  135. TmxObjectGroup2D* Tile2D::GetObjectGroup() const
  136. {
  137. return objectGroup_;
  138. }
  139. bool Tile2D::HasProperty(const String& name) const
  140. {
  141. if (!propertySet_)
  142. return false;
  143. return propertySet_->HasProperty(name);
  144. }
  145. const String& Tile2D::GetProperty(const String& name) const
  146. {
  147. if (!propertySet_)
  148. return String::EMPTY;
  149. return propertySet_->GetProperty(name);
  150. }
  151. TileMapObject2D::TileMapObject2D()
  152. {
  153. }
  154. unsigned TileMapObject2D::GetNumPoints() const
  155. {
  156. return points_.Size();
  157. }
  158. const Vector2& TileMapObject2D::GetPoint(unsigned index) const
  159. {
  160. if (index >= points_.Size())
  161. return Vector2::ZERO;
  162. return points_[index];
  163. }
  164. Sprite2D* TileMapObject2D::GetTileSprite() const
  165. {
  166. return sprite_;
  167. }
  168. bool TileMapObject2D::HasProperty(const String& name) const
  169. {
  170. if (!propertySet_)
  171. return false;
  172. return propertySet_->HasProperty(name);
  173. }
  174. const String& TileMapObject2D::GetProperty(const String& name) const
  175. {
  176. if (!propertySet_)
  177. return String::EMPTY;
  178. return propertySet_->GetProperty(name);
  179. }
  180. bool TileMapObject2D::ValidCollisionShape() const
  181. {
  182. if (objectType_ == OT_RECTANGLE)
  183. return true;
  184. return false;
  185. }
  186. CollisionShape2D* TileMapObject2D::CreateCollisionShape(Node *node) const
  187. {
  188. CollisionShape2D* shape = NULL;
  189. if (objectType_ == OT_RECTANGLE)
  190. {
  191. CollisionBox2D* box = node->CreateComponent<CollisionBox2D>();
  192. box->SetSize(size_);
  193. box->SetCenter(position_);
  194. shape = box;
  195. }
  196. return shape;
  197. }
  198. }