TileMapLayer2D.cpp 7.5 KB

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