Advertisement
SilverTES

Data Oriented Design Entity Component System

Apr 16th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3.  
  4. using namespace std;
  5.  
  6. typedef unsigned long EntityId;
  7. typedef unsigned int EntityType;
  8.  
  9. enum {PLAYER, ALIEN, TOWER};
  10.  
  11. template <typename C> class System
  12. {
  13. public:
  14.  
  15.     std::map<EntityId, C * > components;
  16. };
  17.  
  18. class Component
  19. {
  20.     EntityId id;
  21.     EntityType type;
  22.  
  23. public:
  24.     Component(EntityId pId, EntityType pType) : id(pId), type(pType) {}
  25.     getId()
  26.     {
  27.         return id;
  28.     }
  29.     getType()
  30.     {
  31.         return type;
  32.     }
  33.     void render()
  34.     {
  35.         cout << "---------------------" << endl;
  36.         cout << " - Id   = " << getId() << endl;
  37.         cout << " - Type = " << getType() << endl;
  38.         cout << "---------------------" << endl;
  39.     }
  40.  
  41. };
  42.  
  43.  
  44. class PositionComponent: public Component
  45. {
  46.     int x;
  47.     int y;
  48.     int z;
  49. public:
  50.     PositionComponent(EntityId id, EntityType type) : Component(id, type), x(0), y(0), z(0) {}
  51.     getX(){return x;}
  52.     getY(){return y;}
  53.     getZ(){return z;}
  54. };
  55.  
  56. class RenderComponent : public Component
  57. {
  58. public:
  59.     RenderComponent(EntityId id, EntityType type) : Component(id, type) {}
  60.  
  61. };
  62.  
  63. EntityId uniqId=0;
  64. System<Component> mapComponent;
  65. System<PositionComponent> mapPos;
  66. System<RenderComponent> mapRender;
  67.  
  68. class EntityManager
  69. {
  70. public:
  71.  
  72.     static void addEntity (EntityType type, int x, int y, int z)
  73.     {
  74.         uniqId++;
  75.         switch (type)
  76.         {
  77.         case PLAYER:
  78.             mapComponent.components[uniqId] = new Component(uniqId, type);
  79.             mapPos.components[uniqId] = new PositionComponent(uniqId, type);
  80.             mapRender.components[uniqId] = new RenderComponent(uniqId, type);
  81.             break;
  82.         case ALIEN:
  83.             mapComponent.components[uniqId] = new Component(uniqId, type);
  84.             mapPos.components[uniqId] = new PositionComponent(uniqId, type);
  85.             mapRender.components[uniqId] = new RenderComponent(uniqId, type);
  86.             break;
  87.         case TOWER:
  88.             mapComponent.components[uniqId] = new Component(uniqId, type);
  89.             mapPos.components[uniqId] = new PositionComponent(uniqId, type);
  90.             //mapRender.components[uniqId] = new RenderComponent(uniqId, type);
  91.             break;
  92.         default:
  93.             break;
  94.  
  95.         }
  96.     }
  97.  
  98.     static void render()
  99.     {
  100.         for (std::map<EntityId, RenderComponent*>::iterator it = mapRender.components.begin(); it != mapRender.components.end(); ++it)
  101.         {
  102.             (*it).second->render();
  103.         }
  104.     }
  105. };
  106.  
  107. int main()
  108. {
  109.  
  110.     EntityManager::addEntity(PLAYER, 10,10,0);
  111.     EntityManager::addEntity(ALIEN, 10,10,0);
  112.     EntityManager::addEntity(PLAYER, 10,10,0);
  113.     EntityManager::addEntity(TOWER, 10,10,0);
  114.  
  115.     EntityManager::render();
  116.  
  117.  
  118.  
  119.     cout << "MugenStudio" << endl;
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement