TileMapLayer2D.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "Context.h"
  24. #include "DebugRenderer.h"
  25. #include "Node.h"
  26. #include "ResourceCache.h"
  27. #include "StaticSprite2D.h"
  28. #include "TileMap2D.h"
  29. #include "TileMapLayer2D.h"
  30. #include "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:
  95. 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(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. nodes_[y * width + x] = tileNode;
  253. }
  254. }
  255. }
  256. void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
  257. {
  258. objectGroup_ = objectGroup;
  259. TmxFile2D* tmxFile = objectGroup->GetTmxFile();
  260. nodes_.Resize(objectGroup->GetNumObjects());
  261. for (unsigned i = 0; i < objectGroup->GetNumObjects(); ++i)
  262. {
  263. const TileMapObject2D* object = objectGroup->GetObject(i);
  264. // Create dummy node for all object
  265. SharedPtr<Node> objectNode(GetNode()->CreateChild("Object"));
  266. objectNode->SetTemporary(true);
  267. objectNode->SetPosition(object->GetPosition());
  268. // If object is tile, create static sprite component
  269. if (object->GetObjectType() == OT_TILE && object->GetTileGid() && object->GetTileSprite())
  270. {
  271. StaticSprite2D* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
  272. staticSprite->SetSprite(object->GetTileSprite());
  273. staticSprite->SetLayer(drawOrder_);
  274. staticSprite->SetOrderInLayer((int)((10.0f - object->GetPosition().y_) * 100));
  275. if (tmxFile->GetInfo().orientation_ == O_ISOMETRIC)
  276. {
  277. staticSprite->SetUseHotSpot(true);
  278. staticSprite->SetHotSpot(Vector2(0.5f, 0.0f));
  279. }
  280. }
  281. nodes_[i] = objectNode;
  282. }
  283. }
  284. void TileMapLayer2D::SetImageLayer(const TmxImageLayer2D* imageLayer)
  285. {
  286. imageLayer_ = imageLayer;
  287. if (!imageLayer->GetSprite())
  288. return;
  289. SharedPtr<Node> imageNode(GetNode()->CreateChild("Tile"));
  290. imageNode->SetTemporary(true);
  291. imageNode->SetPosition(imageLayer->GetPosition());
  292. StaticSprite2D* staticSprite = imageNode->CreateComponent<StaticSprite2D>();
  293. staticSprite->SetSprite(imageLayer->GetSprite());
  294. staticSprite->SetOrderInLayer(0);
  295. nodes_.Push(imageNode);
  296. }
  297. }