ECS.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * 2D Game Engine
  3. * ECS.h:
  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. #ifndef GAMEENGINE_ECS_H
  10. #define GAMEENGINE_ECS_H
  11. #include <stdint.h>
  12. #include <bitset>
  13. #include <vector>
  14. #include <unordered_map>
  15. #include <typeindex>
  16. #include <set>
  17. #include <memory>
  18. #include <deque>
  19. #include <Logger.h>
  20. #include <Pool.h>
  21. const uint32_t MAX_COMPONENTS = 32;
  22. typedef std::bitset<MAX_COMPONENTS> Signature;
  23. struct IComponent
  24. {
  25. protected:
  26. static uint32_t nextId;
  27. };
  28. template <typename T> class Component: public IComponent
  29. {
  30. public:
  31. static uint32_t getId()
  32. {
  33. static auto id = IComponent::nextId++;
  34. return id;
  35. }
  36. };
  37. class Entity
  38. {
  39. private:
  40. uint32_t id;
  41. public:
  42. explicit Entity(uint32_t id): id(id) {};
  43. Entity(const Entity & entity) = default;
  44. uint32_t getId() const;
  45. Entity& operator=(const Entity & other) = default;
  46. bool operator==(const Entity &other) const { return this->id == other.id; };
  47. bool operator!=(const Entity &other) const { return this->id != other.id; };
  48. bool operator>(const Entity &other) const { return this->id > other.id; };
  49. bool operator<(const Entity &other) const { return this->id < other.id; };
  50. /* Some syntaxic sugar */
  51. template <typename T, typename ...TArgs> void addComponent(TArgs&& ...args);
  52. template <typename T> void removeComponent();
  53. template <typename T> bool hasComponent();
  54. template <typename T> T& getComponent();
  55. class Registry *registry = nullptr;
  56. void kill();
  57. };
  58. class System
  59. {
  60. private:
  61. Signature componentSignature;
  62. std::vector<Entity> entities;
  63. public:
  64. System() = default;
  65. ~System() = default;
  66. void addEntity(Entity entity);
  67. void removeEntity(Entity entity);
  68. std::vector<Entity> getSystemEntities() const;
  69. const Signature& getComponentSignature() const;
  70. template<typename T> void requireComponent();
  71. };
  72. template<typename T> void System::requireComponent()
  73. {
  74. const auto componentId = Component<T>::getId();
  75. this->componentSignature.set(componentId);
  76. }
  77. class Registry
  78. {
  79. private:
  80. uint32_t numEntities = 0;
  81. std::vector<std::shared_ptr<IPool>> componentPools;
  82. std::vector<Signature> entityComponentSignatures;
  83. std::unordered_map<std::type_index, std::shared_ptr<System>> systems;
  84. std::set<Entity> entitiesToBeAdded;
  85. std::set<Entity> entitiesToBeKilled;
  86. std::deque<int> freeIds;
  87. public:
  88. Registry() = default;
  89. ~Registry() = default;
  90. /* Entities */
  91. Entity createEntity();
  92. void killEntity(Entity entity);
  93. void addEntityToSystems(Entity entity);
  94. void removeEntityFromSystems(Entity entity);
  95. /* Components */
  96. template <typename T, typename ...TArgs> void addComponent(Entity entity, TArgs&& ...args);
  97. template <typename T> void removeComponent(Entity entity);
  98. template <typename T> bool hasComponent(Entity entity);
  99. template <typename T> T& getComponent(Entity entity);
  100. /* Systems */
  101. template<typename T, typename ...TArgs> void addSystem(TArgs&& ...args);
  102. template<typename T> void removeSystem();
  103. template<typename T> bool hasSystem() const;
  104. template<typename T> T& getSystem() const;
  105. /* Others */
  106. void update();
  107. };
  108. template<typename T, typename ...TArgs> void Registry::addComponent(Entity entity, TArgs&& ...args)
  109. {
  110. const auto componentId = Component<T>::getId();
  111. const auto entityId = entity.getId();
  112. std::shared_ptr<Pool<T>> componentPool;
  113. /* Check the pool is big enough */
  114. if (componentId >= this->componentPools.size())
  115. {
  116. this->componentPools.resize(componentId + 1, nullptr);
  117. }
  118. /* Is this component exist in the pool? */
  119. if (!this->componentPools[componentId])
  120. {
  121. this->componentPools[componentId] = std::make_shared<Pool<T>>();
  122. }
  123. componentPool = std::static_pointer_cast<Pool<T>>(this->componentPools[componentId]);
  124. if (entityId >= componentPool->getSize())
  125. {
  126. componentPool->resize(this->numEntities);
  127. }
  128. T newComponent(std::forward<TArgs>(args)...);
  129. componentPool->set(entityId, newComponent);
  130. this->entityComponentSignatures[entityId].set(componentId);
  131. Logger::Debug("Component id#%d was added to entity id#%d", componentId, entityId);
  132. }
  133. template <typename T> void Registry::removeComponent(Entity entity)
  134. {
  135. const auto componentId = Component<T>::getId();
  136. const auto entityId = entity.getId();
  137. this->entityComponentSignatures[entityId].set(componentId, false);
  138. Logger::Debug("Component id#%d was removed to entity id#%d", componentId, entityId);
  139. }
  140. template <typename T> bool Registry::hasComponent(Entity entity)
  141. {
  142. const auto componentId = Component<T>::getId();
  143. const auto entityId = entity.getId();
  144. return this->entityComponentSignatures[entityId].test(componentId);
  145. }
  146. template <typename T> T& Registry::getComponent(Entity entity)
  147. {
  148. const auto componentId = Component<T>::getId();
  149. const auto entityId = entity.getId();
  150. auto componentPool = std::static_pointer_cast<Pool<T>>(this->componentPools[componentId]);
  151. return componentPool->get(entityId);
  152. }
  153. template<typename T, typename ...TArgs> void Registry::addSystem(TArgs&& ...args)
  154. {
  155. std::shared_ptr<T> newSystem = std::make_shared<T>(std::forward<TArgs>(args)...);
  156. this->systems.insert(std::make_pair(std::type_index(typeid(T)), newSystem));
  157. }
  158. template<typename T> void Registry::removeSystem()
  159. {
  160. auto system = this->systems.find(std::type_index(typeid(T)));
  161. this->systems.erase(system);
  162. }
  163. template<typename T> bool Registry::hasSystem() const
  164. {
  165. return this->systems.find(std::type_index(typeid(T))) != this->systems.end();
  166. }
  167. template<typename T> T& Registry::getSystem() const
  168. {
  169. auto system = this->systems.find(std::type_index(typeid(T)));
  170. return *(std::static_pointer_cast<T>(system->second));
  171. }
  172. template <typename T, typename ...TArgs> void Entity::addComponent(TArgs&& ...args)
  173. {
  174. this->registry->addComponent<T>(*this, std::forward<TArgs>(args)...);
  175. }
  176. template <typename T> void Entity::removeComponent()
  177. {
  178. this->registry->removeComponent<T>(*this);
  179. }
  180. template <typename T> bool Entity::hasComponent()
  181. {
  182. return this->registry->hasComponent<T>(*this);
  183. }
  184. template <typename T> T& Entity::getComponent()
  185. {
  186. return this->registry->getComponent<T>(*this);
  187. }
  188. #endif /* GAMEENGINE_ECS_H */