ECS.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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::belongToGroup(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. this->freeIds.push_back(entity.getId());
  120. this->removeEntityTag(entity);
  121. this->removeEntityGroup(entity);
  122. }
  123. this->entitiesToBeKilled.clear();
  124. }
  125. void Registry::tagEntity(Entity entity, const std::string &tag)
  126. {
  127. this->entityPerTag.emplace(tag, entity);
  128. this->tagPerEntity.emplace(entity.getId(), tag);
  129. }
  130. bool Registry::entityHasTag(Entity entity, const std::string &tag) const
  131. {
  132. if (this->tagPerEntity.find(entity.getId()) == this->tagPerEntity.end())
  133. {
  134. return false;
  135. }
  136. return this->entityPerTag.find(tag)->second == entity;
  137. }
  138. Entity Registry::getEntityByTag(const std::string &tag) const
  139. {
  140. return this->entityPerTag.at(tag);
  141. }
  142. void Registry::removeEntityTag(Entity entity)
  143. {
  144. auto taggedEntity = this->tagPerEntity.find(entity.getId());
  145. if (taggedEntity != this->tagPerEntity.end())
  146. {
  147. auto tag = taggedEntity->second;
  148. this->entityPerTag.erase(tag);
  149. this->tagPerEntity.erase(taggedEntity);
  150. }
  151. }
  152. void Registry::groupEntity(Entity entity, const std::string &groupName)
  153. {
  154. this->entitiesPerGroup.emplace(groupName, std::set<Entity>());
  155. this->entitiesPerGroup[groupName].emplace(entity);
  156. this->groupPerEntity.emplace(entity.getId(), groupName);
  157. }
  158. bool Registry::entityBelongsToGroup(Entity entity, const std::string &groupName) const
  159. {
  160. if (this->entitiesPerGroup.find(groupName) == this->entitiesPerGroup.end())
  161. {
  162. return false;
  163. }
  164. auto groupEntities = this->entitiesPerGroup.at(groupName);
  165. return groupEntities.find(entity) != groupEntities.end();
  166. }
  167. std::vector<Entity> Registry::getEntitiesByGroup(const std::string &groupName) const
  168. {
  169. auto &setOfEntities = this->entitiesPerGroup.at(groupName);
  170. return std::vector<Entity>(setOfEntities.begin(), setOfEntities.end());
  171. }
  172. void Registry::removeEntityGroup(Entity entity)
  173. {
  174. auto groupedEntity = this->groupPerEntity.find(entity.getId());
  175. if (groupedEntity != this->groupPerEntity.end())
  176. {
  177. auto group = this->entitiesPerGroup.find(groupedEntity->second);
  178. if (group != this->entitiesPerGroup.end())
  179. {
  180. auto entityInGroup = group->second.find(entity);
  181. if (entityInGroup != group->second.end())
  182. {
  183. group->second.erase(entityInGroup);
  184. }
  185. }
  186. groupPerEntity.erase(groupedEntity);
  187. }
  188. }