Advertisement
Diamond32_Tutoriales

ECS

Aug 9th, 2023
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #ifndef ECSCOMPONENT_H
  2. #define ECSCOMPONENT_H
  3. #include <iostream>
  4. #include <vector>
  5. #include <memory>
  6. #include <algorithm>
  7. #include <bitset>
  8. #include <array>
  9.  
  10.  
  11. class Component;
  12. class Entity;
  13. class Manager;
  14.  
  15. using ComponentID = std::size_t;
  16. using Group = std::size_t;
  17.  
  18. inline ComponentID getNewComponentTypeID()
  19. {
  20.     static ComponentID lastID = 0u;
  21.     return lastID++;
  22. }
  23.  
  24. template <typename T> inline ComponentID getComponentTypeID() noexcept
  25. {
  26.     static_assert (std::is_base_of<Component, T>::value, "");
  27.     static ComponentID typeID = getNewComponentTypeID();
  28.     return typeID;
  29. }
  30.  
  31. constexpr std::size_t maxComponents = 32;
  32. constexpr std::size_t maxGroups = 32;
  33.  
  34. using ComponentBitSet = std::bitset<maxComponents>;
  35. using GroupBitset = std::bitset<maxGroups>;
  36.  
  37. using ComponentArray = std::array<Component*, maxComponents>;
  38.  
  39. class Component
  40. {
  41. public:
  42.     Entity* entity;
  43.  
  44.     virtual void init() {}
  45.     virtual void update() {}
  46.     virtual void draw() {}
  47.     virtual ~Component() {}
  48. };
  49.  
  50. class Entity
  51. {
  52. private:
  53.     //Manager& manager;
  54.     bool active = true;
  55.     std::vector<Component*> components;
  56.  
  57.     ComponentArray componentArray;
  58.     ComponentBitSet componentBitset;
  59.     GroupBitset groupBitset;
  60.  
  61. public:
  62.     std::string ObjectName = "New Entity";
  63.     std::string ObjectTag = "None";
  64.     int objectID = 1;
  65.  
  66.     //Entity(Manager& mManager) : manager(mManager) {}
  67.  
  68.     void update()
  69.     {
  70.         for (auto& c : components) c->update();
  71.     }
  72.     void draw()
  73.     {
  74.         for (auto& c : components) c->draw();
  75.     }
  76.  
  77.     bool isActive() const { return active; }
  78.     void destroy() { active = false; }
  79.  
  80.     bool hasGroup(Group mGroup)
  81.     {
  82.         return groupBitset[mGroup];
  83.     }
  84.  
  85.     void addGroup(Group mGroup);
  86.     void delGroup(Group mGroup)
  87.     {
  88.         groupBitset[mGroup] = false;
  89.     }
  90.  
  91.  
  92.     template <typename T, typename... TArgs>
  93.     T& addComponent(TArgs&&... mArgs)
  94.     {
  95.         T* c = new T(std::forward<TArgs>(mArgs)...);
  96.         c->entity = this;
  97.         components.emplace_back(c);
  98.  
  99.         componentArray[getComponentTypeID<T>()] = c;
  100.         componentBitset[getComponentTypeID<T>()] = true;
  101.  
  102.         c->init();
  103.         return *c;
  104.     }
  105.  
  106.     template<typename T> T& getComponent() const
  107.     {
  108.         auto ptr(componentArray[getComponentTypeID<T>()]);
  109.         return *static_cast<T*>(ptr);
  110.     }
  111.  
  112.     template <typename T> bool hasComponent() const
  113.     {
  114.         //std::cout << "Buscando: " << componentBitset[getComponentTypeID<T>()] << std::endl;
  115.         return componentBitset[getComponentTypeID<T>()];
  116.     }
  117. };
  118. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement