From b87c4f24fe7739bd500ec5dc089033ef51b6842e Mon Sep 17 00:00:00 2001 From: aster Date: Thu, 7 Aug 2014 22:23:38 +0800 Subject: [PATCH] Move some functions from header file to source file. For all of objects create node for it. --- Source/Engine/Urho2D/TileMap2D.cpp | 9 ++-- Source/Engine/Urho2D/TileMap2D.h | 2 +- Source/Engine/Urho2D/TileMapLayer2D.cpp | 71 ++++++++++++++++++------- Source/Engine/Urho2D/TileMapLayer2D.h | 6 +++ Source/Engine/Urho2D/TmxFile2D.cpp | 39 ++++++++++++++ Source/Engine/Urho2D/TmxFile2D.h | 25 +++------ 6 files changed, 108 insertions(+), 44 deletions(-) diff --git a/Source/Engine/Urho2D/TileMap2D.cpp b/Source/Engine/Urho2D/TileMap2D.cpp index 4934f4b36..7d7044358 100644 --- a/Source/Engine/Urho2D/TileMap2D.cpp +++ b/Source/Engine/Urho2D/TileMap2D.cpp @@ -60,10 +60,7 @@ void TileMap2D::SetTmxFile(TmxFile2D* tmxFile) if (tmxFile_) { for (unsigned i = 0; i < layers_.Size(); ++i) - { - if (layers_[i]) - layers_[i]->GetNode()->Remove(); - } + layers_[i]->GetNode()->Remove(); layers_.Clear(); } @@ -124,10 +121,10 @@ TileMapLayer2D* TileMap2D::GetLayer(unsigned index) const return layers_[index]; } -TileMapLayer2D* TileMap2D::GetLayer(const String& name) const +TileMapLayer2D* TileMap2D::GetLayerByName(const String& name) const { for (unsigned i = 0; i < layers_.Size(); ++i) - if (layers_[i] && name == layers_[i]->GetName()) + if (name == layers_[i]->GetName()) return layers_[i]; return 0; diff --git a/Source/Engine/Urho2D/TileMap2D.h b/Source/Engine/Urho2D/TileMap2D.h index f344c714d..fd894cc04 100644 --- a/Source/Engine/Urho2D/TileMap2D.h +++ b/Source/Engine/Urho2D/TileMap2D.h @@ -61,7 +61,7 @@ public: /// Return tile map layer at index. TileMapLayer2D* GetLayer(unsigned index) const; /// Return tile map layer by name. - TileMapLayer2D* GetLayer(const String& name) const; + TileMapLayer2D* GetLayerByName(const String& name) const; /// Set tile map file attribute. void SetTmxFileAttr(ResourceRef value); diff --git a/Source/Engine/Urho2D/TileMapLayer2D.cpp b/Source/Engine/Urho2D/TileMapLayer2D.cpp index 8c8a0dd12..09b6457ee 100644 --- a/Source/Engine/Urho2D/TileMapLayer2D.cpp +++ b/Source/Engine/Urho2D/TileMapLayer2D.cpp @@ -91,8 +91,12 @@ void TileMapLayer2D::SetDrawOrder(int drawOrder) for (unsigned i = 0; i < nodes_.Size(); ++i) { - if (nodes_[i]) - nodes_[i]->GetComponent()->SetLayer(drawOrder_); + if (!nodes_[i]) + continue; + + StaticSprite2D* staticSprite = nodes_[i]->GetComponent(); + if (staticSprite) + staticSprite->SetLayer(drawOrder_); } } @@ -142,6 +146,34 @@ Node* TileMapLayer2D::GetTileNode(int x, int y) const return nodes_[y * tileLayer->width_ + x]; } +unsigned TileMapLayer2D::GetNumObjects() const +{ + if (!tmxLayer_ || tmxLayer_->type_ != LT_OBJECT_GROUP) + return 0; + + return nodes_.Size(); +} + + +const TmxObject* TileMapLayer2D::GetObject(unsigned index) const +{ + if (!tmxLayer_ || tmxLayer_->type_ != LT_OBJECT_GROUP) + return 0; + + return (const TmxObject*)nodes_[index]->GetVar("ObjectData").GetVoidPtr(); +} + +Node* TileMapLayer2D::GetObjectNode(unsigned index) const +{ + if (!tmxLayer_ || tmxLayer_->type_ != LT_OBJECT_GROUP) + return 0; + + if (index >= nodes_.Size()) + return 0; + + return nodes_[index]; +} + Node* TileMapLayer2D::GetImageNode() const { if (!tmxLayer_ || tmxLayer_->type_ != LT_IMAGE_LAYER) @@ -197,26 +229,27 @@ void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup) for (unsigned i = 0; i < objectGroup->objects_.Size(); ++i) { const TmxObject& object = objectGroup->objects_[i]; - if (object.type_ != OT_TILE) - continue; - if (object.gid_ <= 0) - continue; + SharedPtr objectNode(GetNode()->CreateChild("Object")); + objectNode->SetTemporary(true); + objectNode->SetPosition(Vector3(object.x_, object.y_, 0.0f)); - Sprite2D* sprite = tmxFile->GetTileSprite(object.gid_); - if (!sprite) - continue; + // Set object data + objectNode->SetVar("ObjectData", (void*)&object); - SharedPtr tileNode(GetNode()->CreateChild("Tile")); - tileNode->SetTemporary(true); - tileNode->SetPosition(Vector3(object.x_, object.y_, 0.0f)); - - StaticSprite2D* staticSprite = tileNode->CreateComponent(); - staticSprite->SetSprite(sprite); - staticSprite->SetLayer(drawOrder_); - staticSprite->SetOrderInLayer((int)((10.0f - object.y_) * 100.0f)); - - nodes_.Push(tileNode); + if (object.type_ == OT_TILE) + { + Sprite2D* sprite = tmxFile->GetTileSprite(object.gid_); + if (sprite) + { + StaticSprite2D* staticSprite = objectNode->CreateComponent(); + staticSprite->SetSprite(sprite); + staticSprite->SetLayer(drawOrder_); + staticSprite->SetOrderInLayer((int)((10.0f - object.y_) * 100.0f)); + } + } + + nodes_.Push(objectNode); } } diff --git a/Source/Engine/Urho2D/TileMapLayer2D.h b/Source/Engine/Urho2D/TileMapLayer2D.h index 6488c8897..d066c4ec1 100644 --- a/Source/Engine/Urho2D/TileMapLayer2D.h +++ b/Source/Engine/Urho2D/TileMapLayer2D.h @@ -68,6 +68,12 @@ public: /// Return tile node (for tile layer only). Node* GetTileNode(int x, int y) const; + /// Return number of tile nodes (for object group only). + unsigned GetNumObjects() const; + /// Return object. + const TmxObject* GetObject(unsigned index) const; + /// Return tile node (for object group only). + Node* GetObjectNode(unsigned index) const; /// Return image node (for image layer only). Node* GetImageNode() const; diff --git a/Source/Engine/Urho2D/TmxFile2D.cpp b/Source/Engine/Urho2D/TmxFile2D.cpp index 1fc69442f..d40fcbc9e 100644 --- a/Source/Engine/Urho2D/TmxFile2D.cpp +++ b/Source/Engine/Urho2D/TmxFile2D.cpp @@ -38,6 +38,33 @@ namespace Urho3D extern const float PIXEL_SIZE; +TmxLayer2D::TmxLayer2D(TmxFile2D* tmxFile, TmxLayerType2D type) : + tmxFile_(tmxFile), + type_(type) +{ + +} + +TmxLayer2D::~TmxLayer2D() +{ +} + +TmxTileLayer2D::TmxTileLayer2D(TmxFile2D* tmxFile) : + TmxLayer2D(tmxFile, LT_TILE_LAYER) +{ +} + +TmxObjectGroup2D::TmxObjectGroup2D(TmxFile2D* tmxFile) : + TmxLayer2D(tmxFile, LT_OBJECT_GROUP) +{ +} + + +TmxImageLayer2D::TmxImageLayer2D(TmxFile2D* tmxFile) : + TmxLayer2D(tmxFile, LT_IMAGE_LAYER) +{ +} + TmxFile2D::TmxFile2D(Context* context) : Resource(context), width_(0), @@ -156,6 +183,17 @@ 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]->name_) + return layers_[i]; + } + + return 0; +} + Sprite2D* TmxFile2D::GetTileSprite(int gid) const { HashMap >::ConstIterator i = tileSprites_.Find(gid); @@ -385,4 +423,5 @@ void TmxFile2D::LoadProperties(const XMLElement& element, HashMap tmxFile_; @@ -80,9 +73,7 @@ struct URHO3D_API TmxLayer2D /// Tmx tile layer. struct URHO3D_API TmxTileLayer2D : TmxLayer2D { - TmxTileLayer2D(TmxFile2D* tmxFile) : TmxLayer2D(tmxFile, LT_TILE_LAYER) - { - } + TmxTileLayer2D(TmxFile2D* tmxFile); PODVector tileGids_; }; @@ -122,9 +113,7 @@ struct URHO3D_API TmxObject /// Tmx image layer. struct URHO3D_API TmxObjectGroup2D : TmxLayer2D { - TmxObjectGroup2D(TmxFile2D* tmxFile) : TmxLayer2D(tmxFile, LT_OBJECT_GROUP) - { - } + TmxObjectGroup2D(TmxFile2D* tmxFile); /// Objects. Vector objects_; @@ -133,9 +122,7 @@ struct URHO3D_API TmxObjectGroup2D : TmxLayer2D /// Tmx image layer. struct URHO3D_API TmxImageLayer2D : TmxLayer2D { - TmxImageLayer2D(TmxFile2D* tmxFile) : TmxLayer2D(tmxFile, LT_IMAGE_LAYER) - { - } + TmxImageLayer2D(TmxFile2D* tmxFile); /// Sprite. SharedPtr sprite_; @@ -171,6 +158,8 @@ public: 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; /// Return tile sprite by gid. Sprite2D* GetTileSprite(int gid) const; /// Return tile properties by gid.