From 55c8a14a7e2b2dadf0def3e09f970818164366f5 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 18 Jun 2015 12:14:05 -0400 Subject: Now displaying player character --- src/entity_manager.h | 169 +++++++-------------------------------------------- 1 file changed, 21 insertions(+), 148 deletions(-) (limited to 'src/entity_manager.h') diff --git a/src/entity_manager.h b/src/entity_manager.h index 74e758c..5f0f2d4 100644 --- a/src/entity_manager.h +++ b/src/entity_manager.h @@ -6,70 +6,31 @@ #include #include #include "component.h" +#include "algorithms.h" class EntityManager { private: struct EntityData { - int parent = -1; std::map> components; }; std::map entities; - std::map> cachedChildren; std::map, std::set> cachedComponents; int nextEntityID = 0; - bool ensureNoParentCycles(int entity, int parent) + template + std::set getEntitiesWithComponentsHelper(std::set& componentTypes) { - EntityData& data = entities[parent]; - if (data.parent == entity) - { - return false; - } else if (data.parent == -1) - { - return true; - } + componentTypes.insert(typeid(T)); - return ensureNoParentCycles(entity, data.parent); + return getEntitiesWithComponents(componentTypes); } + template std::set getEntitiesWithComponents(std::set& componentTypes) { - if (cachedComponents.count(componentTypes) == 1) - { - return cachedComponents[componentTypes]; - } - - std::set& cache = cachedComponents[componentTypes]; - for (auto& entity : entities) - { - EntityData& data = entity.second; - bool cacheEntity = true; - - for (auto& componentType : componentTypes) - { - if (data.components.count(componentType) == 0) - { - cacheEntity = false; - break; - } - } - - if (cacheEntity) - { - cache.insert(entity.first); - } - } - - return cache; - } - - template std::set getEntitiesWithComponents(std::set& componentTypes) - { - componentTypes.insert(typeid(T)); - - return getEntitiesWithComponents(componentTypes); + return getEntitiesWithComponentsHelper(componentTypes); } public: @@ -107,26 +68,6 @@ class EntityManager { { assert(entities.count(entity) == 1); - EntityData& data = entities[entity]; - - // Destroy the children - std::set children = getChildren(entity); - for (int child : children) - { - EntityData& childData = entities[child]; - childData.parent = -1; - - deleteEntity(child); - } - - // Uncache children - cachedChildren.erase(entity); - - if ((data.parent != -1) && (cachedChildren.count(data.parent) == 1)) - { - cachedChildren[data.parent].erase(entity); - } - // Uncache components for (auto& cache : cachedComponents) { @@ -137,81 +78,8 @@ class EntityManager { entities.erase(entity); } - std::set getChildren(int parent) - { - assert(entities.count(parent) == 1); - - if (cachedChildren.count(parent) == 1) - { - return cachedChildren[parent]; - } - - std::set& cache = cachedChildren[parent]; - for (auto& entity : entities) - { - EntityData& data = entity.second; - if (data.parent == parent) - { - cache.insert(entity.first); - } - } - - return cache; - } - - void setParent(int entity, int parent) - { - assert(entities.count(entity) == 1); - assert(entities.count(parent) == 1); - assert(ensureNoParentCycles(entity, parent)); - - EntityData& data = entities[entity]; - - // Remove from old parent - if (data.parent != -1) - { - if (cachedChildren.count(data.parent) == 1) - { - cachedChildren[data.parent].erase(entity); - } - } - - data.parent = parent; - - // Cache new parent - if (cachedChildren.count(parent) == 1) - { - cachedChildren[parent].insert(entity); - } - } - - void setNoParent(int entity) - { - assert(entities.count(entity) == 1); - - EntityData& data = entities[entity]; - - // Remove from old parent - if (data.parent != -1) - { - if (cachedChildren.count(data.parent) == 1) - { - cachedChildren[data.parent].erase(entity); - } - } - - data.parent = -1; - } - - int getParent(int entity) - { - assert(entities.count(entity) == 1); - - EntityData& data = entities[entity]; - return data.parent; - } - - template T& emplaceComponent(int entity, Args&&... args) + template + T& emplaceComponent(int entity, Args&&... args) { assert(entities.count(entity) == 1); @@ -226,14 +94,15 @@ class EntityManager { data.components[componentType] = std::move(ptr); // Invalidate related caches - std::remove_if(begin(cachedComponents), end(cachedComponents), [&] (std::pair, int>& cache) { + erase_if(cachedComponents, [&componentType] (std::pair, std::set>& cache) { return cache.first.count(componentType) == 1; }); return component; } - template void removeComponent(int entity) + template + void removeComponent(int entity) { assert(entities.count(entity) == 1); @@ -255,7 +124,8 @@ class EntityManager { } } - template T& getComponent(int entity) + template + T& getComponent(int entity) { assert(entities.count(entity) == 1); @@ -264,16 +134,19 @@ class EntityManager { assert(data.components.count(componentType) == 1); - return *(data.components[componentType]); + return *((T*)data.components[componentType].get()); } - template std::set getEntitiesWithComponents() + template + std::set getEntitiesWithComponents() { std::set componentTypes; - componentTypes.insert(typeid(T)); - return getEntitiesWithComponents(componentTypes); + return getEntitiesWithComponentsHelper(componentTypes); } }; +template <> +std::set EntityManager::getEntitiesWithComponents<>(std::set& componentTypes); + #endif /* end of include guard: ENTITY_MANAGER_H_C5832F11 */ -- cgit 1.4.1