Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4.  
  5. #define TOTOAL_ENTITIES 1000000
  6.  
  7. enum class ComponentType { Position, Sprite, Collider };
  8.  
  9. struct PositionComponent
  10. {
  11. float x, y, z;
  12.  
  13. PositionComponent(float x, float y, float z) :
  14. x(x), y(y), z(z)
  15. {
  16. }
  17. };
  18.  
  19. struct SpriteComponent
  20. {
  21. float r, g, b, a;
  22.  
  23. SpriteComponent(float r, float g, float b, float a) :
  24. r(r), g(g), b(b), a(a)
  25. {
  26. }
  27. };
  28.  
  29. struct ColliderComponent
  30. {
  31. float width, height;
  32.  
  33. ColliderComponent(float width, float height) :
  34. width(width), height(height), b(b), a(a)
  35. {
  36. }
  37. };
  38.  
  39. struct Entity
  40. {
  41. int index;
  42. };
  43.  
  44. struct ComponentTable
  45. {
  46. std::unordered_map<ComponentType, int> componentTypes;
  47. };
  48.  
  49. struct EntityManager
  50. {
  51. static int currentEntityID;
  52. static int currentPositionID;
  53. static int currentSpriteID;
  54. static int currentColliderID;
  55.  
  56. std::vector<ComponentTable> componentsTable;
  57.  
  58. std::vector<PositionComponent> positions;
  59. std::vector<SpriteComponent> sprites;
  60. std::vector<ColliderComponent> colliders;
  61.  
  62. EntityManager()
  63. {
  64. componentsTable.resize(TOTOAL_ENTITIES);
  65. }
  66.  
  67. void AddPositionComponent(const Entity& entity)
  68. {
  69. positions.emplace_back(PositionComponent(1, 2, 3));
  70. componentsTable[entity.index].componentTypes.insert({ ComponentType::Position, positions.size() - 1 });
  71. }
  72.  
  73. void AddSpriteComponent(const Entity& entity)
  74. {
  75. sprites.emplace_back(SpriteComponent(1.0f, 0.0f, 0.0f, 0.0f));
  76. componentsTable[entity.index].componentTypes.insert({ ComponentType::Sprite, sprites.size() - 1 });
  77. }
  78.  
  79. void AddColliderComponent(const Entity& entity)
  80. {
  81. colliders.emplace_back(ColliderComponent(100, 200));
  82. componentsTable[entity.index].componentTypes.insert({ ComponentType::Collider, colliders.size() - 1 });
  83. }
  84. };
  85.  
  86. int EntityManager::currentEntityID = 0;
  87. int EntityManager::currentPositionID = 0;
  88. int EntityManager::currentSpriteID = 0;
  89. int EntityManager::currentColliderID = 0;
  90.  
  91. int main()
  92. {
  93. EntityManager em;
  94.  
  95.  
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement