sschmid / Entitas

Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
MIT License
7.09k stars 1.11k forks source link

[Entitas:] Add mechanism to filter entities based on component values in a reactive system #234

Closed sschmid closed 7 years ago

sschmid commented 7 years ago

This issue attempts to provide a solution for #165 History: PR #189 added filter condition to matchers After implementing a solution I removed it in a future version, see #190

[...] It's very convenient, but it has an overall negative effect on performance [...]

Suggestion: Add a new interface, e.g. IFilterEntities which can be implemented by reactive systems

sschmid commented 7 years ago

Hi, I added IFilterEntities

Initially, this should enable us to filter entities based on component values, e.g.

public class KillSystem : IReactiveSystem, IFilterEntities {

    public TriggerOnEvent trigger { get { return CoreMatcher.Health.OnEntityAdded(); } }

    public bool filter(Entity entity) {
        return entity.health.value <= 0;
    }

    public void Execute(List<Entity> entities) {
        foreach(var e in entities) {
            // no need to check for health here anymore, yay
            e.flagDestroy = true;
        }
    }
}

Basically, IFilterEntities can now potentially replace the other existing interfaces IEnsureComponents and IExcludeComponents, since we can now express that in one simple filter(entity) method. This will also simplify the internal implementation of ReactiveSystem since we only have to check on single method now.

I'm thinking of deprecating IEnsureComponents and IExcludeComponents in favour of IFilterEntities.

What do you guys think?

MoritzVossKing commented 7 years ago

If this O(n) filter implementation scales equally well as IEnsureComponents did, sure!

In a recent gamejam project, I actually had to create an "Impassible" negative entity because IEnsureComponents couldn't reliably filter based on some external rules (a boolean array containing a collision map).