-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityManager.cpp
More file actions
52 lines (42 loc) · 1021 Bytes
/
EntityManager.cpp
File metadata and controls
52 lines (42 loc) · 1021 Bytes
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
#include "EntityManager.h"
#include <iostream>
#include <algorithm>
void EntityManager::removeDeadEntities(EntityVec& vec)
{
// Remove all even numbers from vector v
auto ne = remove_if(vec.begin(), vec.end(),
[](std::shared_ptr<Entity> e) {
return !e->isActive();
});
vec.erase(ne, vec.end());
}
EntityManager::EntityManager()
{
}
void EntityManager::update()
{
for (auto& e : m_entitiesToAdd) {
m_entities.push_back(e);
m_entityMap[e->m_tag].push_back(e);
}
m_entitiesToAdd.clear();
//remove dead entities from map
for (auto& [tag, entityVec] : m_entityMap) {
removeDeadEntities(entityVec);
}
removeDeadEntities(m_entities);
}
std::shared_ptr<Entity> EntityManager::addEntity(const std::string& tag)
{
auto e = std::shared_ptr<Entity>(new Entity(m_totalEntities++, tag));
m_entitiesToAdd.push_back(e);
return e;
}
const EntityVec& EntityManager::getEntities()
{
return m_entities;
}
const EntityVec& EntityManager::getEntities(const std::string& tag)
{
return m_entityMap[tag];
}