TileMapLayer2D.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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, int drawOrder)
  47. {
  48. if (tmxLayer == tmxLayer_)
  49. return;
  50. if (tmxLayer_)
  51. {
  52. for (unsigned i = 0; i < tileNodes_.Size(); ++i)
  53. {
  54. if (tileNodes_[i])
  55. tileNodes_[i]->Remove();
  56. }
  57. tileNodes_.Clear();
  58. }
  59. tmxLayer_ = tmxLayer;
  60. drawOrder_ = drawOrder;
  61. if (!tmxLayer_)
  62. return;
  63. int width = tmxLayer_->width_;
  64. int height = tmxLayer->height_;
  65. tileNodes_.Resize(width * height);
  66. TmxFile2D* tmxFile = tmxLayer_->tmxFile_;
  67. float tileWidth = tmxFile->GetTileWidth();
  68. float tileHeight = tmxFile->GetTileHeight();
  69. for (int y = 0; y < height; ++y)
  70. {
  71. for (int x = 0; x < width; ++x)
  72. {
  73. int gid = tmxLayer->tileGids_[y * width + x];
  74. if (gid <= 0)
  75. continue;
  76. Sprite2D* sprite = tmxFile->GetTileSprite(gid);
  77. if (!sprite)
  78. continue;
  79. SharedPtr<Node> tileNode(GetNode()->CreateChild("Tile"));
  80. tileNode->SetTemporary(true);
  81. tileNode->SetPosition(Vector3(x * tileWidth, y * tileHeight, 0.0f));
  82. StaticSprite2D* staticSprite = tileNode->CreateComponent<StaticSprite2D>();
  83. staticSprite->SetSprite(sprite);
  84. staticSprite->SetLayer(drawOrder_);
  85. staticSprite->SetOrderInLayer((height - 1 - y) * width + x);
  86. tileNodes_[y * width + x] = tileNode;
  87. }
  88. }
  89. }
  90. void TileMapLayer2D::SetDrawOrder(int drawOrder)
  91. {
  92. if (drawOrder == drawOrder_)
  93. return;
  94. drawOrder_ = drawOrder;
  95. for (unsigned i = 0; i < tileNodes_.Size(); ++i)
  96. {
  97. if (tileNodes_[i])
  98. tileNodes_[i]->GetComponent<StaticSprite2D>()->SetLayer(drawOrder_);
  99. }
  100. }
  101. int TileMapLayer2D::GetWidth() const
  102. {
  103. return tmxLayer_ ? tmxLayer_->width_ : 0;
  104. }
  105. int TileMapLayer2D::GetHeight() const
  106. {
  107. return tmxLayer_ ? tmxLayer_->height_ : 0;
  108. }
  109. Node* TileMapLayer2D::GetTileNode(int x, int y) const
  110. {
  111. if (!tmxLayer_)
  112. return 0;
  113. if (x < 0 || x >= tmxLayer_->width_ || y < 0 || y >= tmxLayer_->height_)
  114. return 0;
  115. return tileNodes_[y * tmxLayer_->width_ + x];
  116. }
  117. }