Skip to content

Commit

Permalink
Add entity component ID getter
Browse files Browse the repository at this point in the history
  • Loading branch information
cfnptr committed Dec 14, 2023
1 parent 7f7fbca commit 55792e5
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions include/ecsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,49 @@ class Manager final
return View<T>(tryGet(entity, typeid(T)));
}

ID<Component> getID(ID<Entity> entity, type_index componentType) const
{
assert(entity);
auto entityView = entities.get(entity);

#ifndef NDEBUG
if (entityView->components.find(componentType) ==
entityView->components.end())
{
throw runtime_error("Component is not added. ("
"name: " + string(componentType.name()) +
"entity:" + to_string(*entity) + ")");
}
#endif

return entityView->components.at(componentType).second;
}
template<class T = Component>
ID<T> getID(ID<Entity> entity) const
{
static_assert(is_base_of_v<Component, T>,
"Must be derived from the Component class.");
return ID<T>(getID(entity, typeid(T)));
}

ID<Component> tryGetID(ID<Entity> entity, type_index componentType) const
{
assert(entity);
auto entityView = entities.get(entity);
auto& components = entityView->components;
auto result = components.find(componentType);
if (result == components.end()) return {};
auto& pair = result->second;
return pair.second;
}
template<class T = Component>
ID<T> tryGetID(ID<Entity> entity) const
{
static_assert(is_base_of_v<Component, T>,
"Must be derived from the Component class.");
return ID<T>(tryGetID(entity, typeid(T)));
}

uint32_t getComponentCount(ID<Entity> entity) const
{
auto entityView = entities.get(entity);
Expand Down

0 comments on commit 55792e5

Please sign in to comment.