GetGroup() public method

public GetGroup ( IMatcher matcher ) : Group
matcher IMatcher
return Group
Example #1
0
        static void groupExample(Context context) {
            context.GetGroup(Matcher.Position).GetEntities();

            // ----------------------------

            context.GetGroup(Matcher.Position).OnEntityAdded += (group, entity, index, component) => {
                // Do something
            };
        }
 public void Before()
 {
     _context = Helper.CreateContext();
     _context.GetGroup(Matcher.AllOf(new [] { CP.ComponentA }));
     _e = _context.CreateEntity();
     _e.AddComponent(CP.ComponentA, new ComponentA());
 }
    public void Before()
    {
        _context = Helper.CreateContext();
        _index = new PrimaryEntityIndex<string>(_context.GetGroup(Matcher.AllOf(CP.ComponentA)), (e, c) => ((NameComponent)c).name);

        for (int i = 0; i < 10; i++) {
            var nameComponent = new NameComponent();
            nameComponent.name = i.ToString();
            _context.CreateEntity().AddComponent(CP.ComponentA, nameComponent);
        }
    }
    void when_primary_index()
    {
        PrimaryEntityIndex<string> index = null;
        Context ctx = null;
        Group group = null;

        before = () => {
            ctx = new Context(CID.TotalComponents);
            group = ctx.GetGroup(Matcher.AllOf(CID.ComponentA));
            index = new PrimaryEntityIndex<string>(group, (e, c) => {
                var nameAge = c as NameAgeComponent;
                return nameAge != null
                    ? nameAge.name
                    : ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name;
            });
        };

        context["when entity for key doesn't exist"] = () => {

            it["doesn't have entity"] = () => {
                index.HasEntity("unknownKey").should_be_false();
            };

            it["throws exception when attempting to get entity"] = expect<EntityIndexException>(() => {
                index.GetEntity("unknownKey");
            });

            it["returns null when trying to get entity"] = () => {
                index.TryGetEntity("unknownKey").should_be_null();
            };
        };

        context["when entity for key exists"] = () => {

            const string name = "Max";
            Entity entity = null;

            before = () => {
                var nameAgeComponent = new NameAgeComponent();
                nameAgeComponent.name = name;
                entity = ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
            };

            it["has entity"] = () => {
                index.HasEntity(name).should_be_true();
            };

            it["gets entity for key"] = () => {
                index.GetEntity(name).should_be_same(entity);
            };

            it["gets entity when trying"] = () => {
                index.TryGetEntity(name).should_be_same(entity);
            };

            it["retains entity"] = () => {
                entity.retainCount.should_be(3); // Context, Group, EntityIndex
            };

            it["has existing entity"] = () => {
                var newIndex = new PrimaryEntityIndex<string>(group, (e, c) => {
                    var nameAge = c as NameAgeComponent;
                    return nameAge != null
                        ? nameAge.name
                        : ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name;
                });
                newIndex.HasEntity(name).should_be_true();
            };

            it["releases and removes entity from index when component gets removed"] = () => {
                entity.RemoveComponent(CID.ComponentA);
                index.HasEntity(name).should_be_false();
                entity.retainCount.should_be(1); // Context
            };

            it["throws when adding an entity for the same key"] = expect<EntityIndexException>(() => {
                var nameAgeComponent = new NameAgeComponent();
                nameAgeComponent.name = name;
                entity = ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
            });

            context["when deactivated"] = () => {

                before = () => {
                    index.Deactivate();
                };

                it["clears index and releases entity"] = () => {
                    index.HasEntity(name).should_be_false();
                    entity.retainCount.should_be(2); // Context, Group
                };

                it["doesn't add entities anymore"] = () => {
                    var nameAgeComponent = new NameAgeComponent();
                    nameAgeComponent.name = name;
                    ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
                    index.HasEntity(name).should_be_false();
                };

                context["when actrivated"] = () => {

                    before = () => {
                        index.Activate();
                    };

                    it["has existing entity"] = () => {
                        index.HasEntity(name).should_be_true();
                    };

                    it["adds new entities"] = () => {
                        var nameAgeComponent = new NameAgeComponent();
                        nameAgeComponent.name = "Jack";
                        entity = ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);

                        index.HasEntity("Jack").should_be_true();
                    };
                };
            };
        };
    }
    void when_index()
    {
        EntityIndex<string> index = null;
        Context ctx = null;
        Group group = null;

        before = () => {
            ctx = new Context(CID.TotalComponents);
            group = ctx.GetGroup(Matcher.AllOf(CID.ComponentA));
            index = new EntityIndex<string>(group, (e, c) => {
                var nameAge = c as NameAgeComponent;
                return nameAge != null
                    ? nameAge.name
                    : ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name;
            });
        };

        context["when entity for key doesn't exist"] = () => {

            it["has no entities"] = () => {
                index.GetEntities("unknownKey").should_be_empty();
            };
        };

        context["when entity for key exists"] = () => {

            const string name = "Max";
            NameAgeComponent nameAgeComponent = null;
            Entity entity1 = null;
            Entity entity2 = null;

            before = () => {
                nameAgeComponent = new NameAgeComponent();
                nameAgeComponent.name = name;
                entity1 = ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
                entity2 = ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
            };

            it["gets entities for key"] = () => {
                var entities = index.GetEntities(name);
                entities.Count.should_be(2);
                entities.should_contain(entity1);
                entities.should_contain(entity2);
            };

            it["retains entity"] = () => {
                entity1.retainCount.should_be(3); // Context, Group, EntityIndex
                entity2.retainCount.should_be(3); // Context, Group, EntityIndex
            };

            it["has existing entities"] = () => {
                var newIndex = new EntityIndex<string>(group, (e, c) => {
                    var nameAge = c as NameAgeComponent;
                    return nameAge != null
                        ? nameAge.name
                        : ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name;
                });
                newIndex.GetEntities(name).Count.should_be(2);
            };

            it["releases and removes entity from index when component gets removed"] = () => {
                entity1.RemoveComponent(CID.ComponentA);
                index.GetEntities(name).Count.should_be(1);
                entity1.retainCount.should_be(1); // Context
            };

            context["when deactivated"] = () => {

                before = () => {
                    index.Deactivate();
                };

                it["clears index and releases entity"] = () => {
                    index.GetEntities(name).should_be_empty();
                    entity1.retainCount.should_be(2); // Context, Group
                    entity2.retainCount.should_be(2); // Context, Group
                };

                it["doesn't add entities anymore"] = () => {
                    ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
                    index.GetEntities(name).should_be_empty();
                };

                context["when actrivated"] = () => {

                    before = () => {
                        index.Activate();
                    };

                    it["has existing entities"] = () => {
                        var entities = index.GetEntities(name);
                        entities.Count.should_be(2);
                        entities.should_contain(entity1);
                        entities.should_contain(entity2);
                    };

                    it["adds new entities"] = () => {
                        var entity3 = ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);

                        var entities = index.GetEntities(name);
                        entities.Count.should_be(3);
                        entities.should_contain(entity1);
                        entities.should_contain(entity2);
                        entities.should_contain(entity3);
                    };
                };
            };
        };
    }
 public void SetPool(Context pool)
 {
     _group = pool.GetGroup(Matcher.AllOf(GameMatcher.Move, GameMatcher.Position));
 }
    void when_created()
    {
        ReactiveSystemSpy system = null;

        before = () => {
            _contexts = new Contexts { test = new Context(CID.TotalComponents) };
        };

        context["OnEntityAdded"] = () => {

            before = () => {
                system = new ReactiveSystemSpy(_contexts.test.CreateCollector(_matcherAB));
            };

            it["does not execute when no entities were collected"] = () => {
                system.Execute();
                assertEntities(system, null);
            };

            it["executes when triggered"] = () => {
                var e = createEntityAB();
                system.Execute();
                assertEntities(system, e);
            };

            it["executes only once when triggered"] = () => {
                var e = createEntityAB();
                system.Execute();
                system.Execute();
                assertEntities(system, e);
            };

            it["retains and releases collected entities"] = () => {
                var e = createEntityAB();
                var retainCount = e.retainCount;
                system.Execute();
                retainCount.should_be(3); // retained by context, group and collector
                e.retainCount.should_be(2); // retained by context and group
            };

            it["collects changed entities in execute"] = () => {
                var e = createEntityAB();
                system.executeAction = entities => {
                    entities[0].ReplaceComponentA(Component.A);
                };

                system.Execute();
                system.Execute();
                assertEntities(system, e, 2);
            };

            it["collects created entities in execute"] = () => {
                var e1 = createEntityAB();
                Entity e2 = null;
                system.executeAction = entities => {
                    if(e2 == null) {
                        e2 = createEntityAB();
                    }
                };

                system.Execute();
                assertEntities(system, e1);

                system.Execute();
                assertEntities(system, e2, 2);
            };

            it["doesn't execute when not triggered"] = () => {
                _contexts.test.CreateEntity().AddComponentA();
                system.Execute();
                assertEntities(system, null);
            };

            it["deactivates and will not trigger"] = () => {
                system.Deactivate();
                createEntityAB();
                system.Execute();
                assertEntities(system, null);
            };

            it["activates and will trigger again"] = () => {
                system.Deactivate();
                system.Activate();
                var e = createEntityAB();
                system.Execute();
                assertEntities(system, e);
            };

            it["clears"] = () => {
                createEntityAB();
                system.Clear();
                system.Execute();
                assertEntities(system, null);
            };

            it["can ToString"] = () => {
                system.ToString().should_be("ReactiveSystem(ReactiveSystemSpy)");
            };
        };

        context["OnEntityRemoved"] = () => {

            before = () => {
                system = new ReactiveSystemSpy(_contexts.test.CreateCollector(_matcherAB, GroupEvent.Removed));
            };

            it["executes when triggered"] = () => {
                var e = createEntityAB()
                    .RemoveComponentA();

                system.Execute();
                assertEntities(system, e);
            };

            it["executes only once when triggered"] = () => {
                var e = createEntityAB()
                    .RemoveComponentA();

                system.Execute();
                system.Execute();
                assertEntities(system, e);
            };

            it["doesn't execute when not triggered"] = () => {
                createEntityAB()
                    .AddComponentC()
                    .RemoveComponentC();

                system.Execute();
                assertEntities(system, null);
            };

            it["retains entities until execute completed"] = () => {
                var e = createEntityAB();
                var didExecute = 0;
                system.executeAction = entities => {
                    didExecute += 1;
                    entities[0].retainCount.should_be(1);
                };

                _contexts.test.DestroyEntity(e);
                system.Execute();
                didExecute.should_be(1);
                e.retainCount.should_be(0);
            };
        };

        context["OnEntityAddedOrRemoved"] = () => {

            before = () => {
                system = new ReactiveSystemSpy(_contexts.test.CreateCollector(_matcherAB, GroupEvent.AddedOrRemoved));
            };

            it["executes when added"] = () => {
                var e = createEntityAB();
                system.Execute();
                assertEntities(system, e);
            };

            it["executes when removed"] = () => {
                var e = createEntityAB();
                system.Execute();
                e.RemoveComponentA();
                system.Execute();
                assertEntities(system, e, 2);
            };
        };

        context["multiple contexts"] = () => {

            Context context1 = null;
            Context context2 = null;

            before = () => {
                context1 = new Context(CID.TotalComponents);
                context2 = new Context(CID.TotalComponents);

                var groupA = context1.GetGroup(Matcher.AllOf(CID.ComponentA));
                var groupB = context2.GetGroup(Matcher.AllOf(CID.ComponentB));

                var groups = new [] { groupA, groupB };
                var groupEvents = new [] {
                    GroupEvent.Added,
                    GroupEvent.Removed
                };
                var collector = new Collector(groups, groupEvents);

                system = new ReactiveSystemSpy(collector);
            };

            it["executes when a triggered by collector"] = () => {
                var eA1 = context1.CreateEntity().AddComponentA();
                context2.CreateEntity().AddComponentA();

                var eB1 = context1.CreateEntity().AddComponentB();
                var eB2 = context2.CreateEntity().AddComponentB();

                system.Execute();
                assertEntities(system, eA1);

                eB1.RemoveComponentB();
                eB2.RemoveComponentB();
                system.Execute();
                assertEntities(system, eB2, 2);
            };
        };

        context["filter entities"] = () => {

            it["filters entities"] = () => {
                system = new ReactiveSystemSpy( _contexts.test.CreateCollector(_matcherAB),
                                               e => ((NameAgeComponent)e.GetComponent(CID.ComponentA)).age > 42);

                _contexts.test.CreateEntity()
                                   .AddComponentA()
                                   .AddComponentC();

                var eAB1 = _contexts.test.CreateEntity()
                                .AddComponentB()
                                .AddComponent(CID.ComponentA, new NameAgeComponent { age = 10 });

                var eAB2 = _contexts.test.CreateEntity()
                                .AddComponentB()
                                .AddComponent(CID.ComponentA, new NameAgeComponent { age = 50 });

                var didExecute = 0;
                system.executeAction = entities => {
                    didExecute += 1;
                    eAB2.retainCount.should_be(3); // retained by context, group and collector
                };

                system.Execute();
                didExecute.should_be(1);

                system.Execute();

                system.entities.Length.should_be(1);
                system.entities[0].should_be_same(eAB2);

                eAB1.retainCount.should_be(2); // retained by context and group
                eAB2.retainCount.should_be(2);
            };
        };

        context["clear"] = () => {

            it["clears reactive system after execute"] = () => {
                system = new ReactiveSystemSpy(_contexts.test.CreateCollector(_matcherAB));

                system.executeAction = entities => {
                    entities[0].ReplaceComponentA(Component.A);
                };

                var e = createEntityAB();
                system.Execute();
                system.Clear();
                system.Execute();
                assertEntities(system, e);
            };
        };
    }
    void when_created()
    {
        Group groupA = null;
        Collector collectorA = null;

        IMatcher matcherA = Matcher.AllOf(CID.ComponentA);

        before = () => {
            _context = new Context(CID.TotalComponents);
            groupA = _context.GetGroup(matcherA);
        };

        context["when observing with groupEvent OnEntityAdded"] = () => {

            before = () => {
                collectorA = new Collector(groupA, GroupEvent.Added);
            };

            it["is empty when nothing happend"] = () => {
                collectorA.collectedEntities.should_be_empty();
            };

            context["when entity collected"] = () => {

                Entity e = null;

                before = () => {
                    e = createEA();
                };

                it["returns collected entities"] = () => {
                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(e);
                };

                it["only collects matching entities"] = () => {
                    createEB();

                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(e);
                };

                it["collects entities only once"] = () => {
                    e.RemoveComponentA();
                    e.AddComponentA();

                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(e);
                };

                it["clears collected entities on deactivation"] = () => {
                    collectorA.Deactivate();
                    collectorA.collectedEntities.should_be_empty();
                };

                it["doesn't collect entities when deactivated"] = () => {
                    collectorA.Deactivate();
                    createEA();
                    collectorA.collectedEntities.should_be_empty();
                };

                it["continues collecting when activated"] = () => {
                    collectorA.Deactivate();
                    createEA();

                    collectorA.Activate();

                    var e2 = createEA();

                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(e2);
                };

                it["clears collected entities"] = () => {
                    collectorA.ClearCollectedEntities();
                    collectorA.collectedEntities.should_be_empty();
                };

                it["can ToString"] = () => {
                    collectorA.ToString().should_be("Collector(Group(AllOf(1)))");
                };
            };

            context["reference counting"] = () => {

                Entity e = null;

                before = () => {
                    e = createEA();
                };

                it["retains entity even after destroy"] = () => {
                    var didExecute = 0;
                    e.OnEntityReleased += delegate { didExecute += 1; };
                    _context.DestroyEntity(e);
                    e.retainCount.should_be(1);
                    didExecute.should_be(0);
                };

                it["releases entity when clearing collected entities"] = () => {
                    _context.DestroyEntity(e);
                    collectorA.ClearCollectedEntities();
                    e.retainCount.should_be(0);
                };

                it["retains entities only once"] = () => {
                    e.ReplaceComponentA(new ComponentA());
                    _context.DestroyEntity(e);
                    e.retainCount.should_be(1);
                };
            };
        };

        context["when observing with groupEvent OnEntityRemoved"] = () => {

            before = () => {
                collectorA = new Collector(groupA, GroupEvent.Removed);
            };

            it["returns collected entities"] = () => {
                var e = createEA();
                collectorA.collectedEntities.should_be_empty();

                e.RemoveComponentA();
                var entities = collectorA.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
            };
        };

        context["when observing with groupEvent OnEntityAddedOrRemoved"] = () => {

            before = () => {
                collectorA = new Collector(groupA, GroupEvent.AddedOrRemoved);
            };

            it["returns collected entities"] = () => {
                var e = createEA();
                var entities = collectorA.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
                collectorA.ClearCollectedEntities();

                e.RemoveComponentA();
                entities = collectorA.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
            };
        };

        context["when observing multiple groups"] = () => {

            Group groupB = null;

            before = () => {
                groupB = _context.GetGroup(Matcher.AllOf(CID.ComponentB));
            };

            it["throws when group count != groupEvent count"] = expect<CollectorException>(() => {
                collectorA = new Collector(
                    new [] { groupA },
                    new [] {
                        GroupEvent.Added,
                        GroupEvent.Added
                    }
                );
            });

            context["when observing with groupEvent OnEntityAdded"] = () => {

                before = () => {
                    collectorA = new Collector(
                        new [] { groupA, groupB },
                        new [] {
                            GroupEvent.Added,
                            GroupEvent.Added
                        }
                    );
                };

                it["returns collected entities"] = () => {
                    var eA = createEA();
                    var eB = createEB();

                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                };

                it["can ToString"] = () => {
                    collectorA.ToString().should_be("Collector(Group(AllOf(1)), Group(AllOf(2)))");
                };
            };

            context["when observing with groupEvent OnEntityRemoved"] = () => {

                before = () => {
                    collectorA = new Collector(
                        new [] { groupA, groupB },
                        new [] {
                            GroupEvent.Removed,
                            GroupEvent.Removed
                        }
                    );
                };
                it["returns collected entities"] = () => {
                    var eA = createEA();
                    var eB = createEB();
                    collectorA.collectedEntities.should_be_empty();

                    eA.RemoveComponentA();
                    eB.RemoveComponentB();
                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                };
            };

            context["when observing with groupEvent OnEntityAddedOrRemoved"] = () => {

                before = () => {
                    collectorA = new Collector(
                        new [] { groupA, groupB },
                        new [] {
                            GroupEvent.AddedOrRemoved,
                            GroupEvent.AddedOrRemoved
                        }
                    );
                };
                it["returns collected entities"] = () => {
                    var eA = createEA();
                    var eB = createEB();
                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                    collectorA.ClearCollectedEntities();

                    eA.RemoveComponentA();
                    eB.RemoveComponentB();
                    entities = collectorA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                };
            };

            context["when observing with mixed groupEvents"] = () => {

                before = () => {
                    collectorA = new Collector(
                        new [] { groupA, groupB },
                        new [] {
                            GroupEvent.Added,
                            GroupEvent.Removed
                        }
                    );
                };
                it["returns collected entities"] = () => {
                    var eA = createEA();
                    var eB = createEB();
                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eA);
                    collectorA.ClearCollectedEntities();

                    eA.RemoveComponentA();
                    eB.RemoveComponentB();
                    entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eB);
                };
            };
        };
    }
 void createTestGroups(Context context)
 {
     context.GetGroup(VisualDebuggingMatcher.Vector3);
     context.GetGroup(VisualDebuggingMatcher.GameObject);
     context.GetGroup(Matcher.AllOf(VisualDebuggingMatcher.GameObject, VisualDebuggingMatcher.Vector3));
     context.GetGroup(Matcher.AllOf(VisualDebuggingMatcher.GameObject, VisualDebuggingMatcher.Vector3));
 }
    void when_created()
    {
        Context ctx = null;

        before = () => {
            ctx = new Context(CID.TotalComponents);
        };

        it["increments creationIndex"] = () => {
            ctx.CreateEntity().creationIndex.should_be(0);
            ctx.CreateEntity().creationIndex.should_be(1);
        };

        it["starts with given creationIndex"] = () => {
            new Context(CID.TotalComponents, 42, null).CreateEntity().creationIndex.should_be(42);
        };

        it["has no entities when no entities were created"] = () => {
            ctx.GetEntities().should_be_empty();
        };

        it["gets total entity count"] = () => {
            ctx.count.should_be(0);
        };

        it["creates entity"] = () => {
            var e = ctx.CreateEntity();
            e.should_not_be_null();
            e.GetType().should_be(typeof(Entity));
        };

        it["has default ContextInfo"] = () => {
            ctx.contextInfo.name.should_be("Unnamed Context");
            ctx.contextInfo.componentNames.Length.should_be(CID.TotalComponents);
            for (int i = 0; i < ctx.contextInfo.componentNames.Length; i++) {
                ctx.contextInfo.componentNames[i].should_be("Index " + i);
            }
        };

        it["creates component pools"] = () => {
            ctx.componentPools.should_not_be_null();
            ctx.componentPools.Length.should_be(CID.TotalComponents);
        };

        it["creates entity with component pools"] = () => {
            var e = ctx.CreateEntity();
            e.componentPools.should_be_same(ctx.componentPools);
        };

        it["throws when destroying an entity which the context doesn't contain"] = expect<ContextDoesNotContainEntityException>(() => {
            var e = ctx.CreateEntity();
            ctx.DestroyEntity(e);
            ctx.DestroyEntity(e);
        });

        it["can ToString"] = () => {
            ctx.ToString().should_be("Unnamed Context");
        };

        context["when ContextInfo set"] = () => {

            ContextInfo contextInfo = null;

            before = () => {
                var componentNames = new [] { "Health", "Position", "View" };
                var componentTypes = new [] { typeof(ComponentA), typeof(ComponentB), typeof(ComponentC) };
                contextInfo = new ContextInfo("My Context", componentNames, componentTypes);
                ctx = new Context(componentNames.Length, 0, contextInfo);
            };

            it["has custom ContextInfo"] = () => {
                ctx.contextInfo.should_be_same(contextInfo);
            };

            it["creates entity with same ContextInfo"] = () => {
                ctx.CreateEntity().contextInfo.should_be_same(contextInfo);
            };

            it["throws when componentNames is not same length as totalComponents"] = expect<ContextInfoException>(() => {
                new Context(contextInfo.componentNames.Length + 1, 0, contextInfo);
            });
        };

        context["when entity created"] = () => {

            Entity e = null;

            before = () => {
                e = ctx.CreateEntity();
                e.AddComponentA();
            };

            it["gets total entity count"] = () => {
                ctx.count.should_be(1);
            };

            it["has entities that were created with CreateEntity()"] = () => {
                ctx.HasEntity(e).should_be_true();
            };

            it["doesn't have entities that were not created with CreateEntity()"] = () => {
                ctx.HasEntity(this.CreateEntity()).should_be_false();
            };

            it["returns all created entities"] = () => {
                var e2 = ctx.CreateEntity();
                var entities = ctx.GetEntities();
                entities.Length.should_be(2);
                entities.should_contain(e);
                entities.should_contain(e2);
            };

            it["destroys entity and removes it"] = () => {
                ctx.DestroyEntity(e);
                ctx.HasEntity(e).should_be_false();
                ctx.count.should_be(0);
                ctx.GetEntities().should_be_empty();
            };

            it["destroys an entity and removes all its components"] = () => {
                ctx.DestroyEntity(e);
                e.GetComponents().should_be_empty();
            };

            it["destroys all entities"] = () => {
                ctx.CreateEntity();
                ctx.DestroyAllEntities();
                ctx.HasEntity(e).should_be_false();
                ctx.count.should_be(0);
                ctx.GetEntities().should_be_empty();
                e.GetComponents().should_be_empty();
            };

            it["ensures same deterministic order when getting entities after destroying all entities"] = () => {

                // This is a Unity specific problem. Run Unity Test Tools with in the Entitas.Unity project

                const int numEntities = 10;

                for (int i = 0; i < numEntities; i++) {
                    ctx.CreateEntity();
                }

                var order1 = new int[numEntities];
                var entities1 = ctx.GetEntities();
                for (int i = 0; i < numEntities; i++) {
                    order1[i] = entities1[i].creationIndex;
                }

                ctx.DestroyAllEntities();
                ctx.ResetCreationIndex();

                for (int i = 0; i < numEntities; i++) {
                    ctx.CreateEntity();
                }

                var order2 = new int[numEntities];
                var entities2 = ctx.GetEntities();
                for (int i = 0; i < numEntities; i++) {
                    order2[i] = entities2[i].creationIndex;
                }

                for (int i = 0; i < numEntities; i++) {
                    var index1 = order1[i];
                    var index2 = order2[i];
                    index1.should_be(index2);
                }
            };

            it["throws when destroying all entities and there are still entities retained"] = expect<ContextStillHasRetainedEntitiesException>(() => {
                ctx.CreateEntity().Retain(new object());
                ctx.DestroyAllEntities();
            });
        };

        context["internal caching"] = () => {

            it["caches entities"] = () => {
                var entities = ctx.GetEntities();
                ctx.GetEntities().should_be_same(entities);
            };

            it["updates entities cache when creating an entity"] = () => {
                var entities = ctx.GetEntities();
                ctx.CreateEntity();
                ctx.GetEntities().should_not_be_same(entities);
            };

            it["updates entities cache when destroying an entity"] = () => {
                var e = ctx.CreateEntity();
                var entities = ctx.GetEntities();
                ctx.DestroyEntity(e);
                ctx.GetEntities().should_not_be_same(entities);
            };
        };

        context["events"] = () => {

            var didDispatch = 0;

            before = () => {
                didDispatch = 0;
            };

            it["dispatches OnEntityCreated when creating a new entity"] = () => {
                Entity eventEntity = null;
                ctx.OnEntityCreated += (p, entity) => {
                    didDispatch += 1;
                    eventEntity = entity;
                    p.should_be_same(p);
                };

                var e = ctx.CreateEntity();
                didDispatch.should_be(1);
                eventEntity.should_be_same(e);
            };

            it["dispatches OnEntityWillBeDestroyed when destroying an entity"] = () => {
                var e = ctx.CreateEntity();
                e.AddComponentA();
                ctx.OnEntityWillBeDestroyed += (p, entity) => {
                    didDispatch += 1;
                    p.should_be_same(ctx);
                    entity.should_be_same(e);
                    entity.HasComponentA().should_be_true();
                    entity.isEnabled.should_be_true();

                    p.GetEntities().Length.should_be(0);
                };
                ctx.GetEntities();
                ctx.DestroyEntity(e);
                didDispatch.should_be(1);
            };

            it["dispatches OnEntityDestroyed when destroying an entity"] = () => {
                var e = ctx.CreateEntity();
                ctx.OnEntityDestroyed += (p, entity) => {
                    didDispatch += 1;
                    p.should_be_same(ctx);
                    entity.should_be_same(e);
                    entity.HasComponentA().should_be_false();
                    entity.isEnabled.should_be_false();
                };
                ctx.DestroyEntity(e);
                didDispatch.should_be(1);
            };

            it["entity is released after OnEntityDestroyed"] = () => {
                var e = ctx.CreateEntity();
                ctx.OnEntityDestroyed += (p, entity) => {
                    didDispatch += 1;
                    entity.retainCount.should_be(1);
                    var newEntity = ctx.CreateEntity();
                    newEntity.should_not_be_null();
                    newEntity.should_not_be_same(entity);
                };
                ctx.DestroyEntity(e);
                var reusedEntity = ctx.CreateEntity();
                reusedEntity.should_be_same(e);
                didDispatch.should_be(1);
            };

            it["throws if entity is released before it is destroyed"] = expect<EntityIsNotDestroyedException>(() => {
                var e = ctx.CreateEntity();
                e.Release(ctx);
            });

            it["dispatches OnGroupCreated when creating a new group"] = () => {
                Group eventGroup = null;
                ctx.OnGroupCreated += (p, g) => {
                    didDispatch += 1;
                    p.should_be_same(ctx);
                    eventGroup = g;
                };
                var group = ctx.GetGroup(Matcher.AllOf(0));
                didDispatch.should_be(1);
                eventGroup.should_be_same(group);
            };

            it["doesn't dispatch OnGroupCreated when group alredy exists"] = () => {
                ctx.GetGroup(Matcher.AllOf(0));
                ctx.OnGroupCreated += delegate { this.Fail(); };
                ctx.GetGroup(Matcher.AllOf(0));
            };

            it["dispatches OnGroupCleared when clearing groups"] = () => {
                Group eventGroup = null;
                ctx.OnGroupCleared += (p, g) => {
                    didDispatch += 1;
                    p.should_be_same(ctx);
                    eventGroup = g;
                };
                ctx.GetGroup(Matcher.AllOf(0));
                var group2 = ctx.GetGroup(Matcher.AllOf(1));
                ctx.ClearGroups();

                didDispatch.should_be(2);
                eventGroup.should_be_same(group2);
            };

            it["removes all external delegates when destroying an entity"] = () => {
                var e = ctx.CreateEntity();
                e.OnComponentAdded += delegate { this.Fail(); };
                e.OnComponentRemoved += delegate { this.Fail(); };
                e.OnComponentReplaced += delegate { this.Fail(); };
                ctx.DestroyEntity(e);
                var e2 = ctx.CreateEntity();
                e2.should_be_same(e);
                e2.AddComponentA();
                e2.ReplaceComponentA(Component.A);
                e2.RemoveComponentA();
            };

            it["will not remove external delegates for OnEntityReleased"] = () => {
                var e = ctx.CreateEntity();
                var didRelease = 0;
                e.OnEntityReleased += entity => didRelease += 1;
                ctx.DestroyEntity(e);
                didRelease.should_be(1);
            };

            it["removes all external delegates from OnEntityReleased when after being dispatched"] = () => {
                var e = ctx.CreateEntity();
                var didRelease = 0;
                e.OnEntityReleased += entity => didRelease += 1;
                ctx.DestroyEntity(e);
                e.Retain(this);
                e.Release(this);
                didRelease.should_be(1);
            };

            it["removes all external delegates from OnEntityReleased after being dispatched (when delayed release)"] = () => {
                var e = ctx.CreateEntity();
                var didRelease = 0;
                e.OnEntityReleased += entity => didRelease += 1;
                e.Retain(this);
                ctx.DestroyEntity(e);
                didRelease.should_be(0);
                e.Release(this);
                didRelease.should_be(1);

                e.Retain(this);
                e.Release(this);
                didRelease.should_be(1);
            };
        };

        context["entity pool"] = () => {

            it["gets entity from object pool"] = () => {
                var e = ctx.CreateEntity();
                e.should_not_be_null();
                e.GetType().should_be(typeof(Entity));
            };

            it["destroys entity when pushing back to object pool"] = () => {
                var e = ctx.CreateEntity();
                e.AddComponentA();
                ctx.DestroyEntity(e);
                e.HasComponent(CID.ComponentA).should_be_false();
            };

            it["returns pushed entity"] = () => {
                var e = ctx.CreateEntity();
                e.AddComponentA();
                ctx.DestroyEntity(e);
                var entity = ctx.CreateEntity();
                entity.HasComponent(CID.ComponentA).should_be_false();
                entity.should_be_same(e);
            };

            it["only returns released entities"] = () => {
                var e1 = ctx.CreateEntity();
                e1.Retain(this);
                ctx.DestroyEntity(e1);
                var e2 = ctx.CreateEntity();
                e2.should_not_be_same(e1);
                e1.Release(this);
                var e3 = ctx.CreateEntity();
                e3.should_be_same(e1);
            };

            it["returns new entity"] = () => {
                var e1 = ctx.CreateEntity();
                e1.AddComponentA();
                ctx.DestroyEntity(e1);
                ctx.CreateEntity();
                var e2 = ctx.CreateEntity();
                e2.HasComponent(CID.ComponentA).should_be_false();
                e2.should_not_be_same(e1);
            };

            it["sets up entity from pool"] = () => {
                ctx.DestroyEntity(ctx.CreateEntity());
                var g = ctx.GetGroup(Matcher.AllOf(CID.ComponentA));
                var e = ctx.CreateEntity();
                e.AddComponentA();
                g.GetEntities().should_contain(e);
            };

            context["when entity gets destroyed"] = () => {

                Entity e = null;

                before = () => {
                    e = ctx.CreateEntity();
                    e.AddComponentA();
                    ctx.DestroyEntity(e);
                };

                it["throws when adding component"] = expect<EntityIsNotEnabledException>(() => e.AddComponentA());
                it["throws when removing component"] = expect<EntityIsNotEnabledException>(() => e.RemoveComponentA());
                it["throws when replacing component"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(new ComponentA()));
                it["throws when replacing component with null"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(null));
            };
        };

        context["groups"] = () => {

            it["gets empty group for matcher when no entities were created"] = () => {
                var g = ctx.GetGroup(Matcher.AllOf(CID.ComponentA));
                g.should_not_be_null();
                g.GetEntities().should_be_empty();
            };

            context["when entities created"] = () => {

                Entity eAB1 = null;
                Entity eAB2 = null;
                Entity eA = null;

                IMatcher matcherAB = Matcher.AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB
                });

                before = () => {
                    eAB1 = ctx.CreateEntity();
                    eAB1.AddComponentA();
                    eAB1.AddComponentB();

                    eAB2 = ctx.CreateEntity();
                    eAB2.AddComponentA();
                    eAB2.AddComponentB();

                    eA = ctx.CreateEntity();
                    eA.AddComponentA();
                };

                it["gets group with matching entities"] = () => {
                    var g = ctx.GetGroup(matcherAB).GetEntities();
                    g.Length.should_be(2);
                    g.should_contain(eAB1);
                    g.should_contain(eAB2);
                };

                it["gets cached group"] = () => {
                    ctx.GetGroup(matcherAB).should_be_same(ctx.GetGroup(matcherAB));
                };

                it["cached group contains newly created matching entity"] = () => {
                    var g = ctx.GetGroup(matcherAB);
                    eA.AddComponentB();
                    g.GetEntities().should_contain(eA);
                };

                it["cached group doesn't contain entity which are not matching anymore"] = () => {
                    var g = ctx.GetGroup(matcherAB);
                    eAB1.RemoveComponentA();
                    g.GetEntities().should_not_contain(eAB1);
                };

                it["removes destroyed entity"] = () => {
                    var g = ctx.GetGroup(matcherAB);
                    ctx.DestroyEntity(eAB1);
                    g.GetEntities().should_not_contain(eAB1);
                };

                it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => {
                    var g = ctx.GetGroup(matcherAB);
                    var didDispatchRemoved = 0;
                    var didDispatchAdded = 0;
                    var componentA = new ComponentA();
                    g.OnEntityRemoved += (group, entity, index, component) => {
                        group.should_be_same(g);
                        entity.should_be_same(eAB1);
                        index.should_be(CID.ComponentA);
                        component.should_be_same(Component.A);
                        didDispatchRemoved++;
                    };
                    g.OnEntityAdded += (group, entity, index, component) => {
                        group.should_be_same(g);
                        entity.should_be_same(eAB1);
                        index.should_be(CID.ComponentA);
                        component.should_be_same(componentA);
                        didDispatchAdded++;
                    };
                    eAB1.ReplaceComponentA(componentA);

                    didDispatchRemoved.should_be(1);
                    didDispatchAdded.should_be(1);
                };

                it["group dispatches OnEntityUpdated with previous and current component when replacing a component"] = () => {
                    var updated = 0;
                    var prevComp = eA.GetComponent(CID.ComponentA);
                    var newComp = new ComponentA();
                    var g = ctx.GetGroup(Matcher.AllOf(CID.ComponentA));
                    g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => {
                        updated += 1;
                        group.should_be_same(g);
                        entity.should_be_same(eA);
                        index.should_be(CID.ComponentA);
                        previousComponent.should_be_same(prevComp);
                        newComponent.should_be_same(newComp);
                    };

                    eA.ReplaceComponent(CID.ComponentA, newComp);

                    updated.should_be(1);
                };

                it["group with matcher NoneOf doesn't dispatch OnEntityAdded when destroying entity"] = () => {
                    var e = ctx.CreateEntity()
                                .AddComponentA()
                                .AddComponentB();
                    var matcher = Matcher.AllOf(CID.ComponentB).NoneOf(CID.ComponentA);
                    var g = ctx.GetGroup(matcher);
                    g.OnEntityAdded += delegate { this.Fail(); };
                    ctx.DestroyEntity(e);
                };

                context["event timing"] = () => {

                    before = () => {
                        ctx = new Context(CID.TotalComponents);
                    };

                    it["dispatches group.OnEntityAdded events after all groups are updated"] = () => {
                        var groupA = ctx.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB));
                        var groupB = ctx.GetGroup(Matcher.AllOf(CID.ComponentB));

                        groupA.OnEntityAdded += delegate {
                            groupB.count.should_be(1);
                        };

                        var entity = ctx.CreateEntity();
                        entity.AddComponentA();
                        entity.AddComponentB();
                    };

                    it["dispatches group.OnEntityRemoved events after all groups are updated"] = () => {
                        ctx = new Context(CID.TotalComponents);
                        var groupB = ctx.GetGroup(Matcher.AllOf(CID.ComponentB));
                        var groupAB = ctx.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB));

                        groupB.OnEntityRemoved += delegate {
                            groupAB.count.should_be(0);
                        };

                        var entity = ctx.CreateEntity();
                        entity.AddComponentA();
                        entity.AddComponentB();

                        entity.RemoveComponentB();
                    };
                };
            };
        };

        context["EntityIndex"] = () => {

            it["throws when EntityIndex for key doesn't exist"] = expect<ContextEntityIndexDoesNotExistException>(() => {
                ctx.GetEntityIndex("unknown");
            });

            it["adds and EntityIndex"] = () => {
                const int componentIndex = 1;
                var entityIndex = new PrimaryEntityIndex<string>(ctx.GetGroup(Matcher.AllOf(componentIndex)), null);
                ctx.AddEntityIndex(componentIndex.ToString(), entityIndex);
                ctx.GetEntityIndex(componentIndex.ToString()).should_be_same(entityIndex);
            };

            it["throws when adding an EntityIndex with same name"] = expect<ContextEntityIndexDoesAlreadyExistException>(() => {
                const int componentIndex = 1;
                var entityIndex = new PrimaryEntityIndex<string>(ctx.GetGroup(Matcher.AllOf(componentIndex)), null);
                ctx.AddEntityIndex(componentIndex.ToString(), entityIndex);
                ctx.AddEntityIndex(componentIndex.ToString(), entityIndex);
            });
        };

        context["reset"] = () => {

            context["groups"] = () => {

                it["resets and removes groups from context"] = () => {

                    var m = Matcher.AllOf(CID.ComponentA);
                    var groupsCreated = 0;
                    Group createdGroup = null;
                    ctx.OnGroupCreated += (p, g) => {
                        groupsCreated += 1;
                        createdGroup = g;
                    };

                    var initialGroup = ctx.GetGroup(m);

                    ctx.ClearGroups();

                    ctx.GetGroup(m);

                    ctx.CreateEntity().AddComponentA();

                    groupsCreated.should_be(2);
                    createdGroup.should_not_be_same(initialGroup);

                    initialGroup.count.should_be(0);
                    createdGroup.count.should_be(1);
                };

                it["removes all event handlers from groups"] = () => {
                    var m = Matcher.AllOf(CID.ComponentA);
                    var group = ctx.GetGroup(m);

                    group.OnEntityAdded += delegate { this.Fail(); };

                    ctx.ClearGroups();

                    var e = ctx.CreateEntity();
                    e.AddComponentA();
                    group.HandleEntity(e, CID.ComponentA, Component.A);
                };

                it["releases entities in groups"] = () => {
                    var m = Matcher.AllOf(CID.ComponentA);
                    ctx.GetGroup(m);
                    var entity = ctx.CreateEntity();
                    entity.AddComponentA();

                    ctx.ClearGroups();

                    entity.retainCount.should_be(1);
                };
            };

            context["context"] = () => {

                it["resets creation index"] = () => {
                    ctx.CreateEntity();

                    ctx.ResetCreationIndex();

                    ctx.CreateEntity().creationIndex.should_be(0);
                };

                context["removes all event handlers"] = () => {

                    it["removes OnEntityCreated"] = () => {
                        ctx.OnEntityCreated += delegate { this.Fail(); };
                        ctx.Reset();

                        ctx.CreateEntity();
                    };

                    it["removes OnEntityWillBeDestroyed"] = () => {
                        ctx.OnEntityWillBeDestroyed += delegate { this.Fail(); };
                        ctx.Reset();

                        ctx.DestroyEntity(ctx.CreateEntity());
                    };

                    it["removes OnEntityDestroyed"] = () => {
                        ctx.OnEntityDestroyed += delegate { this.Fail(); };
                        ctx.Reset();

                        ctx.DestroyEntity(ctx.CreateEntity());
                    };

                    it["removes OnGroupCreated"] = () => {
                        ctx.OnGroupCreated += delegate { this.Fail(); };
                        ctx.Reset();

                        ctx.GetGroup(Matcher.AllOf(0));
                    };

                    it["removes OnGroupCleared"] = () => {
                        ctx.OnGroupCleared += delegate { this.Fail(); };
                        ctx.Reset();
                        ctx.GetGroup(Matcher.AllOf(0));

                        ctx.ClearGroups();
                    };
                };
            };

            context["component pools"] = () => {

                before = () => {
                    var entity = ctx.CreateEntity();
                    entity.AddComponentA();
                    entity.AddComponentB();
                    entity.RemoveComponentA();
                    entity.RemoveComponentB();
                };

                it["clears all component pools"] = () => {
                    ctx.componentPools[CID.ComponentA].Count.should_be(1);
                    ctx.componentPools[CID.ComponentB].Count.should_be(1);

                    ctx.ClearComponentPools();

                    ctx.componentPools[CID.ComponentA].Count.should_be(0);
                    ctx.componentPools[CID.ComponentB].Count.should_be(0);
                };

                it["clears a specific component pool"] = () => {
                    ctx.ClearComponentPool(CID.ComponentB);

                    ctx.componentPools[CID.ComponentA].Count.should_be(1);
                    ctx.componentPools[CID.ComponentB].Count.should_be(0);
                };

                it["only clears existing component pool"] = () => {
                    ctx.ClearComponentPool(CID.ComponentC);
                };
            };

            context["EntityIndex"] = () => {

                PrimaryEntityIndex<string> entityIndex = null;

                before = () => {
                    entityIndex = new PrimaryEntityIndex<string>(ctx.GetGroup(Matcher.AllOf(CID.ComponentA)),
                        (e, c) => ((NameAgeComponent)(c)).name);
                    ctx.AddEntityIndex(CID.ComponentA.ToString(), entityIndex);
                };

                it["deactivates EntityIndex"] = () => {
                    var nameAgeComponent = new NameAgeComponent();
                    nameAgeComponent.name = "Max";

                    ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
                    entityIndex.HasEntity("Max").should_be_true();

                    ctx.DeactivateAndRemoveEntityIndices();

                    entityIndex.HasEntity("Max").should_be_false();
                };

                it["removes EntityIndex"] = expect<ContextEntityIndexDoesNotExistException>(() => {
                    ctx.DeactivateAndRemoveEntityIndices();
                    ctx.GetEntityIndex(CID.ComponentA.ToString());
                });
            };
        };

        context["EntitasCache"] = () => {

            it["pops new list from list pool"] = () => {
                var groupA = ctx.GetGroup(Matcher.AllOf(CID.ComponentA));
                var groupAB = ctx.GetGroup(Matcher.AnyOf(CID.ComponentA, CID.ComponentB));
                var groupABC = ctx.GetGroup(Matcher.AnyOf(CID.ComponentA, CID.ComponentB, CID.ComponentC));

                var didExecute = 0;

                groupA.OnEntityAdded += (g, entity, index, component) => {
                    didExecute += 1;
                    entity.RemoveComponentA();
                };

                groupAB.OnEntityAdded += (g, entity, index, component) => {
                    didExecute += 1;
                };

                groupABC.OnEntityAdded += (g, entity, index, component) => {
                    didExecute += 1;
                };

                ctx.CreateEntity().AddComponentA();

                didExecute.should_be(3);
            };
        };
    }
Example #11
0
        static void collectorExample(Context context) {
            var group = context.GetGroup(Matcher.Position);
            var collector = group.CreateCollector(GroupEvent.Added);

            // ----------------------------
            foreach(var e in collector.collectedEntities) {
                // do something
            }
            collector.ClearCollectedEntities();


            // ----------------------------
            collector.Deactivate();
        }