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>