6 #include <unordered_map>
8 #include "../../../include/my_exception.h"
10 #include "component_util.h"
11 #include "entity_manager.h"
21 std::unordered_map<ComponentId, std::unique_ptr<Component>> components;
48 bool has_component()
const {
49 return components.count(ComponentUtil::get_type_id<T>());
60 template <
class T,
typename... TArgs>
63 if (has_component<T>())
64 throw MyException(
"Entity: already has that component");
65 T* component(
new T(std::forward<TArgs>(mArgs)...));
66 component->set_entity(
this);
67 ComponentId c_id = ComponentUtil::get_type_id<T>();
68 components[c_id] = std::unique_ptr<Component>(component);
81 if (!has_component<T>())
82 throw MyException(
"Entity: cannot delete an unexistent component");
83 ComponentId c_id = ComponentUtil::get_type_id<T>();
84 components[c_id].reset();
85 components.erase(c_id);
96 if (!has_component<T>())
97 throw MyException(
"Entity: cannot get an unexistent component");
98 ComponentId c_id = ComponentUtil::get_type_id<T>();
99 Component* comp = components.at(c_id).get();
100 return *
reinterpret_cast<T*
>(comp);