-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityManager.cpp
61 lines (48 loc) · 1.51 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
55
56
57
58
59
60
61
#include "EntityManager.h"
#include <iostream>
EntityManager::EntityManager()
: m_totalEntities(0)
{
}
void EntityManager::update()
{
// add entities from m_entitiesToAdd to the proper locations
// add them to the vector of all entities
// add them to the vector inside the map with the tag as the key
for (auto e : m_entitiesToAdd)
{
m_entities.push_back(e);
m_entityMap[e->tag()].push_back(e);
}
// 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);
}
m_entitiesToAdd.clear();
}
void EntityManager::removeDeadEntities(EntityVec &vec)
{
// use removeif to remove the entity from the vector
vec.erase(std::remove_if(vec.begin(), vec.end(), [](const std::shared_ptr<Entity> &e)
{ return !e->isActive(); }),
vec.end());
// std::cout << "Entities: " << vec.size() << std::endl;
}
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];
}