EntityComponentMetaSystem
An implementation of an EntityComponent System with template meta-programming.
|
Manages an EntityComponent system. More...
#include <Manager.hpp>
Classes | |
struct | TPFnDataStructFive |
Temporary struct used internally by ThreadPool. More... | |
struct | TPFnDataStructFour |
Temporary struct used internally by ThreadPool. More... | |
struct | TPFnDataStructOne |
Temporary struct used internally by ThreadPool. More... | |
struct | TPFnDataStructSeven |
Temporary struct used internally by ThreadPool. More... | |
struct | TPFnDataStructSix |
Temporary struct used internally by ThreadPool. More... | |
struct | TPFnDataStructThree |
Temporary struct used internally by ThreadPool. More... | |
struct | TPFnDataStructTwo |
Temporary struct used internally by ThreadPool. More... | |
struct | TPFnDataStructZero |
Temporary struct used internally by ThreadPool. More... | |
Public Types | |
using | Components = ComponentsList |
using | Tags = TagsList |
using | Combined = EC::Meta::Combine< ComponentsList, TagsList > |
using | BitsetType = EC::Bitset< ComponentsList, TagsList > |
typedef void | ForMatchingFn(std::size_t, Manager< ComponentsList, TagsList > *, void *) |
Public Member Functions | |
Manager () | |
Initializes the manager with a default capacity. | |
std::size_t | addEntity () |
Adds an entity to the system, returning the ID of the entity. | |
void | deleteEntity (std::size_t index) |
Marks an entity for deletion. | |
bool | hasEntity (const std::size_t &index) const |
Checks if the Entity with the given ID is in the system. | |
bool | isAlive (const std::size_t &index) const |
Checks if the Entity is not marked as deleted. | |
std::size_t | getCurrentSize () const |
Returns the current size or number of entities in the system. | |
std::size_t | getCurrentCapacity () const |
const EntitiesTupleType & | getEntityInfo (const std::size_t &index) const |
Returns a const reference to an Entity's info. | |
template<typename Component > | |
Component * | getEntityData (const std::size_t &index) |
Returns a pointer to a component belonging to the given Entity. | |
template<typename Component > | |
Component * | getEntityComponent (const std::size_t &index) |
Returns a pointer to a component belonging to the given Entity. | |
template<typename Component > | |
const Component * | getEntityData (const std::size_t &index) const |
Returns a const pointer to a component belonging to the given Entity. | |
template<typename Component > | |
const Component * | getEntityComponent (const std::size_t &index) const |
Returns a const pointer to a component belonging to the given Entity. | |
template<typename Component > | |
bool | hasComponent (const std::size_t &index) const |
Checks whether or not the given Entity has the given Component. | |
template<typename Tag > | |
bool | hasTag (const std::size_t &index) const |
Checks whether or not the given Entity has the given Tag. | |
template<typename Component , typename... Args> | |
void | addComponent (const std::size_t &entityID, Args &&... args) |
Adds a component to the given Entity. | |
template<typename Component > | |
void | removeComponent (const std::size_t &entityID) |
Removes the given Component from the given Entity. | |
template<typename Tag > | |
void | addTag (const std::size_t &entityID) |
Adds the given Tag to the given Entity. | |
template<typename Tag > | |
void | removeTag (const std::size_t &entityID) |
Removes the given Tag from the given Entity. | |
void | reset () |
Resets the Manager, removing all entities. | |
template<typename Signature , typename Function > | |
void | forMatchingSignature (Function &&function, void *userData=nullptr, const bool useThreadPool=false) |
Calls the given function on all Entities matching the given Signature. | |
template<typename Signature , typename Function > | |
void | forMatchingSignaturePtr (Function *function, void *userData=nullptr, const bool useThreadPool=false) |
Calls the given function on all Entities matching the given Signature. | |
template<typename Signature , typename Function > | |
std::size_t | addForMatchingFunction (Function &&function, void *userData=nullptr) |
Stores a function in the manager to be called later. | |
void | callForMatchingFunctions (const bool useThreadPool=false) |
Call all stored functions. | |
bool | callForMatchingFunction (std::size_t id, const bool useThreadPool=false) |
Call a specific stored function. | |
void | clearForMatchingFunctions () |
Remove all stored functions. | |
bool | removeForMatchingFunction (std::size_t id) |
Removes a function that has the given id. | |
template<typename List > | |
std::size_t | keepSomeMatchingFunctions (List list) |
Removes all functions that do not have the index specified in argument "list". | |
std::size_t | keepSomeMatchingFunctions (std::initializer_list< std::size_t > list) |
Removes all functions that do not have the index specified in argument "list". | |
template<typename List > | |
std::size_t | removeSomeMatchingFunctions (List list) |
Removes all functions that do have the index specified in argument "list". | |
std::size_t | removeSomeMatchingFunctions (std::initializer_list< std::size_t > list) |
Removes all functions that do have the index specified in argument "list". | |
bool | changeForMatchingFunctionContext (std::size_t id, void *userData) |
Sets the context pointer of a stored function. | |
template<typename SigList , typename FTuple > | |
void | forMatchingSignatures (FTuple fTuple, void *userData=nullptr, const bool useThreadPool=false) |
Call multiple functions with mulitple signatures on all living entities. | |
template<typename SigList , typename FTuple > | |
void | forMatchingSignaturesPtr (FTuple fTuple, void *userData=nullptr, const bool useThreadPool=false) |
Call multiple functions with mulitple signatures on all living entities. | |
template<typename Signature > | |
void | forMatchingSimple (ForMatchingFn fn, void *userData=nullptr, const bool useThreadPool=false) |
A simple version of forMatchingSignature() | |
template<typename Iterable > | |
void | forMatchingIterable (Iterable iterable, ForMatchingFn fn, void *userData=nullptr, const bool useThreadPool=false) |
Similar to forMatchingSimple(), but with a collection of Component/Tag indices. | |
Manages an EntityComponent system.
EC::Manager must be created with a list of all used Components and all used tags.
Note that all components must have a default constructor.
An optional third template parameter may be given, which is the size of the number of threads in the internal ThreadPool, and should be at least 2. If ThreadCount is 1 or less, then the ThreadPool will not be created and it will never be used, even if the "true" parameter is given for functions that enable its usage.
Note that when calling one of the "forMatching" functions that make use of the internal ThreadPool, it is allowed to call addEntity() or deleteEntity() as the functions cache which entities are alive before running (allowing for addEntity()), and the functions defer deletions during concurrent execution (allowing for deleteEntity()).
Example:
|
inline |
Initializes the manager with a default capacity.
The default capacity is set with macro EC_INIT_ENTITIES_SIZE, and will grow by amounts of EC_GROW_SIZE_AMOUNT when needed.
|
inline |
Adds a component to the given Entity.
Additional parameters given to this function will construct the Component with those parameters.
Note that if the Entity already has the same component, then it will be overwritten by the newly created Component with the given arguments.
If the Entity is not alive or the given Component is not known to the Manager, then nothing will change.
Example:
|
inline |
Adds an entity to the system, returning the ID of the entity.
Note: The ID of an entity is guaranteed to not change.
|
inline |
Stores a function in the manager to be called later.
As an alternative to calling functions directly with forMatchingSignature(), functions can be stored in the manager to be called later with callForMatchingFunctions() and callForMatchingFunction, and removed with clearForMatchingFunctions() and removeForMatchingFunction().
The syntax for the Function is the same as with forMatchingSignature().
Note that functions will be called in the same order they are inserted if called by callForMatchingFunctions() unless the internal functionIndex counter has wrapped around (is a std::size_t). Calling clearForMatchingFunctions() will reset this counter to zero.
Note that the context pointer provided here (default nullptr) will be provided to the stored function when called.
Example:
|
inline |
Adds the given Tag to the given Entity.
Example:
|
inline |
Call a specific stored function.
The second parameter can be optionally used to enable the use of the internal ThreadPool to call the stored function in parallel. Using the value false (which is the default) will not use the ThreadPool and run the stored function sequentially on the main thread. Note that multi-threading is based on splitting the task of calling the functions across sections of entities. Thus if there are only a small amount of entities in the manager, then using multiple threads may not have as great of a speed-up.
Example:
|
inline |
Call all stored functions.
The first (and only) parameter can be optionally used to enable the use of the internal ThreadPool to call all stored functions in parallel. Using the value false (which is the default) will not use the ThreadPool and run all stored functions sequentially on the main thread. Note that multi-threading is based on splitting the task of calling the functions across sections of entities. Thus if there are only a small amount of entities in the manager, then using multiple threads may not have as great of a speed-up.
Example:
|
inline |
Sets the context pointer of a stored function.
|
inline |
Remove all stored functions.
Also resets the index counter of stored functions to 0.
Example:
|
inline |
Marks an entity for deletion.
A deleted Entity's id is stored to be reclaimed later when addEntity is called. Thus calling addEntity may return an id of a previously deleted Entity.
|
inline |
Similar to forMatchingSimple(), but with a collection of Component/Tag indices.
This function works like forMatchingSimple(), but instead of providing template types that filter out non-matching entities, an iterable of indices must be provided which correlate to matching Component/Tag indices. The function given must match the previously defined typedef of type ForMatchingFn.
The fourth parameter can be optionally used to enable the use of the internal ThreadPool to call the function in parallel. Using the value false (which is the default) will not use the ThreadPool and run the function sequentially on all entities on the main thread. Note that multi-threading is based on splitting the task of calling the functions across sections of entities. Thus if there are only a small amount of entities in the manager, then using multiple threads may not have as great of a speed-up.
|
inline |
Calls the given function on all Entities matching the given Signature.
The function object given to this function must accept std::size_t as its first parameter, void* as its second parameter, and Component pointers for the rest of the parameters. Tags specified in the Signature are only used as filters and will not be given as a parameter to the function.
The second parameter is default nullptr and will be passed to the function call as the second parameter as a means of providing context (useful when the function is not a lambda function).
The third parameter is default false (not multi-threaded). Otherwise, if true, then the thread pool will be used to call the given function in parallel across all entities. Note that multi-threading is based on splitting the task of calling the function across sections of entities. Thus if there are only a small amount of entities in the manager, then using multiple threads may not have as great of a speed-up.
Example:
Note, the ID given to the function is not permanent. An entity's ID may change when cleanup() is called.
|
inline |
Calls the given function on all Entities matching the given Signature.
The function pointer given to this function must accept std::size_t as its first parameter, void* as its second parameter, and Component pointers for the rest of the parameters. Tags specified in the Signature are only used as filters and will not be given as a parameter to the function.
The second parameter is default nullptr and will be passed to the function call as the second parameter as a means of providing context (useful when the function is not a lambda function).
The third parameter is default false (not multi-threaded). Otherwise, if true, then the thread pool will be used to call the given function in parallel across all entities. Note that multi-threading is based on splitting the task of calling the function across sections of entities. Thus if there are only a small amount of entities in the manager, then using multiple threads may not have as great of a speed-up.
Example:
Note, the ID given to the function is not permanent. An entity's ID may change when cleanup() is called.
|
inline |
Call multiple functions with mulitple signatures on all living entities.
(Living entities as in entities that have not been marked for deletion.)
This function requires the first template parameter to be a EC::Meta::TypeList of signatures. Note that a signature is a EC::Meta::TypeList of components and tags, meaning that SigList is a TypeList of TypeLists.
The second template parameter can be inferred from the function parameter which should be a tuple of functions. The function at any index in the tuple should match with a signature of the same index in the SigList. Behavior is undefined if there are less functions than signatures.
See the Unit Test of this function in src/test/ECTest.cpp for usage examples.
The second parameter (default nullptr) will be provided to every function call as a void* (context).
The third parameter is default false (not multi-threaded). Otherwise, if true, then the thread pool will be used to call the given function in parallel across all entities. Note that multi-threading is based on splitting the task of calling the function across sections of entities. Thus if there are only a small amount of entities in the manager, then using multiple threads may not have as great of a speed-up.
This function was created for the use case where there are many entities in the system which can cause multiple calls to forMatchingSignature to be slow due to the overhead of iterating through the entire list of entities on each invocation. This function instead iterates through all entities once, storing matching entities in a vector of vectors (for each signature and function pair) and then calling functions with the matching list of entities.
Note that multi-threaded or not, functions will be called in order of signatures. The first function signature pair will be called first, then the second, third, and so on. If this function is called with more than 1 thread specified, then the order of entities called is not guaranteed. Otherwise entities will be called in consecutive order by their ID.
|
inline |
Call multiple functions with mulitple signatures on all living entities.
(Living entities as in entities that have not been marked for deletion.)
Note that this function requires the tuple of functions to hold pointers to functions, not just functions.
This function requires the first template parameter to be a EC::Meta::TypeList of signatures. Note that a signature is a EC::Meta::TypeList of components and tags, meaning that SigList is a TypeList of TypeLists.
The second template parameter can be inferred from the function parameter which should be a tuple of functions. The function at any index in the tuple should match with a signature of the same index in the SigList. Behavior is undefined if there are less functions than signatures.
See the Unit Test of this function in src/test/ECTest.cpp for usage examples.
The second parameter (default nullptr) will be provided to every function call as a void* (context).
The third parameter is default false (not multi-threaded). Otherwise, if true, then the thread pool will be used to call the given function in parallel across all entities. Note that multi-threading is based on splitting the task of calling the function across sections of entities. Thus if there are only a small amount of entities in the manager, then using multiple threads may not have as great of a speed-up.
This function was created for the use case where there are many entities in the system which can cause multiple calls to forMatchingSignature to be slow due to the overhead of iterating through the entire list of entities on each invocation. This function instead iterates through all entities once, storing matching entities in a vector of vectors (for each signature and function pair) and then calling functions with the matching list of entities.
Note that multi-threaded or not, functions will be called in order of signatures. The first function signature pair will be called first, then the second, third, and so on. If this function is called with more than 1 thread specified, then the order of entities called is not guaranteed. Otherwise entities will be called in consecutive order by their ID.
|
inline |
A simple version of forMatchingSignature()
This function behaves like forMatchingSignature(), but instead of providing a function with each requested component as a parameter, the function receives a pointer to the manager itself, with which to query component/tag data.
The third parameter can be optionally used to enable the use of the internal ThreadPool to call the function in parallel. Using the value false (which is the default) will not use the ThreadPool and run the function sequentially on all entities on the main thread. Note that multi-threading is based on splitting the task of calling the functions across sections of entities. Thus if there are only a small amount of entities in the manager, then using multiple threads may not have as great of a speed-up.
|
inline |
Returns the current size or number of entities in the system.
Note this function will only count entities where isAlive() returns true.
|
inline |
Returns a pointer to a component belonging to the given Entity.
Note that this function is the same as getEntityData().
This function will return a pointer to a Component regardless of whether or not the Entity actually owns the Component. If the Entity doesn't own the Component, changes to the Component will not affect any Entity. It is recommended to use hasComponent() to determine if the Entity actually owns that Component.
If the given Component is unknown to the Manager, then this function will return a nullptr.
|
inline |
Returns a const pointer to a component belonging to the given Entity.
Note that this function is the same as getEntityData() (const).
This function will return a const pointer to a Component regardless of whether or not the Entity actually owns the Component. If the Entity doesn't own the Component, changes to the Component will not affect any Entity. It is recommended to use hasComponent() to determine if the Entity actually owns that Component.
If the given Component is unknown to the Manager, then this function will return a nullptr.
|
inline |
Returns a pointer to a component belonging to the given Entity.
This function will return a pointer to a Component regardless of whether or not the Entity actually owns the Component. If the Entity doesn't own the Component, changes to the Component will not affect any Entity. It is recommended to use hasComponent() to determine if the Entity actually owns that Component.
If the given Component is unknown to the Manager, then this function will return a nullptr.
|
inline |
Returns a const pointer to a component belonging to the given Entity.
This function will return a const pointer to a Component regardless of whether or not the Entity actually owns the Component. If the Entity doesn't own the Component, changes to the Component will not affect any Entity. It is recommended to use hasComponent() to determine if the Entity actually owns that Component.
If the given Component is unknown to the Manager, then this function will return a nullptr.
|
inline |
Returns a const reference to an Entity's info.
An Entity's info is a std::tuple with a bool, and a bitset.
The bool determines if the Entity is alive.
The bitset shows what Components and Tags belong to the Entity.
|
inline |
Checks whether or not the given Entity has the given Component.
Example:
|
inline |
Checks if the Entity with the given ID is in the system.
Note that deleted Entities are still considered in the system. Consider using isAlive().
|
inline |
Checks whether or not the given Entity has the given Tag.
Example:
|
inline |
Checks if the Entity is not marked as deleted.
Note that invalid Entities (Entities where calls to hasEntity() returns false) will return false.
|
inline |
Removes all functions that do not have the index specified in argument "list".
The given List must be iterable. This is the only requirement, so a set could also be given.
|
inline |
Removes all functions that do not have the index specified in argument "list".
This function allows for passing an initializer list.
|
inline |
Removes the given Component from the given Entity.
If the Entity does not have the Component given, nothing will change.
Example:
|
inline |
Removes a function that has the given id.
|
inline |
Removes all functions that do have the index specified in argument "list".
The given List must be iterable. This is the only requirement, so a set could also be given.
|
inline |
Removes all functions that do have the index specified in argument "list".
This function allows for passing an initializer list.
|
inline |
Removes the given Tag from the given Entity.
If the Entity does not have the Tag given, nothing will change.
Example:
|
inline |