Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "EntityManager.h"
- #include "../Components/ComponentDrawable.h"
- #include "../Components/ComponentPosition.h"
- //Entity Pool
- EntityManager::EntityPool::EntityPool()
- : m_entityPool()
- {
- //Load in 50 projectiles
- for (int i = 0; i < 50; ++i)
- {
- m_entityPool.emplace_back(std::make_unique<Entity>(EntityName::Projectile, m_entityCount));
- ++m_entityCount;
- }
- //load in 10 enemies
- for (int i = 0; i < 10; ++i)
- {
- m_entityPool.emplace_back(std::make_unique<Entity>(EntityName::Enemy, m_entityCount));
- }
- }
- Entity * EntityManager::EntityPool::getEntity(EntityName name) const
- {
- Entity* availableEntity = nullptr;
- for (auto& entity : m_entityPool)
- {
- if (entity->inUse() && entity->getName() != name)
- {
- continue;
- }
- availableEntity = entity.get();
- availableEntity->activate();
- break;
- }
- return availableEntity;
- }
- //ComponentFactory
- EntityManager::ComponentFactory::ComponentFactory()
- {
- registerComponent<ComponentPosition>(ComponentType::Position);
- registerComponent<ComponentDrawable>(ComponentType::Drawable);
- }
- std::unique_ptr<ComponentBase> EntityManager::ComponentFactory::getComponent(ComponentType type) const
- {
- auto iter = m_componentFactory.find(type);
- assert(iter != m_componentFactory.cend());
- return iter->second();
- }
- //EntityFactory
- EntityManager::EntityFactory::EntityFactory()
- {
- }
- //EntityManager
- EntityManager::EntityManager()
- {
- }
- void EntityManager::addEntity(EntityName name)
- {
- assert(m_entityPool.getEntity(name));
- //Assign entity starting position here
- m_entities.push_back(m_entityPool.getEntity(name));
- }
- void EntityManager::removeEntity(int entityID)
- {
- auto entity = std::find_if(m_entities.begin(), m_entities.end(), [entityID](const auto& entity) { return entity->getID() == entityID; });
- assert(entity != m_entities.cend());
- (*entity)->deactivate();
- m_entities.erase(entity);
- }
- void EntityManager::update()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement