123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- /*
- * 2D Game Engine
- * ECS.cpp:
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 11/02/2021.
- */
- #include <Logger.h>
- #include <algorithm>
- #include <ECS.h>
- /* Entity */
- uint32_t IComponent::nextId = 0;
- /* TODO: this should be moved to the header file for optimisation purposes */
- uint32_t Entity::getId() const
- {
- return this->id;
- }
- void Entity::kill()
- {
- this->registry->killEntity(*this);
- }
- void Entity::tag(const std::string &tag)
- {
- this->registry->tagEntity(*this, tag);
- }
- bool Entity::hasTag(const std::string &tag) const
- {
- return this->registry->entityHasTag(*this, tag);
- }
- void Entity::group(const std::string &groupName)
- {
- this->registry->groupEntity(*this, groupName);
- }
- bool Entity::belongToGroup(const std::string &groupName) const
- {
- return this->registry->entityBelongsToGroup(*this, groupName);
- }
- /* System */
- void System::addEntity(Entity entity)
- {
- this->entities.push_back(entity);
- }
- void System::removeEntity(Entity entity)
- {
- this->entities.erase(std::remove_if(this->entities.begin(), this->entities.end(), [&entity](Entity other)
- {
- return other.getId() == entity.getId();
- }), this->entities.end());
- }
- std::vector<Entity> System::getSystemEntities() const
- {
- return this->entities;
- }
- const Signature& System::getComponentSignature() const
- {
- return this->componentSignature;
- }
- Entity Registry::createEntity()
- {
- uint32_t entityId;
- if (this->freeIds.empty())
- {
- entityId = this->numEntities++;
- }
- else
- {
- entityId = this->freeIds.front();
- this->freeIds.pop_front();
- }
- Entity entity(entityId);
- entity.registry = this;
- this->entitiesToBeAdded.insert(entity);
- if (entityId >= this->entityComponentSignatures.size())
- {
- this->entityComponentSignatures.resize(entityId + 1);
- }
- Logger::Info("Entity created with id = %d", entityId);
- return entity;
- }
- void Registry::killEntity(Entity entity)
- {
- Logger::Info("Entity #%d flagged for killing", entity.getId());
- this->entitiesToBeKilled.insert(entity);
- }
- void Registry::addEntityToSystems(Entity entity)
- {
- const auto entityId = entity.getId();
- const auto entityComponentSignature = this->entityComponentSignatures[entityId];
- for(auto &system: this->systems)
- {
- const auto systemComponentSignature = system.second->getComponentSignature();
- bool isInterested = (entityComponentSignature & systemComponentSignature) == systemComponentSignature;
- if (isInterested)
- {
- system.second->addEntity(entity);
- }
- }
- }
- void Registry::removeEntityFromSystems(Entity entity)
- {
- for (auto &system: this->systems)
- {
- system.second->removeEntity(entity);
- }
- }
- void Registry::update()
- {
- /* Add pending entities */
- for(auto entity: this->entitiesToBeAdded)
- {
- this->addEntityToSystems(entity);
- }
- this->entitiesToBeAdded.clear();
- for(auto entity: this->entitiesToBeKilled)
- {
- this->removeEntityFromSystems(entity);
- this->entityComponentSignatures[entity.getId()].reset();
- this->freeIds.push_back(entity.getId());
- this->removeEntityTag(entity);
- this->removeEntityGroup(entity);
- }
- this->entitiesToBeKilled.clear();
- }
- void Registry::tagEntity(Entity entity, const std::string &tag)
- {
- this->entityPerTag.emplace(tag, entity);
- this->tagPerEntity.emplace(entity.getId(), tag);
- }
- bool Registry::entityHasTag(Entity entity, const std::string &tag) const
- {
- if (this->tagPerEntity.find(entity.getId()) == this->tagPerEntity.end())
- {
- return false;
- }
- return this->entityPerTag.find(tag)->second == entity;
- }
- Entity Registry::getEntityByTag(const std::string &tag) const
- {
- return this->entityPerTag.at(tag);
- }
- void Registry::removeEntityTag(Entity entity)
- {
- auto taggedEntity = this->tagPerEntity.find(entity.getId());
- if (taggedEntity != this->tagPerEntity.end())
- {
- auto tag = taggedEntity->second;
- this->entityPerTag.erase(tag);
- this->tagPerEntity.erase(taggedEntity);
- }
- }
- void Registry::groupEntity(Entity entity, const std::string &groupName)
- {
- this->entitiesPerGroup.emplace(groupName, std::set<Entity>());
- this->entitiesPerGroup[groupName].emplace(entity);
- this->groupPerEntity.emplace(entity.getId(), groupName);
- }
- bool Registry::entityBelongsToGroup(Entity entity, const std::string &groupName) const
- {
- if (this->entitiesPerGroup.find(groupName) == this->entitiesPerGroup.end())
- {
- return false;
- }
- auto groupEntities = this->entitiesPerGroup.at(groupName);
- return groupEntities.find(entity) != groupEntities.end();
- }
- std::vector<Entity> Registry::getEntitiesByGroup(const std::string &groupName) const
- {
- auto &setOfEntities = this->entitiesPerGroup.at(groupName);
- return std::vector<Entity>(setOfEntities.begin(), setOfEntities.end());
- }
- void Registry::removeEntityGroup(Entity entity)
- {
- auto groupedEntity = this->groupPerEntity.find(entity.getId());
- if (groupedEntity != this->groupPerEntity.end())
- {
- auto group = this->entitiesPerGroup.find(groupedEntity->second);
- if (group != this->entitiesPerGroup.end())
- {
- auto entityInGroup = group->second.find(entity);
- if (entityInGroup != group->second.end())
- {
- group->second.erase(entityInGroup);
- }
- }
- groupPerEntity.erase(groupedEntity);
- }
- }
|