Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
Add other body nodeID attribute to Constraint2D for proper body conne…
Browse files Browse the repository at this point in the history
…ction serialization. Closes #1825.
  • Loading branch information
cadaver committed Feb 25, 2017
1 parent 5ffb40b commit e1202b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
31 changes: 30 additions & 1 deletion Source/Urho3D/Urho2D/Constraint2D.cpp
Expand Up @@ -41,7 +41,8 @@ extern const char* URHO2D_CATEGORY;
Constraint2D::Constraint2D(Context* context) :
Component(context),
joint_(0),
collideConnected_(false)
collideConnected_(false),
otherBodyNodeIDDirty_(false)
{

}
Expand All @@ -54,6 +55,31 @@ Constraint2D::~Constraint2D()
void Constraint2D::RegisterObject(Context* context)
{
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()
Expand Down Expand Up @@ -107,6 +133,9 @@ void Constraint2D::SetOtherBody(RigidBody2D* body)

otherBody_ = body;

Node* otherNode = body ? body->GetNode() : (Node*)0;
otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;

RecreateJoint();
MarkNetworkUpdate();
}
Expand Down
10 changes: 9 additions & 1 deletion Source/Urho3D/Urho2D/Constraint2D.h
Expand Up @@ -45,6 +45,10 @@ class URHO3D_API Constraint2D : public Component
/// Register object factory.
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.
virtual void OnSetEnabled();
/// Create joint.
Expand Down Expand Up @@ -94,8 +98,12 @@ class URHO3D_API Constraint2D : public Component
WeakPtr<RigidBody2D> ownerBody_;
/// Other body.
WeakPtr<RigidBody2D> otherBody_;
/// Collide connected.
/// Other body node ID for serialization.
unsigned otherBodyNodeID_;
/// Collide connected flag.
bool collideConnected_;
/// Other body node ID dirty flag.
bool otherBodyNodeIDDirty_;
/// Attached constraint.
WeakPtr<Constraint2D> attachedConstraint_;
};
Expand Down

0 comments on commit e1202b7

Please sign in to comment.