ECS.h 6.1 KB

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