TileMapLayer2D.cpp 13 KB

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