TileMap2D.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "Node.h"
  25. #include "ResourceCache.h"
  26. #include "TileMap2D.h"
  27. #include "TileMapLayer2D.h"
  28. #include "TmxFile2D.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. extern const float PIXEL_SIZE;
  33. extern const char* URHO2D_CATEGORY;
  34. TileMap2D::TileMap2D(Context* context) :
  35. Component(context)
  36. {
  37. }
  38. TileMap2D::~TileMap2D()
  39. {
  40. }
  41. void TileMap2D::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<TileMap2D>(URHO2D_CATEGORY);
  44. ACCESSOR_ATTRIBUTE(TileMap2D, VAR_RESOURCEREF, "Tmx File", GetTmxFileAttr, SetTmxFileAttr, ResourceRef, ResourceRef(TmxFile2D::GetTypeStatic()), AM_DEFAULT);
  45. }
  46. void TileMap2D::SetTmxFile(TmxFile2D* tmxFile)
  47. {
  48. if (tmxFile == tmxFile_)
  49. return;
  50. if (tmxFile_)
  51. {
  52. for (unsigned i = 0; i < layers_.Size(); ++i)
  53. layers_[i]->GetNode()->Remove();
  54. layers_.Clear();
  55. }
  56. tmxFile_ = tmxFile;
  57. if (!tmxFile_)
  58. return;
  59. unsigned numLayers = tmxFile_->GetNumLayers();
  60. for (unsigned i = 0; i < numLayers; ++i)
  61. {
  62. const TmxLayer2D* tmxLayer = tmxFile_->GetLayer(i);
  63. SharedPtr<Node> layerNode(GetNode()->CreateChild(tmxLayer->name_, LOCAL));
  64. layerNode->SetTemporary(true);
  65. SharedPtr<TileMapLayer2D> tileMapLayer(layerNode->CreateComponent<TileMapLayer2D>());
  66. tileMapLayer->SetTmxLayer(tmxLayer, i);
  67. layers_.Push(tileMapLayer);
  68. }
  69. }
  70. TmxFile2D* TileMap2D::GetTmxFile() const
  71. {
  72. return tmxFile_;
  73. }
  74. int TileMap2D::GetWidth() const
  75. {
  76. return tmxFile_ ? tmxFile_->GetWidth() : 0;
  77. }
  78. int TileMap2D::GetHeight() const
  79. {
  80. return tmxFile_ ? tmxFile_->GetHeight() : 0;
  81. }
  82. int TileMap2D::GetTileWidth() const
  83. {
  84. return tmxFile_ ? tmxFile_->GetTileWidth() : 0;
  85. }
  86. int TileMap2D::GetTileHeight() const
  87. {
  88. return tmxFile_ ? tmxFile_->GetTileHeight() : 0;
  89. }
  90. Vector2 TileMap2D::GetSize() const
  91. {
  92. if (!tmxFile_)
  93. return Vector2::ZERO;
  94. return Vector2(tmxFile_->GetWidth() * tmxFile_->GetTileWidth() * PIXEL_SIZE,
  95. tmxFile_->GetHeight() * tmxFile_->GetTileHeight() * PIXEL_SIZE);
  96. }
  97. Vector2 TileMap2D::GetTileSize() const
  98. {
  99. if (!tmxFile_)
  100. return Vector2::ZERO;
  101. return Vector2(tmxFile_->GetTileWidth() * PIXEL_SIZE, tmxFile_->GetTileHeight() * PIXEL_SIZE);
  102. }
  103. TileMapLayer2D* TileMap2D::GetLayer(unsigned index) const
  104. {
  105. if (index >= layers_.Size())
  106. return 0;
  107. return layers_[index];
  108. }
  109. void TileMap2D::SetTmxFileAttr(ResourceRef value)
  110. {
  111. ResourceCache* cache = GetSubsystem<ResourceCache>();
  112. SetTmxFile(cache->GetResource<TmxFile2D>(value.name_));
  113. }
  114. ResourceRef TileMap2D::GetTmxFileAttr() const
  115. {
  116. return GetResourceRef(tmxFile_, TmxFile2D::GetTypeStatic());
  117. }
  118. }