Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- template <class M = std::string, class E = int>
- E log(M msg, E error = 0)
- {
- std::cout << msg;
- return error;
- }
- struct CollisionInfo
- {
- int _id1 = 0;
- int _id2 = 0;
- };
- struct Entity
- {
- int _id = 0;
- int _x = 0;
- int _y = 0;
- std::string _name = "";
- Entity(std::string name, int id = 0)
- {
- _id = id;
- _name = name;
- log ("- Monster created !\n");
- }
- virtual~Entity()
- {
- log("- Monster deleted !\n");
- }
- void update()
- {
- }
- void render()
- {
- std::cout << " : _id = "<< _id << " : _name = "<< _name << "\n";
- }
- };
- struct EntityManager
- {
- std::vector<Entity*> _vecEntity;
- std::vector<int> _vecFreeEntity;
- EntityManager()
- {
- }
- virtual ~EntityManager()
- {
- // clean vector
- for (auto & it: _vecEntity)
- {
- if (it != nullptr)
- {
- delete it;
- it = nullptr;
- }
- }
- _vecEntity.clear();
- _vecFreeEntity.clear();
- }
- void add(Entity *entity)
- {
- if (entity != nullptr)
- {
- if (!_vecFreeEntity.empty())
- {
- int freeEntityIndex = _vecFreeEntity.back();
- _vecFreeEntity.pop_back();
- _vecEntity[freeEntityIndex] = entity;
- entity->_id = freeEntityIndex;
- }
- else
- {
- int id = _vecEntity.size();
- entity->_id = id;
- _vecEntity.push_back(entity);
- }
- }
- }
- void del(int id)
- {
- if (id > -1 && id < _vecEntity.size())
- if (_vecEntity[id] != nullptr)
- {
- delete _vecEntity[id];
- _vecEntity[id] = nullptr;
- _vecFreeEntity.push_back(id);
- }
- }
- int get(int id)
- {
- return _vecEntity[id]->_id;
- }
- int idByName(std::string name)
- {
- for (auto & it: _vecEntity)
- {
- if (it != nullptr)
- if (it->_name == name)
- return it->_id;
- }
- return -1;
- }
- void update()
- {
- }
- void render()
- {
- }
- };
- bool CollideEntity(Entity *e1, Entity *e2, CollisionInfo &info)
- {
- if (e1->_x == e2->_x &&
- e1->_y == e2->_y)
- {
- return true;
- }
- return false;
- }
- int main()
- {
- EntityManager *_manEntity = new EntityManager();
- for (unsigned i = 0; i < 5; i++)
- {
- _manEntity->add(new Entity("Mugen"));
- //unsigned index = _vecEntity.size(); // get the current index ;
- //_vecEntity.push_back(new Entity("Monster", _vecEntity.size()));
- }
- _manEntity->del(2);
- _manEntity->del(10);
- _manEntity->del(3);
- _manEntity->add(new Entity("Silver"));
- _manEntity->add(new Entity("Chrome"));
- _manEntity->add(new Entity("Opera"));
- _manEntity->add(new Entity("brandon"));
- _manEntity->add(new Entity("FireFox"));
- // _manEntity->del(_manEntity->idByName("Opera"));
- // _manEntity->del(_manEntity->idByName("FireFox"));
- for (unsigned index = 0; index < _manEntity->_vecEntity.size(); index++)
- {
- if (_manEntity->_vecEntity[index] != nullptr)
- {
- std::cout << "index = "<< index;
- _manEntity->_vecEntity[index]->render();
- }
- }
- if (_manEntity->idByName("Chrome") != -1)
- std::cout << "--- index = _id of Chrome = "<< _manEntity->idByName("Chrome") << "\n";
- // colision test
- // for (unsigned i = 0; i < _manEntity->_vecEntity.size(); i++)
- // {
- // for (unsigned j = i+1; j < _manEntity->_vecEntity.size(); j++)
- // {
- //
- // }
- // }
- delete _manEntity;
- return log("--- End of main ---");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement