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,46 @@
// 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
#pragma once
#ifndef NAZARA_ECS_HPP
#define NAZARA_ECS_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Core.hpp>
#include <entt/entt.hpp>
namespace Nz
{
class GraphicsComponent;
class NodeComponent;
class RigidBody3DComponent;
class ECS : public ModuleBase<ECS>
{
friend ModuleBase;
friend class Audio;
friend class Graphics;
friend class Physics2D;
friend class Physics3D;
friend class Utility;
public:
using Dependencies = TypeList<Core>;
struct Config {};
inline ECS(Config /*config*/);
~ECS() = default;
private:
static inline void RegisterComponents();
NAZARA_CORE_API static ECS* s_instance;
};
}
#include <Nazara/Core/ECS.inl>
#endif

View File

@ -0,0 +1,35 @@
// 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 <stdexcept>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
/*!
* \ingroup core
* \class Nz::ECS
* \brief Core class that represents the ECS module
*/
inline ECS::ECS(Config /*config*/) :
ModuleBase("ECS", this)
{
RegisterComponents();
}
inline void ECS::RegisterComponents()
{
if (entt::type_seq<NodeComponent>() != 0)
throw std::runtime_error("NodeComponent has wrong index, please initialize Nazara ECS before instancing your own components");
if (entt::type_seq<GraphicsComponent>() != 1)
throw std::runtime_error("GraphicsComponent has wrong index, please initialize Nazara ECS before instancing your own components");
if (entt::type_seq<RigidBody3DComponent>() != 2)
throw std::runtime_error("GraphicsComponent has wrong index, please initialize Nazara ECS before instancing your own components");
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@ -0,0 +1,45 @@
// Copyright (C) 2021 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_GRAPHICSCOMPONENT_HPP
#define NAZARA_GRAPHICSCOMPONENT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/ECS.hpp>
#include <Nazara/Graphics/InstancedRenderable.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#include <memory>
#include <vector>
namespace Nz
{
class NAZARA_GRAPHICS_API GraphicsComponent
{
public:
GraphicsComponent() = default;
GraphicsComponent(const GraphicsComponent&) = default;
GraphicsComponent(GraphicsComponent&&) = default;
~GraphicsComponent() = default;
inline void AttachRenderable(std::shared_ptr<InstancedRenderable> renderable);
inline void DetachRenderable(const std::shared_ptr<InstancedRenderable>& renderable);
inline const std::vector<std::shared_ptr<InstancedRenderable>>& GetRenderables() const;
inline WorldInstance& GetWorldInstance();
inline const WorldInstance& GetWorldInstance() const;
GraphicsComponent& operator=(const GraphicsComponent&) = default;
GraphicsComponent& operator=(GraphicsComponent&&) = default;
private:
std::vector<std::shared_ptr<InstancedRenderable>> m_renderables;
WorldInstance m_worldInstance;
};
}
#include <Nazara/Graphics/Components/GraphicsComponent.inl>
#endif

View File

@ -0,0 +1,37 @@
// 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/Utility/Debug.hpp>
#include "GraphicsComponent.hpp"
namespace Nz
{
inline void GraphicsComponent::AttachRenderable(std::shared_ptr<InstancedRenderable> renderable)
{
m_renderables.push_back(std::move(renderable));
}
inline void GraphicsComponent::DetachRenderable(const std::shared_ptr<InstancedRenderable>& renderable)
{
auto it = std::find(m_renderables.begin(), m_renderables.end(), renderable);
if (it != m_renderables.end())
m_renderables.erase(it);
}
inline const std::vector<std::shared_ptr<InstancedRenderable>>& GraphicsComponent::GetRenderables() const
{
return m_renderables;
}
inline WorldInstance& GraphicsComponent::GetWorldInstance()
{
return m_worldInstance;
}
inline const WorldInstance& GraphicsComponent::GetWorldInstance() const
{
return m_worldInstance;
}
}

View File

@ -0,0 +1,30 @@
// 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 Config.hpp
#pragma once
#ifndef NAZARA_RIGIDBODYCOMPONENT_HPP
#define NAZARA_RIGIDBODYCOMPONENT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Physics3D/RigidBody3D.hpp>
namespace Nz
{
class NAZARA_PHYSICS3D_API RigidBody3DComponent : public RigidBody3D
{
public:
using RigidBody3D::RigidBody3D;
RigidBody3DComponent(const RigidBody3DComponent&) = default;
RigidBody3DComponent(RigidBody3DComponent&&) noexcept = default;
~RigidBody3DComponent() = default;
RigidBody3DComponent& operator=(const RigidBody3DComponent&) = default;
RigidBody3DComponent& operator=(RigidBody3DComponent&&) noexcept = default;
};
}
#include <Nazara/Physics3D/Components/RigidBody3DComponent.inl>
#endif

View File

@ -0,0 +1,12 @@
// 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
{
}
#include <Nazara/Physics3D/DebugOff.hpp>

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 Config.hpp
#pragma once
#ifndef NAZARA_PHYSICS3DSYSTEM_HPP
#define NAZARA_PHYSICS3DSYSTEM_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/ECS.hpp>
#include <Nazara/Physics3D/PhysWorld3D.hpp>
#include <Nazara/Physics3D/Components/RigidBody3DComponent.hpp>
namespace Nz
{
class NAZARA_PHYSICS3D_API Physics3DSystem
{
public:
Physics3DSystem(entt::registry& registry);
Physics3DSystem(const Physics3DSystem&) = delete;
Physics3DSystem(Physics3DSystem&&) = delete;
~Physics3DSystem();
template<typename... Args> RigidBody3DComponent CreateRigidBody(Args&&... args);
inline PhysWorld3D& GetPhysWorld();
inline const PhysWorld3D& GetPhysWorld() const;
void Update(entt::registry& registry, float elapsedTime);
Physics3DSystem& operator=(const Physics3DSystem&) = delete;
Physics3DSystem& operator=(Physics3DSystem&&) = delete;
private:
static void OnConstruct(entt::registry& registry, entt::entity entity);
entt::connection m_constructConnection;
PhysWorld3D m_physWorld;
};
}
#include <Nazara/Physics3D/Systems/Physics3DSystem.inl>
#endif

View File

@ -0,0 +1,27 @@
// 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/Physics3D/Debug.hpp>
namespace Nz
{
template<typename... Args>
RigidBody3DComponent Physics3DSystem::CreateRigidBody(Args&&... args)
{
return RigidBody3DComponent(&m_physWorld, std::forward<Args>(args)...);
}
inline PhysWorld3D& Physics3DSystem::GetPhysWorld()
{
return m_physWorld;
}
inline const PhysWorld3D& Physics3DSystem::GetPhysWorld() const
{
return m_physWorld;
}
}
#include <Nazara/Physics3D/DebugOff.hpp>

View File

@ -0,0 +1,34 @@
// 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 Config.hpp
#pragma once
#ifndef NAZARA_NODECOMPONENT_HPP
#define NAZARA_NODECOMPONENT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/ECS.hpp>
#include <Nazara/Utility/Node.hpp>
namespace Nz
{
class NAZARA_UTILITY_API NodeComponent : public Node
{
public:
NodeComponent() = default;
NodeComponent(const NodeComponent&) = default;
NodeComponent(NodeComponent&&) noexcept = default;
~NodeComponent() = default;
void SetParent(entt::registry& registry, entt::entity entity, bool keepDerived = false);
using Node::SetParent;
NodeComponent& operator=(const NodeComponent&) = default;
NodeComponent& operator=(NodeComponent&&) noexcept = default;
};
}
#include <Nazara/Utility/Components/NodeComponent.inl>
#endif

View File

@ -0,0 +1,12 @@
// 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/Utility/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Utility/DebugOff.hpp>

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");