Rework constraint2D

This commit is contained in:
Lynix
2019-12-08 18:56:21 +01:00
parent 164633e518
commit ef030ddaac
7 changed files with 74 additions and 134 deletions

View File

@@ -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 <NDK/Component.hpp>
#include <Nazara/Physics2D/Constraint2D.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <vector>
#include <NDK/Component.hpp>
#include <NDK/Entity.hpp>
#include <memory>
#include <vector>
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<typename T, typename... Args> inline Nz::ObjectRef<T> CreateConstraint(const Ndk::EntityHandle& first, const Ndk::EntityHandle& second, Args&&... args);
template<typename T, typename... Args> 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<Nz::Constraint2D> constraint;
std::vector<Nz::Constraint2DRef> m_constraints;
NazaraSlot(Ndk::Entity, OnEntityDestruction, onBodyADestruction);
NazaraSlot(Ndk::Entity, OnEntityDestruction, onBodyBDestruction);
};
std::vector<ConstraintData> m_constraints;
};
}

View File

@@ -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 <NDK/Components/ConstraintComponent2D.hpp>
#include <NDK/Components/PhysicsComponent2D.hpp>
#include <NDK/Components/CollisionComponent2D.hpp>
@@ -5,7 +9,7 @@
namespace Ndk
{
template<typename T, typename ...Args>
Nz::ObjectRef<T> 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<T> constraint = T::New(*firstBody, *secondBody, std::forward<Args>(args)...);
m_constraints.push_back(constraint);
m_constraints.emplace_back();
auto& constraintData = m_constraints.back();
constraintData.constraint = std::make_unique<T>(*firstBody, *secondBody, std::forward<Args>(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<T*>(constraintData.constraint.get());
}
}