SolarSim
Loading...
Searching...
No Matches
registry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include <unordered_set>
5#include <vector>
6#include <tuple>
7#include <typeindex>
8#include <memory>
9#include "entity.hpp"
10
11namespace solarsim {
12
16 class Registry {
17 public:
22 [[nodiscard]] Entity createEntity() {
23 Entity e = nextEntity++;
24 entities.insert(e);
25 return e;
26 }
27
37 template<typename Component>
38 void addComponent(Entity e, Component c) {
40 }
41
50 template<typename Component>
51 Component& getComponent(Entity e) {
52 return getComponentStorage<Component>().at(e);
53 }
54
61 template<typename Component>
63 auto& storage = getComponentStorage<Component>();
64 return storage.find(e) != storage.end();
65 }
66
72 template<typename... Components>
73 [[nodiscard]] std::vector<Entity> view() {
74 std::vector<Entity> result;
75 if constexpr (sizeof...(Components) == 0) return result;
76
77 auto& firstStorage = getComponentStorage<std::tuple_element_t<0, std::tuple<Components...>>>();
78
79 for (auto& [entity, comp] : firstStorage) {
80 bool hasAll = (hasComponent<Components>(entity) && ...);
81 if (hasAll) result.push_back(entity);
82 }
83
84 return result;
85 }
86
91 const auto& getEntities() const { return entities; }
92
93 private:
95 std::unordered_set<Entity> entities;
97 uint32_t nextEntity = 0;
98
103 virtual ~IComponentStorage() = default;
104 };
105
110 template<typename Component>
113 std::unordered_map<Entity, Component> components;
114 };
115
117 std::unordered_map<std::type_index, std::unique_ptr<IComponentStorage>> componentStorages;
118
126 template<typename Component>
127 std::unordered_map<Entity, Component>& getComponentStorage() {
128 auto typeIndex = std::type_index(typeid(Component));
129 auto it = componentStorages.find(typeIndex);
130 if (it == componentStorages.end()) {
131 auto storage = std::make_unique<ComponentStorage<Component>>();
132 auto& components = storage->components;
133 componentStorages[typeIndex] = std::move(storage);
134 return components;
135 }
136 return static_cast<ComponentStorage<Component>*>(it->second.get())->components;
137 }
138 };
139}
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
Definition engine.cpp:23
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