-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityManager.cpp
More file actions
82 lines (67 loc) · 1.93 KB
/
EntityManager.cpp
File metadata and controls
82 lines (67 loc) · 1.93 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "EntityManager.h"
#include "IComponent.h"
EntityManager::EntityManager(){}
EntityManager::~EntityManager(){}
void EntityManager::Update()
{
int count = 0;
// Loop through entity map
for (std::map<unsigned int, Entity>::iterator it = entities.begin(); it != entities.end(); it++)
{
it->second.Update();
}
}
IComponent* EntityManager::GetComponent(const unsigned int &entity, std::type_index type)
{
return entities[entity].GetComponent(type);
}
std::vector<IComponent*> EntityManager::GetAllComponentsOfType(std::type_index type)
{
std::vector<IComponent*> components;
// Loop through entity map
for (std::map<unsigned int, Entity>::iterator it = entities.begin(); it != entities.end(); it++)
{
// If component exists in entity, push it into the returning vector
if (entities[it->first].ComponentExists(type))
{
components.push_back(it->second.GetComponent(type));
}
}
// Return components
return components;
}
std::vector<unsigned int> EntityManager::GetAllEntitiesPossessingComponents(std::initializer_list<std::type_index> types)
{
std::vector<unsigned int> entity_IDs;
// Loop through entity map
for (std::map<unsigned int, Entity>::iterator it = entities.begin(); it != entities.end(); it++)
{
entity_IDs.push_back(it->first);
for (std::type_index type : types)
{
if (!entities[it->first].ComponentExists(type))
{
entity_IDs.pop_back();
break;
}
}
}
return entity_IDs;
}
void EntityManager::AddComponent(const unsigned int entityID, IComponent* component, std::type_index type, const bool replace)
{
entities[entityID].AddComponent(component, type, replace);
}
void EntityManager::RemoveComponent(const unsigned int entityID, std::type_index type)
{
entities[entityID].RemoveComponent(type);
}
unsigned int EntityManager::CreateEntity()
{
entities[lastID] = Entity{ lastID };
return lastID++;
}
void EntityManager::DestroyEntity(unsigned int entityID)
{
entities.erase(entityID);
}