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