TileMapLayer2D.cpp 9.9 KB

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