Add initial ECS support

This commit is contained in:
Jérôme Leclercq
2021-06-20 13:33:10 +02:00
parent 6f87a01fb2
commit c1a9a22177
17 changed files with 423 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
// Copyright (C) 2021 Jérôme Leclercq
// This file is part of the "Nazara Engine - Physics 3D module"
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
#include <Nazara/Physics3D/Components/RigidBody3DComponent.hpp>
#include <Nazara/Physics3D/Debug.hpp>
namespace Nz
{
}

View File

@@ -0,0 +1,45 @@
// Copyright (C) 2021 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
#include <Nazara/Physics3D/Systems/Physics3DSystem.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <Nazara/Physics3D/Debug.hpp>
namespace Nz
{
Physics3DSystem::Physics3DSystem(entt::registry& registry)
{
m_constructConnection = registry.on_construct<RigidBody3DComponent>().connect<OnConstruct>();
}
Physics3DSystem::~Physics3DSystem()
{
m_constructConnection.release();
}
void Physics3DSystem::Update(entt::registry& registry, float elapsedTime)
{
m_physWorld.Step(elapsedTime);
// Replicate rigid body position to their node components
auto velView = registry.view<Nz::NodeComponent, const RigidBody3DComponent>();
for (auto [entity, nodeComponent, rigidBodyComponent] : velView.each())
{
nodeComponent.SetPosition(rigidBodyComponent.GetPosition(), CoordSys::Global);
nodeComponent.SetRotation(rigidBodyComponent.GetRotation(), CoordSys::Global);
}
}
void Physics3DSystem::OnConstruct(entt::registry& registry, entt::entity entity)
{
// If our entity already has a node component when adding a rigid body, initialize it with
Nz::NodeComponent* node = registry.try_get<NodeComponent>(entity);
if (node)
{
RigidBody3DComponent& rigidBody = registry.get<RigidBody3DComponent>(entity);
rigidBody.SetPosition(node->GetPosition());
rigidBody.SetRotation(node->GetRotation());
}
}
}