TileMapLayer2D.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 "../Core/Context.h"
  24. #include "../Graphics/DebugRenderer.h"
  25. #include "../Resource/ResourceCache.h"
  26. #include "../Scene/Node.h"
  27. #include "../Urho2D/StaticSprite2D.h"
  28. #include "../Urho2D/TileMap2D.h"
  29. #include "../Urho2D/TileMapLayer2D.h"
  30. #include "../Urho2D/TmxFile2D.h"
  31. #include "../DebugNew.h"
  32. namespace Urho3D
  33. {
  34. TileMapLayer2D::TileMapLayer2D(Context* context) :
  35. Component(context)
  36. {
  37. }
  38. TileMapLayer2D::~TileMapLayer2D() = default;
  39. void TileMapLayer2D::RegisterObject(Context* context)
  40. {
  41. context->RegisterFactory<TileMapLayer2D>();
  42. }
  43. // Transform vector from node-local space to global space
  44. static Vector2 TransformNode2D(const Matrix3x4& transform, Vector2 local)
  45. {
  46. Vector3 transformed = transform * Vector4(local.x_, local.y_, 0.f, 1.f);
  47. return Vector2(transformed.x_, transformed.y_);
  48. }
  49. void TileMapLayer2D::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  50. {
  51. if (!debug)
  52. return;
  53. if (objectGroup_)
  54. {
  55. const Matrix3x4 transform = GetTileMap()->GetNode()->GetTransform();
  56. for (unsigned i = 0; i < objectGroup_->GetNumObjects(); ++i)
  57. {
  58. TileMapObject2D* object = objectGroup_->GetObject(i);
  59. const Color& color = Color::YELLOW;
  60. const Vector2& size = object->GetSize();
  61. const TileMapInfo2D& info = tileMap_->GetInfo();
  62. switch (object->GetObjectType())
  63. {
  64. case OT_RECTANGLE:
  65. {
  66. Vector<Vector2> points;
  67. switch (info.orientation_)
  68. {
  69. case O_ORTHOGONAL:
  70. case O_HEXAGONAL:
  71. case O_STAGGERED:
  72. {
  73. points.Push(Vector2::ZERO);
  74. points.Push(Vector2(size.x_, 0.0f));
  75. points.Push(Vector2(size.x_, -size.y_));
  76. points.Push(Vector2(0.0f, -size.y_));
  77. break;
  78. }
  79. case O_ISOMETRIC:
  80. {
  81. float ratio = (info.tileWidth_ / info.tileHeight_) * 0.5f;
  82. points.Push(Vector2::ZERO);
  83. points.Push(Vector2(size.y_ * ratio, size.y_ * 0.5f));
  84. points.Push(Vector2((size.x_ + size.y_) * ratio, (-size.x_ + size.y_) * 0.5f));
  85. points.Push(Vector2(size.x_ * ratio, -size.x_ * 0.5f));
  86. break;
  87. }
  88. }
  89. for (unsigned j = 0; j < points.Size(); ++j)
  90. debug->AddLine(Vector3(TransformNode2D(transform, points[j] + object->GetPosition())),
  91. Vector3(TransformNode2D(transform, points[(j + 1) % points.Size()] + object->GetPosition())), color,
  92. depthTest);
  93. }
  94. break;
  95. case OT_ELLIPSE:
  96. {
  97. const Vector2 halfSize = object->GetSize() * 0.5f;
  98. float ratio = (info.tileWidth_ / info.tileHeight_) * 0.5f; // For isometric only
  99. Vector2 pivot = object->GetPosition();
  100. if (info.orientation_ == O_ISOMETRIC)
  101. {
  102. pivot += Vector2((halfSize.x_ + halfSize.y_) * ratio, (-halfSize.x_ + halfSize.y_) * 0.5f);
  103. }
  104. else
  105. {
  106. pivot += halfSize;
  107. }
  108. for (unsigned i = 0; i < 360; i += 30)
  109. {
  110. unsigned j = i + 30;
  111. float x1 = halfSize.x_ * Cos((float)i);
  112. float y1 = halfSize.y_ * Sin((float)i);
  113. float x2 = halfSize.x_ * Cos((float)j);
  114. float y2 = halfSize.y_ * Sin((float)j);
  115. Vector2 point1 = Vector2(x1, - y1);
  116. Vector2 point2 = Vector2(x2, - y2);
  117. if (info.orientation_ == O_ISOMETRIC)
  118. {
  119. point1 = Vector2((point1.x_ + point1.y_) * ratio, (point1.y_ - point1.x_) * 0.5f);
  120. point2 = Vector2((point2.x_ + point2.y_) * ratio, (point2.y_ - point2.x_) * 0.5f);
  121. }
  122. debug->AddLine(Vector3(TransformNode2D(transform, pivot + point1)),
  123. Vector3(TransformNode2D(transform, pivot + point2)), color, depthTest);
  124. }
  125. }
  126. break;
  127. case OT_POLYGON:
  128. case OT_POLYLINE:
  129. {
  130. for (unsigned j = 0; j < object->GetNumPoints() - 1; ++j)
  131. debug->AddLine(Vector3(TransformNode2D(transform, object->GetPoint(j))),
  132. Vector3(TransformNode2D(transform, object->GetPoint(j + 1))), color, depthTest);
  133. if (object->GetObjectType() == OT_POLYGON)
  134. debug->AddLine(Vector3(TransformNode2D(transform, object->GetPoint(0))),
  135. Vector3(TransformNode2D(transform, object->GetPoint(object->GetNumPoints() - 1))), color, depthTest);
  136. // Also draw a circle at origin to indicate direction
  137. else
  138. debug->AddCircle(Vector3(TransformNode2D(transform, object->GetPoint(0))), Vector3::FORWARD, 0.05f, color,
  139. 64, depthTest);
  140. }
  141. break;
  142. default: break;
  143. }
  144. }
  145. }
  146. }
  147. void TileMapLayer2D::Initialize(TileMap2D* tileMap, const TmxLayer2D* tmxLayer)
  148. {
  149. if (tileMap == tileMap_ && tmxLayer == tmxLayer_)
  150. return;
  151. if (tmxLayer_)
  152. {
  153. for (unsigned i = 0; i < nodes_.Size(); ++i)
  154. {
  155. if (nodes_[i])
  156. nodes_[i]->Remove();
  157. }
  158. nodes_.Clear();
  159. }
  160. tileLayer_ = nullptr;
  161. objectGroup_ = nullptr;
  162. imageLayer_ = nullptr;
  163. tileMap_ = tileMap;
  164. tmxLayer_ = tmxLayer;
  165. if (!tmxLayer_)
  166. return;
  167. switch (tmxLayer_->GetType())
  168. {
  169. case LT_TILE_LAYER:
  170. SetTileLayer((const TmxTileLayer2D*)tmxLayer_);
  171. break;
  172. case LT_OBJECT_GROUP:
  173. SetObjectGroup((const TmxObjectGroup2D*)tmxLayer_);
  174. break;
  175. case LT_IMAGE_LAYER:
  176. SetImageLayer((const TmxImageLayer2D*)tmxLayer_);
  177. break;
  178. default:
  179. break;
  180. }
  181. SetVisible(tmxLayer_->IsVisible());
  182. }
  183. void TileMapLayer2D::SetDrawOrder(int drawOrder)
  184. {
  185. if (drawOrder == drawOrder_)
  186. return;
  187. drawOrder_ = drawOrder;
  188. for (unsigned i = 0; i < nodes_.Size(); ++i)
  189. {
  190. if (!nodes_[i])
  191. continue;
  192. auto* staticSprite = nodes_[i]->GetComponent<StaticSprite2D>();
  193. if (staticSprite)
  194. staticSprite->SetLayer(drawOrder_);
  195. }
  196. }
  197. void TileMapLayer2D::SetVisible(bool visible)
  198. {
  199. if (visible == visible_)
  200. return;
  201. visible_ = visible;
  202. for (unsigned i = 0; i < nodes_.Size(); ++i)
  203. {
  204. if (nodes_[i])
  205. nodes_[i]->SetEnabled(visible_);
  206. }
  207. }
  208. TileMap2D* TileMapLayer2D::GetTileMap() const
  209. {
  210. return tileMap_;
  211. }
  212. bool TileMapLayer2D::HasProperty(const String& name) const
  213. {
  214. if (!tmxLayer_)
  215. return false;
  216. return tmxLayer_->HasProperty(name);
  217. }
  218. const String& TileMapLayer2D::GetProperty(const String& name) const
  219. {
  220. if (!tmxLayer_)
  221. return String::EMPTY;
  222. return tmxLayer_->GetProperty(name);
  223. }
  224. TileMapLayerType2D TileMapLayer2D::GetLayerType() const
  225. {
  226. return tmxLayer_ ? tmxLayer_->GetType() : LT_INVALID;
  227. }
  228. int TileMapLayer2D::GetWidth() const
  229. {
  230. return tmxLayer_ ? tmxLayer_->GetWidth() : 0;
  231. }
  232. int TileMapLayer2D::GetHeight() const
  233. {
  234. return tmxLayer_ ? tmxLayer_->GetHeight() : 0;
  235. }
  236. Tile2D* TileMapLayer2D::GetTile(int x, int y) const
  237. {
  238. if (!tileLayer_)
  239. return nullptr;
  240. return tileLayer_->GetTile(x, y);
  241. }
  242. Node* TileMapLayer2D::GetTileNode(int x, int y) const
  243. {
  244. if (!tileLayer_)
  245. return nullptr;
  246. if (x < 0 || x >= tileLayer_->GetWidth() || y < 0 || y >= tileLayer_->GetHeight())
  247. return nullptr;
  248. return nodes_[y * tileLayer_->GetWidth() + x];
  249. }
  250. unsigned TileMapLayer2D::GetNumObjects() const
  251. {
  252. if (!objectGroup_)
  253. return 0;
  254. return objectGroup_->GetNumObjects();
  255. }
  256. TileMapObject2D* TileMapLayer2D::GetObject(unsigned index) const
  257. {
  258. if (!objectGroup_)
  259. return nullptr;
  260. return objectGroup_->GetObject(index);
  261. }
  262. Node* TileMapLayer2D::GetObjectNode(unsigned index) const
  263. {
  264. if (!objectGroup_)
  265. return nullptr;
  266. if (index >= nodes_.Size())
  267. return nullptr;
  268. return nodes_[index];
  269. }
  270. Node* TileMapLayer2D::GetImageNode() const
  271. {
  272. if (!imageLayer_ || nodes_.Empty())
  273. return nullptr;
  274. return nodes_[0];
  275. }
  276. void TileMapLayer2D::SetTileLayer(const TmxTileLayer2D* tileLayer)
  277. {
  278. tileLayer_ = tileLayer;
  279. int width = tileLayer->GetWidth();
  280. int height = tileLayer->GetHeight();
  281. nodes_.Resize((unsigned)(width * height));
  282. const TileMapInfo2D& info = tileMap_->GetInfo();
  283. for (int y = 0; y < height; ++y)
  284. {
  285. for (int x = 0; x < width; ++x)
  286. {
  287. const Tile2D* tile = tileLayer->GetTile(x, y);
  288. if (!tile)
  289. continue;
  290. SharedPtr<Node> tileNode(GetNode()->CreateTemporaryChild("Tile"));
  291. tileNode->SetPosition(Vector3(info.TileIndexToPosition(x, y)));
  292. auto* staticSprite = tileNode->CreateComponent<StaticSprite2D>();
  293. staticSprite->SetSprite(tile->GetSprite());
  294. staticSprite->SetFlip(tile->GetFlipX(), tile->GetFlipY(), tile->GetSwapXY());
  295. staticSprite->SetLayer(drawOrder_);
  296. staticSprite->SetOrderInLayer(y * width + x);
  297. nodes_[y * width + x] = tileNode;
  298. }
  299. }
  300. }
  301. void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
  302. {
  303. objectGroup_ = objectGroup;
  304. TmxFile2D* tmxFile = objectGroup->GetTmxFile();
  305. nodes_.Resize(objectGroup->GetNumObjects());
  306. for (unsigned i = 0; i < objectGroup->GetNumObjects(); ++i)
  307. {
  308. const TileMapObject2D* object = objectGroup->GetObject(i);
  309. // Create dummy node for all object
  310. SharedPtr<Node> objectNode(GetNode()->CreateTemporaryChild("Object"));
  311. objectNode->SetPosition(Vector3(object->GetPosition()));
  312. // If object is tile, create static sprite component
  313. if (object->GetObjectType() == OT_TILE && object->GetTileGid() && object->GetTileSprite())
  314. {
  315. auto* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
  316. staticSprite->SetSprite(object->GetTileSprite());
  317. staticSprite->SetFlip(object->GetTileFlipX(), object->GetTileFlipY(), object->GetTileSwapXY());
  318. staticSprite->SetLayer(drawOrder_);
  319. staticSprite->SetOrderInLayer((int)((10.0f - object->GetPosition().y_) * 100));
  320. if (tmxFile->GetInfo().orientation_ == O_ISOMETRIC)
  321. {
  322. staticSprite->SetUseHotSpot(true);
  323. staticSprite->SetHotSpot(Vector2(0.5f, 0.0f));
  324. }
  325. }
  326. nodes_[i] = objectNode;
  327. }
  328. }
  329. void TileMapLayer2D::SetImageLayer(const TmxImageLayer2D* imageLayer)
  330. {
  331. imageLayer_ = imageLayer;
  332. if (!imageLayer->GetSprite())
  333. return;
  334. SharedPtr<Node> imageNode(GetNode()->CreateTemporaryChild("Tile"));
  335. imageNode->SetPosition(Vector3(imageLayer->GetPosition()));
  336. auto* staticSprite = imageNode->CreateComponent<StaticSprite2D>();
  337. staticSprite->SetSprite(imageLayer->GetSprite());
  338. staticSprite->SetOrderInLayer(0);
  339. nodes_.Push(imageNode);
  340. }
  341. }