ECS.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /* System */
  24. void System::addEntity(Entity entity)
  25. {
  26. this->entities.push_back(entity);
  27. }
  28. void System::removeEntity(Entity entity)
  29. {
  30. this->entities.erase(std::remove_if(this->entities.begin(), this->entities.end(), [&entity](Entity other)
  31. {
  32. return other.getId() == entity.getId();
  33. }), this->entities.end());
  34. }
  35. std::vector<Entity> System::getSystemEntities() const
  36. {
  37. return this->entities;
  38. }
  39. const Signature& System::getComponentSignature() const
  40. {
  41. return this->componentSignature;
  42. }
  43. Entity Registry::createEntity()
  44. {
  45. uint32_t entityId;
  46. if (this->freeIds.empty())
  47. {
  48. entityId = this->numEntities++;
  49. }
  50. else
  51. {
  52. entityId = this->freeIds.front();
  53. this->freeIds.pop_front();
  54. }
  55. Entity entity(entityId);
  56. entity.registry = this;
  57. this->entitiesToBeAdded.insert(entity);
  58. if (entityId >= this->entityComponentSignatures.size())
  59. {
  60. this->entityComponentSignatures.resize(entityId + 1);
  61. }
  62. Logger::Info("Entity created with id = %d", entityId);
  63. return entity;
  64. }
  65. void Registry::killEntity(Entity entity)
  66. {
  67. Logger::Info("Entity #%d flagged for killing", entity.getId());
  68. this->entitiesToBeKilled.insert(entity);
  69. }
  70. void Registry::addEntityToSystems(Entity entity)
  71. {
  72. const auto entityId = entity.getId();
  73. const auto entityComponentSignature = this->entityComponentSignatures[entityId];
  74. for(auto &system: this->systems)
  75. {
  76. const auto systemComponentSignature = system.second->getComponentSignature();
  77. bool isInterested = (entityComponentSignature & systemComponentSignature) == systemComponentSignature;
  78. if (isInterested)
  79. {
  80. system.second->addEntity(entity);
  81. }
  82. }
  83. }
  84. void Registry::removeEntityFromSystems(Entity entity)
  85. {
  86. for (auto &system: this->systems)
  87. {
  88. system.second->removeEntity(entity);
  89. }
  90. }
  91. void Registry::update()
  92. {
  93. /* Add pending entities */
  94. for(auto entity: this->entitiesToBeAdded)
  95. {
  96. this->addEntityToSystems(entity);
  97. }
  98. this->entitiesToBeAdded.clear();
  99. for(auto entity: this->entitiesToBeKilled)
  100. {
  101. this->removeEntityFromSystems(entity);
  102. this->entityComponentSignatures[entity.getId()].reset();
  103. this->freeIds.push_back(entity.getId());
  104. }
  105. this->entitiesToBeKilled.clear();
  106. }