JoltPhysics3D: Allow to construct components without using the system

This commit is contained in:
SirLynix
2023-07-23 13:05:33 +02:00
parent 20f000c8dc
commit c081811760
14 changed files with 167 additions and 87 deletions

View File

@@ -17,16 +17,21 @@ namespace Nz
friend class JoltPhysics3DSystem;
public:
using JoltCharacter::JoltCharacter;
inline JoltCharacterComponent(const JoltCharacter::Settings& settings);
JoltCharacterComponent(const JoltCharacterComponent&) = default;
JoltCharacterComponent(JoltCharacterComponent&&) noexcept = default;
~JoltCharacterComponent() = default;
JoltCharacterComponent& operator=(const JoltCharacterComponent&) = default;
JoltCharacterComponent& operator=(JoltCharacterComponent&&) noexcept = default;
private:
inline void Construct(JoltPhysWorld3D& world);
std::unique_ptr<JoltCharacter::Settings> m_settings;
};
}
#include <Nazara/JoltPhysics3D/Components/JoltRigidBody3DComponent.inl>
#include <Nazara/JoltPhysics3D/Components/JoltCharacterComponent.inl>
#endif // NAZARA_JOLTPHYSICS3D_COMPONENTS_JOLTCHARACTERCOMPONENT_HPP

View File

@@ -6,6 +6,17 @@
namespace Nz
{
inline JoltCharacterComponent::JoltCharacterComponent(const JoltCharacter::Settings& settings)
{
m_settings = std::make_unique<JoltCharacter::Settings>(settings);
}
inline void JoltCharacterComponent::Construct(JoltPhysWorld3D& world)
{
assert(m_settings);
Create(world, *m_settings);
m_settings.reset();
}
}
#include <Nazara/JoltPhysics3D/DebugOff.hpp>

View File

@@ -9,6 +9,7 @@
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/JoltRigidBody3D.hpp>
#include <variant>
namespace Nz
{
@@ -17,13 +18,20 @@ namespace Nz
friend class JoltPhysics3DSystem;
public:
using JoltRigidBody3D::JoltRigidBody3D;
inline JoltRigidBody3DComponent(const JoltRigidBody3D::DynamicSettings& settings);
inline JoltRigidBody3DComponent(const JoltRigidBody3D::StaticSettings& settings);
JoltRigidBody3DComponent(const JoltRigidBody3DComponent&) = default;
JoltRigidBody3DComponent(JoltRigidBody3DComponent&&) noexcept = default;
~JoltRigidBody3DComponent() = default;
JoltRigidBody3DComponent& operator=(const JoltRigidBody3DComponent&) = default;
JoltRigidBody3DComponent& operator=(JoltRigidBody3DComponent&&) noexcept = default;
private:
inline void Construct(JoltPhysWorld3D& world);
using Setting = std::variant<JoltRigidBody3D::DynamicSettings, JoltRigidBody3D::StaticSettings>;
std::unique_ptr<Setting> m_settings;
};
}

View File

@@ -3,9 +3,29 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/JoltPhysics3D/Debug.hpp>
#include <cassert>
namespace Nz
{
inline JoltRigidBody3DComponent::JoltRigidBody3DComponent(const JoltRigidBody3D::DynamicSettings& settings)
{
m_settings = std::make_unique<Setting>(settings);
}
inline JoltRigidBody3DComponent::JoltRigidBody3DComponent(const JoltRigidBody3D::StaticSettings& settings)
{
m_settings = std::make_unique<Setting>(settings);
}
inline void JoltRigidBody3DComponent::Construct(JoltPhysWorld3D& world)
{
assert(m_settings);
std::visit([&](auto&& arg)
{
Create(world, arg);
}, *m_settings);
m_settings.reset();
}
}
#include <Nazara/JoltPhysics3D/DebugOff.hpp>

View File

@@ -32,7 +32,9 @@ namespace Nz
friend JoltPhysWorld3D;
public:
JoltCharacter(JoltPhysWorld3D& physWorld, std::shared_ptr<JoltCollider3D> collider, const Vector3f& position = Vector3f::Zero(), const Quaternionf& rotation = Quaternionf::Identity());
struct Settings;
JoltCharacter(JoltPhysWorld3D& physWorld, const Settings& settings);
JoltCharacter(const JoltCharacter&) = delete;
JoltCharacter(JoltCharacter&& character) noexcept;
~JoltCharacter();
@@ -62,7 +64,17 @@ namespace Nz
JoltCharacter& operator=(const JoltCharacter&) = delete;
JoltCharacter& operator=(JoltCharacter&& character) noexcept;
struct Settings
{
std::shared_ptr<JoltCollider3D> collider;
Quaternionf rotation = Quaternionf::Identity();
Vector3f position = Vector3f::Zero();
};
protected:
JoltCharacter();
void Create(JoltPhysWorld3D& physWorld, const Settings& settings);
void Destroy();
private:

View File

@@ -32,8 +32,8 @@ namespace Nz
struct DynamicSettings;
struct StaticSettings;
JoltRigidBody3D(JoltPhysWorld3D& world, const DynamicSettings& settings);
JoltRigidBody3D(JoltPhysWorld3D& world, const StaticSettings& settings);
inline JoltRigidBody3D(JoltPhysWorld3D& world, const DynamicSettings& settings);
inline JoltRigidBody3D(JoltPhysWorld3D& world, const StaticSettings& settings);
JoltRigidBody3D(const JoltRigidBody3D& object) = delete;
JoltRigidBody3D(JoltRigidBody3D&& object) noexcept;
~JoltRigidBody3D();
@@ -123,6 +123,9 @@ namespace Nz
};
protected:
JoltRigidBody3D() = default;
void Create(JoltPhysWorld3D& world, const DynamicSettings& settings);
void Create(JoltPhysWorld3D& world, const StaticSettings& settings);
void Destroy(bool worldDestruction = false);
private:

View File

@@ -6,6 +6,16 @@
namespace Nz
{
inline JoltRigidBody3D::JoltRigidBody3D(JoltPhysWorld3D& world, const DynamicSettings& settings)
{
Create(world, settings);
}
inline JoltRigidBody3D::JoltRigidBody3D(JoltPhysWorld3D& world, const StaticSettings& settings)
{
Create(world, settings);
}
inline void JoltRigidBody3D::DisableSimulation()
{
return EnableSimulation(false);

View File

@@ -32,9 +32,6 @@ namespace Nz
JoltPhysics3DSystem(JoltPhysics3DSystem&&) = delete;
~JoltPhysics3DSystem();
template<typename... Args> JoltCharacterComponent CreateCharacter(Args&&... args);
template<typename... Args> JoltRigidBody3DComponent CreateRigidBody(Args&&... args);
void Dump();
inline JoltPhysWorld3D& GetPhysWorld();
@@ -55,6 +52,7 @@ namespace Nz
private:
void OnBodyConstruct(entt::registry& registry, entt::entity entity);
void OnCharacterConstruct(entt::registry& registry, entt::entity entity);
void OnBodyDestruct(entt::registry& registry, entt::entity entity);
std::size_t m_stepCount;
@@ -62,8 +60,9 @@ namespace Nz
entt::registry& m_registry;
entt::observer m_characterConstructObserver;
entt::observer m_rigidBodyConstructObserver;
entt::scoped_connection m_constructConnection;
entt::scoped_connection m_destructConnection;
entt::scoped_connection m_bodyConstructConnection;
entt::scoped_connection m_characterConstructConnection;
entt::scoped_connection m_bodyDestructConnection;
JoltPhysWorld3D m_physWorld;
Time m_physicsTime;
Time m_updateTime;

View File

@@ -6,18 +6,6 @@
namespace Nz
{
template<typename... Args>
JoltCharacterComponent JoltPhysics3DSystem::CreateCharacter(Args&& ...args)
{
return JoltCharacterComponent(m_physWorld, std::forward<Args>(args)...);
}
template<typename... Args>
JoltRigidBody3DComponent JoltPhysics3DSystem::CreateRigidBody(Args&&... args)
{
return JoltRigidBody3DComponent(m_physWorld, std::forward<Args>(args)...);
}
inline JoltPhysWorld3D& JoltPhysics3DSystem::GetPhysWorld()
{
return m_physWorld;