4#include <unordered_map>
32 template <Val
idEntity entityType = ecs::entity>
40 template <
typename ComponentType>
66 if (m_dead_entity_head != 0)
68 entityType dead_entity = m_dead_entity_head;
73 m_dead_entity_head = m_entities[dead_index];
75 IncreaseEntityVersion(dead_entity);
76 m_entities[dead_index] = dead_entity;
77 return {
true, dead_entity};
81 auto new_entity = createNewEntity();
82 m_entities.push_back(new_entity);
83 return {
true, new_entity};
101 std::swap(m_entities[index], m_dead_entity_head);
104 for (
auto & [type, set] : m_components)
112 if (index >= m_entities.size())
115 if (m_entities[index] !=
entity)
153 template <
typename ComponentType>
157 ComponentSetType & set = getSet<ComponentType>();
159 set.insert(
entity, component);
169 template <
typename... ComponentTypes>
170 void add(entityType
entity, ComponentTypes... components)
172 (add<ComponentTypes>(
entity, components), ...);
181 template <
typename... ComponentTypes>
184 (_remove_component<ComponentTypes>(
entity), ...);
197 template <
typename ComponentType>
201 ComponentSetType & set = getSet<ComponentType>();
217 template <
typename... ComponentTypes>
220 return {getComponent<ComponentTypes>(
entity)...};
230 template <
typename ComponentType>
234 ComponentSetType & set = getSet<ComponentType>();
236 return set.tryGet(
entity);
246 template <
typename... ComponentTypes>
249 return std::make_tuple(tryGetComponent<ComponentTypes>(
entity)...);
262 template <
typename... ComponentTypes>
265 return View<entityType, ComponentTypes...>(*this);
277 const char *
what() const noexcept
override {
return std::string(
"Entity does not exist " + std::to_string(m_entity)).c_str(); }
288 const char *
what() const noexcept
override {
return std::string(
"Entity already exists " + std::to_string(m_entity)).c_str(); }
298 template <
typename ComponentType>
300 : m_type(typeid(ComponentType)) {}
301 const char *
what() const noexcept
override
303 std::string message =
"Component does not exist ";
304 message += m_type.name();
305 return message.c_str();
307 const std::type_index &
getType()
const {
return m_type; }
309 std::type_index m_type;
315 template <
typename ComponentType>
317 : m_type(typeid(ComponentType)) {}
318 const char *
what() const noexcept
override
320 std::string message =
"Component already exists ";
321 message += m_type.name();
322 return message.c_str();
324 const std::type_index &
getType()
const {
return m_type; }
326 std::type_index m_type;
330 std::vector<entityType> m_entities;
332 entityType m_dead_entity_head = 0;
334 std::unordered_map<std::type_index, std::shared_ptr<entitySet>> m_components;
345 void IncreaseEntityVersion(entityType &
entity)
355 entityType createNewEntity()
357 return m_entities.size() << 4 | 1;
363 template <
typename ComponentType>
364 componentSet<ComponentType> & getSet()
366 return *getSetPtr<ComponentType>();
376 template <
typename ComponentType>
377 std::shared_ptr<componentSet<ComponentType>> getSetPtr()
379 using componentSetType = componentSet<ComponentType>;
380 const std::type_index type = std::type_index(
typeid(ComponentType));
382 auto it = m_components.find(type);
383 if (it == m_components.end())
385 auto ret_pair = m_components.insert({type, std::make_shared<componentSetType>()});
388 return std::static_pointer_cast<componentSetType>(it->second);
397 template <
typename ComponentType>
398 void _remove_component(entityType
entity)
400 using ComponentSetType = ComponentStorage<entityType, ComponentType>;
401 ComponentSetType & set = getSet<ComponentType>();
Definition: ComponentStorage.hpp:12
Definition: Manager.hpp:313
const std::type_index & getType() const
Definition: Manager.hpp:324
ComponentAlreadyExists()
Definition: Manager.hpp:316
const char * what() const noexcept override
Definition: Manager.hpp:318
Definition: Manager.hpp:296
const std::type_index & getType() const
Definition: Manager.hpp:307
ComponentDoesNotExist()
Definition: Manager.hpp:299
const char * what() const noexcept override
Definition: Manager.hpp:301
Definition: Manager.hpp:284
EntityAlreadyExists(entityType entity)
Definition: Manager.hpp:286
entityType getEntity() const
Definition: Manager.hpp:289
const char * what() const noexcept override
Definition: Manager.hpp:288
Definition: Manager.hpp:273
const char * what() const noexcept override
Definition: Manager.hpp:277
EntityDoesNotExist(entityType entity)
Definition: Manager.hpp:275
entityType getEntity() const
Definition: Manager.hpp:278
This class is the main class of the ECS system you can use it to create entitites,...
Definition: Manager.hpp:34
ComponentType & getComponent(entityType entity)
get a component attached to an entity
Definition: Manager.hpp:198
SparseSet< entityType > entitySet
Definition: Manager.hpp:39
std::tuple< ComponentTypes *... > tryGetComponents(entityType entity)
try to get multiple components attached to an entity as a tuple
Definition: Manager.hpp:247
void removeEntity(entityType entity)
Remove an entity from the manager.
Definition: Manager.hpp:94
void remove(entityType entity)
remove any components from an entity
Definition: Manager.hpp:182
Manager(Manager &other)=delete
Manager & operator=(Manager &&other)=delete
void add(entityType entity, ComponentType component)
add a component to an entity
Definition: Manager.hpp:154
std::pair< bool, entityType > createEntity()
Create a new entity.
Definition: Manager.hpp:61
Manager()
Definition: Manager.hpp:43
Manager(Manager &&other)=delete
~Manager()
Definition: Manager.hpp:44
std::tuple< ComponentTypes &... > getComponents(entityType entity)
Get multiple components attached to an entity as a tuple.
Definition: Manager.hpp:218
static constexpr uint32_t getEntityVersion(const entityType &entity)
Get the version of the entity, see the entity doc for more info.
Definition: Manager.hpp:126
View< entityType, ComponentTypes... > view()
get an iterable view of entities that have all the components listed in the tparams
Definition: Manager.hpp:263
Manager & operator=(Manager &other)=delete
static constexpr uint32_t getEntityIndex(const entityType &entity)
Get the index of the entity in the entity array, see the entity doc for more info.
Definition: Manager.hpp:137
void add(entityType entity, ComponentTypes... components)
add multiple components to an entity
Definition: Manager.hpp:170
ComponentType * tryGetComponent(entityType entity)
Try to get a component attached to an entity.
Definition: Manager.hpp:231
bool isAlive(entityType entity) const
Definition: Manager.hpp:108
Definition: SparseSet.hpp:20
Definition: ecs_utils.hpp:11
Definition: ComponentStorage.hpp:9
constexpr size_t MAX_ENTITIES
Definition: ecs_CONSTANTS.hpp:8
uint32_t entity
Definition: ecs_CONSTANTS.hpp:10