TileMapLayer2D.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "Node.h"
  25. #include "ResourceCache.h"
  26. #include "StaticSprite2D.h"
  27. #include "TileMap2D.h"
  28. #include "TileMapLayer2D.h"
  29. #include "TmxFile2D.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. TileMapLayer2D::TileMapLayer2D(Context* context) :
  34. Component(context),
  35. tmxLayer_(0),
  36. drawOrder_(0),
  37. visible_(true)
  38. {
  39. }
  40. TileMapLayer2D::~TileMapLayer2D()
  41. {
  42. }
  43. void TileMapLayer2D::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<TileMapLayer2D>();
  46. }
  47. void TileMapLayer2D::Initialize(TileMap2D* tileMap, const TmxLayer2D* tmxLayer)
  48. {
  49. if (tileMap == tileMap_ && tmxLayer == tmxLayer_)
  50. return;
  51. if (tmxLayer_)
  52. {
  53. for (unsigned i = 0; i < nodes_.Size(); ++i)
  54. {
  55. if (nodes_[i])
  56. nodes_[i]->Remove();
  57. }
  58. nodes_.Clear();
  59. }
  60. tileLayer_ = 0;
  61. objectGroup_ = 0;
  62. imageLayer_ = 0;
  63. tileMap_ = tileMap;
  64. tmxLayer_ = tmxLayer;
  65. if (!tmxLayer_)
  66. return;
  67. switch (tmxLayer_->GetType())
  68. {
  69. case LT_TILE_LAYER:
  70. SetTileLayer((const TmxTileLayer2D*)tmxLayer_);
  71. break;
  72. case LT_OBJECT_GROUP:
  73. SetObjectGroup((const TmxObjectGroup2D*)tmxLayer_);
  74. break;
  75. case LT_IMAGE_LAYER:
  76. SetImageLayer((const TmxImageLayer2D*)tmxLayer_);
  77. break;
  78. default:
  79. break;
  80. }
  81. SetVisible(tmxLayer_->IsVisible());
  82. }
  83. void TileMapLayer2D::SetDrawOrder(int drawOrder)
  84. {
  85. if (drawOrder == drawOrder_)
  86. return;
  87. drawOrder_ = drawOrder;
  88. for (unsigned i = 0; i < nodes_.Size(); ++i)
  89. {
  90. if (!nodes_[i])
  91. continue;
  92. StaticSprite2D* staticSprite = nodes_[i]->GetComponent<StaticSprite2D>();
  93. if (staticSprite)
  94. staticSprite->SetLayer(drawOrder_);
  95. }
  96. }
  97. void TileMapLayer2D::SetVisible(bool visible)
  98. {
  99. if (visible == visible_)
  100. return;
  101. visible_ = visible;
  102. for (unsigned i = 0; i < nodes_.Size(); ++i)
  103. {
  104. if (nodes_[i])
  105. nodes_[i]->SetEnabled(visible_);
  106. }
  107. }
  108. TileMap2D* TileMapLayer2D::GetTileMap() const
  109. {
  110. return tileMap_;
  111. }
  112. bool TileMapLayer2D::HasProperty(const String& name) const
  113. {
  114. if (!tmxLayer_)
  115. return false;
  116. return tmxLayer_->HasProperty(name);
  117. }
  118. const String& TileMapLayer2D::GetProperty(const String& name) const
  119. {
  120. if (!tmxLayer_)
  121. return String::EMPTY;
  122. return tmxLayer_->GetProperty(name);
  123. }
  124. TileMapLayerType2D TileMapLayer2D::GetLayerType() const
  125. {
  126. return tmxLayer_ ? tmxLayer_->GetType() : LT_INVALID;
  127. }
  128. int TileMapLayer2D::GetWidth() const
  129. {
  130. return tmxLayer_ ? tmxLayer_->GetWidth(): 0;
  131. }
  132. int TileMapLayer2D::GetHeight() const
  133. {
  134. return tmxLayer_ ? tmxLayer_->GetHeight(): 0;
  135. }
  136. Tile2D* TileMapLayer2D::GetTile(int x, int y) const
  137. {
  138. if (!tileLayer_)
  139. return 0;
  140. return tileLayer_->GetTile(x, y);
  141. }
  142. Node* TileMapLayer2D::GetTileNode(int x, int y) const
  143. {
  144. if (!tileLayer_)
  145. return 0;
  146. if (x < 0 || x >= tileLayer_->GetWidth() || y < 0 || y >= tileLayer_->GetHeight())
  147. return 0;
  148. return nodes_[y * tileLayer_->GetWidth() + x];
  149. }
  150. unsigned TileMapLayer2D::GetNumObjects() const
  151. {
  152. if (!objectGroup_)
  153. return 0;
  154. return objectGroup_->GetNumObjects();
  155. }
  156. TileObject2D* TileMapLayer2D::GetObject(unsigned index) const
  157. {
  158. if (!objectGroup_)
  159. return 0;
  160. return objectGroup_->GetObject(index);
  161. }
  162. Node* TileMapLayer2D::GetObjectNode(unsigned index) const
  163. {
  164. if (!objectGroup_)
  165. return 0;
  166. if (index >= nodes_.Size())
  167. return 0;
  168. return nodes_[index];
  169. }
  170. Node* TileMapLayer2D::GetImageNode() const
  171. {
  172. if (!imageLayer_)
  173. return 0;
  174. if (nodes_.Empty())
  175. return 0;
  176. return nodes_[0];
  177. }
  178. void TileMapLayer2D::SetTileLayer(const TmxTileLayer2D* tileLayer)
  179. {
  180. tileLayer_ = tileLayer;
  181. int width = tileLayer->GetWidth();
  182. int height = tileLayer->GetHeight();
  183. nodes_.Resize(width * height);
  184. TmxFile2D* tmxFile = tileLayer->GetTmxFile();
  185. const TileMapInfo2D& info = tmxFile->GetInfo();
  186. for (int y = 0; y < height; ++y)
  187. {
  188. for (int x = 0; x < width; ++x)
  189. {
  190. const Tile2D* tile = tileLayer->GetTile(x, y);
  191. if (!tile)
  192. continue;
  193. SharedPtr<Node> tileNode(GetNode()->CreateChild("Tile"));
  194. tileNode->SetTemporary(true);
  195. tileNode->SetPosition(IndexToPosition2D(x, y, info));
  196. StaticSprite2D* staticSprite = tileNode->CreateComponent<StaticSprite2D>();
  197. staticSprite->SetSprite(tile->GetSprite());
  198. staticSprite->SetLayer(drawOrder_);
  199. staticSprite->SetOrderInLayer((height - 1 - y) * width + x);
  200. nodes_[y * width + x] = tileNode;
  201. }
  202. }
  203. }
  204. void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
  205. {
  206. objectGroup_ = objectGroup;
  207. TmxFile2D* tmxFile = objectGroup->GetTmxFile();
  208. nodes_.Resize(objectGroup->GetNumObjects());
  209. for (unsigned i = 0; i < objectGroup->GetNumObjects(); ++i)
  210. {
  211. const TileObject2D* object = objectGroup->GetObject(i);
  212. // Create dummy node for all object
  213. SharedPtr<Node> objectNode(GetNode()->CreateChild("Object"));
  214. objectNode->SetTemporary(true);
  215. objectNode->SetPosition(object->GetPosition());
  216. // If object is tile, create static sprite component
  217. if (object->GetType() == OT_TILE && object->GetTileGid() && object->GetTileSprite())
  218. {
  219. StaticSprite2D* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
  220. staticSprite->SetSprite(object->GetTileSprite());
  221. staticSprite->SetLayer(drawOrder_);
  222. staticSprite->SetOrderInLayer((int)((10.0f - object->GetPosition().y_) * 100));
  223. }
  224. nodes_[i] = objectNode;
  225. }
  226. }
  227. void TileMapLayer2D::SetImageLayer(const TmxImageLayer2D* imageLayer)
  228. {
  229. imageLayer_ = imageLayer;
  230. if (!imageLayer->GetSprite())
  231. return;
  232. TmxFile2D* tmxFile = imageLayer->GetTmxFile();
  233. float mapHeight = tmxFile->GetInfo().GetMapHeight();
  234. SharedPtr<Node> imageNode(GetNode()->CreateChild("Tile"));
  235. imageNode->SetTemporary(true);
  236. imageNode->SetPosition(Vector3(0.0f, mapHeight, 0.0f));
  237. StaticSprite2D* staticSprite = imageNode->CreateComponent<StaticSprite2D>();
  238. staticSprite->SetSprite(imageLayer->GetSprite());
  239. staticSprite->SetOrderInLayer(0);
  240. nodes_.Push(imageNode);
  241. }
  242. }