Acheron
Loading...
Searching...
No Matches
acheron::ecs::World Class Reference

Central context for the ECS. More...

#include <world.hpp>

Public Member Functions

 World ()
 Constructs a new World, initializing managers.
Entity Spawn ()
 Spawns a new entity.
void AddStageBefore (std::string name, std::string after)
 Creates a stage before another.
void AddStageAfter (std::string name, std::string before)
 Creates a stage before another.
template<typename... Cs, typename... Overrides>
Entity SpawnWith (Overrides &&... overrides)
 Spawns an entity with components.
void Despawn (Entity entity)
 Despawns(destroys) an entity.
template<typename T>
void RegisterComponent ()
 Registers a new component type with the ECS.
template<typename T>
bool HasComponent (Entity entity)
 Checks if en entity has a component.
template<typename... Components, typename Func>
void View (Func &&func)
template<typename T, typename... Args>
void AddComponent (Entity entity, Args &&... args)
 Adds a component to an entity.
template<typename T>
void RemoveComponent (Entity entity)
 Removes a component from an entity.
template<typename T>
T & GetComponent (Entity entity)
 Retrieves a reference to a component on an entity.
template<typename T>
ComponentID GetComponentID ()
 Retrieves the unique ID associated with a component type.
template<typename... Components>
Signature MakeSignature ()
 Creates a signature that includes all specified component types.
template<typename T>
std::shared_ptr< T > RegisterSystem (Signature signature={}, std::string stage="Update")
 Registers a system of type T.
template<typename Func>
std::shared_ptr< SystemRegisterSystemExplicit (Func &&func, Signature signature={}, std::string stage="Update")
 Registers a system explicitly from a function object or lambda.
template<typename... Components, typename Func>
std::shared_ptr< SystemRegisterSystem (Func &&func, std::string stage="Update")
 Registers a system from a lambda or callable with component type deduction.
template<typename T>
void SetSystemSignature (Signature signature)
 Sets the component signature for a system.
template<typename T>
void SetSingleton (T value)
 Stores a singleton instance in the world.
template<typename T>
T & GetSingleton ()
 Retrieves a singleton instance from the world.
template<typename T>
bool IsSingletonSet ()
 Checks if singleton is set.
template<typename T>
void Import ()
 Imports a module into the world.
template<typename T>
void SubscribeEvent (EventManager::Callback< T > cb)
 Subscribes a callback to an event.
template<typename T>
void EmitEvent (const T &event)
 Emit an event to the event queue.
void DispatchEvents ()
 Dispatch events.
void Update (double dt=0.0)
 Runs system updates in the world.

Detailed Description

Central context for the ECS.

This class manages EVERYTHING to do with the ECS

Member Function Documentation

◆ AddComponent()

template<typename T, typename... Args>
void acheron::ecs::World::AddComponent ( Entity entity,
Args &&... args )
inline

Adds a component to an entity.

Also updates the entity's signature and notifies systems

Template Parameters
TComponent type
Parameters
entityThe entity receiving the component
componentOptional instance of the component (default constructed if omitted)

◆ AddStageAfter()

void World::AddStageAfter ( std::string name,
std::string before )

Creates a stage before another.

Parameters
nameThe name of the system to register after
afterThe system that will be before the registered stage

◆ AddStageBefore()

void World::AddStageBefore ( std::string name,
std::string after )

Creates a stage before another.

Parameters
nameThe name of the system to register before
afterThe system that will be after the registered stage

◆ Despawn()

void World::Despawn ( Entity entity)

Despawns(destroys) an entity.

Cleans up all associated components and notifies systems

Parameters
entityThe entity to remove

◆ EmitEvent()

template<typename T>
void acheron::ecs::World::EmitEvent ( const T & event)
inline

Emit an event to the event queue.

Template Parameters
TEvent type to emit
Parameters
eventEvent and its data

◆ GetComponent()

template<typename T>
T & acheron::ecs::World::GetComponent ( Entity entity)
inline

Retrieves a reference to a component on an entity.

Template Parameters
TComponent type
Parameters
entityThe entity to query
Returns
Reference to the component

◆ GetComponentID()

template<typename T>
ComponentID acheron::ecs::World::GetComponentID ( )
inline

Retrieves the unique ID associated with a component type.

Template Parameters
TComponent type
Returns
ComponentID for the given type

◆ GetSingleton()

template<typename T>
T & acheron::ecs::World::GetSingleton ( )
inline

Retrieves a singleton instance from the world.

Template Parameters
TSingleton type
Returns
Reference to the stored singleton
Exceptions
Assertfailure if the singleton has not been set

◆ HasComponent()

template<typename T>
bool acheron::ecs::World::HasComponent ( Entity entity)
inline

Checks if en entity has a component.

Parameters
entityThe entity to check
Returns
If component has entity

◆ Import()

template<typename T>
void acheron::ecs::World::Import ( )
inline

Imports a module into the world.

The module must inherit from Module

Template Parameters
TModule type

◆ IsSingletonSet()

template<typename T>
bool acheron::ecs::World::IsSingletonSet ( )
inline

Checks if singleton is set.

Template Parameters
TThe singleton to check
Returns
If the singleton is set or not

◆ MakeSignature()

template<typename... Components>
Signature acheron::ecs::World::MakeSignature ( )
inline

Creates a signature that includes all specified component types.

Template Parameters
Components...Variadic list of component types
Returns
Signature representing the component set

◆ RegisterComponent()

template<typename T>
void acheron::ecs::World::RegisterComponent ( )
inline

Registers a new component type with the ECS.

Template Parameters
TThe component type to register

◆ RegisterSystem() [1/2]

template<typename... Components, typename Func>
std::shared_ptr< System > acheron::ecs::World::RegisterSystem ( Func && func,
std::string stage = "Update" )
inline

Registers a system from a lambda or callable with component type deduction.

Example:

world.RegisterSystem<Position, Velocity>(
[](Entity e, Position& pos, Velocity& vel) { ... }
);
Template Parameters
Components...The component types the system operates on
FuncCallable type
Parameters
funcThe system function/lambda
stageThe update stage (default Update)
Returns
Shared pointer to the created system

◆ RegisterSystem() [2/2]

template<typename T>
std::shared_ptr< T > acheron::ecs::World::RegisterSystem ( Signature signature = {},
std::string stage = "Update" )
inline

Registers a system of type T.

Template Parameters
TSystem type (must inherit from System)
Parameters
signatureThe component signature this system cares about (default empty)
stageThe update stage to run this system in (default Update)
Returns
Shared pointer to the created system

◆ RegisterSystemExplicit()

template<typename Func>
std::shared_ptr< System > acheron::ecs::World::RegisterSystemExplicit ( Func && func,
Signature signature = {},
std::string stage = "Update" )
inline

Registers a system explicitly from a function object or lambda.

Allows flexible system definition without requiring a class

Template Parameters
FuncCallable type.
Parameters
funcThe system function/lambda
signatureComponent signature this system cares about (default empty)
stageUpdate stage to run the system in (default Update)
Returns
Shared pointer to the created system

◆ RemoveComponent()

template<typename T>
void acheron::ecs::World::RemoveComponent ( Entity entity)
inline

Removes a component from an entity.

Updates the entity's signature and notifies systems

Template Parameters
TComponent type to remove
Parameters
entityThe entity to modify

◆ SetSingleton()

template<typename T>
void acheron::ecs::World::SetSingleton ( T value)
inline

Stores a singleton instance in the world.

Template Parameters
TSingleton type
Parameters
valueThe instance to store

◆ SetSystemSignature()

template<typename T>
void acheron::ecs::World::SetSystemSignature ( Signature signature)
inline

Sets the component signature for a system.

Template Parameters
TSystem type
Parameters
signatureThe signature to associate with the system

◆ Spawn()

Entity World::Spawn ( )

Spawns a new entity.

Returns
The newly created entity

◆ SpawnWith()

template<typename... Cs, typename... Overrides>
Entity acheron::ecs::World::SpawnWith ( Overrides &&... overrides)
inline

Spawns an entity with components.

Example:

world.SpawnWith<Player, Health>(Health{20});
Template Parameters
CsComponents that are to be added to the entity
OverridesThe actual component data passed to the function
Parameters
overridesSame as the template parameter Overrides
Exceptions
StaticAssert Fail if Overrides has a type Cs doesnt contain
Returns
The newly created entity with the components specified

◆ SubscribeEvent()

template<typename T>
void acheron::ecs::World::SubscribeEvent ( EventManager::Callback< T > cb)
inline

Subscribes a callback to an event.

Template Parameters
TEvent type
Parameters
cbCallback to subscribe to the event

◆ Update()

void World::Update ( double dt = 0.0)

Runs system updates in the world.

Systems are executed in stage order:

  • Start (once)
  • PreUpdate
  • Update
  • PostUpdate
Parameters
dtDelta time passed to systems (default 0.0)

The documentation for this class was generated from the following files: