TileMapLayer2D.cpp 11 KB

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