TileMapLayer2D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //
  2. // Copyright (c) 2008-2014 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 "../Scene/Node.h"
  26. #include "../Resource/ResourceCache.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:
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. void TileMapLayer2D::Initialize(TileMap2D* tileMap, const TmxLayer2D* tmxLayer)
  102. {
  103. if (tileMap == tileMap_ && tmxLayer == tmxLayer_)
  104. return;
  105. if (tmxLayer_)
  106. {
  107. for (unsigned i = 0; i < nodes_.Size(); ++i)
  108. {
  109. if (nodes_[i])
  110. nodes_[i]->Remove();
  111. }
  112. nodes_.Clear();
  113. }
  114. tileLayer_ = 0;
  115. objectGroup_ = 0;
  116. imageLayer_ = 0;
  117. tileMap_ = tileMap;
  118. tmxLayer_ = tmxLayer;
  119. if (!tmxLayer_)
  120. return;
  121. switch (tmxLayer_->GetType())
  122. {
  123. case LT_TILE_LAYER:
  124. SetTileLayer((const TmxTileLayer2D*)tmxLayer_);
  125. break;
  126. case LT_OBJECT_GROUP:
  127. SetObjectGroup((const TmxObjectGroup2D*)tmxLayer_);
  128. break;
  129. case LT_IMAGE_LAYER:
  130. SetImageLayer((const TmxImageLayer2D*)tmxLayer_);
  131. break;
  132. default:
  133. break;
  134. }
  135. SetVisible(tmxLayer_->IsVisible());
  136. }
  137. void TileMapLayer2D::SetDrawOrder(int drawOrder)
  138. {
  139. if (drawOrder == drawOrder_)
  140. return;
  141. drawOrder_ = drawOrder;
  142. for (unsigned i = 0; i < nodes_.Size(); ++i)
  143. {
  144. if (!nodes_[i])
  145. continue;
  146. StaticSprite2D* staticSprite = nodes_[i]->GetComponent<StaticSprite2D>();
  147. if (staticSprite)
  148. staticSprite->SetLayer(drawOrder_);
  149. }
  150. }
  151. void TileMapLayer2D::SetVisible(bool visible)
  152. {
  153. if (visible == visible_)
  154. return;
  155. visible_ = visible;
  156. for (unsigned i = 0; i < nodes_.Size(); ++i)
  157. {
  158. if (nodes_[i])
  159. nodes_[i]->SetEnabled(visible_);
  160. }
  161. }
  162. TileMap2D* TileMapLayer2D::GetTileMap() const
  163. {
  164. return tileMap_;
  165. }
  166. bool TileMapLayer2D::HasProperty(const String& name) const
  167. {
  168. if (!tmxLayer_)
  169. return false;
  170. return tmxLayer_->HasProperty(name);
  171. }
  172. const String& TileMapLayer2D::GetProperty(const String& name) const
  173. {
  174. if (!tmxLayer_)
  175. return String::EMPTY;
  176. return tmxLayer_->GetProperty(name);
  177. }
  178. TileMapLayerType2D TileMapLayer2D::GetLayerType() const
  179. {
  180. return tmxLayer_ ? tmxLayer_->GetType() : LT_INVALID;
  181. }
  182. int TileMapLayer2D::GetWidth() const
  183. {
  184. return tmxLayer_ ? tmxLayer_->GetWidth(): 0;
  185. }
  186. int TileMapLayer2D::GetHeight() const
  187. {
  188. return tmxLayer_ ? tmxLayer_->GetHeight(): 0;
  189. }
  190. Tile2D* TileMapLayer2D::GetTile(int x, int y) const
  191. {
  192. if (!tileLayer_)
  193. return 0;
  194. return tileLayer_->GetTile(x, y);
  195. }
  196. Node* TileMapLayer2D::GetTileNode(int x, int y) const
  197. {
  198. if (!tileLayer_)
  199. return 0;
  200. if (x < 0 || x >= tileLayer_->GetWidth() || y < 0 || y >= tileLayer_->GetHeight())
  201. return 0;
  202. return nodes_[y * tileLayer_->GetWidth() + x];
  203. }
  204. unsigned TileMapLayer2D::GetNumObjects() const
  205. {
  206. if (!objectGroup_)
  207. return 0;
  208. return objectGroup_->GetNumObjects();
  209. }
  210. TileMapObject2D* TileMapLayer2D::GetObject(unsigned index) const
  211. {
  212. if (!objectGroup_)
  213. return 0;
  214. return objectGroup_->GetObject(index);
  215. }
  216. Node* TileMapLayer2D::GetObjectNode(unsigned index) const
  217. {
  218. if (!objectGroup_)
  219. return 0;
  220. if (index >= nodes_.Size())
  221. return 0;
  222. return nodes_[index];
  223. }
  224. Node* TileMapLayer2D::GetImageNode() const
  225. {
  226. if (!imageLayer_)
  227. return 0;
  228. if (nodes_.Empty())
  229. return 0;
  230. return nodes_[0];
  231. }
  232. void TileMapLayer2D::SetTileLayer(const TmxTileLayer2D* tileLayer)
  233. {
  234. tileLayer_ = tileLayer;
  235. int width = tileLayer->GetWidth();
  236. int height = tileLayer->GetHeight();
  237. nodes_.Resize(width * height);
  238. const TileMapInfo2D& info = tileMap_->GetInfo();
  239. for (int y = 0; y < height; ++y)
  240. {
  241. for (int x = 0; x < width; ++x)
  242. {
  243. const Tile2D* tile = tileLayer->GetTile(x, y);
  244. if (!tile)
  245. continue;
  246. SharedPtr<Node> tileNode(GetNode()->CreateChild("Tile"));
  247. tileNode->SetTemporary(true);
  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. // collision
  254. RigidBody2D *body = NULL;
  255. TmxObjectGroup2D* group = tile->GetObjectGroup();
  256. if (group)
  257. {
  258. for (unsigned i = 0; i < group->GetNumObjects(); i++)
  259. {
  260. TileMapObject2D* o = group->GetObject(i);
  261. if (o->ValidCollisionShape())
  262. {
  263. if (!body)
  264. {
  265. body = tileNode->CreateComponent<RigidBody2D>();
  266. body->SetBodyType(BT_STATIC);
  267. }
  268. o->CreateCollisionShape(tileNode);
  269. }
  270. }
  271. }
  272. nodes_[y * width + x] = tileNode;
  273. }
  274. }
  275. }
  276. void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
  277. {
  278. objectGroup_ = objectGroup;
  279. TmxFile2D* tmxFile = objectGroup->GetTmxFile();
  280. nodes_.Resize(objectGroup->GetNumObjects());
  281. for (unsigned i = 0; i < objectGroup->GetNumObjects(); ++i)
  282. {
  283. const TileMapObject2D* object = objectGroup->GetObject(i);
  284. // Create dummy node for all object
  285. SharedPtr<Node> objectNode(GetNode()->CreateChild("Object"));
  286. objectNode->SetTemporary(true);
  287. objectNode->SetPosition(object->GetPosition());
  288. // If object is tile, create static sprite component
  289. if (object->GetObjectType() == OT_TILE && object->GetTileGid() && object->GetTileSprite())
  290. {
  291. StaticSprite2D* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
  292. staticSprite->SetSprite(object->GetTileSprite());
  293. staticSprite->SetLayer(drawOrder_);
  294. staticSprite->SetOrderInLayer((int)((10.0f - object->GetPosition().y_) * 100));
  295. if (tmxFile->GetInfo().orientation_ == O_ISOMETRIC)
  296. {
  297. staticSprite->SetUseHotSpot(true);
  298. staticSprite->SetHotSpot(Vector2(0.5f, 0.0f));
  299. }
  300. }
  301. nodes_[i] = objectNode;
  302. }
  303. }
  304. void TileMapLayer2D::SetImageLayer(const TmxImageLayer2D* imageLayer)
  305. {
  306. imageLayer_ = imageLayer;
  307. if (!imageLayer->GetSprite())
  308. return;
  309. SharedPtr<Node> imageNode(GetNode()->CreateChild("Tile"));
  310. imageNode->SetTemporary(true);
  311. imageNode->SetPosition(imageLayer->GetPosition());
  312. StaticSprite2D* staticSprite = imageNode->CreateComponent<StaticSprite2D>();
  313. staticSprite->SetSprite(imageLayer->GetSprite());
  314. staticSprite->SetOrderInLayer(0);
  315. nodes_.Push(imageNode);
  316. }
  317. const String& TileMapLayer2D::GetName() const
  318. {
  319. static String none("");
  320. if (tmxLayer_)
  321. return tmxLayer_->GetName();
  322. return none;
  323. }
  324. }