#pragma once #include "ComponentArray.hpp" #include "Types.hpp" #include #include #include class ComponentManager { public: template void RegisterComponent() { const char* typeName = typeid(T).name(); assert(mComponentTypes.find(typeName) == mComponentTypes.end() && "Registering component type more than once."); mComponentTypes.insert({typeName, mNextComponentType}); mComponentArrays.insert({typeName, std::make_shared>()}); ++mNextComponentType; } template ComponentType GetComponentType() { const char* typeName = typeid(T).name(); assert(mComponentTypes.find(typeName) != mComponentTypes.end() && "Component not registered before use."); return mComponentTypes[typeName]; } template void AddComponent(Entity entity, T component) { GetComponentArray()->InsertData(entity, component); } template void RemoveComponent(Entity entity) { GetComponentArray()->RemoveData(entity); } template T& GetComponent(Entity entity) { return GetComponentArray()->GetData(entity); } void EntityDestroyed(Entity entity) { for (auto const& pair : mComponentArrays) { auto const& component = pair.second; component->EntityDestroyed(entity); } } private: std::unordered_map mComponentTypes{}; std::unordered_map> mComponentArrays{}; ComponentType mNextComponentType{}; template std::shared_ptr> GetComponentArray() { const char* typeName = typeid(T).name(); assert(mComponentTypes.find(typeName) != mComponentTypes.end() && "Component not registered before use."); return std::static_pointer_cast>(mComponentArrays[typeName]); } };