Skip to content

Commit

Permalink
Merge pull request #189 from sschmid/feature/#165-matchers-with-filter
Browse files Browse the repository at this point in the history
Added filter condition to matchers
  • Loading branch information
sschmid authored Sep 26, 2016
2 parents 922cae9 + d47b5fd commit 87a10cd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Entitas/Entitas/Interfaces/IMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace Entitas {

public delegate bool MatcherFilter(Entity entity);

public interface IMatcher {
int[] indices { get; }
IMatcher Where(MatcherFilter filter);
bool Matches(Entity entity);
}
}
10 changes: 9 additions & 1 deletion Entitas/Entitas/Matcher/Matcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public int[] indices {
int[] _anyOfIndices;
int[] _noneOfIndices;

MatcherFilter _filter;

Matcher() {
}

Expand All @@ -46,11 +48,17 @@ public INoneOfMatcher NoneOf(params IMatcher[] matchers) {
return NoneOf(mergeIndices(matchers));
}

public IMatcher Where(MatcherFilter filter) {
_filter = filter;
return this;
}

public bool Matches(Entity entity) {
var matchesAllOf = _allOfIndices == null || entity.HasComponents(_allOfIndices);
var matchesAnyOf = _anyOfIndices == null || entity.HasAnyComponent(_anyOfIndices);
var matchesNoneOf = _noneOfIndices == null || !entity.HasAnyComponent(_noneOfIndices);
return matchesAllOf && matchesAnyOf && matchesNoneOf;
var passesFilter = _filter == null || _filter(entity);
return matchesAllOf && matchesAnyOf && matchesNoneOf && passesFilter;
}

int[] mergeIndices() {
Expand Down
23 changes: 23 additions & 0 deletions Tests/Tests/Entitas/describe_Matcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,29 @@ void when_creating_matcher() {
mX.GetHashCode().should_be(mY.GetHashCode());
};
};

context["when filtering"] = () => {
IMatcher m = null;
before = () => m = Matcher.AllOf(CID.ComponentD).Where(entity => {
var component = (NameAgeComponent)entity.GetComponent(CID.ComponentD);
return component.age > 30;
});
it["only contains entities passing the filter condition"] = () => {
var e1 = this.CreateEntity();
var nameAge1 = new NameAgeComponent { name = "Max", age = 42 };
e1.AddComponent(CID.ComponentD, nameAge1);
var e2 = this.CreateEntity();
var nameAge2 = new NameAgeComponent { name = "Jack", age = 24 };
e2.AddComponent(CID.ComponentD, nameAge2);
m.Matches(e1).should_be_true();
m.Matches(e2).should_be_false();
};
};
}

static IMatcher allOfAB() {
Expand Down

0 comments on commit 87a10cd

Please sign in to comment.