TileMapDefs2D.cpp 6.8 KB

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