summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/entity_manager.cpp4
-rw-r--r--src/entity_manager.h29
2 files changed, 21 insertions, 12 deletions
diff --git a/src/entity_manager.cpp b/src/entity_manager.cpp index f792e17..0aaaf8e 100644 --- a/src/entity_manager.cpp +++ b/src/entity_manager.cpp
@@ -5,7 +5,7 @@
5 5
6template <> 6template <>
7std::set<EntityManager::id_type> EntityManager::getEntitiesWithComponents<>( 7std::set<EntityManager::id_type> EntityManager::getEntitiesWithComponents<>(
8 std::set<std::type_index>& componentTypes) 8 std::set<std::type_index>& componentTypes) const
9{ 9{
10 if (cachedComponents.count(componentTypes) == 1) 10 if (cachedComponents.count(componentTypes) == 1)
11 { 11 {
@@ -15,7 +15,7 @@ std::set<EntityManager::id_type> EntityManager::getEntitiesWithComponents<>(
15 std::set<id_type>& cache = cachedComponents[componentTypes]; 15 std::set<id_type>& cache = cachedComponents[componentTypes];
16 for (id_type entity = 0; entity < entities.size(); entity++) 16 for (id_type entity = 0; entity < entities.size(); entity++)
17 { 17 {
18 EntityData& data = entities[entity]; 18 const EntityData& data = entities[entity];
19 bool cacheEntity = true; 19 bool cacheEntity = true;
20 20
21 for (auto& componentType : componentTypes) 21 for (auto& componentType : componentTypes)
diff --git a/src/entity_manager.h b/src/entity_manager.h index 1e8d31c..b2ef0de 100644 --- a/src/entity_manager.h +++ b/src/entity_manager.h
@@ -27,13 +27,15 @@ private:
27 database_type entities; 27 database_type entities;
28 std::vector<bool> slotAvailable; 28 std::vector<bool> slotAvailable;
29 std::set<id_type> allEntities; 29 std::set<id_type> allEntities;
30 std::map<std::set<std::type_index>, std::set<id_type>> cachedComponents; 30
31 mutable std::map<std::set<std::type_index>, std::set<id_type>>
32 cachedComponents;
31 33
32 id_type nextEntityID = 0; 34 id_type nextEntityID = 0;
33 35
34 template <class T, class... R> 36 template <class T, class... R>
35 std::set<id_type> getEntitiesWithComponentsHelper( 37 std::set<id_type> getEntitiesWithComponentsHelper(
36 std::set<std::type_index>& componentTypes) 38 std::set<std::type_index>& componentTypes) const
37 { 39 {
38 componentTypes.insert(typeid(T)); 40 componentTypes.insert(typeid(T));
39 41
@@ -42,7 +44,7 @@ private:
42 44
43 template <class... R> 45 template <class... R>
44 std::set<id_type> getEntitiesWithComponents( 46 std::set<id_type> getEntitiesWithComponents(
45 std::set<std::type_index>& componentTypes) 47 std::set<std::type_index>& componentTypes) const
46 { 48 {
47 return getEntitiesWithComponentsHelper<R...>(componentTypes); 49 return getEntitiesWithComponentsHelper<R...>(componentTypes);
48 } 50 }
@@ -168,14 +170,14 @@ public:
168 } 170 }
169 171
170 template <class T> 172 template <class T>
171 T& getComponent(id_type entity) 173 const T& getComponent(id_type entity) const
172 { 174 {
173 if ((entity >= entities.size()) || slotAvailable[entity]) 175 if ((entity >= entities.size()) || slotAvailable[entity])
174 { 176 {
175 throw std::invalid_argument("Cannot get non-existent entity"); 177 throw std::invalid_argument("Cannot get non-existent entity");
176 } 178 }
177 179
178 EntityData& data = entities[entity]; 180 const EntityData& data = entities[entity];
179 std::type_index componentType = typeid(T); 181 std::type_index componentType = typeid(T);
180 182
181 if (!data.components.count(componentType)) 183 if (!data.components.count(componentType))
@@ -183,25 +185,32 @@ public:
183 throw std::invalid_argument("Cannot get non-existent component"); 185 throw std::invalid_argument("Cannot get non-existent component");
184 } 186 }
185 187
186 return *dynamic_cast<T*>(data.components[componentType].get()); 188 return *dynamic_cast<const T*>(data.components.at(componentType).get());
187 } 189 }
188 190
189 template <class T> 191 template <class T>
190 bool hasComponent(id_type entity) 192 T& getComponent(id_type entity)
193 {
194 return const_cast<T&>(
195 static_cast<const EntityManager&>(*this).getComponent<T>(entity));
196 }
197
198 template <class T>
199 bool hasComponent(id_type entity) const
191 { 200 {
192 if ((entity >= entities.size()) || slotAvailable[entity]) 201 if ((entity >= entities.size()) || slotAvailable[entity])
193 { 202 {
194 throw std::invalid_argument("Cannot get non-existent entity"); 203 throw std::invalid_argument("Cannot get non-existent entity");
195 } 204 }
196 205
197 EntityData& data = entities[entity]; 206 const EntityData& data = entities[entity];
198 std::type_index componentType = typeid(T); 207 std::type_index componentType = typeid(T);
199 208
200 return data.components.count(componentType); 209 return data.components.count(componentType);
201 } 210 }
202 211
203 template <class... R> 212 template <class... R>
204 std::set<id_type> getEntitiesWithComponents() 213 std::set<id_type> getEntitiesWithComponents() const
205 { 214 {
206 std::set<std::type_index> componentTypes; 215 std::set<std::type_index> componentTypes;
207 216
@@ -216,6 +225,6 @@ public:
216 225
217template <> 226template <>
218std::set<EntityManager::id_type> EntityManager::getEntitiesWithComponents<>( 227std::set<EntityManager::id_type> EntityManager::getEntitiesWithComponents<>(
219 std::set<std::type_index>& componentTypes); 228 std::set<std::type_index>& componentTypes) const;
220 229
221#endif /* end of include guard: ENTITY_MANAGER_H_C5832F11 */ 230#endif /* end of include guard: ENTITY_MANAGER_H_C5832F11 */