Raftel Engine
 
Loading...
Searching...
No Matches
ecs.hpp
1#ifndef __ECS_H__
2#define __ECS_H__ 1
3
4#pragma once
5
6#include <cstddef>
7#include <vector>
8#include <optional>
9#include <memory>
10#include <string>
11#include "raftel/imguiRenderer.hpp"
12#include <raftel/global_macros.hpp>
13#include <glm/gtc/matrix_transform.hpp>
14#include <glm/gtx/quaternion.hpp>
15#include <raftel/components.hpp>
16
17namespace Raftel {
18 class Camera;
19 class Mesh;
20 class ShaderProgram;
21 class EntityManager;
22
23 constexpr size_t MAX_ENTITIES = 200;
24
28 class Entity {
29 public:
30 size_t ID;
31
37
42 std::optional<TransformComponent>& getTransformComp();
43
48 const std::optional<TransformComponent>& getTransformComp() const;
49
55
60 std::optional<ScriptComponent>& getScriptComp();
61
67
72 void addMeshComp(const std::shared_ptr<Mesh>& mesh);
77 bool hasMeshComp() const;
82 bool hasTransformComp() const;
87 bool hasRenderComp() const;
92 bool hasLightComp() const;
97 bool hasScriptComp() const;
98
103
113 std::optional<LightComponent>& getLightComp();
118 const std::optional<LightComponent>& getLightComp() const;
119
124 std::optional<MeshComponent> getMeshComp() const;
125
130 Entity(const Entity& other) = default;
131
136 Entity(Entity&& other) noexcept = default;
137
143 Entity& operator=(const Entity& other) = default;
144
150 Entity& operator=(Entity&& other) noexcept = default;
151
152 //COPYABLE_AND_MOVABLE(Entity)
153 private:
154 friend class EntityManager;
155 EntityManager* em;
156
162 Entity(size_t ID, EntityManager* em);
163
168 void setPosition(const glm::vec3& pos);
169
174 glm::vec3 getPosition() const;
175
180 void setRotation(const glm::vec3& rot);
181
186 glm::vec3 getRotation() const;
187
192 void setScale(const glm::vec3& scale);
193
198 glm::vec3 getScale() const;
199
204 void setMesh(const std::shared_ptr<Mesh>& mesh);
205
210 std::optional<MeshComponent> getMesh() const;
211
217 void moveTo(const glm::vec3& target, float speed);
218 };
219
224 public:
229
235
240 const std::vector<Entity>& getActiveEntities() const;
245 std::vector<Entity>& getActiveEntities();
246
251 void Update(float deltaTime);
252
258 void setPosition(size_t ID, const glm::vec3& pos);
259
265 glm::vec3 getPosition(size_t ID) const;
266
272 void setRotation(size_t ID, const glm::vec3& rot);
273
279 glm::vec3 getRotation(size_t ID) const;
280
286 void setScale(size_t ID, const glm::vec3& scale);
287
293 glm::vec3 getScale(size_t ID) const;
294
300 void setMesh(size_t ID, const std::shared_ptr<Mesh>& mesh);
301
307 std::optional<MeshComponent> getMesh(size_t ID) const;
308
315 void moveTo(size_t ID, const glm::vec3& target, float speed);
316
323 void constantStraightMovement(size_t ID, float speed, bool isHorizontal);
324
330 void addMeshComp(size_t ID, const std::shared_ptr<Mesh>& mesh);
331
337 std::optional<MeshComponent> getMeshComponent(size_t ID);
338
344 bool hasMeshComponent(size_t ID) const;
345
350 void removeMeshComponent(size_t ID);
351
357 void addTransformComp(size_t ID, TransformComponent&& trans);
358
364 void addRenderComp(size_t ID, RenderComponent&& rend);
365
371 void addScriptComp(size_t ID, ScriptComponent&& script);
372
378 void addLightComp(size_t ID, LightComponent&& light);
379
385 std::optional<Raftel::LightComponent>& getLightComponent(size_t ID);
386
391 void removeEntity(size_t id);
396 void removeTransformComp(size_t ID);
397
402 void removeLightComp(size_t ID);
403
408 void removeRenderComp(size_t ID);
409
414 void removeScriptComp(size_t ID);
415
421 std::optional<ScriptComponent>& getScriptComponent(size_t ID);
422
428 std::optional<TransformComponent>& getTransformComponent(size_t ID);
429
435 std::optional<RenderComponent>& getRenderComponent(size_t ID);
436
437 private:
438 friend class RenderSystem;
439 friend class Editor;
440
441 friend void UpdateRenderSystem(EntityManager& em, Camera& cam, ShaderProgram& shader, glm::ivec2 screen, bool shadow);
442 friend void UpdateTransformSystem(EntityManager& em);
443 friend void UpdateScriptingSystem(EntityManager& em);
444 friend void UpdateLightSystem(EntityManager& em, ShaderProgram& lit_shader, Camera& cam);
445
446 friend bool Entity::hasLightComp() const;
447
448 std::vector<std::optional<ScriptComponent>> scriptComps;
449 std::vector<std::optional<TransformComponent>> transformComps;
450 std::vector<std::optional<RenderComponent>> renderComps;
451 std::vector<std::optional<MeshComponent>> meshComps;
452 std::vector<std::optional<LightComponent>> lightComps;
453 std::vector<std::optional<BasicComponent>> basicComps;
454
455 std::vector<Entity> activeEntities;
456 size_t lastEntity = 0;
457
458 MOVABLE_BUT_NOT_COPYABLE(EntityManager);
459 };
460
461
470 int pickEntity(const std::unique_ptr<EntityManager>& ecs, Camera& cam, glm::vec2 mousePos, glm::ivec2 screenSize);
471
481 bool intersectRaySphere(const glm::vec3& rayOrigin, const glm::vec3& rayDir, const glm::vec3& sphereCenter, float sphereRadius, float& distance);
482}
483
484
485#endif
Camera class for handling movement and view transformations.
Definition camera.hpp:27
Represents an entity in the ECS (Entity Component System).
Definition ecs.hpp:28
bool hasScriptComp() const
Checks if the entity has a script component.
void addTransformComp(TransformComponent &&trans)
Adds a TransformComponent to the entity.
void removeMeshComp()
Removes the mesh component from the entity.
Entity & operator=(Entity &&other) noexcept=default
Default move assignment operator.
bool hasRenderComp() const
Checks if the entity has a render component.
const std::optional< TransformComponent > & getTransformComp() const
Retrieves the TransformComponent of the entity (const version).
bool hasLightComp() const
Checks if the entity has a light component.
void addRenderComp(RenderComponent &&rend)
Adds a RenderComponent to the entity.
void addLightComp(LightComponent &&light)
Adds a LightComponent to the entity.
void addMeshComp(const std::shared_ptr< Mesh > &mesh)
Adds a MeshComponent to the entity.
Entity & operator=(const Entity &other)=default
Default copy assignment operator.
void addScriptComp(ScriptComponent &&script)
Adds a ScriptComponent to the entity.
size_t ID
Definition ecs.hpp:30
bool hasMeshComp() const
Checks if the entity has a mesh component.
Entity(const Entity &other)=default
Default copy constructor.
std::optional< ScriptComponent > & getScriptComp()
Retrieves the ScriptComponent of the entity.
std::optional< LightComponent > & getLightComp()
Retrieves the LightComponent of the entity.
bool hasTransformComp() const
Checks if the entity has a transform component.
std::optional< TransformComponent > & getTransformComp()
Retrieves the TransformComponent of the entity.
std::optional< MeshComponent > getMeshComp() const
Retrieves the MeshComponent of the entity.
Entity(Entity &&other) noexcept=default
Default move constructor.
const std::optional< LightComponent > & getLightComp() const
Retrieves the LightComponent of the entity (const version).
Manages entities in the ECS (Entity Component System).
Definition ecs.hpp:223
void setScale(size_t ID, const glm::vec3 &scale)
Sets the scale of an entity.
std::optional< Raftel::LightComponent > & getLightComponent(size_t ID)
Retrieves the light component of an entity.
std::optional< MeshComponent > getMesh(size_t ID) const
Retrieves the mesh component of an entity.
void addTransformComp(size_t ID, TransformComponent &&trans)
Adds a transform component to an entity.
void constantStraightMovement(size_t ID, float speed, bool isHorizontal)
Moves an entity in a straight direction continuously.
void setPosition(size_t ID, const glm::vec3 &pos)
Sets the position of an entity.
std::optional< MeshComponent > getMeshComponent(size_t ID)
Retrieves the mesh component of an entity.
Entity CreateEntity()
Creates a new entity and returns it.
void removeLightComp(size_t ID)
Removes the light component from an entity.
void removeRenderComp(size_t ID)
Removes the render component from an entity.
void removeMeshComponent(size_t ID)
Removes the mesh component from an entity.
glm::vec3 getPosition(size_t ID) const
Gets the position of an entity.
std::vector< Entity > & getActiveEntities()
Retrieves a mutable reference to the list of active entities.
void Update(float deltaTime)
Updates the entity manager, processing entity behavior.
void removeScriptComp(size_t ID)
Removes the script component from an entity.
void moveTo(size_t ID, const glm::vec3 &target, float speed)
Moves an entity to a target position at a given speed.
void addLightComp(size_t ID, LightComponent &&light)
Adds a light component to an entity.
glm::vec3 getRotation(size_t ID) const
Gets the rotation of an entity.
std::optional< RenderComponent > & getRenderComponent(size_t ID)
Retrieves the render component of an entity.
void setRotation(size_t ID, const glm::vec3 &rot)
Sets the rotation of an entity.
void setMesh(size_t ID, const std::shared_ptr< Mesh > &mesh)
Assigns a mesh to an entity.
std::optional< ScriptComponent > & getScriptComponent(size_t ID)
Retrieves the script component of an entity.
void removeEntity(size_t id)
Removes an entity from the system.
const std::vector< Entity > & getActiveEntities() const
Retrieves a constant reference to the list of active entities.
bool hasMeshComponent(size_t ID) const
Checks if an entity has a mesh component.
std::optional< TransformComponent > & getTransformComponent(size_t ID)
Retrieves the transform component of an entity.
void removeTransformComp(size_t ID)
Removes the transform component from an entity.
void addRenderComp(size_t ID, RenderComponent &&rend)
Adds a render component to an entity.
glm::vec3 getScale(size_t ID) const
Gets the scale of an entity.
void addMeshComp(size_t ID, const std::shared_ptr< Mesh > &mesh)
Adds a mesh component to an entity.
void addScriptComp(size_t ID, ScriptComponent &&script)
Adds a script component to an entity.
EntityManager()
Constructs an EntityManager.
Represents a 3D mesh, including vertex data, indices, and materials.
Definition mesh.hpp:89
Component that handles Lua scripting.
A class to represent an OpenGL shader program, combining vertex and fragment shaders.
Definition shader.hpp:119
Represents a light component with different light types.
Render component that determines visibility.
Represents the transformation component of an entity.