-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityManager.cpp
54 lines (42 loc) · 1.25 KB
/
EntityManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "EntityManager.h"
EntityManager::EntityManager()
{
}
void EntityManager::update()
{
// TODO: add entities from m_entitiesToAdd the proper location(s)
// - add them to the vector of all entities
// - add them to the vector inside the map, with the tag as a key
for (auto e : m_entitiesToAdd)
{
m_entities.push_back(e);
m_entityMap[e->tag()].push_back(e);
}
m_entitiesToAdd.clear();
// remove dead entities from the vector of all entities
removeDeadEntities(m_entities);
// remove dead entities from each vector in the entity map
// C++17 way of iterating through [key, value] pairs in a map
for (auto& [tag, entityVec] : m_entityMap)
{
removeDeadEntities(entityVec);
}
}
void EntityManager::removeDeadEntities(EntityVec& vec)
{
vec.erase(std::remove_if(vec.begin(), vec.end(), [](const std::shared_ptr<Entity>& entity) {return !entity->isActive(); }), vec.end());
}
std::shared_ptr<Entity> EntityManager::addEntity(const std::string& tag)
{
auto entity = std::shared_ptr<Entity>(new Entity(m_totalEntities, tag));
m_entitiesToAdd.push_back(entity);
return entity;
}
const EntityVec& EntityManager::getEntities()
{
return m_entities;
}
const EntityVec& EntityManager::getEntities(const std::string& tag)
{
return m_entityMap[tag];
}