diff --git a/Source/Engine/Urho2D/TmxFile2D.cpp b/Source/Engine/Urho2D/TmxFile2D.cpp index bee7d7eb5..d78357c99 100644 --- a/Source/Engine/Urho2D/TmxFile2D.cpp +++ b/Source/Engine/Urho2D/TmxFile2D.cpp @@ -198,7 +198,7 @@ bool TmxTileLayer2D::Load(const XMLElement& element) } XMLElement tileElem = dataElem.GetChild("tile"); - tileGids_.Resize(width_ * height_); + tiles_.Resize(width_ * height_); // Flip y for (int y = height_ - 1; y >= 0; --y) @@ -215,7 +215,7 @@ bool TmxTileLayer2D::Load(const XMLElement& element) tile->gid_ = gid; tile->sprite_ = tmxFile_->GetTileSprite(gid); tile->properties_ = tmxFile_->GetTileProperties(gid); - tileGids_[y * width_ + x] = tile; + tiles_[y * width_ + x] = tile; } tileElem = tileElem.GetNext("tile"); @@ -233,7 +233,7 @@ Tile2D* TmxTileLayer2D::GetTile(int x, int y) const if (x < 0 || x > width_ || y < 0 || y > height_) return 0; - return tileGids_[y * width_ + x]; + return tiles_[y * width_ + x]; } @@ -331,7 +331,8 @@ bool TmxImageLayer2D::Load(const XMLElement& element) if (!imageElem) return false; - String textureFilePath = GetParentPath(GetName()) + imageElem.GetAttribute("source"); + source_ = imageElem.GetAttribute("source"); + String textureFilePath = GetParentPath(GetName()) + source_; ResourceCache* cache = tmxFile_->GetSubsystem(); SharedPtr texture(cache->GetResource(textureFilePath)); if (!texture) @@ -450,11 +451,27 @@ bool TmxFile2D::EndLoad() if (name == "tileset") ret = LoadTileSet(childElement); else if (name == "layer") - ret = LoadLayer(childElement); + { + TmxTileLayer2D* tileLayer = new TmxTileLayer2D(this); + ret = tileLayer->Load(childElement); + + layers_.Push(tileLayer); + } else if (name == "objectgroup") - ret = LoadObjectGroup(childElement); + { + TmxObjectGroup2D* objectGroup = new TmxObjectGroup2D(this); + ret = objectGroup->Load(childElement); + + layers_.Push(objectGroup); + + } else if (name == "imagelayer") - ret = LoadImageLayer(childElement); + { + TmxImageLayer2D* imageLayer = new TmxImageLayer2D(this); + ret = imageLayer->Load(childElement); + + layers_.Push(imageLayer); + } if (!ret) { @@ -492,17 +509,6 @@ const TmxLayer2D* TmxFile2D::GetLayer(unsigned index) const return layers_[index]; } -const TmxLayer2D* TmxFile2D::GetLayerByName(const String& name) const -{ - for (unsigned i = 0; i < layers_.Size(); ++i) - { - if (name == layers_[i]->GetName()) - return layers_[i]; - } - - return 0; -} - bool TmxFile2D::LoadTileSet(const XMLElement& element) { XMLElement imageElem = element.GetChild("image"); @@ -553,28 +559,4 @@ bool TmxFile2D::LoadTileSet(const XMLElement& element) return true; } - -bool TmxFile2D::LoadLayer(const XMLElement& element) -{ - TmxTileLayer2D* tileLayer = new TmxTileLayer2D(this); - layers_.Push(tileLayer); - - return tileLayer->Load(element); -} - -bool TmxFile2D::LoadObjectGroup(const XMLElement& element) -{ - TmxObjectGroup2D* objectGroup = new TmxObjectGroup2D(this); - layers_.Push(objectGroup); - - return objectGroup->Load(element); -} - -bool TmxFile2D::LoadImageLayer(const XMLElement& element) -{ - TmxImageLayer2D* imageLayer = new TmxImageLayer2D(this); - layers_.Push(imageLayer); - - return imageLayer->Load(element); -} } diff --git a/Source/Engine/Urho2D/TmxFile2D.h b/Source/Engine/Urho2D/TmxFile2D.h index 2e859587e..e742e820f 100644 --- a/Source/Engine/Urho2D/TmxFile2D.h +++ b/Source/Engine/Urho2D/TmxFile2D.h @@ -50,11 +50,11 @@ public: const String& GetProperty(const String& name) const; protected: - /// Properties. + /// Property name to value mapping. HashMap properties_; }; -/// Tile. +/// Tile define. class URHO3D_API Tile2D : public RefCounted { public: @@ -101,7 +101,8 @@ enum TileObjectType2D class URHO3D_API TileObject2D : public RefCounted { public: - TileObject2D();; + TileObject2D(); + /// Return type. TileObjectType2D GetType() const { return type_; } /// Return position. @@ -205,13 +206,14 @@ class TmxTileLayer2D : public TmxLayer2D public: TmxTileLayer2D(TmxFile2D* tmxFile); - /// Load from xml element. + /// Load from XML element. bool Load(const XMLElement& element); /// Return tile. Tile2D* GetTile(int x, int y) const; + protected: /// Tile. - Vector > tileGids_; + Vector > tiles_; }; /// Tmx image layer. @@ -240,10 +242,14 @@ public: /// Load from XML element. bool Load(const XMLElement& element); + /// Return source. + const String& GetSource() const { return source_; } /// Return sprite. Sprite2D* GetSprite() const; private: + /// Source. + String source_; /// Sprite. SharedPtr sprite_; }; @@ -266,35 +272,27 @@ public: /// Finish resource loading. Always called from the main thread. Return true if successful. virtual bool EndLoad(); - /// Return width. + /// Return width in tiles. int GetWidth() const { return width_; } - /// Return height. + /// Return height in tiles. int GetHeight() const { return height_; } /// Return tile width. float GetTileWidth() const { return tileWidth_; } /// Return tile height. float GetTileHeight() const { return tileHeight_; } - /// Return tile sprite by gid. + /// Return tile sprite by gid, if not exist return 0. Sprite2D* GetTileSprite(int gid) const; - /// Return tile properties by gid. + /// Return tile properties by gid, if not exist return 0. Properties2D* GetTileProperties(int gid) const; /// Return number of layers. unsigned GetNumLayers() const { return layers_.Size(); } /// Return layer at index. const TmxLayer2D* GetLayer(unsigned index) const; - /// Return layer by name. - const TmxLayer2D* GetLayerByName(const String& name) const; private: /// Load tile set. bool LoadTileSet(const XMLElement& element); - /// Load layer. - bool LoadLayer(const XMLElement& element); - /// Load object group. - bool LoadObjectGroup(const XMLElement& element); - /// Load image layer. - bool LoadImageLayer(const XMLElement& element); - + /// XML file used during loading. SharedPtr loadXMLFile_; /// Width.