瀏覽代碼

Move some functions from header file to source file. For all of objects create node for it.

aster 11 年之前
父節點
當前提交
b87c4f24fe

+ 3 - 6
Source/Engine/Urho2D/TileMap2D.cpp

@@ -60,10 +60,7 @@ void TileMap2D::SetTmxFile(TmxFile2D* tmxFile)
     if (tmxFile_)
     if (tmxFile_)
     {
     {
         for (unsigned i = 0; i < layers_.Size(); ++i)
         for (unsigned i = 0; i < layers_.Size(); ++i)
-        {
-            if (layers_[i])
-                layers_[i]->GetNode()->Remove();
-        }
+            layers_[i]->GetNode()->Remove();
         layers_.Clear();
         layers_.Clear();
     }
     }
 
 
@@ -124,10 +121,10 @@ TileMapLayer2D* TileMap2D::GetLayer(unsigned index) const
     return layers_[index];
     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)
     for (unsigned i = 0; i < layers_.Size(); ++i)
-        if (layers_[i] && name == layers_[i]->GetName())
+        if (name == layers_[i]->GetName())
             return layers_[i];
             return layers_[i];
 
 
     return 0;
     return 0;

+ 1 - 1
Source/Engine/Urho2D/TileMap2D.h

@@ -61,7 +61,7 @@ public:
     /// Return tile map layer at index.
     /// Return tile map layer at index.
     TileMapLayer2D* GetLayer(unsigned index) const;
     TileMapLayer2D* GetLayer(unsigned index) const;
     /// Return tile map layer by name.
     /// Return tile map layer by name.
-    TileMapLayer2D* GetLayer(const String& name) const;
+    TileMapLayer2D* GetLayerByName(const String& name) const;
     
     
     /// Set tile map file attribute.
     /// Set tile map file attribute.
     void SetTmxFileAttr(ResourceRef value);
     void SetTmxFileAttr(ResourceRef value);

+ 52 - 19
Source/Engine/Urho2D/TileMapLayer2D.cpp

@@ -91,8 +91,12 @@ void TileMapLayer2D::SetDrawOrder(int drawOrder)
 
 
     for (unsigned i = 0; i < nodes_.Size(); ++i)
     for (unsigned i = 0; i < nodes_.Size(); ++i)
     {
     {
-        if (nodes_[i])
-            nodes_[i]->GetComponent<StaticSprite2D>()->SetLayer(drawOrder_);
+        if (!nodes_[i])
+            continue;
+
+        StaticSprite2D* staticSprite = nodes_[i]->GetComponent<StaticSprite2D>();
+        if (staticSprite)
+            staticSprite->SetLayer(drawOrder_);
     }
     }
 }
 }
 
 
@@ -142,6 +146,34 @@ Node* TileMapLayer2D::GetTileNode(int x, int y) const
     return nodes_[y * tileLayer->width_ + x];
     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
 Node* TileMapLayer2D::GetImageNode() const
 {
 {
     if (!tmxLayer_ || tmxLayer_->type_ != LT_IMAGE_LAYER)
     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)
     for (unsigned i = 0; i < objectGroup->objects_.Size(); ++i)
     {
     {
         const TmxObject& object = objectGroup->objects_[i];
         const TmxObject& object = objectGroup->objects_[i];
-        if (object.type_ != OT_TILE)
-            continue;
-
-        if (object.gid_ <= 0)
-            continue;
 
 
-        Sprite2D* sprite = tmxFile->GetTileSprite(object.gid_);
-        if (!sprite)
-            continue;
+        SharedPtr<Node> objectNode(GetNode()->CreateChild("Object"));
+        objectNode->SetTemporary(true);
+        objectNode->SetPosition(Vector3(object.x_, object.y_, 0.0f));
 
 
-        SharedPtr<Node> tileNode(GetNode()->CreateChild("Tile"));
-        tileNode->SetTemporary(true);
-        tileNode->SetPosition(Vector3(object.x_, object.y_, 0.0f));
+        // Set object data
+        objectNode->SetVar("ObjectData", (void*)&object);
 
 
-        StaticSprite2D* staticSprite = tileNode->CreateComponent<StaticSprite2D>();
-        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<StaticSprite2D>();
+                staticSprite->SetSprite(sprite);
+                staticSprite->SetLayer(drawOrder_);
+                staticSprite->SetOrderInLayer((int)((10.0f - object.y_) * 100.0f));
+            }
+        }
+        
+        nodes_.Push(objectNode);
     }
     }
 }
 }
 
 

+ 6 - 0
Source/Engine/Urho2D/TileMapLayer2D.h

@@ -68,6 +68,12 @@ public:
 
 
     /// Return tile node (for tile layer only).
     /// Return tile node (for tile layer only).
     Node* GetTileNode(int x, int y) const;
     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).
     /// Return image node (for image layer only).
     Node* GetImageNode() const;
     Node* GetImageNode() const;
 
 

+ 39 - 0
Source/Engine/Urho2D/TmxFile2D.cpp

@@ -38,6 +38,33 @@ namespace Urho3D
 
 
 extern const float PIXEL_SIZE;
 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) :
 TmxFile2D::TmxFile2D(Context* context) :
     Resource(context),
     Resource(context),
     width_(0),
     width_(0),
@@ -156,6 +183,17 @@ const TmxLayer2D* TmxFile2D::GetLayer(unsigned index) const
     return layers_[index];
     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
 Sprite2D* TmxFile2D::GetTileSprite(int gid) const
 {
 {
     HashMap<int, SharedPtr<Sprite2D> >::ConstIterator i = tileSprites_.Find(gid);
     HashMap<int, SharedPtr<Sprite2D> >::ConstIterator i = tileSprites_.Find(gid);
@@ -385,4 +423,5 @@ void TmxFile2D::LoadProperties(const XMLElement& element, HashMap<String, String
         peoperties[propertyElem.GetAttribute("name")] = propertyElem.GetAttribute("value");
         peoperties[propertyElem.GetAttribute("name")] = propertyElem.GetAttribute("value");
 }
 }
 
 
+
 }
 }

+ 7 - 18
Source/Engine/Urho2D/TmxFile2D.h

@@ -51,15 +51,8 @@ enum TmxLayerType2D
 /// Tmx layer.
 /// Tmx layer.
 struct URHO3D_API TmxLayer2D
 struct URHO3D_API TmxLayer2D
 {
 {
-    TmxLayer2D(TmxFile2D* tmxFile, TmxLayerType2D type) :
-        tmxFile_(tmxFile),
-        type_(type)
-    {
-    }
-    
-    virtual ~TmxLayer2D()
-    {
-    }
+    TmxLayer2D(TmxFile2D* tmxFile, TmxLayerType2D type);
+    virtual ~TmxLayer2D();
 
 
     /// Tmx file.
     /// Tmx file.
     WeakPtr<TmxFile2D> tmxFile_;
     WeakPtr<TmxFile2D> tmxFile_;
@@ -80,9 +73,7 @@ struct URHO3D_API TmxLayer2D
 /// Tmx tile layer.
 /// Tmx tile layer.
 struct URHO3D_API TmxTileLayer2D : TmxLayer2D
 struct URHO3D_API TmxTileLayer2D : TmxLayer2D
 {
 {
-    TmxTileLayer2D(TmxFile2D* tmxFile) : TmxLayer2D(tmxFile, LT_TILE_LAYER)
-    {
-    }
+    TmxTileLayer2D(TmxFile2D* tmxFile);
 
 
     PODVector<int> tileGids_;
     PODVector<int> tileGids_;
 };
 };
@@ -122,9 +113,7 @@ struct URHO3D_API TmxObject
 /// Tmx image layer.
 /// Tmx image layer.
 struct URHO3D_API TmxObjectGroup2D : TmxLayer2D
 struct URHO3D_API TmxObjectGroup2D : TmxLayer2D
 {
 {
-    TmxObjectGroup2D(TmxFile2D* tmxFile) : TmxLayer2D(tmxFile, LT_OBJECT_GROUP)
-    {
-    }
+    TmxObjectGroup2D(TmxFile2D* tmxFile);
 
 
     /// Objects.
     /// Objects.
     Vector<TmxObject> objects_;
     Vector<TmxObject> objects_;
@@ -133,9 +122,7 @@ struct URHO3D_API TmxObjectGroup2D : TmxLayer2D
 /// Tmx image layer.
 /// Tmx image layer.
 struct URHO3D_API TmxImageLayer2D : TmxLayer2D
 struct URHO3D_API TmxImageLayer2D : TmxLayer2D
 {
 {
-    TmxImageLayer2D(TmxFile2D* tmxFile) : TmxLayer2D(tmxFile, LT_IMAGE_LAYER)
-    {
-    }
+    TmxImageLayer2D(TmxFile2D* tmxFile);
 
 
     /// Sprite.
     /// Sprite.
     SharedPtr<Sprite2D> sprite_;
     SharedPtr<Sprite2D> sprite_;
@@ -171,6 +158,8 @@ public:
     unsigned GetNumLayers() const { return layers_.Size(); }
     unsigned GetNumLayers() const { return layers_.Size(); }
     /// Return layer at index.
     /// Return layer at index.
     const TmxLayer2D* GetLayer(unsigned index) const;
     const TmxLayer2D* GetLayer(unsigned index) const;
+    /// Return layer by name.
+    const TmxLayer2D* GetLayerByName(const String& name) const;
     /// Return tile sprite by gid.
     /// Return tile sprite by gid.
     Sprite2D* GetTileSprite(int gid) const;
     Sprite2D* GetTileSprite(int gid) const;
     /// Return tile properties by gid.
     /// Return tile properties by gid.