TileMap2D.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. {
  54. if (layers_[i])
  55. layers_[i]->GetNode()->Remove();
  56. }
  57. layers_.Clear();
  58. }
  59. tmxFile_ = tmxFile;
  60. if (!tmxFile_)
  61. return;
  62. unsigned numLayers = tmxFile_->GetNumLayers();
  63. layers_.Resize(numLayers);
  64. for (unsigned i = 0; i < numLayers; ++i)
  65. {
  66. const TmxLayer2D* tmxLayer = tmxFile_->GetLayer(i);
  67. SharedPtr<Node> layerNode(GetNode()->CreateChild(tmxLayer->name_, LOCAL));
  68. layerNode->SetTemporary(true);
  69. SharedPtr<TileMapLayer2D> layer(layerNode->CreateComponent<TileMapLayer2D>());
  70. layer->SetTmxLayer(tmxLayer);
  71. layer->SetDrawOrder(i);
  72. layers_[i] = layer;
  73. }
  74. }
  75. TmxFile2D* TileMap2D::GetTmxFile() const
  76. {
  77. return tmxFile_;
  78. }
  79. int TileMap2D::GetWidth() const
  80. {
  81. return tmxFile_ ? tmxFile_->GetWidth() : 0;
  82. }
  83. int TileMap2D::GetHeight() const
  84. {
  85. return tmxFile_ ? tmxFile_->GetHeight() : 0;
  86. }
  87. float TileMap2D::GetTileWidth() const
  88. {
  89. return tmxFile_ ? tmxFile_->GetTileWidth() : 0.0f;
  90. }
  91. float TileMap2D::GetTileHeight() const
  92. {
  93. return tmxFile_ ? tmxFile_->GetTileHeight() : 0.0f;
  94. }
  95. TileMapLayer2D* TileMap2D::GetLayer(unsigned index) const
  96. {
  97. if (index >= layers_.Size())
  98. return 0;
  99. return layers_[index];
  100. }
  101. TileMapLayer2D* TileMap2D::GetLayer(const String& name) const
  102. {
  103. for (unsigned i = 0; i < layers_.Size(); ++i)
  104. if (layers_[i] && name == layers_[i]->GetName())
  105. return layers_[i];
  106. return 0;
  107. }
  108. void TileMap2D::SetTmxFileAttr(ResourceRef value)
  109. {
  110. ResourceCache* cache = GetSubsystem<ResourceCache>();
  111. SetTmxFile(cache->GetResource<TmxFile2D>(value.name_));
  112. }
  113. ResourceRef TileMap2D::GetTmxFileAttr() const
  114. {
  115. return GetResourceRef(tmxFile_, TmxFile2D::GetTypeStatic());
  116. }
  117. }