84 lines
2.7 KiB
C++
84 lines
2.7 KiB
C++
#ifndef HYDRAECSCOMPONENTMANAGER
|
|
#define HYDRAECSCOMPONENTMANAGER
|
|
|
|
#include "../engine/HydraObject.h"
|
|
#include "HydraComponentArray.h"
|
|
#include <unordered_map>
|
|
#include <memory>
|
|
|
|
using ComponentTypeID = uint8_t;
|
|
|
|
class HydraComponentManager :public HydraObject
|
|
{
|
|
private:
|
|
std::unordered_map<std::string, std::shared_ptr<IHydraComponentArray>> m_component_arrays;
|
|
std::unordered_map<std::string, ComponentTypeID> m_component_types;
|
|
ComponentTypeID m_current_component_type_id = 0;
|
|
|
|
template<typename T>
|
|
std::shared_ptr<HydraComponentArray<T>> _GetComponentArray()
|
|
{
|
|
std::string type_name = std::string(typeid(T).name());
|
|
|
|
assert(m_component_types.find(type_name) != m_component_types.end() && "Component type does not exist");
|
|
return std::static_pointer_cast<HydraComponentArray<T>>(m_component_arrays[type_name]);
|
|
}
|
|
public:
|
|
|
|
template<typename T>
|
|
ComponentTypeID RegisterComponentType()
|
|
{
|
|
std::string type_name = std::string(typeid(T).name());
|
|
|
|
assert(m_component_types.find(type_name) == m_component_types.end() && "Component name already in use");
|
|
|
|
m_component_types.insert({type_name, m_current_component_type_id});
|
|
m_component_arrays.insert({type_name, std::make_shared<HydraComponentArray<T>>()});
|
|
|
|
return m_current_component_type_id++;
|
|
}
|
|
|
|
template<typename T>
|
|
ComponentTypeID GetComponentTypeID()
|
|
{
|
|
std::string type_name = std::string(typeid(T).name());
|
|
assert(m_component_types.find(type_name) != m_component_types.end() && "Component type does not exist");
|
|
return m_component_types[type_name];
|
|
}
|
|
|
|
|
|
template<typename T>
|
|
void AddComponent(HydraID entity_id, T component)
|
|
{
|
|
_GetComponentArray<T>()->AddComponent(entity_id, component);
|
|
}
|
|
|
|
template<typename T>
|
|
T& GetComponent(HydraID entity_id)
|
|
{
|
|
return _GetComponentArray<T>()->GetComponent(entity_id);
|
|
}
|
|
|
|
template<typename T>
|
|
bool HasComponent(HydraID entity_id)
|
|
{
|
|
return _GetComponentArray<T>()->HasComponent(entity_id);
|
|
}
|
|
|
|
template<typename T>
|
|
void RemoveComponent(HydraID entity_id)
|
|
{
|
|
_GetComponentArray<T>()->RemoveComponent(entity_id);
|
|
}
|
|
|
|
void EntityDeleted(HydraID entity_id)
|
|
{
|
|
for(auto comp_array : m_component_arrays)
|
|
{
|
|
(*comp_array.second).EntityDeleted(entity_id);
|
|
}
|
|
}
|
|
};
|
|
|
|
#endif /* HYDRACOMPONENTMANAGER */
|