TileMap2D.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // Copyright (c) 2008-2017 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. // ATOMIC BEGIN
  35. // extern const float PIXEL_SIZE;
  36. // ATOMIC END
  37. extern const char* ATOMIC2D_CATEGORY;
  38. TileMap2D::TileMap2D(Context* context) :
  39. Component(context)
  40. {
  41. }
  42. TileMap2D::~TileMap2D()
  43. {
  44. }
  45. void TileMap2D::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<TileMap2D>(ATOMIC2D_CATEGORY);
  48. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  49. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Tmx File", GetTmxFileAttr, SetTmxFileAttr, ResourceRef, ResourceRef(TmxFile2D::GetTypeStatic()),
  50. AM_DEFAULT);
  51. }
  52. // Transform vector from node-local space to global space
  53. static Vector2 TransformNode2D(Matrix3x4 transform, Vector2 local)
  54. {
  55. Vector3 transformed = transform * Vector4(local.x_, local.y_, 0.f, 1.f);
  56. return Vector2(transformed.x_, transformed.y_);
  57. }
  58. void TileMap2D::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  59. {
  60. const Color& color = Color::RED;
  61. float mapW = info_.GetMapWidth();
  62. float mapH = info_.GetMapHeight();
  63. const Matrix3x4 transform = GetNode()->GetTransform();
  64. switch (info_.orientation_)
  65. {
  66. case O_ORTHOGONAL:
  67. case O_STAGGERED:
  68. case O_HEXAGONAL:
  69. debug->AddLine(TransformNode2D(transform, Vector2(0.0f, 0.0f)), TransformNode2D(transform, Vector2(mapW, 0.0f)), color);
  70. debug->AddLine(TransformNode2D(transform, Vector2(mapW, 0.0f)), TransformNode2D(transform, Vector2(mapW, mapH)), color);
  71. debug->AddLine(TransformNode2D(transform, Vector2(mapW, mapH)), TransformNode2D(transform, Vector2(0.0f, mapH)), color);
  72. debug->AddLine(TransformNode2D(transform, Vector2(0.0f, mapH)), TransformNode2D(transform, Vector2(0.0f, 0.0f)), color);
  73. break;
  74. case O_ISOMETRIC:
  75. debug->AddLine(TransformNode2D(transform, Vector2(0.0f, mapH * 0.5f)), TransformNode2D(transform, Vector2(mapW * 0.5f, 0.0f)), color);
  76. debug->AddLine(TransformNode2D(transform, Vector2(mapW * 0.5f, 0.0f)), TransformNode2D(transform, Vector2(mapW, mapH * 0.5f)), color);
  77. debug->AddLine(TransformNode2D(transform, Vector2(mapW, mapH * 0.5f)), TransformNode2D(transform, Vector2(mapW * 0.5f, mapH)), color);
  78. debug->AddLine(TransformNode2D(transform, Vector2(mapW * 0.5f, mapH)), TransformNode2D(transform, Vector2(0.0f, mapH * 0.5f)), color);
  79. break;
  80. }
  81. for (unsigned i = 0; i < layers_.Size(); ++i)
  82. layers_[i]->DrawDebugGeometry(debug, depthTest);
  83. }
  84. void TileMap2D::DrawDebugGeometry()
  85. {
  86. Scene* scene = GetScene();
  87. if (!scene)
  88. return;
  89. DebugRenderer* debug = scene->GetComponent<DebugRenderer>();
  90. if (!debug)
  91. return;
  92. DrawDebugGeometry(debug, false);
  93. }
  94. void TileMap2D::SetTmxFile(TmxFile2D* tmxFile)
  95. {
  96. if (tmxFile == tmxFile_)
  97. return;
  98. if (rootNode_)
  99. rootNode_->RemoveAllChildren();
  100. layers_.Clear();
  101. tmxFile_ = tmxFile;
  102. if (!tmxFile_)
  103. return;
  104. info_ = tmxFile_->GetInfo();
  105. if (!rootNode_)
  106. {
  107. rootNode_ = GetNode()->CreateTemporaryChild("_root_", LOCAL);
  108. }
  109. unsigned numLayers = tmxFile_->GetNumLayers();
  110. layers_.Resize(numLayers);
  111. for (unsigned i = 0; i < numLayers; ++i)
  112. {
  113. const TmxLayer2D* tmxLayer = tmxFile_->GetLayer(i);
  114. Node* layerNode(rootNode_->CreateTemporaryChild(tmxLayer->GetName(), LOCAL));
  115. TileMapLayer2D* layer = layerNode->CreateComponent<TileMapLayer2D>();
  116. layer->Initialize(this, tmxLayer);
  117. layer->SetDrawOrder(i * 10);
  118. layers_[i] = layer;
  119. }
  120. }
  121. TmxFile2D* TileMap2D::GetTmxFile() const
  122. {
  123. return tmxFile_;
  124. }
  125. TileMapLayer2D* TileMap2D::GetLayer(unsigned index) const
  126. {
  127. if (index >= layers_.Size())
  128. return 0;
  129. return layers_[index];
  130. }
  131. Vector2 TileMap2D::TileIndexToPosition(int x, int y) const
  132. {
  133. return info_.TileIndexToPosition(x, y);
  134. }
  135. bool TileMap2D::PositionToTileIndex(int& x, int& y, const Vector2& position) const
  136. {
  137. return info_.PositionToTileIndex(x, y, position);
  138. }
  139. void TileMap2D::SetTmxFileAttr(const ResourceRef& value)
  140. {
  141. ResourceCache* cache = GetSubsystem<ResourceCache>();
  142. SetTmxFile(cache->GetResource<TmxFile2D>(value.name_));
  143. }
  144. ResourceRef TileMap2D::GetTmxFileAttr() const
  145. {
  146. return GetResourceRef(tmxFile_, TmxFile2D::GetTypeStatic());
  147. }
  148. Vector<SharedPtr<TileMapObject2D> > TileMap2D::GetTileCollisionShapes(int gid) const
  149. {
  150. Vector<SharedPtr<TileMapObject2D> > shapes;
  151. return tmxFile_ ? tmxFile_->GetTileCollisionShapes(gid) : shapes;
  152. }
  153. // ATOMIC BEGIN
  154. TileMapLayer2D* TileMap2D::GetLayerByName(const String& name) const
  155. {
  156. for (unsigned i = 0; i < layers_.Size(); i++)
  157. {
  158. if (layers_[i]->GetName() == name)
  159. return layers_[i];
  160. }
  161. return 0;
  162. }
  163. // ATOMIC END
  164. }