TileMapLayer2D.cpp 11 KB

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