Entitas  0.35.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
Matcher.cs
1 using System;
2 using System.Collections.Generic;
3 
4 namespace Entitas {
5 
6  public partial class Matcher : IAllOfMatcher, IAnyOfMatcher, INoneOfMatcher {
7 
8  public int[] indices {
9  get {
10  if(_indices == null) {
11  _indices = mergeIndices();
12  }
13  return _indices;
14  }
15  }
16 
17  public int[] allOfIndices { get { return _allOfIndices; } }
18  public int[] anyOfIndices { get { return _anyOfIndices; } }
19  public int[] noneOfIndices { get { return _noneOfIndices; } }
20 
21  int[] _indices;
22  int[] _allOfIndices;
23  int[] _anyOfIndices;
24  int[] _noneOfIndices;
25 
26  Matcher() {
27  }
28 
29  IAnyOfMatcher IAllOfMatcher.AnyOf(params int[] indices) {
30  _anyOfIndices = distinctIndices(indices);
31  _indices = null;
32  return this;
33  }
34 
35  IAnyOfMatcher IAllOfMatcher.AnyOf(params IMatcher[] matchers) {
36  return ((IAllOfMatcher)this).AnyOf(mergeIndices(matchers));
37  }
38 
39  public INoneOfMatcher NoneOf(params int[] indices) {
40  _noneOfIndices = distinctIndices(indices);
41  _indices = null;
42  return this;
43  }
44 
45  public INoneOfMatcher NoneOf(params IMatcher[] matchers) {
46  return NoneOf(mergeIndices(matchers));
47  }
48 
49  public bool Matches(Entity entity) {
50  return (_allOfIndices == null || entity.HasComponents(_allOfIndices))
51  && (_anyOfIndices == null || entity.HasAnyComponent(_anyOfIndices))
52  && (_noneOfIndices == null || !entity.HasAnyComponent(_noneOfIndices));
53  }
54 
55  int[] mergeIndices() {
56  var indicesList = EntitasCache.GetIntList();
57 
58  if(_allOfIndices != null) {
59  indicesList.AddRange(_allOfIndices);
60  }
61  if(_anyOfIndices != null) {
62  indicesList.AddRange(_anyOfIndices);
63  }
64  if(_noneOfIndices != null) {
65  indicesList.AddRange(_noneOfIndices);
66  }
67 
68  var mergedIndices = distinctIndices(indicesList);
69 
70  EntitasCache.PushIntList(indicesList);
71 
72  return mergedIndices;
73  }
74 
75  static int[] mergeIndices(IMatcher[] matchers) {
76  var indices = new int[matchers.Length];
77  for (int i = 0; i < matchers.Length; i++) {
78  var matcher = matchers[i];
79  if(matcher.indices.Length != 1) {
80  throw new MatcherException(matcher);
81  }
82  indices[i] = matcher.indices[0];
83  }
84 
85  return indices;
86  }
87 
88  static string[] getComponentNames(IMatcher[] matchers) {
89  for (int i = 0; i < matchers.Length; i++) {
90  var matcher = matchers[i] as Matcher;
91  if(matcher != null && matcher.componentNames != null) {
92  return matcher.componentNames;
93  }
94  }
95 
96  return null;
97  }
98 
99  static void setComponentNames(Matcher matcher, IMatcher[] matchers) {
100  var componentNames = getComponentNames(matchers);
101  if(componentNames != null) {
102  matcher.componentNames = componentNames;
103  }
104  }
105 
106  static int[] distinctIndices(IList<int> indices) {
107  var indicesSet = EntitasCache.GetIntHashSet();
108 
109  foreach(var index in indices) {
110  indicesSet.Add(index);
111  }
112  var uniqueIndices = new int[indicesSet.Count];
113  indicesSet.CopyTo(uniqueIndices);
114  Array.Sort(uniqueIndices);
115 
116  EntitasCache.PushIntHashSet(indicesSet);
117 
118  return uniqueIndices;
119  }
120  }
121 
122  public class MatcherException : Exception {
123  public MatcherException(IMatcher matcher) : base(
124  "matcher.indices.Length must be 1 but was " + matcher.indices.Length) {
125  }
126  }
127 }
bool HasComponents(int[] indices)
Definition: Entity.cs:292
bool HasAnyComponent(int[] indices)
Definition: Entity.cs:304