-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityManager.cpp
63 lines (53 loc) · 1.23 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
62
63
#include "EntityManager.h"
typedef std::vector<std::shared_ptr<Entity>> EntityVec;
typedef std::map<std::string, EntityVec> EntityMap;
EntityManager::EntityManager() {};
std::shared_ptr<Entity> EntityManager::addEntity(const std::string tag)
{
auto e = std::shared_ptr<Entity>(new Entity(tag, m_totalEntities++));
m_toAdd.push_back(e);
return e;
}
void EntityManager::destroyEntity(std::shared_ptr<Entity> e)
{
if (e->isAlive())
{
e->destroy();
m_toDelete.push_back(e);
}
}
void EntityManager::update()
{
for (auto e : m_toDelete)
{
auto it = std::find(m_entities.begin(), m_entities.end(), e);
if (it != m_entities.end())
{
m_entities.erase(it);
}
auto it2 = std::find(m_entityMap[e->tag()].begin(), m_entityMap[e->tag()].end(), e);
if (it2 != m_entityMap[e->tag()].end())
{
m_entityMap[e->tag()].erase(it2);
}
}
for (auto e : m_toAdd)
{
m_entities.push_back(e);
m_entityMap[e->tag()].push_back(e);
}
m_toAdd.clear();
m_toDelete.clear();
}
const EntityVec& EntityManager::getEntities()
{
return m_entities;
}
const EntityVec& EntityManager::getEntities(const std::string& tag)
{
return m_entityMap[tag];
}
const std::map<std::string, EntityVec>& EntityManager::getEntityMap()
{
return m_entityMap;
}