Browse Source

Add other body nodeID attribute to Constraint2D for proper body connection serialization. Closes #1825.

Lasse Öörni 8 years ago
parent
commit
e1202b725a
2 changed files with 39 additions and 2 deletions
  1. 30 1
      Source/Urho3D/Urho2D/Constraint2D.cpp
  2. 9 1
      Source/Urho3D/Urho2D/Constraint2D.h

+ 30 - 1
Source/Urho3D/Urho2D/Constraint2D.cpp

@@ -41,7 +41,8 @@ extern const char* URHO2D_CATEGORY;
 Constraint2D::Constraint2D(Context* context) :
 Constraint2D::Constraint2D(Context* context) :
     Component(context),
     Component(context),
     joint_(0),
     joint_(0),
-    collideConnected_(false)
+    collideConnected_(false),
+    otherBodyNodeIDDirty_(false)
 {
 {
 
 
 }
 }
@@ -54,6 +55,31 @@ Constraint2D::~Constraint2D()
 void Constraint2D::RegisterObject(Context* context)
 void Constraint2D::RegisterObject(Context* context)
 {
 {
     URHO3D_ACCESSOR_ATTRIBUTE("Collide Connected", GetCollideConnected, SetCollideConnected, bool, false, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Collide Connected", GetCollideConnected, SetCollideConnected, bool, false, AM_DEFAULT);
+    URHO3D_ATTRIBUTE("Other Body NodeID", unsigned, otherBodyNodeID_, 0, AM_DEFAULT | AM_NODEID);
+}
+
+void Constraint2D::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
+{
+    Serializable::OnSetAttribute(attr, src);
+
+    if (!attr.accessor_ && attr.offset_ == offsetof(Constraint2D, otherBodyNodeID_))
+        otherBodyNodeIDDirty_ = true;
+}
+
+void Constraint2D::ApplyAttributes()
+{
+    // If other body node ID dirty, try to find it now and apply
+    if (otherBodyNodeIDDirty_)
+    {
+        Scene* scene = GetScene();
+        if (scene)
+        {
+            Node* otherNode = scene->GetNode(otherBodyNodeID_);
+            if (otherNode)
+                SetOtherBody(otherNode->GetComponent<RigidBody2D>());
+        }
+        otherBodyNodeIDDirty_ = false;
+    }
 }
 }
 
 
 void Constraint2D::OnSetEnabled()
 void Constraint2D::OnSetEnabled()
@@ -107,6 +133,9 @@ void Constraint2D::SetOtherBody(RigidBody2D* body)
 
 
     otherBody_ = body;
     otherBody_ = body;
 
 
+    Node* otherNode = body ? body->GetNode() : (Node*)0;
+    otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
+
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }

+ 9 - 1
Source/Urho3D/Urho2D/Constraint2D.h

@@ -45,6 +45,10 @@ public:
     /// Register object factory.
     /// Register object factory.
     static void RegisterObject(Context* context);
     static void RegisterObject(Context* context);
 
 
+    /// Handle attribute write access.
+    virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
+    /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
+    virtual void ApplyAttributes();
     /// Handle enabled/disabled state change.
     /// Handle enabled/disabled state change.
     virtual void OnSetEnabled();
     virtual void OnSetEnabled();
     /// Create joint.
     /// Create joint.
@@ -94,8 +98,12 @@ protected:
     WeakPtr<RigidBody2D> ownerBody_;
     WeakPtr<RigidBody2D> ownerBody_;
     /// Other body.
     /// Other body.
     WeakPtr<RigidBody2D> otherBody_;
     WeakPtr<RigidBody2D> otherBody_;
-    /// Collide connected.
+    /// Other body node ID for serialization.
+    unsigned otherBodyNodeID_;
+    /// Collide connected flag.
     bool collideConnected_;
     bool collideConnected_;
+    /// Other body node ID dirty flag.
+    bool otherBodyNodeIDDirty_;
     /// Attached constraint.
     /// Attached constraint.
     WeakPtr<Constraint2D> attachedConstraint_;
     WeakPtr<Constraint2D> attachedConstraint_;
 };
 };