Advertisement
SilverTES

Entity Manager System : C++

Jan 8th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <class M = std::string, class E = int>
  5. E log(M msg, E error = 0)
  6. {
  7.     std::cout << msg;
  8.     return error;
  9. }
  10.  
  11.  
  12. struct CollisionInfo
  13. {
  14.     int _id1 = 0;
  15.     int _id2 = 0;
  16.  
  17. };
  18.  
  19. struct Entity
  20. {
  21.     int _id = 0;
  22.  
  23.     int _x = 0;
  24.     int _y = 0;
  25.  
  26.     std::string _name = "";
  27.  
  28.     Entity(std::string name, int id = 0)
  29.     {
  30.         _id = id;
  31.         _name = name;
  32.         log ("- Monster created !\n");
  33.     }
  34.  
  35.     virtual~Entity()
  36.     {
  37.         log("- Monster deleted !\n");
  38.     }
  39.  
  40.     void update()
  41.     {
  42.  
  43.     }
  44.  
  45.  
  46.     void render()
  47.     {
  48.         std::cout << " : _id = "<< _id << " : _name = "<< _name << "\n";
  49.     }
  50.  
  51.  
  52. };
  53.  
  54. struct EntityManager
  55. {
  56.     std::vector<Entity*> _vecEntity;
  57.     std::vector<int> _vecFreeEntity;
  58.  
  59.  
  60.  
  61.     EntityManager()
  62.     {
  63.  
  64.     }
  65.     virtual ~EntityManager()
  66.     {
  67.         // clean vector
  68.         for (auto & it: _vecEntity)
  69.         {
  70.             if (it != nullptr)
  71.             {
  72.                 delete it;
  73.                 it = nullptr;
  74.             }
  75.         }
  76.         _vecEntity.clear();
  77.  
  78.         _vecFreeEntity.clear();
  79.  
  80.     }
  81.  
  82.     void add(Entity *entity)
  83.     {
  84.         if (entity != nullptr)
  85.         {
  86.             if (!_vecFreeEntity.empty())
  87.             {
  88.                 int freeEntityIndex = _vecFreeEntity.back();
  89.                 _vecFreeEntity.pop_back();
  90.  
  91.                 _vecEntity[freeEntityIndex] = entity;
  92.                 entity->_id = freeEntityIndex;
  93.  
  94.             }
  95.             else
  96.             {
  97.                 int id = _vecEntity.size();
  98.                 entity->_id = id;
  99.                 _vecEntity.push_back(entity);
  100.             }
  101.         }
  102.     }
  103.  
  104.     void del(int id)
  105.     {
  106.         if (id > -1 && id < _vecEntity.size())
  107.             if (_vecEntity[id] != nullptr)
  108.             {
  109.                 delete _vecEntity[id];
  110.                 _vecEntity[id] = nullptr;
  111.  
  112.                 _vecFreeEntity.push_back(id);
  113.             }
  114.     }
  115.  
  116.     int get(int id)
  117.     {
  118.         return _vecEntity[id]->_id;
  119.     }
  120.  
  121.     int idByName(std::string name)
  122.     {
  123.         for (auto & it: _vecEntity)
  124.         {
  125.             if (it != nullptr)
  126.                 if (it->_name == name)
  127.                     return it->_id;
  128.         }
  129.  
  130.         return -1;
  131.     }
  132.  
  133.     void update()
  134.     {
  135.  
  136.     }
  137.  
  138.  
  139.     void render()
  140.     {
  141.  
  142.     }
  143.  
  144.  
  145. };
  146.  
  147.  
  148.  
  149. bool CollideEntity(Entity *e1, Entity *e2, CollisionInfo &info)
  150. {
  151.  
  152.     if (e1->_x == e2->_x &&
  153.         e1->_y == e2->_y)
  154.     {
  155.  
  156.  
  157.         return true;
  158.     }
  159.  
  160.     return false;
  161. }
  162.  
  163. int main()
  164. {
  165.     EntityManager *_manEntity = new EntityManager();
  166.  
  167.     for (unsigned i = 0; i < 5; i++)
  168.     {
  169.  
  170.         _manEntity->add(new Entity("Mugen"));
  171.         //unsigned index = _vecEntity.size(); // get the current index ;
  172.         //_vecEntity.push_back(new Entity("Monster", _vecEntity.size()));
  173.  
  174.     }
  175.  
  176.     _manEntity->del(2);
  177.     _manEntity->del(10);
  178.     _manEntity->del(3);
  179.  
  180.     _manEntity->add(new Entity("Silver"));
  181.     _manEntity->add(new Entity("Chrome"));
  182.     _manEntity->add(new Entity("Opera"));
  183.     _manEntity->add(new Entity("brandon"));
  184.     _manEntity->add(new Entity("FireFox"));
  185.  
  186. //    _manEntity->del(_manEntity->idByName("Opera"));
  187. //    _manEntity->del(_manEntity->idByName("FireFox"));
  188.  
  189.     for (unsigned index = 0; index < _manEntity->_vecEntity.size(); index++)
  190.     {
  191.         if (_manEntity->_vecEntity[index] != nullptr)
  192.         {
  193.             std::cout << "index = "<<  index;
  194.             _manEntity->_vecEntity[index]->render();
  195.         }
  196.     }
  197.  
  198.  
  199.     if (_manEntity->idByName("Chrome") != -1)
  200.         std::cout << "--- index = _id of Chrome = "<< _manEntity->idByName("Chrome") << "\n";
  201.  
  202.     // colision test
  203. //    for (unsigned i = 0; i < _manEntity->_vecEntity.size(); i++)
  204. //    {
  205. //        for (unsigned j = i+1; j < _manEntity->_vecEntity.size(); j++)
  206. //        {
  207. //
  208. //        }
  209. //    }
  210.  
  211.     delete _manEntity;
  212.  
  213.     return log("--- End of main ---");
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement