21 #include "hx3d/ecs/engine.hpp" 29 Ptr<Entity> entity(Make<Entity>(lastEntityAvailable()));
30 _entities[entity->getId()] = entity;
31 _components[entity->getId()] = std::map<std::type_index, Ptr<Component>>();
32 _bits[entity->getId()] =
Bitset();
38 if (entity->getId() != 0) {
39 Log.
Error(
"Engine: entity `%d` already registered.", entity->getId());
43 entity->setId(lastEntityAvailable());
44 _entities[entity->getId()] = entity;
45 _components[entity->getId()] = std::map<std::type_index, Ptr<Component>>();
46 _bits[entity->getId()] =
Bitset();
50 _toRemove.push_back(entity->getId());
54 if (_components.find(entity->getId()) == _components.end()) {
55 Log.
Error(
"No entity %ld", entity->getId());
59 const std::map<std::type_index, Ptr<Component>>& compMap = _components[entity->getId()];
60 return compMap.size();
64 return _entities.size();
68 const unsigned int id = entity->getId();
69 return _bits[id].getBits();
73 for (
auto&
id: _entities) {
74 for (
auto& pair: _systems) {
76 if (sys->canProcess(_bits[
id.first].getBits())) {
77 sys->process(
id.second, delta);
85 void Engine::cleanEntities() {
86 for (
const unsigned int i: _toRemove) {
96 unsigned int Engine::lastEntityAvailable() {
97 unsigned int first = 1;
98 for (
auto& pair: _entities) {
99 if (pair.first != first)
108 void Engine::removeComponents(
const unsigned int entityId) {
109 if (_components.find(entityId) != _components.end()) {
110 while (_components[entityId].size() > 0) {
111 const auto& comp = *(_components[entityId].begin());
113 if (_onComponentRemoved.find(comp.first) != _onComponentRemoved.end()) {
114 _onComponentRemoved[comp.first](comp.second, _entities[entityId]);
117 _components[entityId].erase(comp.first);
123 for (
auto& entity: _entities) {
124 removeComponents(entity.first);
134 _onComponentAdded.clear();
135 _onComponentRemoved.clear();
void clear()
Remove everything from the engine.
void Error(const std::string fmt,...)
Write an error message.
void update(const float delta)
Update the engine.
static hx3d::LogImpl Log
Current log implementation.
Bitset helper in an unsigned int.
Ptr< Entity > createEntity()
Create a new entity with the last entity id available.
void removeEntity(const Ptr< Entity > &entity)
Mark the entity for deletion from the engine.
unsigned int getComponentSize(const Ptr< Entity > &entity)
Get the number of components for an entity.
unsigned int getEntityCount()
Get the number of entities.
unsigned int getBits(const Ptr< Entity > &entity)
Get the bits corresponding to the entity components.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
void registerEntity(const Ptr< Entity > &entity)
Affect an ID to a uninitialized entity.