Systems and entities coordinator. More...
#include <ecsm.hpp>
Classes | |
struct | Event |
Event subscribers holder. More... | |
Public Types | |
using | Systems = std::unordered_map<std::type_index, System*> |
using | ComponentTypes = std::unordered_map<std::type_index, System*> |
using | ComponentNames = std::map<std::string, System*> |
using | Events = std::unordered_map<std::string, Event*> |
using | OrderedEvents = std::vector<const Event*> |
using | EntityPool = LinearPool<Entity> |
using | GarbageComponent = std::pair<std::type_index, ID<Entity>> |
using | GarbageComponents = std::set<GarbageComponent> |
typedef Singleton< Manager, UseManager > | Instance |
Singleton class type. | |
Public Member Functions | |
Manager (bool setSingleton=true) | |
Creates a new manager instance. | |
~Manager () | |
Destroys manager and all components/entities/systems. | |
template<class T = System, typename... Args> | |
void | createSystem (Args &&... args) |
Creates a new system instance. | |
void | destroySystem (std::type_index type) |
Terminates and destroys system. | |
template<class T = System> | |
void | destroySystem () |
Terminates and destroys system. | |
bool | tryDestroySystem (std::type_index type) |
Terminates and destroys system if exist. | |
template<class T = System> | |
bool | tryDestroySystem () |
Terminates and destroys system if exist. | |
bool | has (std::type_index type) const noexcept |
Returns true if system is created. | |
template<class T = System> | |
bool | has () const noexcept |
Returns true if system is created. | |
System * | get (std::type_index type) const |
Returns system instance. | |
template<class T = System> | |
T * | get () const |
Returns system instance. | |
System * | tryGet (std::type_index type) const noexcept |
Returns system instance if created, otherwise nullptr. | |
template<class T = System> | |
T * | tryGet () const noexcept |
Returns system instance if created, otherwise nullptr. | |
ID< Entity > | createEntity () |
Creates a new entity instance. | |
void | destroy (ID< Entity > instance) |
Destroys entity instance and it components. | |
View< Entity > | get (ID< Entity > instance) const noexcept |
Returns entity internal data accessor. (View) | |
View< Component > | add (ID< Entity > entity, std::type_index componentType) |
Adds a new component to the entity. | |
template<class T = Component> | |
View< T > | add (ID< Entity > entity) |
Adds a new component to the entity. | |
void | remove (ID< Entity > entity, std::type_index componentType) |
Removes component from the entity. | |
template<class T = Component> | |
void | remove (ID< Entity > entity) |
Removes component from the entity. | |
bool | isGarbage (ID< Entity > entity, std::type_index componentType) const noexcept |
Returns true if target entity component was removed and is in the garbage pool. | |
template<class T = Component> | |
bool | isGarbage (ID< Entity > entity) const noexcept |
Returns true if target entity component was removed and is in the garbage pool. | |
void | copy (ID< Entity > source, ID< Entity > destination, std::type_index componentType) |
Copies component data from source entity to destination. | |
template<class T = Component> | |
void | copy (ID< Entity > source, ID< Entity > destination) |
Copies component data from source entity to destination. | |
ID< Entity > | duplicate (ID< Entity > entity) |
Creates a duplicate of specified entity. | |
bool | has (ID< Entity > entity, std::type_index componentType) const noexcept |
Returns true if entity has target component. | |
template<class T = Component> | |
bool | has (ID< Entity > entity) const noexcept |
Returns true if entity has target component. | |
View< Component > | get (ID< Entity > entity, std::type_index componentType) const |
Returns component data accessor. (View) | |
template<class T = Component> | |
View< T > | get (ID< Entity > entity) const |
Returns component data accessor. (View) | |
View< Component > | tryGet (ID< Entity > entity, std::type_index componentType) const noexcept |
Returns component data accessor if exist, otherwise null. (View) | |
template<class T = Component> | |
View< T > | tryGet (ID< Entity > entity) const noexcept |
Returns component data accessor if exist, otherwise null. (View) | |
ID< Component > | getID (ID< Entity > entity, std::type_index componentType) const |
Returns entity component ID. | |
template<class T = Component> | |
ID< T > | getID (ID< Entity > entity) const |
Returns entity component ID. | |
ID< Component > | tryGetID (ID< Entity > entity, std::type_index componentType) const noexcept |
Returns entity component ID if added, otherwise null. | |
template<class T = Component> | |
ID< T > | tryGetID (ID< Entity > entity) const noexcept |
Returns entity component ID if added, otherwise null. | |
uint32_t | getComponentCount (ID< Entity > entity) const noexcept |
Returns entity component count. | |
void | registerEvent (const std::string &name) |
Registers a new unordered event. | |
bool | tryRegisterEvent (const std::string &name) |
Registers a new unordered event if not exist. | |
void | registerEventBefore (const std::string &newEvent, const std::string &beforeEvent) |
Registers a new ordered event before another. | |
void | registerEventAfter (const std::string &newEvent, const std::string &afterEvent) |
Registers a new ordered event after another. | |
void | unregisterEvent (const std::string &name) |
Unregisters existing event. | |
bool | tryUnregisterEvent (const std::string &name) |
Unregisters event if exist. | |
bool | hasEvent (const std::string &name) const noexcept |
Returns true if event is registered. | |
bool | isEventOrdered (const std::string &name) const |
Returns true if event is ordered. | |
const Event::Subscribers & | getEventSubscribers (const std::string &name) const |
Returns all event subscribers. | |
bool | isEventHasSubscribers (const std::string &name) const |
Returns true if event has subscribers. | |
void | runEvent (const std::string &name) |
Calls all event subscribers. | |
bool | tryRunEvent (const std::string &name) |
Calls all event subscribers if event exist. | |
void | runOrderedEvents () |
Runs all ordered events. | |
void | subscribeToEvent (const std::string &name, const std::function< void()> &onEvent) |
Adds a new event subscriber. | |
void | unsubscribeFromEvent (const std::string &name, const std::function< void()> &onEvent) |
Removes existing event subscriber. | |
bool | trySubscribeToEvent (const std::string &name, const std::function< void()> &onEvent) |
Adds a new event subscriber if not exist. | |
bool | tryUnsubscribeFromEvent (const std::string &name, const std::function< void()> &onEvent) |
Removes existing event subscriber if exist. | |
const Systems & | getSystems () const noexcept |
Returns all manager systems. | |
const ComponentTypes & | getComponentTypes () const noexcept |
Returns all manager component types. | |
const ComponentNames & | getComponentNames () const noexcept |
Returns all manager component names. | |
const Events & | getEvents () const noexcept |
Returns all manager events. | |
const OrderedEvents & | getOrderedEvents () const noexcept |
Returns ordered manager events. | |
const EntityPool & | getEntities () const noexcept |
Returns all manager entities. | |
const GarbageComponents & | getGarbageComponents () const noexcept |
Returns manager garbage components pool. | |
bool | isInitialized () const noexcept |
Returns true if manager is initialized. | |
void | initialize () |
Initializes all created systems. | |
void | update () |
Runs ordered events and disposes destroyed resources on each tick. | |
void | start () |
Enters update loop. Executes update() on each tick. | |
void | disposeGarbageComponents () |
Actually destroys garbage components. | |
void | disposeSystemComponents () |
Actually destroys system components and internal resources. | |
void | disposeEntities () |
Actually destroys entities. | |
void | lock () |
Locks manager for synchronous access. (MT-Safe) | |
bool | tryLock () noexcept |
Tries to locks manager for synchronous access. (MT-Safe) | |
void | unlock () noexcept |
Unlock manager after synchronous access. (MT-Safe) | |
void | setSingletonCurrent () noexcept |
Sets manager singleton to this instance. | |
void | unsetSingletonCurrent () noexcept |
Unsets manager singleton instance. | |
Static Public Member Functions | |
static bool | has () |
Returns true if class singleton or manager instance is exist. | |
static Manager * | get () |
Returns class singleton or manager instance. | |
static Manager * | tryGet () |
Returns class singleton or manager instance if exist. | |
Public Attributes | |
bool | isRunning = false |
Protected Member Functions | |
void | setSingleton () |
Sets a new class singleton instance. | |
void | unsetSingleton () noexcept |
Unsets this class singleton instance. | |
Static Protected Attributes | |
static Manager * | singletonInstance |
Singleton class instance. | |
Systems and entities coordinator.
Manager serves as a central coordinating object responsible for overseeing various aspects of the system. It manages entity-related tasks such as creation, destruction, and component assignment. Additionally, it handles the storage and retrieval of components, facilitates system initialization, configuration and supports event handling for communication and reaction to changes.
Manager | ( | bool | setSingleton = true | ) |
Creates a new manager instance.
setSingleton | set manager singleton instance |
|
inline |
Creates a new system instance.
Instantiates a new system and registers it component, but initialization occurs only after the initialize() call.
T | target system type |
Args | additional argument types |
args | additional system creation arguments |
EcsmError | if system is already created or component type registered. |
void destroySystem | ( | std::type_index | type | ) |
Terminates and destroys system.
type | target system typeid() |
EcsmError | if system is not found. |
|
inline |
Terminates and destroys system.
T | target system type |
EcsmError | if system is not found. |
bool tryDestroySystem | ( | std::type_index | type | ) |
Terminates and destroys system if exist.
type | target system typeid() |
|
inline |
Terminates and destroys system if exist.
T | target system type |
|
inlinenoexcept |
Returns true if system is created.
type | target system typeid() |
|
inlinenoexcept |
Returns true if system is created.
T | target system type |
|
inline |
Returns system instance.
type | target system typeid() |
EcsmError | if system is not found. |
|
inline |
Returns system instance.
T | target system type |
EcsmError | if system is not found. |
|
inlinenoexcept |
Returns system instance if created, otherwise nullptr.
type | target system typeid() |
|
inlinenoexcept |
Returns system instance if created, otherwise nullptr.
T | target system type |
Creates a new entity instance.
Created entity does not have any default component.
Destroys entity instance and it components.
instance | target entity instance or null |
Returns entity internal data accessor. (View)
instance | target entity instance |
View< Component > add | ( | ID< Entity > | entity, |
std::type_index | componentType ) |
Adds a new component to the entity.
See the add<T>(ID<Entity> entity).
entity | entity instance |
componentType | target component typeid() |
EcsmError | if component type is not registered, or component is already added. |
Adds a new component to the entity.
Only one component type can be attached to the entity, but several components with different types. Target component should be registered by some System.
entity | entity instance |
T | target component type |
EcsmError | if component type is not registered, or component is already added. |
void remove | ( | ID< Entity > | entity, |
std::type_index | componentType ) |
Removes component from the entity.
See the remove<T>(ID<Entity> entity).
entity | entity instance |
componentType | target component typeid() |
EcsmError | if component is not found. |
|
inlinenoexcept |
Returns true if target entity component was removed and is in the garbage pool.
See the remove<T>(ID<Entity> entity).
entity | entity instance |
componentType | target component typeid() |
Returns true if target entity component was removed and is in the garbage pool.
See the remove<T>(ID<Entity> entity).
entity | entity instance |
T | target component type |
void copy | ( | ID< Entity > | source, |
ID< Entity > | destination, | ||
std::type_index | componentType ) |
Copies component data from source entity to destination.
See the copy<T>(ID<Entity> source, ID<Entity> destination).
source | copy from entity instance |
destination | copy to entity instance |
componentType | target component typeid() |
EcsmError | if source or destination component is not found. |
|
inlinenoexcept |
Returns true if entity has target component.
entity | entity instance |
componentType | target component typeid() |
Returns true if entity has target component.
entity | entity instance |
T | target component type |
|
inline |
|
inlinenoexcept |
Returns component data accessor if exist, otherwise null. (View)
entity | entity instance |
componentType | target component typeid() |
Returns component data accessor if exist, otherwise null. (View)
entity | entity instance |
T | target component type |
|
inline |
|
inlinenoexcept |
Returns entity component ID if added, otherwise null.
See the tryGetID<T>(ID<Entity> entity).
entity | entity instance |
componentType | target component typeid() |
Returns entity component ID if added, otherwise null.
Useful when we need to access and store entity component identifiers.
entity | entity instance |
T | target component type |
Returns entity component count.
entity | target entity instance |
void registerEvent | ( | const std::string & | name | ) |
Registers a new unordered event.
[in] | name | target event name |
EcsmError | if event is already registered. |
bool tryRegisterEvent | ( | const std::string & | name | ) |
Registers a new unordered event if not exist.
[in] | name | target event name |
void registerEventBefore | ( | const std::string & | newEvent, |
const std::string & | beforeEvent ) |
Registers a new ordered event before another.
[in] | newEvent | target event name |
[in] | beforeEvent | name of the event after target event |
EcsmError | if event is already registered. |
void registerEventAfter | ( | const std::string & | newEvent, |
const std::string & | afterEvent ) |
Registers a new ordered event after another.
[in] | newEvent | target event name |
[in] | afterEvent | name of the event before target event |
EcsmError | if event is already registered. |
void unregisterEvent | ( | const std::string & | name | ) |
Unregisters existing event.
[in] | name | target event name |
EcsmError | if event is not registered, or not found. |
bool tryUnregisterEvent | ( | const std::string & | name | ) |
Unregisters event if exist.
[in] | name | target event name |
|
inlinenoexcept |
Returns true if event is registered.
[in] | name | target event name |
bool isEventOrdered | ( | const std::string & | name | ) | const |
Returns true if event is ordered.
[in] | name | target event name |
EcsmError | if event is not registered. |
const Event::Subscribers & getEventSubscribers | ( | const std::string & | name | ) | const |
Returns all event subscribers.
[in] | name | target event name |
EcsmError | if event is not registered. |
bool isEventHasSubscribers | ( | const std::string & | name | ) | const |
Returns true if event has subscribers.
[in] | name | target event name |
EcsmError | if event is not registered. |
void runEvent | ( | const std::string & | name | ) |
Calls all event subscribers.
[in] | name | target event name |
EcsmError | if event is not registered. |
bool tryRunEvent | ( | const std::string & | name | ) |
Calls all event subscribers if event exist.
[in] | name | target event name |
void runOrderedEvents | ( | ) |
Runs all ordered events.
Unordered events subscribers are not called.
void subscribeToEvent | ( | const std::string & | name, |
const std::function< void()> & | onEvent ) |
Adds a new event subscriber.
[in] | name | target event name |
[in] | onEvent | on event function callback |
EcsmError | if event is not registered. |
void unsubscribeFromEvent | ( | const std::string & | name, |
const std::function< void()> & | onEvent ) |
Removes existing event subscriber.
[in] | name | target event name |
[in] | onEvent | on event function callback |
EcsmError | if event is not registered, or not subscribed. |
bool trySubscribeToEvent | ( | const std::string & | name, |
const std::function< void()> & | onEvent ) |
Adds a new event subscriber if not exist.
[in] | name | target event name |
[in] | onEvent | on event function callback |
bool tryUnsubscribeFromEvent | ( | const std::string & | name, |
const std::function< void()> & | onEvent ) |
Removes existing event subscriber if exist.
[in] | name | target event name |
[in] | onEvent | on event function callback |
True | if unsubscribed from the event, otherwise false. |
|
inlinenoexcept |
Returns all manager systems.
|
inlinenoexcept |
Returns all manager component types.
|
inlinenoexcept |
Returns all manager component names.
|
inlinenoexcept |
Returns all manager events.
|
inlinenoexcept |
Returns ordered manager events.
|
inlinenoexcept |
Returns all manager entities.
|
inlinenoexcept |
Returns manager garbage components pool.
void initialize | ( | ) |
Initializes all created systems.
EcsmError | if manager is already initialized. |
void update | ( | ) |
Runs ordered events and disposes destroyed resources on each tick.
EcsmError | if manager is not initialized. |
void start | ( | ) |
void disposeGarbageComponents | ( | ) |
Actually destroys garbage components.
Components are not destroyed immediately, only after the dispose call.
void disposeSystemComponents | ( | ) |
Actually destroys system components and internal resources.
Systen components are not destroyed immediately, only after the dispose call.
|
inline |
Actually destroys entities.
Entities are not destroyed immediately, only after the dispose call.
|
inline |
Locks manager for synchronous access. (MT-Safe)
|
inlinenoexcept |
Tries to locks manager for synchronous access. (MT-Safe)
|
inlinenoexcept |
Unlock manager after synchronous access. (MT-Safe)
|
inlinenoexcept |
Sets manager singleton to this instance.
Useful in cases when we need to switch between multiple managers.
|
inlinenoexcept |
Unsets manager singleton instance.
See the setSingletonCurrent().