#ifndef COMPONENTMANAGER_HPP #define COMPONENTMANAGER_HPP #include #include #include #include "entity.hpp" #include "component.hpp" class IComponentArray { public: virtual ~IComponentArray() = default; virtual void EntityDestroyed(Entity entity) = 0; }; template class ComponentArray : public IComponentArray { // The packed array of components (of generic type T), // set to a specified maximum amount, matching the maximum number // of entities allowed to exist simultaneously, so that each entity // has a unique spot. std::array mComponentArray; // Map from an entity ID to an array index. std::unordered_map mEntityToIndexMap; // Map from an array index to an entity ID. std::unordered_map mIndexToEntityMap; // Total size of valid entries in the array. size_t mSize; public: void InsertData(Entity entity, T component); void RemoveData(Entity entity); T &GetData(Entity entity); void EntityDestroyed(Entity entity) override; }; class ComponentManager { // Map from type string pointer to a component type std::unordered_map mComponentTypes{}; // Map from type string pointer to a component array std::unordered_map> mComponentArrays{}; // The component type to be assigned to the next registered component - // starting at 0 ComponentType mNextComponentType{}; // Convenience function to get the statically casted pointer to the // ComponentArray of type T. template std::shared_ptr> GetComponentArray(); public: template void RegisterComponent(); template ComponentType GetComponentType(); template void AddComponent(Entity entity, T component); template void RemoveComponent(Entity entity); template T &GetComponent(Entity entity); void EntityDestroyed(Entity entity); }; #endif // COMPONENTMANAGER_HPP