diff --git a/ChangeLog.md b/ChangeLog.md index c378f942c..b2f87f16a 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -200,6 +200,7 @@ Nazara Engine: - Add Flags::Clear(Flags) helper method, to clear one or more flags. - Add Flags::Clear() helper method, to reset flags - Add Flags::Set(Flags) helper method, to enable flags +- ⚠ Constraint2D are no longer managed by references and are now handled objects Nazara Development Kit: - Added ImageWidget (#139) @@ -297,6 +298,7 @@ Nazara Development Kit: - Added World::CloneEntity overload taking an EntityHandle const reference, allowing to copy entities from other worlds - Fixed PhysicsComponent2D copy not copying physics attributes - Added Entity::DropComponent which detaches a component without necessarily destroying it +- ⚠ ConstraintComponent2D has been reworked to handle entity destruction and remove constraints at will # 0.4: diff --git a/SDK/include/NDK/Components/ConstraintComponent2D.hpp b/SDK/include/NDK/Components/ConstraintComponent2D.hpp index 23fcfe4c8..652559481 100644 --- a/SDK/include/NDK/Components/ConstraintComponent2D.hpp +++ b/SDK/include/NDK/Components/ConstraintComponent2D.hpp @@ -1,13 +1,18 @@ +// Copyright (C) 2019 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequisites.hpp + #pragma once #ifndef NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP #define NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP -#include #include #include -#include +#include +#include #include +#include namespace Ndk { @@ -19,16 +24,24 @@ namespace Ndk { public: ConstraintComponent2D() = default; - ConstraintComponent2D(const ConstraintComponent2D& joint) = default; + ConstraintComponent2D(const ConstraintComponent2D& joint); ConstraintComponent2D(ConstraintComponent2D&& joint) = default; - template inline Nz::ObjectRef CreateConstraint(const Ndk::EntityHandle& first, const Ndk::EntityHandle& second, Args&&... args); - + template T* CreateConstraint(const Ndk::EntityHandle& first, const Ndk::EntityHandle& second, Args&&... args); + bool RemoveConstraint(Nz::Constraint2D* constraint); + static ComponentIndex componentIndex; private: + struct ConstraintData + { + std::unique_ptr constraint; - std::vector m_constraints; + NazaraSlot(Ndk::Entity, OnEntityDestruction, onBodyADestruction); + NazaraSlot(Ndk::Entity, OnEntityDestruction, onBodyBDestruction); + }; + + std::vector m_constraints; }; } diff --git a/SDK/include/NDK/Components/ConstraintComponent2D.inl b/SDK/include/NDK/Components/ConstraintComponent2D.inl index 51e64ba6e..5db943439 100644 --- a/SDK/include/NDK/Components/ConstraintComponent2D.inl +++ b/SDK/include/NDK/Components/ConstraintComponent2D.inl @@ -1,3 +1,7 @@ +// Copyright (C) 2019 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequisites.hpp + #include #include #include @@ -5,7 +9,7 @@ namespace Ndk { template - Nz::ObjectRef ConstraintComponent2D::CreateConstraint(const Ndk::EntityHandle& first, const Ndk::EntityHandle& second, Args && ...args) + T* ConstraintComponent2D::CreateConstraint(const Ndk::EntityHandle& first, const Ndk::EntityHandle& second, Args&& ...args) { auto FetchBody = [](const Ndk::EntityHandle& entity) -> Nz::RigidBody2D* { @@ -23,9 +27,20 @@ namespace Ndk Nz::RigidBody2D* secondBody = FetchBody(second); NazaraAssert(secondBody, "Second entity has no CollisionComponent2D nor PhysicsComponent2D component"); - Nz::ObjectRef constraint = T::New(*firstBody, *secondBody, std::forward(args)...); - m_constraints.push_back(constraint); + m_constraints.emplace_back(); + auto& constraintData = m_constraints.back(); + constraintData.constraint = std::make_unique(*firstBody, *secondBody, std::forward(args)...); - return constraint; + constraintData.onBodyADestruction.Connect(first->OnEntityDestruction, [this, constraint = constraintData.constraint.get()](const Ndk::Entity* /*entity*/) + { + RemoveConstraint(constraint); + }); + + constraintData.onBodyBDestruction.Connect(second->OnEntityDestruction, [this, constraint = constraintData.constraint.get()](const Ndk::Entity* /*entity*/) + { + RemoveConstraint(constraint); + }); + + return static_cast(constraintData.constraint.get()); } } diff --git a/SDK/src/NDK/Components/ConstraintComponent2D.cpp b/SDK/src/NDK/Components/ConstraintComponent2D.cpp index ef841c210..6c75f39ee 100644 --- a/SDK/src/NDK/Components/ConstraintComponent2D.cpp +++ b/SDK/src/NDK/Components/ConstraintComponent2D.cpp @@ -1,6 +1,23 @@ +// Copyright (C) 2019 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequisites.hpp + #include namespace Ndk { + ConstraintComponent2D::ConstraintComponent2D(const ConstraintComponent2D& /*joint*/) + { + } + + bool ConstraintComponent2D::RemoveConstraint(Nz::Constraint2D* constraintPtr) + { + auto it = std::find_if(m_constraints.begin(), m_constraints.end(), [constraintPtr](const ConstraintData& constraintData) { return constraintData.constraint.get() == constraintPtr; }); + if (it != m_constraints.end()) + m_constraints.erase(it); + + return !m_constraints.empty(); + } + ComponentIndex ConstraintComponent2D::componentIndex; } diff --git a/include/Nazara/Core/ObjectRef.inl b/include/Nazara/Core/ObjectRef.inl index f592fd501..25ff82a95 100644 --- a/include/Nazara/Core/ObjectRef.inl +++ b/include/Nazara/Core/ObjectRef.inl @@ -335,7 +335,7 @@ namespace Nz template bool operator<(const ObjectRef& lhs, const ObjectRef& rhs) { - return lhs.m_object < rhs.m_object; + return lhs.Get() < rhs.Get(); } /*! @@ -348,7 +348,7 @@ namespace Nz template bool operator<(const T& lhs, const ObjectRef& rhs) { - return &lhs < rhs.m_object; + return &lhs < rhs.Get(); } /*! @@ -361,7 +361,7 @@ namespace Nz template bool operator<(const ObjectRef& lhs, const T& rhs) { - return lhs.m_object < &rhs; + return lhs.Get() < &rhs; } /*! diff --git a/include/Nazara/Physics2D/Constraint2D.hpp b/include/Nazara/Physics2D/Constraint2D.hpp index 74cded64d..13ee68c86 100644 --- a/include/Nazara/Physics2D/Constraint2D.hpp +++ b/include/Nazara/Physics2D/Constraint2D.hpp @@ -8,7 +8,9 @@ #define NAZARA_CONSTRAINT2D_HPP #include +#include #include +#include #include #include #include @@ -21,11 +23,9 @@ namespace Nz { class Constraint2D; - using Constraint2DConstRef = ObjectRef; - using Constraint2DLibrary = ObjectLibrary; - using Constraint2DRef = ObjectRef; + using Constraint2DHandle = ObjectHandle; - class NAZARA_PHYSICS2D_API Constraint2D : public RefCounted + class NAZARA_PHYSICS2D_API Constraint2D : public HandledObject { public: Constraint2D(const Constraint2D&) = delete; @@ -57,16 +57,12 @@ namespace Nz Constraint2D(Nz::PhysWorld2D* world, cpConstraint* constraint); MovablePtr m_constraint; - - private: - static Constraint2DLibrary s_library; }; class DampedSpringConstraint2D; - using DampedSpringConstraint2DConstRef = ObjectRef; - using DampedSpringConstraint2DRef = ObjectRef; - + using DampedSpringConstraint2DHandle = ObjectHandle; + class NAZARA_PHYSICS2D_API DampedSpringConstraint2D : public Constraint2D { public: @@ -84,14 +80,11 @@ namespace Nz void SetRestLength(float newLength); void SetSecondAnchor(const Vector2f& firstAnchor); void SetStiffness(float newStiffness); - - template static DampedSpringConstraint2DRef New(Args&&... args); }; class DampedRotarySpringConstraint2D; - using DampedRotarySpringConstraint2DConstRef = ObjectRef; - using DampedRotarySpringConstraint2DRef = ObjectRef; + using DampedRotarySpringConstraint2DHandle = ObjectHandle; class NAZARA_PHYSICS2D_API DampedRotarySpringConstraint2D : public Constraint2D { @@ -106,13 +99,11 @@ namespace Nz void SetDamping(float newDamping); void SetRestAngle(const RadianAnglef& newAngle); void SetStiffness(float newStiffness); - - template static DampedRotarySpringConstraint2DRef New(Args&&... args); }; class GearConstraint2D; - using GearConstraint2DConstRef = ObjectRef; + using GearConstraint2DHandle = ObjectHandle; using GearConstraint2DRef = ObjectRef; class NAZARA_PHYSICS2D_API GearConstraint2D : public Constraint2D @@ -132,8 +123,7 @@ namespace Nz class MotorConstraint2D; - using MotorConstraint2DConstRef = ObjectRef; - using MotorConstraint2DRef = ObjectRef; + using MotorConstraint2DHandle = ObjectHandle; class NAZARA_PHYSICS2D_API MotorConstraint2D : public Constraint2D { @@ -143,14 +133,11 @@ namespace Nz float GetRate() const; void SetRate(float rate); - - template static MotorConstraint2DRef New(Args&&... args); }; class PinConstraint2D; - using PinConstraint2DConstRef = ObjectRef; - using PinConstraint2DRef = ObjectRef; + using PinConstraint2DHandle = ObjectHandle; class NAZARA_PHYSICS2D_API PinConstraint2D : public Constraint2D { @@ -165,14 +152,11 @@ namespace Nz void SetDistance(float newDistance); void SetFirstAnchor(const Vector2f& firstAnchor); void SetSecondAnchor(const Vector2f& firstAnchor); - - template static PinConstraint2DRef New(Args&&... args); }; class PivotConstraint2D; - using PivotConstraint2DConstRef = ObjectRef; - using PivotConstraint2DRef = ObjectRef; + using PivotConstraint2DHandle = ObjectHandle; class NAZARA_PHYSICS2D_API PivotConstraint2D : public Constraint2D { @@ -186,14 +170,11 @@ namespace Nz void SetFirstAnchor(const Vector2f& firstAnchor); void SetSecondAnchor(const Vector2f& firstAnchor); - - template static PivotConstraint2DRef New(Args&&... args); }; class RatchetConstraint2D; - using RatchetConstraint2DConstRef = ObjectRef; - using RatchetConstraint2DRef = ObjectRef; + using RatchetConstraint2DHandle = ObjectHandle; class NAZARA_PHYSICS2D_API RatchetConstraint2D : public Constraint2D { @@ -208,14 +189,11 @@ namespace Nz void SetAngle(const RadianAnglef& angle); void SetPhase(float phase); void SetRatchet(float ratchet); - - template static RatchetConstraint2DRef New(Args&&... args); }; class RotaryLimitConstraint2D; - using RotaryLimitConstraint2DConstRef = ObjectRef; - using RotaryLimitConstraint2DRef = ObjectRef; + using RotaryLimitConstraint2DHandle = ObjectHandle; class NAZARA_PHYSICS2D_API RotaryLimitConstraint2D : public Constraint2D { @@ -228,14 +206,11 @@ namespace Nz void SetMaxAngle(const RadianAnglef& maxAngle); void SetMinAngle(const RadianAnglef& minAngle); - - template static RotaryLimitConstraint2DRef New(Args&&... args); }; class SlideConstraint2D; - using SlideConstraint2DConstRef = ObjectRef; - using SlideConstraint2DRef = ObjectRef; + using SlideConstraint2DHandle = ObjectHandle; class NAZARA_PHYSICS2D_API SlideConstraint2D : public Constraint2D { @@ -252,8 +227,6 @@ namespace Nz void SetMaxDistance(float newMaxDistance); void SetMinDistance(float newMinDistance); void SetSecondAnchor(const Vector2f& firstAnchor); - - template static SlideConstraint2DRef New(Args&&... args); }; } diff --git a/include/Nazara/Physics2D/Constraint2D.inl b/include/Nazara/Physics2D/Constraint2D.inl index 9bdb1d3ad..2ed7744f3 100644 --- a/include/Nazara/Physics2D/Constraint2D.inl +++ b/include/Nazara/Physics2D/Constraint2D.inl @@ -8,86 +8,6 @@ namespace Nz { - template - DampedSpringConstraint2DRef DampedSpringConstraint2D::New(Args&&... args) - { - std::unique_ptr object(new DampedSpringConstraint2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - DampedRotarySpringConstraint2DRef DampedRotarySpringConstraint2D::New(Args&&... args) - { - std::unique_ptr object(new DampedRotarySpringConstraint2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - GearConstraint2DRef GearConstraint2D::New(Args&&... args) - { - std::unique_ptr object(new GearConstraint2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - MotorConstraint2DRef MotorConstraint2D::New(Args&&... args) - { - std::unique_ptr object(new MotorConstraint2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - PinConstraint2DRef PinConstraint2D::New(Args&&... args) - { - std::unique_ptr object(new PinConstraint2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - PivotConstraint2DRef PivotConstraint2D::New(Args&&... args) - { - std::unique_ptr object(new PivotConstraint2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - RatchetConstraint2DRef RatchetConstraint2D::New(Args&&... args) - { - std::unique_ptr object(new RatchetConstraint2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - RotaryLimitConstraint2DRef RotaryLimitConstraint2D::New(Args&&... args) - { - std::unique_ptr object(new RotaryLimitConstraint2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - SlideConstraint2DRef SlideConstraint2D::New(Args&&... args) - { - std::unique_ptr object(new SlideConstraint2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } } #include