Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include "EntityManager.h"
  2. #include "../Components/ComponentDrawable.h"
  3. #include "../Components/ComponentPosition.h"
  4.  
  5. //Entity Pool
  6. EntityManager::EntityPool::EntityPool()
  7.     : m_entityPool()
  8. {
  9.     //Load in 50 projectiles
  10.     for (int i = 0; i < 50; ++i)
  11.     {
  12.         m_entityPool.emplace_back(std::make_unique<Entity>(EntityName::Projectile, m_entityCount));
  13.         ++m_entityCount;
  14.     }
  15.  
  16.     //load in 10 enemies
  17.     for (int i = 0; i < 10; ++i)
  18.     {
  19.         m_entityPool.emplace_back(std::make_unique<Entity>(EntityName::Enemy, m_entityCount));
  20.     }
  21. }
  22.  
  23. Entity * EntityManager::EntityPool::getEntity(EntityName name) const
  24. {
  25.     Entity* availableEntity = nullptr;
  26.     for (auto& entity : m_entityPool)
  27.     {
  28.         if (entity->inUse() && entity->getName() != name)
  29.         {
  30.             continue;
  31.         }
  32.    
  33.         availableEntity = entity.get();
  34.         availableEntity->activate();
  35.         break;
  36.     }
  37.  
  38.     return availableEntity;
  39. }
  40.  
  41. //ComponentFactory
  42. EntityManager::ComponentFactory::ComponentFactory()
  43. {
  44.     registerComponent<ComponentPosition>(ComponentType::Position);
  45.     registerComponent<ComponentDrawable>(ComponentType::Drawable);
  46. }
  47.  
  48. std::unique_ptr<ComponentBase> EntityManager::ComponentFactory::getComponent(ComponentType type) const
  49. {
  50.     auto iter = m_componentFactory.find(type);
  51.     assert(iter != m_componentFactory.cend());
  52.     return iter->second();
  53. }
  54.  
  55. //EntityFactory
  56. EntityManager::EntityFactory::EntityFactory()
  57. {
  58.  
  59. }
  60.  
  61. //EntityManager
  62. EntityManager::EntityManager()
  63. {
  64. }
  65.  
  66. void EntityManager::addEntity(EntityName name)
  67. {
  68.     assert(m_entityPool.getEntity(name));
  69.     //Assign entity starting position here
  70.  
  71.     m_entities.push_back(m_entityPool.getEntity(name));
  72. }
  73.  
  74. void EntityManager::removeEntity(int entityID)
  75. {
  76.     auto entity = std::find_if(m_entities.begin(), m_entities.end(), [entityID](const auto& entity) { return entity->getID() == entityID; });
  77.     assert(entity != m_entities.cend());
  78.     (*entity)->deactivate();
  79.     m_entities.erase(entity);
  80. }
  81.  
  82. void EntityManager::update()
  83. {
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement