-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_stream.cpp
74 lines (67 loc) · 1.58 KB
/
entity_stream.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
64
65
66
67
68
69
70
71
72
73
74
#include "entity_stream.h"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include "component.h"
set<Entity*> EntityStream::WithTag(Component::Tag tag)
{
return index[tag];
}
set<Entity*> EntityStream::WithTags(set<Component::Tag> tags)
{
if (tags.size() == 1)
{
return WithTag(*tags.begin());
}
set<Entity*> comp;
set<Entity*> output;
set<Component::Tag>::iterator it = tags.begin();
comp.insert(index[*it].begin(), index[*it].end());
it++;
while (it != tags.end())
{
Component::Tag tag = *it;
if (index.count(tag) == 0)
{
return output;
}
set_intersection(comp.begin(), comp.end(), index[tag].begin(), index[tag].end(), inserter(output, output.begin()));
comp.clear();
comp.insert(output.begin(), output.end());
output.clear();
it++;
}
return comp;
}
void EntityStream::EntityAdded(Entity* entity)
{
vector<Component::Tag> tags = entity->GetTags();
for (vector<Component::Tag>::iterator it = tags.begin(); it != tags.end(); it++)
{
map<Component::Tag, set<Entity*>>::iterator got = index.find(*it);
if (got != index.end())
{
got->second.insert(entity);
}
else
{
set<Entity*> tmp;
tmp.insert(entity);
index.insert(make_pair(*it, tmp));
}
}
}
void EntityStream::EntityRemoved(Entity* entity)
{
map<Component::Tag, set<Entity*>>::iterator it = index.begin();
while (it != index.end())
{
map<Component::Tag, set<Entity*>>::iterator got = index.find(it->first);
if (got != index.end())
{
got->second.erase(entity);
}
it++;
}
}