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

11
src/Nazara/Core/ECS.cpp Normal file
View File

@@ -0,0 +1,11 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/ECS.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
NAZARA_CORE_API ECS* ECS::s_instance = nullptr;
}

View File

@@ -0,0 +1,10 @@
// 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/Graphics/Components/GraphicsComponent.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Core/ECS.hpp>
#include <Nazara/Graphics/MaterialPipeline.hpp>
#include <Nazara/Graphics/PredefinedShaderStructs.hpp>
#include <stdexcept>
@@ -18,6 +19,8 @@ namespace Nz
Graphics::Graphics(Config config) :
ModuleBase("Graphics", this)
{
ECS::RegisterComponents();
Renderer* renderer = Renderer::Instance();
const std::vector<RenderDeviceInfo>& renderDeviceInfo = renderer->QueryRenderDevices();

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());
}
}
}

View File

@@ -0,0 +1,18 @@
// 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/Utility/Components/NodeComponent.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
void NodeComponent::SetParent(entt::registry& registry, entt::entity entity, bool keepDerived)
{
NodeComponent* nodeComponent = registry.try_get<NodeComponent>(entity);
NazaraAssert(nodeComponent, "entity doesn't have a NodeComponent");
Node::SetParent(nodeComponent, keepDerived);
}
}

View File

@@ -5,6 +5,7 @@
#include <Nazara/Utility/Utility.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Core/ECS.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Animation.hpp>
#include <Nazara/Utility/Buffer.hpp>
@@ -39,6 +40,8 @@ namespace Nz
Utility::Utility(Config /*config*/) :
ModuleBase("Utility", this)
{
ECS::RegisterComponents();
if (!Buffer::Initialize())
throw std::runtime_error("failed to initialize buffers");