Advertisement
Guest User

ECS.h

a guest
Dec 11th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <vector>
  4. #include <string>
  5.  
  6. #include "Components.h"
  7.  
  8. #define MAX_ENTITIES 1024
  9.  
  10. template <typename ComponentType>
  11. struct ComponentList
  12. {
  13.     std::vector<ComponentType> List;
  14.     std::vector<int> Unused;
  15.     std::vector<int> Map;
  16.     COMP_MASKS Mask;
  17.     int Count;
  18.  
  19.     ComponentList()
  20.     {
  21.         for (int i = 0; i < MAX_ENTITIES; i++)
  22.             Map.push_back(0);
  23.     }  
  24. };
  25.  
  26. class ECS
  27. {
  28.     public:
  29.         ECS();
  30.         ~ECS();
  31.  
  32.         //-- Entity Handling Methods --//
  33.         int EntityCreate();
  34.         void EntityDestroy(int EntityId);
  35.  
  36.         //-- Component handling methods --//
  37.         template <typename CompType>
  38.         CompType* EntityAddComponent(int EntityId, ComponentList<CompType> &Component);
  39.  
  40.         template <typename CompType>
  41.         void EntityRemoveComponent(int EntityId, ComponentList<CompType> &Component);
  42.  
  43.         template <typename CompType>
  44.         bool EntityHasComponent(int EntityId, ComponentList<CompType> Component);
  45.  
  46.         //-- Entity Masks --//
  47.         std::vector<int64_t> Entities;
  48.         std::vector<int> FreeEntitiesList;
  49.         int EntitiesCount;
  50.  
  51.         //-- List of Component Lists --//
  52.         std::vector<ComponentList<CompGeneric>*> ComponentLists;
  53.  
  54.         //-- Component Lists --//
  55.         ComponentList<CompInfo>     InfoComps;     // Information Components
  56.         ComponentList<CompPosition> PositionComps; // Position Components
  57.         ComponentList<CompSpeed>    SpeedComps;    // Speed Components
  58.         ComponentList<CompSprite>   SpriteComps;   // Sprite Components
  59. };
  60.  
  61. //----------------------------------------------------
  62. //----- Attach a component to an entity --------------
  63. //----------------------------------------------------
  64. template <typename CompType>
  65. CompType* ECS::EntityAddComponent(int EntityId, ComponentList<CompType> &Component)
  66. {
  67.     Entities[EntityId] |= Component.Mask;
  68.  
  69.     if (Component.Unused.size() == 0)
  70.     {
  71.         CompType newComponent;
  72.         Component.Map[EntityId] = Component.Count;
  73.         Component.Count++;
  74.         Component.List.push_back(newComponent);
  75.         return &Component.List.back();            // Return pointer to added component for editing.
  76.     }
  77.     else
  78.     {
  79.         Component.Map[EntityId] = Component.Unused[Component.Unused.back()];
  80.         Component.Unused.pop_back();
  81.         return &Component.List[Component.Map[EntityId]]; // Return pointer to added component for editing.
  82.     }
  83. };
  84.  
  85. //----------------------------------------------------
  86. //----- Remove a component from an entity ------------
  87. //----------------------------------------------------
  88. template <typename CompType>
  89. void ECS::EntityRemoveComponent(int EntityId, ComponentList<CompType> &Component)
  90. {
  91.     Entities[EntityId] ^= Component.Mask;
  92.  
  93.     Component.Unused.push_back(Component.Map[EntityId]);
  94. };
  95.  
  96. //----------------------------------------------------
  97. //----- Check if an entity has a component -----------
  98. //----------------------------------------------------
  99. template <typename CompType>
  100. bool ECS::EntityHasComponent(int EntityId, ComponentList<CompType> Component)
  101. {
  102.     return ((Entities[EntityId] & Component.Mask) == Component.Mask);
  103. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement