Nui Engine
A game engine framework
Loading...
Searching...
No Matches
Context.h
1#pragma once
2#include "Core/Engine/ECS/Common.h"
3#include <concepts>
4
5namespace Nui::ECS
6{
10 template <typename T>
11 concept IsSystem = std::is_base_of<SystemBase, T>::value;
12
16 class Context
17 {
18 public:
22 using SubscriberMap = std::unordered_map<TypeIndex, std::vector<Internal::EventSubscriberBase*>, std::hash<TypeIndex>, std::equal_to<TypeIndex>>;
23
24
25 public:
30
34 virtual ~Context();
35
40 inline U64 GetEntityCount() const noexcept { return m_entities.size(); }
41
47 Entity* GetEntityById(U64 id);
48
54 Entity* GetEntityByIndex(U64 index);
55
61
67 void DestroyEntity(Entity* e, bool immediate = false);
68
73 bool ClearPending();
74
78 void Reset();
79
87 template <typename T, typename... Args>
88 T* RegisterSystem(Args&&... args) requires IsSystem<T>;
89
94 template <typename T>
95 void UnregisterSystem() requires IsSystem<T>;
96
101
107 template <typename T>
108 T* GetSystem() requires IsSystem<T>;
109
114 template <typename T>
115 void EnableSystem() requires IsSystem<T>;
116
121 template <typename T>
122 void DisableSystem() requires IsSystem<T>;
123
129 template <typename T>
130 bool IsSystmEnabled() requires IsSystem<T>;
131
137 template <typename T>
138 void SubscribeEvent(EventSubscriber<T>* subscriber);
139
145 template<typename T>
146 void UnsubscribeEvent(EventSubscriber<T>* subscriber);
147
152 void UnsubscribeAll(void* subscriber);
153
159 template<typename T>
160 void EmitEvent(const T& event);
161
168 template<typename... Types>
169 Internal::EntityComponentView<Types...> Each(bool includePendingDestroy = false);
170
177 template<typename... Types>
178 void Each(typename std::common_type<std::function<void(Entity*, ComponentHandle<Types>...)>>::type viewFunc, bool includePendingDestroy = false);
179
185 Internal::EntityView All(bool includePendingDestroy = false);
186
192 void All(std::function<void(Entity*)> viewFunc, bool includePendingDestroy = false);
193
198 void Tick(const F64 dt);
199
200 private:
204 std::vector<std::unique_ptr<Entity>> m_entities;
205
209 std::vector<std::unique_ptr<SystemBase>> m_systems;
210
214 SubscriberMap m_subscribers;
215
219 U64 m_lastEntityId{ 0 };
220 };
221}
This class provides a handle to a component.
Definition Component.h:52
The ECS context class manages entities, systems, and events in the ECS framework.
Definition Context.h:17
void UnregisterSystem()
Unregisters a system from the ECS context.
Definition ECS.h:387
void UnregisterAllSystems()
Un-registers all systems from the ECS context.
Definition ECS.h:355
Internal::EntityComponentView< Types... > Each(bool includePendingDestroy=false)
Iterates over all entities in the ECS context (used in ranged loops)
Definition ECS.h:551
void DisableSystem()
Disables a system in the ECS context.
Definition ECS.h:436
Internal::EntityView All(bool includePendingDestroy=false)
Iterates over all entities in the ECS context (used in ranged loops)
Definition ECS.h:519
void SubscribeEvent(EventSubscriber< T > *subscriber)
Subscribes to an event in the ECS context.
Definition ECS.h:460
void UnsubscribeAll(void *subscriber)
Un-subscribes from all events in the ECS context (usually called in the system destructor)
Definition ECS.h:501
void Reset()
Resets the ECS context, clearing all entities but keeping the systems.
Definition ECS.h:340
void Tick(const F64 dt)
Updates the ECS context (updates all the systems)
Definition ECS.h:567
Entity * CreateEntity()
Creates a new entity in the ECS context.
Definition ECS.h:266
bool ClearPending()
Clears all entities pending destruction in the ECS context.
Definition ECS.h:317
void EmitEvent(const T &event)
Emits an event in the ECS context to all subscribers.
Definition ECS.h:535
U64 GetEntityCount() const noexcept
Gets the total count of entities in the ECS context.
Definition Context.h:40
virtual ~Context()
Destroys the ECS context.
Definition ECS.h:216
void UnsubscribeEvent(EventSubscriber< T > *subscriber)
Unsubscribes from an event in the ECS context.
Definition ECS.h:480
T * RegisterSystem(Args &&... args)
Registers a new system in the ECS context.
Definition ECS.h:367
bool IsSystmEnabled()
Checks if a system is enabled in the ECS context.
Definition ECS.h:448
void EnableSystem()
Enables a system in the ECS context.
Definition ECS.h:424
void DestroyEntity(Entity *e, bool immediate=false)
Destroys an entity in the ECS context.
Definition ECS.h:278
Entity * GetEntityById(U64 id)
Retrieves an entity by its unique identifier.
Definition ECS.h:243
Context()
Constructs a new ECS context.
Entity * GetEntityByIndex(U64 index)
Retrieves an entity by its index.
Definition ECS.h:258
T * GetSystem()
Gets a system from the ECS context.
Definition ECS.h:411
std::unordered_map< TypeIndex, std::vector< Internal::EventSubscriberBase * >, std::hash< TypeIndex >, std::equal_to< TypeIndex > > SubscriberMap
Unordered map of subscribers to array of events subscriber base pointers.
Definition Context.h:22
Class to represent an entity in an ECS Context.
Definition Entity.h:10
Base class for all event subscribers.
Definition Event.h:27
Base class for all ECS systems.
Definition System.h:10
A concept that checks if a class derives from SystemBase.
Definition Context.h:11