R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ECSWorld.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** rtype
4** File description:
5** ECSWorld - High-level wrapper for the ECS Registry
6*/
7
8#pragma once
9
10#include <functional>
11#include <memory>
12#include <string>
13#include <string_view>
14#include <unordered_map>
15#include <vector>
16
17#include "../ECS/Registry.hpp"
18#include "../ECS/Systems/ISystem.hpp"
19
20namespace ecs::wrapper {
21
23
24 std::string_view systemIdToName(SystemId id);
25
33 class Entity {
34 private:
37
38 public:
45 Entity(Address address, Registry *registry);
46
52 Address getAddress() const;
53
61 template <typename T>
62 Entity &with(const T &component);
63
71 template <typename T>
72 T &get();
73
81 template <typename T>
82 const T &get() const;
83
90 template <typename T>
91 bool has() const;
92
99 template <typename T>
101
107 bool isValid() const;
108
112 operator Address() const;
113 };
114
122 class ECSWorld {
123 private:
124 std::unique_ptr<Registry> _registry;
125 std::unordered_map<std::string, std::unique_ptr<ISystem>> _systems;
126 std::vector<std::string> _systemsOrder;
127
128 public:
132 ECSWorld();
133
138
139 // Entity Management
140
147
154 std::vector<Entity> createEntities(size_t count);
155
162 Entity getEntity(Address address);
163
169 void destroyEntity(const Entity &entity);
170
176 void destroyEntity(Address address);
177
178 // Component Queries
179
186 template <typename... Components>
187 std::vector<Entity> query();
188
201 template <typename... Components>
202 void forEach(std::function<void(Entity, Components &...)> callback);
203
204 // System Management
205
213 template <typename T>
214 void registerSystem(const std::string &name, std::unique_ptr<T> system);
215
224 template <typename T, typename... Args>
225 void createSystem(const std::string &name, Args &&...args);
226
235 template <typename T, typename... Args>
236 void createSystem(SystemId id, Args &&...args);
237
245 template <typename T>
246 T *getSystem(const std::string &name);
247
255 template <typename T>
257
263 void removeSystem(const std::string &name);
264
270 void removeSystem(SystemId id);
271
279 void update(float deltaTime);
280
288 bool updateSystem(const std::string &name, float deltaTime);
289
297 bool updateSystem(SystemId id, float deltaTime);
298
299 // Direct Registry Access (for advanced usage)
300
308
314 const Registry &getRegistry() const;
315
320 bool startingEvent = false;
321
327 void clear();
328
334 size_t getSystemCount() const;
335 };
336
337} // namespace ecs::wrapper
338
339#include "ECSWorld.tpp"
Manages entities, their signatures and component type registrations.
Definition Registry.hpp:68
Component for entities capable of shooting projectiles.
Definition Weapon.hpp:28
High-level ECS manager providing clean server-side API.
Definition ECSWorld.hpp:122
void createSystem(const std::string &name, Args &&...args)
Create and register a system.
std::vector< Entity > query()
Get all entities with specific components.
Entity createEntity()
Create a new entity.
Definition ECSWorld.cpp:67
void createSystem(SystemId id, Args &&...args)
Create and register a system using an enum identifier.
std::unordered_map< std::string, std::unique_ptr< ISystem > > _systems
Definition ECSWorld.hpp:125
Entity getEntity(Address address)
Get an entity wrapper from an address.
Definition ECSWorld.cpp:83
void update(float deltaTime)
Update all registered systems.
Definition ECSWorld.cpp:114
~ECSWorld()
Destroy the ECSWorld and cleanup resources.
std::unique_ptr< Registry > _registry
Definition ECSWorld.hpp:124
T * getSystem(SystemId id)
Get a registered system by enum identifier.
bool startingEvent
World state (for accessing from Lua scripts)d 0 = not running, 1 = starting event,...
Definition ECSWorld.hpp:320
std::vector< Entity > createEntities(size_t count)
Create multiple entities at once.
Definition ECSWorld.cpp:72
Registry & getRegistry()
Get direct access to the underlying registry.
Definition ECSWorld.cpp:146
void destroyEntity(const Entity &entity)
Destroy an entity and remove it from the world.
Definition ECSWorld.cpp:87
void clear()
Clear all entities from the world.
Definition ECSWorld.cpp:154
ECSWorld()
Construct a new ECSWorld.
Definition ECSWorld.cpp:63
std::vector< std::string > _systemsOrder
Definition ECSWorld.hpp:126
void registerSystem(const std::string &name, std::unique_ptr< T > system)
Register a system with the world.
bool updateSystem(const std::string &name, float deltaTime)
Update a specific system by name.
Definition ECSWorld.cpp:127
void removeSystem(const std::string &name)
Remove a system from the world.
Definition ECSWorld.cpp:97
T * getSystem(const std::string &name)
Get a registered system by name.
size_t getSystemCount() const
Get the number of systems registered.
Definition ECSWorld.cpp:162
void forEach(std::function< void(Entity, Components &...)> callback)
Iterate over entities with specific components.
High-level entity wrapper providing fluent interface.
Definition ECSWorld.hpp:33
Entity & with(const T &component)
Add/set a component to this entity.
bool has() const
Check if this entity has a specific component.
Registry * _registry
Definition ECSWorld.hpp:36
Entity & remove()
Remove a component from this entity.
const T & get() const
Get a component from this entity (const version).
T & get()
Get a component from this entity.
Address getAddress() const
Get the entity's address.
Definition ECSWorld.cpp:39
bool isValid() const
Check if this entity is valid.
Definition ECSWorld.cpp:43
std::string_view systemIdToName(SystemId id)
Definition ECSWorld.cpp:14
std::uint32_t Address
Type used to represent an entity address/ID.