ECS.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * 2D Game Engine
  3. * ECS.cpp:
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 11/02/2021.
  8. */
  9. #include <Logger.h>
  10. #include <algorithm>
  11. #include <ECS.h>
  12. /* Entity */
  13. uint32_t IComponent::nextId = 0;
  14. /* TODO: this should be moved to the header file for optimisation purposes */
  15. uint32_t Entity::getId() const
  16. {
  17. return this->id;
  18. }
  19. void Entity::kill()
  20. {
  21. this->registry->killEntity(*this);
  22. }
  23. void Entity::tag(const std::string &tag)
  24. {
  25. this->registry->tagEntity(*this, tag);
  26. }
  27. bool Entity::hasTag(const std::string &tag) const
  28. {
  29. return this->registry->entityHasTag(*this, tag);
  30. }
  31. void Entity::group(const std::string &groupName)
  32. {
  33. this->registry->groupEntity(*this, groupName);
  34. }
  35. bool Entity::belongsToGroup(const std::string &groupName) const
  36. {
  37. return this->registry->entityBelongsToGroup(*this, groupName);
  38. }
  39. /* System */
  40. void System::addEntity(Entity entity)
  41. {
  42. this->entities.push_back(entity);
  43. }
  44. void System::removeEntity(Entity entity)
  45. {
  46. this->entities.erase(std::remove_if(this->entities.begin(), this->entities.end(), [&entity](Entity other)
  47. {
  48. return other.getId() == entity.getId();
  49. }), this->entities.end());
  50. }
  51. std::vector<Entity> System::getSystemEntities() const
  52. {
  53. return this->entities;
  54. }
  55. const Signature& System::getComponentSignature() const
  56. {
  57. return this->componentSignature;
  58. }
  59. Entity Registry::createEntity()
  60. {
  61. uint32_t entityId;
  62. if (this->freeIds.empty())
  63. {
  64. entityId = this->numEntities++;
  65. }
  66. else
  67. {
  68. entityId = this->freeIds.front();
  69. this->freeIds.pop_front();
  70. }
  71. Entity entity(entityId);
  72. entity.registry = this;
  73. this->entitiesToBeAdded.insert(entity);
  74. if (entityId >= this->entityComponentSignatures.size())
  75. {
  76. this->entityComponentSignatures.resize(entityId + 1);
  77. }
  78. Logger::Info("Entity created with id = %d", entityId);
  79. return entity;
  80. }
  81. void Registry::killEntity(Entity entity)
  82. {
  83. Logger::Info("Entity #%d flagged for killing", entity.getId());
  84. this->entitiesToBeKilled.insert(entity);
  85. }
  86. void Registry::addEntityToSystems(Entity entity)
  87. {
  88. const auto entityId = entity.getId();
  89. const auto entityComponentSignature = this->entityComponentSignatures[entityId];
  90. for(auto &system: this->systems)
  91. {
  92. const auto systemComponentSignature = system.second->getComponentSignature();
  93. bool isInterested = (entityComponentSignature & systemComponentSignature) == systemComponentSignature;
  94. if (isInterested)
  95. {
  96. system.second->addEntity(entity);
  97. }
  98. }
  99. }
  100. void Registry::removeEntityFromSystems(Entity entity)
  101. {
  102. for (auto &system: this->systems)
  103. {
  104. system.second->removeEntity(entity);
  105. }
  106. }
  107. void Registry::update()
  108. {
  109. /* Add pending entities */
  110. for(auto entity: this->entitiesToBeAdded)
  111. {
  112. this->addEntityToSystems(entity);
  113. }
  114. this->entitiesToBeAdded.clear();
  115. for(auto entity: this->entitiesToBeKilled)
  116. {
  117. this->removeEntityFromSystems(entity);
  118. this->entityComponentSignatures[entity.getId()].reset();
  119. for(auto pool: this->componentPools)
  120. {
  121. if (pool != nullptr)
  122. {
  123. pool->removeEntityFromPool(entity.getId());
  124. }
  125. }
  126. this->freeIds.push_back(entity.getId());
  127. this->removeEntityTag(entity);
  128. this->removeEntityGroup(entity);
  129. }
  130. this->entitiesToBeKilled.clear();
  131. }
  132. void Registry::tagEntity(Entity entity, const std::string &tag)
  133. {
  134. this->entityPerTag.emplace(tag, entity);
  135. this->tagPerEntity.emplace(entity.getId(), tag);
  136. }
  137. bool Registry::entityHasTag(Entity entity, const std::string &tag) const
  138. {
  139. if (this->tagPerEntity.find(entity.getId()) == this->tagPerEntity.end())
  140. {
  141. return false;
  142. }
  143. return this->entityPerTag.find(tag)->second == entity;
  144. }
  145. Entity Registry::getEntityByTag(const std::string &tag) const
  146. {
  147. return this->entityPerTag.at(tag);
  148. }
  149. void Registry::removeEntityTag(Entity entity)
  150. {
  151. auto taggedEntity = this->tagPerEntity.find(entity.getId());
  152. if (taggedEntity != this->tagPerEntity.end())
  153. {
  154. auto tag = taggedEntity->second;
  155. this->entityPerTag.erase(tag);
  156. this->tagPerEntity.erase(taggedEntity);
  157. }
  158. }
  159. void Registry::groupEntity(Entity entity, const std::string &groupName)
  160. {
  161. this->entitiesPerGroup.emplace(groupName, std::set<Entity>());
  162. this->entitiesPerGroup[groupName].emplace(entity);
  163. this->groupPerEntity.emplace(entity.getId(), groupName);
  164. }
  165. bool Registry::entityBelongsToGroup(Entity entity, const std::string &groupName) const
  166. {
  167. if (this->entitiesPerGroup.find(groupName) == this->entitiesPerGroup.end())
  168. {
  169. return false;
  170. }
  171. auto groupEntities = this->entitiesPerGroup.at(groupName);
  172. return groupEntities.find(entity) != groupEntities.end();
  173. }
  174. std::vector<Entity> Registry::getEntitiesByGroup(const std::string &groupName) const
  175. {
  176. auto &setOfEntities = this->entitiesPerGroup.at(groupName);
  177. return std::vector<Entity>(setOfEntities.begin(), setOfEntities.end());
  178. }
  179. void Registry::removeEntityGroup(Entity entity)
  180. {
  181. auto groupedEntity = this->groupPerEntity.find(entity.getId());
  182. if (groupedEntity != this->groupPerEntity.end())
  183. {
  184. auto group = this->entitiesPerGroup.find(groupedEntity->second);
  185. if (group != this->entitiesPerGroup.end())
  186. {
  187. auto entityInGroup = group->second.find(entity);
  188. if (entityInGroup != group->second.end())
  189. {
  190. group->second.erase(entityInGroup);
  191. }
  192. }
  193. groupPerEntity.erase(groupedEntity);
  194. }
  195. }