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