TileMapLayer2D.cpp 12 KB

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