// // Copyright (c) 2008-2014 the Urho3D project. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #include "Precompiled.h" #include "Context.h" #include "Node.h" #include "ResourceCache.h" #include "TileMap2D.h" #include "TileMapLayer2D.h" #include "TmxFile2D.h" #include "DebugNew.h" namespace Urho3D { extern const float PIXEL_SIZE; extern const char* URHO2D_CATEGORY; TileMap2D::TileMap2D(Context* context) : Component(context) { } TileMap2D::~TileMap2D() { } void TileMap2D::RegisterObject(Context* context) { context->RegisterFactory(URHO2D_CATEGORY); ACCESSOR_ATTRIBUTE(TileMap2D, VAR_RESOURCEREF, "Tmx File", GetTmxFileAttr, SetTmxFileAttr, ResourceRef, ResourceRef(TmxFile2D::GetTypeStatic()), AM_DEFAULT); } void TileMap2D::SetTmxFile(TmxFile2D* tmxFile) { if (tmxFile == tmxFile_) return; if (tmxFile_) { for (unsigned i = 0; i < layers_.Size(); ++i) layers_[i]->GetNode()->Remove(); layers_.Clear(); } tmxFile_ = tmxFile; if (!tmxFile_) return; unsigned numLayers = tmxFile_->GetNumLayers(); for (unsigned i = 0; i < numLayers; ++i) { const TmxLayer2D* tmxLayer = tmxFile_->GetLayer(i); SharedPtr layerNode(GetNode()->CreateChild(tmxLayer->name_, LOCAL)); layerNode->SetTemporary(true); SharedPtr tileMapLayer(layerNode->CreateComponent()); tileMapLayer->SetTmxLayer(tmxLayer, i); layers_.Push(tileMapLayer); } } TmxFile2D* TileMap2D::GetTmxFile() const { return tmxFile_; } int TileMap2D::GetWidth() const { return tmxFile_ ? tmxFile_->GetWidth() : 0; } int TileMap2D::GetHeight() const { return tmxFile_ ? tmxFile_->GetHeight() : 0; } int TileMap2D::GetTileWidth() const { return tmxFile_ ? tmxFile_->GetTileWidth() : 0; } int TileMap2D::GetTileHeight() const { return tmxFile_ ? tmxFile_->GetTileHeight() : 0; } Vector2 TileMap2D::GetSize() const { if (!tmxFile_) return Vector2::ZERO; return Vector2(tmxFile_->GetWidth() * tmxFile_->GetTileWidth() * PIXEL_SIZE, tmxFile_->GetHeight() * tmxFile_->GetTileHeight() * PIXEL_SIZE); } Vector2 TileMap2D::GetTileSize() const { if (!tmxFile_) return Vector2::ZERO; return Vector2(tmxFile_->GetTileWidth() * PIXEL_SIZE, tmxFile_->GetTileHeight() * PIXEL_SIZE); } TileMapLayer2D* TileMap2D::GetLayer(unsigned index) const { if (index >= layers_.Size()) return 0; return layers_[index]; } void TileMap2D::SetTmxFileAttr(ResourceRef value) { ResourceCache* cache = GetSubsystem(); SetTmxFile(cache->GetResource(value.name_)); } ResourceRef TileMap2D::GetTmxFileAttr() const { return GetResourceRef(tmxFile_, TmxFile2D::GetTypeStatic()); } }