Registry
A Registry is the core class of the ECS, which stores the differents SparseArrays in an unordered_map, using the type of the Component stored in the SparseArray as the index in the associative array.
When a SparseArray is created, it creates multiple functions: - a function that allows us to delete the Component of a given Entity (stored in _erase_functions). - a function that allows us to add a new Entity to the array (stored in _add_functions).
There is also the Entity management part in the Registry, to create, delete and update the differents Components of the Entity.
The Registry class looks like this:
class Registry
{
public:
Registry(){};
template <class Component>
sparse_array<Component> ®ister_component();
template <class Component>
sparse_array<Component> &get_components();
template <class Component>
sparse_array<Component> const &get_components() const;
// #########################################################
// # ENTITY MANAGEMENT #
// #########################################################
Entity spawn_entity();
void kill_entity(Entity const &e)
template <typename Component>
typename sparse_array<Component>::reference_type add_component(Entity const &to, Component &&c);
template <typename Component, typename... Params>
typename sparse_array<Component>::reference_type emplace_component(Entity const &to, Params &&...p);
template <typename Component>
void remove_component(Entity const &from);
private:
std::unordered_map<std::type_index, std::any> _components_arrays;
std::unordered_map<std::type_index, std::function<void(Registry &, Entity const &)>> _erase_functions;
std::unordered_map<std::type_index, std::function<void(Registry &)>> _add_functions;
std::list<int> _empty_entities;
};
Constructor
Registry()
Default constructor of RegistryFunctions
register_components()
Creates and adds a SparseArray using the given Component in the Registry, and returns it.Usage
Here the Velocity parameter is a Component.get_components()
Returns the SparseArray of the given Component.get_components()
Same but returns a const SparseArray of the given Component.Entity management
Functions
spawn_entity()
Creates a new Entity, by using the index of a previously deleted Entity (using_empty_entities), or by calling _add_functions on every SparseArray existing, and returns an Entity containing the index.
kill_entity()
Deletes the Entity, by calling_delete_functions on every SparseArray, and adds the index of the newly deleted Entity in _empty_entities.
add_component()
template <typename Component>
typename sparse_array<Component>::reference_type add_component(Entity const &to, Component &&c);
emplace_component()
template <typename Component, typename... Params>
typename sparse_array<Component>::reference_type emplace_component(Entity const &to, Params &&...p);
remove_component()
Removes the value at the given index (Entity) from the SparseArray corresponding to the given Component;Values
_components_arrays
Stores the different SparseArrays._erase_functions
std::unordered_map<std::type_index, std::function<void(Registry &, Entity const &)>> _erase_functions;