3#include <unordered_map>
4#include <unordered_set>
37 template<
typename Component>
50 template<
typename Component>
61 template<
typename Component>
64 return storage.find(e) != storage.end();
72 template<
typename... Components>
73 [[nodiscard]] std::vector<Entity>
view() {
74 std::vector<Entity> result;
75 if constexpr (
sizeof...(Components) == 0)
return result;
77 auto& firstStorage =
getComponentStorage<std::tuple_element_t<0, std::tuple<Components...>>>();
79 for (
auto& [entity, comp] : firstStorage) {
81 if (hasAll) result.push_back(entity);
110 template<
typename Component>
126 template<
typename Component>
128 auto typeIndex = std::type_index(
typeid(Component));
131 auto storage = std::make_unique<ComponentStorage<Component>>();
132 auto& components = storage->components;
Manages entities and their components in the ECS.
Definition registry.hpp:16
bool hasComponent(Entity e)
Check if an entity has a specific component.
Definition registry.hpp:62
Component & getComponent(Entity e)
Get a component from an entity.
Definition registry.hpp:51
void addComponent(Entity e, Component c)
Add a component to an entity.
Definition registry.hpp:38
std::unordered_map< Entity, Component > & getComponentStorage()
Get or create the storage for a component type.
Definition registry.hpp:127
const auto & getEntities() const
Definition registry.hpp:91
uint32_t nextEntity
Definition registry.hpp:97
std::unordered_set< Entity > entities
Definition registry.hpp:95
std::unordered_map< std::type_index, std::unique_ptr< IComponentStorage > > componentStorages
Definition registry.hpp:117
std::vector< Entity > view()
Get all entities that have all specified components.
Definition registry.hpp:73
Entity createEntity()
Create a new entity.
Definition registry.hpp:22
uint32_t Entity
Entity identifier type for ECS.
Definition entity.hpp:9
Typed component storage for a specific component type.
Definition registry.hpp:111
std::unordered_map< Entity, Component > components
Definition registry.hpp:113
Base interface for type-erased component storage.
Definition registry.hpp:102
virtual ~IComponentStorage()=default