3#include "singleton.hpp"
5#include "system_function.hpp"
9#include "component.hpp"
14namespace acheron::ecs {
64 template<
typename... Cs,
typename... Overrides>
68 auto check_override = [](
auto&& override_val) {
69 using T = std::decay_t<
decltype(override_val)>;
70 static_assert((std::disjunction_v<std::is_same<T, Cs>...>),
71 "Override type not in component list!");
74 (check_override(overrides), ...);
77 (AddOrDefault<Cs>(e, std::forward<Overrides>(overrides)...), ...);
97 componentManager->RegisterComponent<T>();
109 return componentManager->HasComponent<T>(entity);
112 template<
typename... Components,
typename Func>
113 void View(Func&& func) {
116 for (
auto& [entity, sig] : entityManager->signatures) {
117 bool match = std::includes(
118 sig.begin(), sig.end(),
119 required.begin(), required.end()
137 template<
typename T,
typename... Args>
139 componentManager->AddComponent<T>(entity, T(std::forward<Args>(args)...));
141 auto signature = entityManager->GetSignature(entity);
142 signature.insert(componentManager->GetComponentID<T>());
143 entityManager->SetSignature(entity, signature);
145 systemManager->EntitySignatureChanged(entity, signature);
158 componentManager->RemoveComponent<T>(entity);
160 auto signature = entityManager->GetSignature(entity);
161 signature.erase(componentManager->GetComponentID<T>());
162 entityManager->SetSignature(entity, signature);
164 systemManager->EntitySignatureChanged(entity, signature);
178 return componentManager->GetComponent<T>(entity);
188 return componentManager->GetComponentID<T>();
198 template<
typename... Components>
216 std::shared_ptr<T>
RegisterSystem(Signature signature = {}, std::string stage =
"Update") {
217 auto system = systemManager->RegisterSystem<T>(stage);
235 template<
typename Func>
238 auto systemFunc = std::make_shared<SystemType>(std::forward<Func>(func));
240 static int counter = 0;
241 std::string typeName =
"LambdaSystem_" + std::to_string(counter++);
243 systemManager->systems[typeName] = systemFunc;
244 if (!signature.empty()) {
245 systemManager->signatures[typeName] = signature;
247 systemManager->stageSystems[systemManager->GetStageOrFail(stage)].push_back(systemFunc);
270 template<
typename... Components,
typename Func>
271 std::shared_ptr<System>
RegisterSystem(Func&& func, std::string stage =
"Update") {
285 systemManager->SetSignature<T>(signature);
334 static_assert(std::is_base_of<Module, T>::value,
"Import must inherit from Module");
337 module.Register(*this);
349 eventManager->Subscribe<T>(*
this, cb);
361 eventManager->Emit<T>(event);
368 eventManager->Dispatch();
382 void Update(
double dt = 0.0);
397 template<
typename C,
typename... Overrides>
398 void AddOrDefault(Entity entity, Overrides&&... overrides) {
404 if constexpr(std::is_same_v<std::decay_t<Overrides>, C>) {
416 bool hasStarted =
false;
417 std::unique_ptr<EntityManager> entityManager;
418 std::unique_ptr<ComponentManager> componentManager;
419 std::unique_ptr<SystemManager> systemManager;
420 std::unique_ptr<EventManager> eventManager;
std::function< void(World &, const T &)> Callback
Function signature for event callbacks.
Definition event.hpp:29
static bool IsSet()
Check if the singleton is set.
Definition singleton.hpp:40
static void Set(const T &value)
Initializes singleton and sets the instance value.
Definition singleton.hpp:19
static T & Get()
Get the instance of a singleton.
Definition singleton.hpp:30
Wrapper class for systems to support lambda and function pointers.
Definition system_function.hpp:15
void SetSystemSignature(Signature signature)
Sets the component signature for a system.
Definition world.hpp:284
void AddComponent(Entity entity, Args &&... args)
Adds a component to an entity.
Definition world.hpp:138
void RemoveComponent(Entity entity)
Removes a component from an entity.
Definition world.hpp:157
void Despawn(Entity entity)
Despawns(destroys) an entity.
Definition world.cpp:44
std::shared_ptr< System > RegisterSystemExplicit(Func &&func, Signature signature={}, std::string stage="Update")
Registers a system explicitly from a function object or lambda.
Definition world.hpp:236
Signature MakeSignature()
Creates a signature that includes all specified component types.
Definition world.hpp:199
std::shared_ptr< System > RegisterSystem(Func &&func, std::string stage="Update")
Registers a system from a lambda or callable with component type deduction.
Definition world.hpp:271
void SetSingleton(T value)
Stores a singleton instance in the world.
Definition world.hpp:296
void RegisterComponent()
Registers a new component type with the ECS.
Definition world.hpp:96
void Update(double dt=0.0)
Runs system updates in the world.
Definition world.cpp:51
Entity SpawnWith(Overrides &&... overrides)
Spawns an entity with components.
Definition world.hpp:65
Entity Spawn()
Spawns a new entity.
Definition world.cpp:40
void SubscribeEvent(EventManager::Callback< T > cb)
Subscribes a callback to an event.
Definition world.hpp:348
void EmitEvent(const T &event)
Emit an event to the event queue.
Definition world.hpp:360
T & GetSingleton()
Retrieves a singleton instance from the world.
Definition world.hpp:309
ComponentID GetComponentID()
Retrieves the unique ID associated with a component type.
Definition world.hpp:187
bool IsSingletonSet()
Checks if singleton is set.
Definition world.hpp:321
void DispatchEvents()
Dispatch events.
Definition world.hpp:367
void Import()
Imports a module into the world.
Definition world.hpp:333
bool HasComponent(Entity entity)
Checks if en entity has a component.
Definition world.hpp:108
T & GetComponent(Entity entity)
Retrieves a reference to a component on an entity.
Definition world.hpp:177
void AddStageBefore(std::string name, std::string after)
Creates a stage before another.
Definition world.cpp:30
std::shared_ptr< T > RegisterSystem(Signature signature={}, std::string stage="Update")
Registers a system of type T.
Definition world.hpp:216
void AddStageAfter(std::string name, std::string before)
Creates a stage before another.
Definition world.cpp:35
World()
Constructs a new World, initializing managers.
Definition world.cpp:10