TileMapLayer2D.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "ResourceCache.h"
  25. #include "TileMapLayer2D.h"
  26. #include "TmxFile2D.h"
  27. #include "TileMapLayer2D.h"
  28. #include "Node.h"
  29. #include "StaticSprite2D.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. TileMapLayer2D::TileMapLayer2D(Context* context) :
  34. Component(context),
  35. tmxLayer_(0),
  36. drawOrder_(0)
  37. {
  38. }
  39. TileMapLayer2D::~TileMapLayer2D()
  40. {
  41. }
  42. void TileMapLayer2D::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<TileMapLayer2D>();
  45. }
  46. void TileMapLayer2D::SetTmxLayer(const TmxLayer2D* tmxLayer)
  47. {
  48. if (tmxLayer == tmxLayer_)
  49. return;
  50. if (tmxLayer_)
  51. {
  52. for (unsigned i = 0; i < nodes_.Size(); ++i)
  53. {
  54. if (nodes_[i])
  55. nodes_[i]->Remove();
  56. }
  57. nodes_.Clear();
  58. }
  59. tmxLayer_ = tmxLayer;
  60. if (!tmxLayer_)
  61. return;
  62. if (tmxLayer_->type_ == LT_TILE_LAYER)
  63. SetTileLayer((const TmxTileLayer2D*)tmxLayer_);
  64. else if (tmxLayer_->type_ == LT_OBJECT_GROUP)
  65. SetObjectGroup((const TmxObjectGroup2D*)tmxLayer_);
  66. else if (tmxLayer_->type_ == LT_IMAGE_LAYER)
  67. SetImageLayer((const TmxImageLayer2D*)tmxLayer_);
  68. }
  69. void TileMapLayer2D::SetDrawOrder(int drawOrder)
  70. {
  71. if (drawOrder == drawOrder_)
  72. return;
  73. drawOrder_ = drawOrder;
  74. for (unsigned i = 0; i < nodes_.Size(); ++i)
  75. {
  76. if (nodes_[i])
  77. nodes_[i]->GetComponent<StaticSprite2D>()->SetLayer(drawOrder_);
  78. }
  79. }
  80. TmxLayerType2D TileMapLayer2D::GetLayerType() const
  81. {
  82. return tmxLayer_ ? tmxLayer_->type_ : LT_INVALID;
  83. }
  84. const String& TileMapLayer2D::GetName() const
  85. {
  86. return tmxLayer_ ? tmxLayer_->name_ : String::EMPTY;
  87. }
  88. int TileMapLayer2D::GetWidth() const
  89. {
  90. return tmxLayer_ ? tmxLayer_->width_ : 0;
  91. }
  92. int TileMapLayer2D::GetHeight() const
  93. {
  94. return tmxLayer_ ? tmxLayer_->height_ : 0;
  95. }
  96. Node* TileMapLayer2D::GetTileNode(int x, int y) const
  97. {
  98. if (!tmxLayer_ || tmxLayer_->type_ != LT_TILE_LAYER)
  99. return 0;
  100. const TmxTileLayer2D* tileLayer = (const TmxTileLayer2D*)tmxLayer_;
  101. if (x < 0 || x >= tileLayer->width_ || y < 0 || y >= tileLayer->height_)
  102. return 0;
  103. return nodes_[y * tileLayer->width_ + x];
  104. }
  105. Node* TileMapLayer2D::GetImageNode() const
  106. {
  107. if (!tmxLayer_ || tmxLayer_->type_ != LT_IMAGE_LAYER)
  108. return 0;
  109. if (nodes_.Empty())
  110. return 0;
  111. return nodes_[0];
  112. }
  113. void TileMapLayer2D::SetTileLayer(const TmxTileLayer2D* tileLayer)
  114. {
  115. int width = tileLayer->width_;
  116. int height = tileLayer->height_;
  117. nodes_.Resize(width * height);
  118. TmxFile2D* tmxFile = tileLayer->tmxFile_;
  119. float tileWidth = tmxFile->GetTileWidth();
  120. float tileHeight = tmxFile->GetTileHeight();
  121. for (int y = 0; y < height; ++y)
  122. {
  123. for (int x = 0; x < width; ++x)
  124. {
  125. int gid = tileLayer->tileGids_[y * width + x];
  126. if (gid <= 0)
  127. continue;
  128. Sprite2D* sprite = tmxFile->GetTileSprite(gid);
  129. if (!sprite)
  130. continue;
  131. SharedPtr<Node> tileNode(GetNode()->CreateChild("Tile"));
  132. tileNode->SetTemporary(true);
  133. tileNode->SetPosition(Vector3(x * tileWidth, y * tileHeight, 0.0f));
  134. StaticSprite2D* staticSprite = tileNode->CreateComponent<StaticSprite2D>();
  135. staticSprite->SetSprite(sprite);
  136. staticSprite->SetLayer(drawOrder_);
  137. staticSprite->SetOrderInLayer((height - 1 - y) * width + x);
  138. nodes_[y * width + x] = tileNode;
  139. }
  140. }
  141. }
  142. void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
  143. {
  144. TmxFile2D* tmxFile = objectGroup->tmxFile_;
  145. for (unsigned i = 0; i < objectGroup->objects_.Size(); ++i)
  146. {
  147. const TmxObject& object = objectGroup->objects_[i];
  148. if (object.type_ != OT_TILE)
  149. continue;
  150. if (object.gid_ <= 0)
  151. continue;
  152. Sprite2D* sprite = tmxFile->GetTileSprite(object.gid_);
  153. if (!sprite)
  154. continue;
  155. SharedPtr<Node> tileNode(GetNode()->CreateChild("Tile"));
  156. tileNode->SetTemporary(true);
  157. tileNode->SetPosition(Vector3(object.x_, object.y_, 0.0f));
  158. StaticSprite2D* staticSprite = tileNode->CreateComponent<StaticSprite2D>();
  159. staticSprite->SetSprite(sprite);
  160. staticSprite->SetLayer(drawOrder_);
  161. staticSprite->SetOrderInLayer((int)((10.0f - object.y_) * 100.0f));
  162. nodes_.Push(tileNode);
  163. }
  164. }
  165. void TileMapLayer2D::SetImageLayer(const TmxImageLayer2D* imageLayer)
  166. {
  167. if (!imageLayer->sprite_)
  168. return;
  169. TmxFile2D* tmxFile = imageLayer->tmxFile_;
  170. int height = tmxFile->GetHeight();
  171. float tileHeight = tmxFile->GetTileHeight();
  172. SharedPtr<Node> imageNode(GetNode()->CreateChild("Tile"));
  173. imageNode->SetTemporary(true);
  174. imageNode->SetPosition(Vector3(0.0f, height * tileHeight, 0.0f));
  175. StaticSprite2D* staticSprite = imageNode->CreateComponent<StaticSprite2D>();
  176. staticSprite->SetSprite(imageLayer->sprite_);
  177. staticSprite->SetOrderInLayer(0);
  178. nodes_.Push(imageNode);
  179. }
  180. }