#include "componentmanager.hpp" #include "componentarray.hpp" #include // Private functions template std::shared_ptr> ComponentManager::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]); } // Public functions template void ComponentManager::RegisterComponent() { const char *typeName = typeid(T).name(); assert(mComponentTypes.find(typeName) == mComponentTypes.end() && "Registering component type more than once."); // Add this component type to the component type map mComponentTypes.insert({typeName, mNextComponentType}); // Create a ComponentArray pointer and add it to the component arrays map mComponentArrays.insert({typeName, std::make_shared>()}); // Increment the value so that the next component registered will be different ++mNextComponentType; }; template ComponentType ComponentManager::GetComponentType() { const char *typeName = typeid(T).name(); assert(mComponentTypes.find(typeName) != mComponentTypes.end() && "Component not registered before use."); // Return this component's type - used for creating signatures return mComponentTypes[typeName]; } template void ComponentManager::AddComponent(Entity entity, T component) { // Add a component to the array for an entity GetComponentArray()->InsertData(entity, component); } template void ComponentManager::RemoveComponent(Entity entity) { // Remove a component from the array for an entity GetComponentArray()->RemoveData(entity); } template T &ComponentManager::GetComponent(Entity entity) { // Get a reference to a component from the array for an entity return GetComponentArray()->GetData(entity); } void ComponentManager::EntityDestroyed(Entity entity) { // Notify each component array that an entity has been destroyed // If it has a component for that entity, it will remove it for (auto const &pair : mComponentArrays) { auto const &component = pair.second; component->EntityDestroyed(entity); } };