TileMap2D.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/Context.h"
  24. #include "../Graphics/DebugRenderer.h"
  25. #include "../Resource/ResourceCache.h"
  26. #include "../Scene/Node.h"
  27. #include "../Scene/Scene.h"
  28. #include "../Atomic2D/TileMap2D.h"
  29. #include "../Atomic2D/TileMapLayer2D.h"
  30. #include "../Atomic2D/TmxFile2D.h"
  31. #include "../DebugNew.h"
  32. namespace Atomic
  33. {
  34. extern const float PIXEL_SIZE;
  35. extern const char* ATOMIC2D_CATEGORY;
  36. TileMap2D::TileMap2D(Context* context) :
  37. Component(context)
  38. {
  39. }
  40. TileMap2D::~TileMap2D()
  41. {
  42. }
  43. void TileMap2D::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<TileMap2D>(ATOMIC2D_CATEGORY);
  46. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  47. MIXED_ACCESSOR_ATTRIBUTE("Tmx File", GetTmxFileAttr, SetTmxFileAttr, ResourceRef, ResourceRef(TmxFile2D::GetTypeStatic()),
  48. AM_DEFAULT);
  49. }
  50. void TileMap2D::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  51. {
  52. const Color& color = Color::RED;
  53. float mapW = info_.GetMapWidth();
  54. float mapH = info_.GetMapHeight();
  55. switch (info_.orientation_)
  56. {
  57. case O_ORTHOGONAL:
  58. debug->AddLine(Vector2(0.0f, 0.0f), Vector2(mapW, 0.0f), color);
  59. debug->AddLine(Vector2(mapW, 0.0f), Vector2(mapW, mapH), color);
  60. debug->AddLine(Vector2(mapW, mapH), Vector2(0.0f, mapH), color);
  61. debug->AddLine(Vector2(0.0f, mapH), Vector2(0.0f, 0.0f), color);
  62. break;
  63. case O_ISOMETRIC:
  64. debug->AddLine(Vector2(0.0f, mapH * 0.5f), Vector2(mapW * 0.5f, 0.0f), color);
  65. debug->AddLine(Vector2(mapW * 0.5f, 0.0f), Vector2(mapW, mapH * 0.5f), color);
  66. debug->AddLine(Vector2(mapW, mapH * 0.5f), Vector2(mapW * 0.5f, mapH), color);
  67. debug->AddLine(Vector2(mapW * 0.5f, mapH), Vector2(0.0f, mapH * 0.5f), color);
  68. break;
  69. case O_STAGGERED:
  70. debug->AddLine(Vector2(0.0f, 0.0f), Vector2(mapW, 0.0f), color);
  71. debug->AddLine(Vector2(mapW, 0.0f), Vector2(mapW, mapH), color);
  72. debug->AddLine(Vector2(mapW, mapH), Vector2(0.0f, mapH), color);
  73. debug->AddLine(Vector2(0.0f, mapH), Vector2(0.0f, 0.0f), color);
  74. break;
  75. }
  76. for (unsigned i = 0; i < layers_.Size(); ++i)
  77. layers_[i]->DrawDebugGeometry(debug, depthTest);
  78. }
  79. void TileMap2D::DrawDebugGeometry()
  80. {
  81. Scene* scene = GetScene();
  82. if (!scene)
  83. return;
  84. DebugRenderer* debug = scene->GetComponent<DebugRenderer>();
  85. if (!debug)
  86. return;
  87. DrawDebugGeometry(debug, false);
  88. }
  89. void TileMap2D::SetTmxFile(TmxFile2D* tmxFile)
  90. {
  91. if (tmxFile == tmxFile_)
  92. return;
  93. if (rootNode_)
  94. rootNode_->RemoveAllChildren();
  95. layers_.Clear();
  96. tmxFile_ = tmxFile;
  97. if (!tmxFile_)
  98. return;
  99. info_ = tmxFile_->GetInfo();
  100. if (!rootNode_)
  101. {
  102. rootNode_ = GetNode()->CreateChild("_root_", LOCAL);
  103. rootNode_->SetTemporary(true);
  104. }
  105. unsigned numLayers = tmxFile_->GetNumLayers();
  106. layers_.Resize(numLayers);
  107. for (unsigned i = 0; i < numLayers; ++i)
  108. {
  109. const TmxLayer2D* tmxLayer = tmxFile_->GetLayer(i);
  110. Node* layerNode(rootNode_->CreateChild(tmxLayer->GetName(), LOCAL));
  111. layerNode->SetTemporary(true);
  112. TileMapLayer2D* layer = layerNode->CreateComponent<TileMapLayer2D>();
  113. layer->Initialize(this, tmxLayer);
  114. layer->SetDrawOrder(i * 10);
  115. layers_[i] = layer;
  116. }
  117. }
  118. TmxFile2D* TileMap2D::GetTmxFile() const
  119. {
  120. return tmxFile_;
  121. }
  122. TileMapLayer2D* TileMap2D::GetLayer(unsigned index) const
  123. {
  124. if (index >= layers_.Size())
  125. return 0;
  126. return layers_[index];
  127. }
  128. TileMapLayer2D* TileMap2D::GetLayerByName(const String& name) const
  129. {
  130. for (unsigned i = 0; i < layers_.Size(); i++)
  131. {
  132. if (layers_[i]->GetName() == name)
  133. return layers_[i];
  134. }
  135. return 0;
  136. }
  137. Vector2 TileMap2D::TileIndexToPosition(int x, int y) const
  138. {
  139. return info_.TileIndexToPosition(x, y);
  140. }
  141. bool TileMap2D::PositionToTileIndex(int& x, int& y, const Vector2& position) const
  142. {
  143. return info_.PositionToTileIndex(x, y, position);
  144. }
  145. void TileMap2D::SetTmxFileAttr(const ResourceRef& value)
  146. {
  147. ResourceCache* cache = GetSubsystem<ResourceCache>();
  148. SetTmxFile(cache->GetResource<TmxFile2D>(value.name_));
  149. }
  150. ResourceRef TileMap2D::GetTmxFileAttr() const
  151. {
  152. return GetResourceRef(tmxFile_, TmxFile2D::GetTypeStatic());
  153. }
  154. }