-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityManager.cpp
More file actions
67 lines (53 loc) · 1.42 KB
/
EntityManager.cpp
File metadata and controls
67 lines (53 loc) · 1.42 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "EntityManager.h"
EntityManager::EntityManager()
{
}
void EntityManager::update()
{
// TODO: add entities from m_entitiesToAdd the proper location(s)
// - add them to the vector ao all entities
// - add them to the vector inside the map, with the tag as a key
for (std::shared_ptr<Entity> 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(), [](auto e) {return !e->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)
{
// TODO: this is incorrect, return the correct vector from the map
if (m_entityMap.find(tag) != m_entityMap.end())
{
return m_entityMap.find(tag)->second;
}
else
{
EntityVec empty;
return empty;
}
}