-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.cs
83 lines (66 loc) · 2.75 KB
/
Query.cs
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
using System;
using System.Collections.Generic;
namespace MelonECS
{
public class Query
{
private readonly HashSet<int> includeSet = new HashSet<int>();
private readonly HashSet<int> excludeSet = new HashSet<int>();
private Entity[] entities;
private readonly HashSet<Entity> entitiesSet = new HashSet<Entity>();
private readonly List<Entity> addedEntities = new List<Entity>();
private readonly List<Entity> removedEntities = new List<Entity>();
internal Query(IEnumerable<Type> include, IEnumerable<Type> exclude)
{
entities = new Entity[4];
if (include != null)
{
foreach (var type in include)
includeSet.Add(ComponentType.Index(type));
}
if (exclude != null)
{
foreach (var type in exclude)
excludeSet.Add(ComponentType.Index(type));
}
}
internal void AddEntity(Entity entity)
{
if (entitiesSet.Contains(entity) || addedEntities.Contains(entity))
return;
addedEntities.Add(entity);
}
internal void RemoveEntity(Entity entity)
{
if (!entitiesSet.Contains(entity) || removedEntities.Contains(entity))
return;
removedEntities.Add(entity);
}
internal void FlushAddsAndRemoves()
{
if ( addedEntities.Count == 0 && removedEntities.Count == 0 )
return;
for (int i = 0; i < removedEntities.Count; i++)
{
int index = Array.IndexOf(entities, removedEntities[i]);
entities[index] = entities[entitiesSet.Count - 1];
entitiesSet.Remove(removedEntities[i]);
}
ArrayUtil.EnsureLength(ref entities, entitiesSet.Count + addedEntities.Count);
for (int i = 0; i < addedEntities.Count; i++)
{
entities[entitiesSet.Count] = addedEntities[i];
entitiesSet.Add(addedEntities[i]);
}
addedEntities.Clear();
removedEntities.Clear();
}
internal bool IsMatch(HashSet<int> components)
=> includeSet.IsSubsetOf(components) && (excludeSet.Count == 0 || !excludeSet.IsSubsetOf(components));
internal bool IsMatch(int component)
=> includeSet.Contains(component) && !excludeSet.Contains(component);
internal bool IsMatch(Query query)
=> includeSet.SetEquals(query.includeSet) && excludeSet.SetEquals(query.excludeSet);
public ArrayRef<Entity> GetEntities() => new ArrayRef<Entity>(entities, 0, entitiesSet.Count);
}
}