-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEntityQuery.cpp
149 lines (128 loc) · 3.61 KB
/
EntityQuery.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "EntityQuery.h"
EntityQuery::EntityQuery(std::vector<ComponentType> _compTypes, std::vector<Tag> _tags)
{
compTypes = _compTypes;
std::sort(compTypes.begin(), compTypes.end());
tags = _tags;
std::sort(tags.begin(), tags.end());
}
EntityQuery::EntityQuery()
{
}
std::vector<Chunk*> EntityQuery::foundChunks()
{
return chunks;
}
void EntityQuery::DeleteFoundEntities()
{
for (int i = 0; i < chunks.size(); i++)
{
chunks[i]->scheduleAllEntitiesToDelete();
}
}
std::size_t EntityQuery::TagsHash(std::vector<Tag>& tags)
{
std::size_t seed = tags.size();
for (int i = 0; i < tags.size(); i++)
{
seed ^= tags[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
std::size_t EntityQuery::ComponentTypesHash(std::vector<ComponentType>& compTypes)
{
std::size_t seed = compTypes.size();
for (int i = 0; i < compTypes.size(); i++)
{
seed ^= compTypes[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
std::size_t EntityQuery::QueryParamterHash(std::vector<ComponentType>& compTypes, std::vector<Tag>& tags)
{
std::size_t seed = compTypes.size() + tags.size();
std::size_t hash = ComponentTypesHash(compTypes);
seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
hash = TagsHash(tags);
seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
EntityQuery::EntityQuery(std::vector<Chunk*> chosenChunks)
{
chunks = chosenChunks;
for (int i = 0; i < chunks.size(); i++)
{
entityCount += chunks[i]->getCurrEntCount();
}
}
void EntityQuery::searchChunks(std::vector<Chunk*>& allChunks,int _chunkListVersion)
{
chunks.clear();
for (int i = 0; i < allChunks.size(); i++)
{
Chunk* chunk = allChunks[i];
const std::vector<ComponentType>& chunkComps = chunk->getArchetype().getComponentTypeArray();
std::vector<Tag>& chunkTags = chunk->getAllTags();
if (chunkComps.size() < compTypes.size() || chunkTags.size() < tags.size())
{
continue;
}
int c_search = 0;
// check that chunk contains query components
for (int j = 0; j < compTypes.size() && c_search < chunkComps.size(); j++)
{
for (; c_search < chunkComps.size(); c_search++)
{
if (compTypes[j] == chunkComps[c_search])
{
break;
}
}
}
int t_search = 0;
// check that chunk contains tags, if there are no tags in the query, pass
if (tags.size() != 0)
{
for (int j = 0; j < tags.size() && t_search < chunkTags.size(); j++)
{
for (; t_search < chunkTags.size(); t_search++)
{
if (tags[j] == chunkTags[t_search])
{
break;
}
}
}
}
if ((c_search != chunkComps.size() || compTypes.size() == 0) && (t_search != chunkTags.size() || tags.size() == 0))
{
chunks.push_back(allChunks[i]);
}
}
recountFoundEntities();
chunkListVersion = _chunkListVersion;
}
void EntityQuery::recountFoundEntities()
{
entityCount = 0;
for (int i = 0; i < chunks.size(); i++)
{
entityCount += chunks[i]->getCurrEntCount();
}
}
int EntityQuery::totalEntitiesFound()
{
return entityCount;
}
int EntityQuery::chunkCount()
{
return chunks.size();
}
int EntityQuery::getChunkListVersion()
{
return chunkListVersion;
}
Chunk* EntityQuery::chunk(int i)
{
return chunks[i];
}