summary refs log tree commit diff stats
path: root/src/entity_manager.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-02-07 14:13:32 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-02-07 14:13:32 -0500
commit97d7fb425da906947cc45e11fadb35f708da50d6 (patch)
treeae1a4a5bbbce97e5dcf50e7390f005fe9be61356 /src/entity_manager.cpp
parentda3df061699203eccc9a0c98becaee3ce8050a4f (diff)
downloadtherapy-97d7fb425da906947cc45e11fadb35f708da50d6.tar.gz
therapy-97d7fb425da906947cc45e11fadb35f708da50d6.tar.bz2
therapy-97d7fb425da906947cc45e11fadb35f708da50d6.zip
Changed EntityManager to dense vector
This should improve speed, because entity lookup will be O(1) instead of O(log n). Deletion is also O(1). Insert stays at potentially O(n), but still should be overall faster than the previous method.

Also replaced some asserts with exceptions.

Also made Component polymorphic so that deletion actually works properly.
Diffstat (limited to 'src/entity_manager.cpp')
-rw-r--r--src/entity_manager.cpp11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/entity_manager.cpp b/src/entity_manager.cpp index 9ad758b..f792e17 100644 --- a/src/entity_manager.cpp +++ b/src/entity_manager.cpp
@@ -4,17 +4,18 @@
4#include "entity_manager.h" 4#include "entity_manager.h"
5 5
6template <> 6template <>
7std::set<int> EntityManager::getEntitiesWithComponents<>(std::set<std::type_index>& componentTypes) 7std::set<EntityManager::id_type> EntityManager::getEntitiesWithComponents<>(
8 std::set<std::type_index>& componentTypes)
8{ 9{
9 if (cachedComponents.count(componentTypes) == 1) 10 if (cachedComponents.count(componentTypes) == 1)
10 { 11 {
11 return cachedComponents[componentTypes]; 12 return cachedComponents[componentTypes];
12 } 13 }
13 14
14 std::set<int>& cache = cachedComponents[componentTypes]; 15 std::set<id_type>& cache = cachedComponents[componentTypes];
15 for (auto& entity : entities) 16 for (id_type entity = 0; entity < entities.size(); entity++)
16 { 17 {
17 EntityData& data = entity.second; 18 EntityData& data = entities[entity];
18 bool cacheEntity = true; 19 bool cacheEntity = true;
19 20
20 for (auto& componentType : componentTypes) 21 for (auto& componentType : componentTypes)
@@ -28,7 +29,7 @@ std::set<int> EntityManager::getEntitiesWithComponents<>(std::set<std::type_inde
28 29
29 if (cacheEntity) 30 if (cacheEntity)
30 { 31 {
31 cache.insert(entity.first); 32 cache.insert(entity);
32 } 33 }
33 } 34 }
34 35