public void Before()
    {
        var pool  = Helper.CreatePool();
        var group = pool.GetGroup(Matcher.AllOf(new [] { CP.ComponentA }));

        _observer = group.CreateObserver();
    }
    public void Before()
    {
        var pool  = new Pool(CP.NumComponents);
        var group = pool.GetGroup(Matcher.AllOf(new [] { CP.ComponentA }));

        _observer = group.CreateObserver();

        for (int i = 0; i < 1000; i++)
        {
            var e = pool.CreateEntity();
            e.AddComponent(CP.ComponentA, new ComponentA());
        }
    }
Exemplo n.º 3
0
    void when_created()
    {
        ReactiveSystem       reactiveSystem = null;
        ReactiveSubSystemSpy subSystem      = null;

        before = () => {
            _pool = new Pool(CID.TotalComponents);
        };

        context["OnEntityAdded"] = () => {
            before = () => {
                subSystem      = new ReactiveSubSystemSpy(_matcherAB, GroupEventType.OnEntityAdded);
                reactiveSystem = new ReactiveSystem(_pool, subSystem);
            };

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

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

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

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

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

                reactiveSystem.Execute();
                reactiveSystem.Execute();
                assertEntities(subSystem, e, 2);
            };

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

                reactiveSystem.Execute();
                assertEntities(subSystem, e1);

                reactiveSystem.Execute();
                assertEntities(subSystem, e2, 2);
            };

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

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

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

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

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

        context["OnEntityRemoved"] = () => {
            before = () => {
                subSystem      = new ReactiveSubSystemSpy(_matcherAB, GroupEventType.OnEntityRemoved);
                reactiveSystem = new ReactiveSystem(_pool, subSystem);
            };

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

                reactiveSystem.Execute();
                assertEntities(subSystem, e);
            };

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

                reactiveSystem.Execute();
                reactiveSystem.Execute();
                assertEntities(subSystem, e);
            };

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

                reactiveSystem.Execute();
                assertEntities(subSystem, null);
            };

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

                _pool.DestroyEntity(e);
                reactiveSystem.Execute();
                didExecute.should_be(1);
                e.retainCount.should_be(0);
            };
        };

        context["OnEntityAddedOrRemoved"] = () => {
            before = () => {
                subSystem      = new ReactiveSubSystemSpy(_matcherAB, GroupEventType.OnEntityAddedOrRemoved);
                reactiveSystem = new ReactiveSystem(_pool, subSystem);
            };

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

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

        context["MultiReactiveSystem"] = () => {
            MultiReactiveSubSystemSpy multiSubSystem = null;

            before = () => {
                var triggers = new [] {
                    Matcher.AllOf(CID.ComponentA).OnEntityAdded(),
                    Matcher.AllOf(CID.ComponentB).OnEntityRemoved()
                };
                multiSubSystem = new MultiReactiveSubSystemSpy(triggers);
                reactiveSystem = new ReactiveSystem(_pool, multiSubSystem);
            };

            it["executes when any trigger is triggered"] = () => {
                var eA = _pool.CreateEntity().AddComponentA();
                var eB = _pool.CreateEntity().AddComponentB();
                reactiveSystem.Execute();
                assertEntities(multiSubSystem, eA);
                eB.RemoveComponentB();
                reactiveSystem.Execute();
                assertEntities(multiSubSystem, eB, 2);
            };
        };

        context["GroupObserverSystem"] = () => {
            GroupObserverSubSystemSpy groupObserverSubSystem = null;
            Pool pool1 = null;
            Pool pool2 = null;

            before = () => {
                pool1 = new Pool(CID.TotalComponents);
                pool2 = new Pool(CID.TotalComponents);

                var groupA = pool1.GetGroup(Matcher.AllOf(CID.ComponentA));
                var groupB = pool2.GetGroup(Matcher.AllOf(CID.ComponentB));

                var groups     = new [] { groupA, groupB };
                var eventTypes = new [] {
                    GroupEventType.OnEntityAdded,
                    GroupEventType.OnEntityRemoved
                };
                var groupObserver = new GroupObserver(groups, eventTypes);

                groupObserverSubSystem = new GroupObserverSubSystemSpy(groupObserver);
                reactiveSystem         = new ReactiveSystem(groupObserverSubSystem);
            };

            it["executes when a triggered by groupObserver"] = () => {
                var eA1 = pool1.CreateEntity().AddComponentA();
                pool2.CreateEntity().AddComponentA();

                var eB1 = pool1.CreateEntity().AddComponentB();
                var eB2 = pool2.CreateEntity().AddComponentB();

                reactiveSystem.Execute();
                assertEntities(groupObserverSubSystem, eA1);

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

        context["ensure / exlude"] = () => {
            Entity eAB  = null;
            Entity eAC  = null;
            Entity eABC = null;

            context["ensure components"] = () => {
                context["reactive system"] = () => {
                    ReactiveEnsureSubSystemSpy ensureSubSystem = null;

                    before = () => {
                        ensureSubSystem = new ReactiveEnsureSubSystemSpy(_matcherAB, GroupEventType.OnEntityAdded, Matcher.AllOf(
                                                                             CID.ComponentA,
                                                                             CID.ComponentB,
                                                                             CID.ComponentC
                                                                             ));

                        reactiveSystem = new ReactiveSystem(_pool, ensureSubSystem);

                        eAB  = createEntityAB();
                        eABC = createEntityABC();
                    };

                    it["only passes in entities matching required matcher"] = () => {
                        reactiveSystem.Execute();
                        assertEntities(ensureSubSystem, eABC);
                    };

                    it["retains included entities until execute completed"] = () => {
                        var retainCount = eABC.retainCount;
                        var didExecute  = 0;
                        ensureSubSystem.executeAction = entities => {
                            didExecute += 1;
                            eABC.retainCount.should_be(3);
                        };

                        reactiveSystem.Execute();
                        didExecute.should_be(1);
                        retainCount.should_be(3);      // retained by pool, group and group observer
                        eABC.retainCount.should_be(2); // retained by pool and group
                    };

                    it["doesn't retain not included entities until execute completed"] = () => {
                        var retainCount = eAB.retainCount;
                        var didExecute  = 0;
                        ensureSubSystem.executeAction = entity => {
                            didExecute += 1;
                            eAB.retainCount.should_be(2);
                        };

                        reactiveSystem.Execute();
                        didExecute.should_be(1);
                        retainCount.should_be(3);      // retained by pool, group and group observer
                        eAB.retainCount.should_be(2);  // retained by pool and group
                        eABC.retainCount.should_be(2); // retained by pool and group
                    };

                    it["doesn't call execute when no entities left after filtering"] = () => {
                        ensureSubSystem = new ReactiveEnsureSubSystemSpy(_matcherAB, GroupEventType.OnEntityAdded, Matcher.AllOf(
                                                                             CID.ComponentA,
                                                                             CID.ComponentB,
                                                                             CID.ComponentC,
                                                                             CID.ComponentD
                                                                             ));

                        reactiveSystem = new ReactiveSystem(_pool, ensureSubSystem);

                        createEntityAB();
                        createEntityABC();
                        reactiveSystem.Execute();
                        assertEntities(ensureSubSystem, null);
                    };
                };


                context["multi reactive system"] = () => {
                    it["only passes in entities matching required matcher"] = () => {
                        var triggers = new [] {
                            Matcher.AllOf(CID.ComponentA).OnEntityAdded(),
                            Matcher.AllOf(CID.ComponentB).OnEntityAdded()
                        };

                        var ensure = Matcher.AllOf(
                            CID.ComponentA,
                            CID.ComponentB,
                            CID.ComponentC
                            );

                        var ensureSubSystem = new MultiReactiveEnsureSubSystemSpy(triggers, ensure);
                        reactiveSystem = new ReactiveSystem(_pool, ensureSubSystem);

                        createEntityAB();
                        eABC = createEntityABC();
                        reactiveSystem.Execute();
                        assertEntities(ensureSubSystem, eABC);
                    };
                };
            };

            context["exlude components"] = () => {
                context["reactive system"] = () => {
                    ReactiveExcludeSubSystemSpy excludeSubSystem = null;

                    before = () => {
                        excludeSubSystem = new ReactiveExcludeSubSystemSpy(_matcherAB,
                                                                           GroupEventType.OnEntityAdded,
                                                                           Matcher.AllOf(CID.ComponentC)
                                                                           );

                        reactiveSystem = new ReactiveSystem(_pool, excludeSubSystem);

                        eAB  = createEntityAB();
                        eABC = createEntityABC();
                    };

                    it["only passes in entities not matching matcher"] = () => {
                        reactiveSystem.Execute();
                        assertEntities(excludeSubSystem, eAB);
                    };

                    it["retains included entities until execute completed"] = () => {
                        var didExecute = 0;
                        excludeSubSystem.executeAction = entities => {
                            didExecute += 1;
                            eAB.retainCount.should_be(3);
                        };

                        reactiveSystem.Execute();
                        didExecute.should_be(1);
                    };

                    it["doesn't retain not included entities until execute completed"] = () => {
                        var didExecute = 0;
                        excludeSubSystem.executeAction = entities => {
                            didExecute += 1;
                            eABC.retainCount.should_be(2);
                        };

                        reactiveSystem.Execute();
                        didExecute.should_be(1);
                    };
                };

                context["multi reactive system"] = () => {
                    it["only passes in entities not matching required matcher"] = () => {
                        var triggers = new [] {
                            Matcher.AllOf(CID.ComponentA).OnEntityAdded(),
                            Matcher.AllOf(CID.ComponentB).OnEntityAdded()
                        };
                        var exclude = Matcher.AllOf(CID.ComponentC);

                        var excludeSubSystem = new MultiReactiveExcludeSubSystemSpy(triggers, exclude);
                        reactiveSystem = new ReactiveSystem(_pool, excludeSubSystem);

                        eAB = createEntityAB();
                        createEntityABC();
                        reactiveSystem.Execute();
                        assertEntities(excludeSubSystem, eAB);
                    };
                };
            };

            context["ensure and exlude mix"] = () => {
                ReactiveEnsureExcludeSubSystemSpy ensureExcludeSystem = null;

                before = () => {
                    ensureExcludeSystem = new ReactiveEnsureExcludeSubSystemSpy(_matcherAB, GroupEventType.OnEntityAdded,
                                                                                Matcher.AllOf(CID.ComponentA, CID.ComponentB),
                                                                                Matcher.AllOf(CID.ComponentC)
                                                                                );
                    reactiveSystem = new ReactiveSystem(_pool, ensureExcludeSystem);

                    eAB  = createEntityAB();
                    eAC  = createEntityAC();
                    eABC = createEntityABC();
                };

                it["only passes in correct entities"] = () => {
                    reactiveSystem.Execute();
                    assertEntities(ensureExcludeSystem, eAB);
                };

                it["retains included entities until execute completed"] = () => {
                    var didExecute = 0;
                    ensureExcludeSystem.executeAction = entities => {
                        didExecute += 1;
                        eAB.retainCount.should_be(3);
                    };

                    reactiveSystem.Execute();
                    didExecute.should_be(1);
                };

                it["doesn't retain not included entities until execute completed"] = () => {
                    var didExecute = 0;
                    ensureExcludeSystem.executeAction = entities => {
                        didExecute += 1;
                        eAC.retainCount.should_be(1);
                        eABC.retainCount.should_be(2);
                    };

                    reactiveSystem.Execute();
                    didExecute.should_be(1);
                };
            };
        };

        context["IClearReactiveSystem"] = () => {
            ClearReactiveSubSystemSpy clearSubSystem = null;

            before = () => {
                clearSubSystem = new ClearReactiveSubSystemSpy(_matcherAB, GroupEventType.OnEntityAdded);
                reactiveSystem = new ReactiveSystem(_pool, clearSubSystem);
            };

            it["clears reactive system after execute when implementing IClearReactiveSystem"] = () => {
                subSystem.executeAction = entities => {
                    entities[0].ReplaceComponentA(Component.A);
                };

                var e = createEntityAB();
                reactiveSystem.Execute();
                reactiveSystem.Execute();
                assertEntities(clearSubSystem, e);
            };
        };
    }
 public ReactiveSystem(Pool pool, IReactiveSystem subSystem)
 {
     _subsystem = subSystem;
     _observer = new GroupObserver(pool.GetGroup(subSystem.trigger), subSystem.eventType);
 }
Exemplo n.º 5
0
    void when_created()
    {
        Pool          pool      = null;
        Group         groupA    = null;
        GroupObserver observerA = null;

        IMatcher mactherA = Matcher.AllOf(new[] { CID.ComponentA });

        before = () => {
            pool   = new Pool(CID.NumComponents);
            groupA = pool.GetGroup(mactherA);
        };

        context["when observing with eventType OnEntityAdded"] = () => {
            before = () => {
                observerA = new GroupObserver(groupA, GroupEventType.OnEntityAdded);
            };

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

            context["when entity collected"] = () => {
                Entity e = null;
                before = () => {
                    e = pool.CreateEntity();
                    e.AddComponentA();
                };

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

                it["only collects matching entities"] = () => {
                    var e2 = pool.CreateEntity();
                    e2.AddComponentB();

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

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

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

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

                it["doesn't collect entities when deactivated"] = () => {
                    observerA.Deactivate();
                    var e2 = pool.CreateEntity();
                    e2.AddComponentA();
                    observerA.collectedEntities.should_be_empty();
                };

                it["continues collecting when activated"] = () => {
                    observerA.Deactivate();
                    var e1 = pool.CreateEntity();
                    e1.AddComponentA();

                    observerA.Activate();

                    var e2 = pool.CreateEntity();
                    e2.AddComponentA();

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

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

            context["reference counting"] = () => {
                it["retains entity even after destroy"] = () => {
                    var e = pool.CreateEntity();
                    e.AddComponentA();
                    e.OnEntityReleased += entity => this.Fail();
                    pool.DestroyEntity(e);
                    e.refCount.should_be(1);
                };

                it["releases entity when clearing collected entities"] = () => {
                    var e = pool.CreateEntity();
                    e.AddComponentA();
                    pool.DestroyEntity(e);
                    observerA.ClearCollectedEntities();
                    e.refCount.should_be(0);
                };

                it["retains entities only once"] = () => {
                    var e = pool.CreateEntity();
                    e.AddComponentA();
                    e.ReplaceComponentA(new ComponentA());
                    pool.DestroyEntity(e);
                    e.refCount.should_be(1);
                };
            };
        };

        context["when observing with eventType OnEntityRemoved"] = () => {
            before = () => {
                observerA = new GroupObserver(groupA, GroupEventType.OnEntityRemoved);
            };

            it["returns collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                observerA.collectedEntities.should_be_empty();

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

        context["when observing with eventType OnEntityAddedOrRemoved"] = () => {
            before = () => {
                observerA = new GroupObserver(groupA, GroupEventType.OnEntityAddedOrRemoved);
            };

            it["returns collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                var entities = observerA.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
                observerA.ClearCollectedEntities();

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

        context["when observing multiple groups"] = () => {
            Group groupB = null;
            before = () => {
                groupB = pool.GetGroup(Matcher.AllOf(new[] { CID.ComponentB }));
            };

            it["throws when group count != eventType count"] = expect <GroupObserverException>(() => {
                observerA = new GroupObserver(
                    new [] { groupA },
                    new [] {
                    GroupEventType.OnEntityAdded,
                    GroupEventType.OnEntityAdded
                }
                    );
            });

            context["when observing with eventType OnEntityAdded"] = () => {
                before = () => {
                    observerA = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEventType.OnEntityAdded,
                        GroupEventType.OnEntityAdded
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = pool.CreateEntity();
                    eA.AddComponentA();
                    var eB = pool.CreateEntity();
                    eB.AddComponentB();

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

            context["when observing with eventType OnEntityRemoved"] = () => {
                before = () => {
                    observerA = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEventType.OnEntityRemoved,
                        GroupEventType.OnEntityRemoved
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = pool.CreateEntity();
                    eA.AddComponentA();
                    var eB = pool.CreateEntity();
                    eB.AddComponentB();
                    observerA.collectedEntities.should_be_empty();

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

            context["when observing with eventType OnEntityAddedOrRemoved"] = () => {
                before = () => {
                    observerA = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEventType.OnEntityAddedOrRemoved,
                        GroupEventType.OnEntityAddedOrRemoved
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = pool.CreateEntity();
                    eA.AddComponentA();
                    var eB = pool.CreateEntity();
                    eB.AddComponentB();
                    var entities = observerA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                    observerA.ClearCollectedEntities();

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

            context["when observing with mixed eventTypes"] = () => {
                before = () => {
                    observerA = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEventType.OnEntityAdded,
                        GroupEventType.OnEntityRemoved
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = pool.CreateEntity();
                    eA.AddComponentA();
                    var eB = pool.CreateEntity();
                    eB.AddComponentB();
                    var entities = observerA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eA);
                    observerA.ClearCollectedEntities();

                    eA.RemoveComponentA();
                    eB.RemoveComponentB();
                    entities = observerA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eB);
                };
            };
        };
    }
Exemplo n.º 6
0
 public ReactiveSystem(Pool pool, IReactiveSystem subSystem)
 {
     _subsystem = subSystem;
     _observer = new GroupObserver(pool.GetGroup(subSystem.GetTriggeringMatcher()), subSystem.GetEventType());
 }
Exemplo n.º 7
0
    void when_created()
    {
        Pool          pool     = null;
        Group         group    = null;
        GroupObserver observer = null;

        before = () => {
            pool  = new Pool(CID.NumComponents);
            group = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA }));
        };

        context["when observing with eventType OnEntityAdded"] = () => {
            before = () => {
                observer = new GroupObserver(group, GroupEventType.OnEntityAdded);
            };

            it["returns collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();

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

            it["only returns matching collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                var e2 = pool.CreateEntity();
                e2.AddComponentB();

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

            it["collects entites only once"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.RemoveComponentA();
                e.AddComponentA();

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

            it["returns empty list when no entities were collected"] = () => {
                observer.collectedEntities.should_be_empty();
            };

            it["clears collected entities on deactivation"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();

                observer.Deactivate();
                observer.collectedEntities.should_be_empty();
            };

            it["doesn't collect entities when deactivated"] = () => {
                observer.Deactivate();
                var e = pool.CreateEntity();
                e.AddComponentA();
                observer.collectedEntities.should_be_empty();
            };

            it["continues collecting when activated"] = () => {
                observer.Deactivate();
                var e1 = pool.CreateEntity();
                e1.AddComponentA();

                observer.Activate();

                var e2 = pool.CreateEntity();
                e2.AddComponentA();

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

            it["clears collected entites"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();

                observer.ClearCollectedEntities();
                observer.collectedEntities.should_be_empty();
            };
        };

        context["when observing with eventType OnEntityRemoved"] = () => {
            before = () => {
                observer = new GroupObserver(group, GroupEventType.OnEntityRemoved);
            };

            it["returns collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                observer.collectedEntities.should_be_empty();

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

        context["when observing with eventType OnEntityAddedOrRemoved"] = () => {
            before = () => {
                observer = new GroupObserver(group, GroupEventType.OnEntityAddedOrRemoved);
            };

            it["returns collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                var entities = observer.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
                observer.ClearCollectedEntities();

                e.RemoveComponentA();
                entities = observer.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
            };
        };
    }
Exemplo n.º 8
0
 public void SetPool(Pool pool)
 {
     _energy   = pool.GetGroup(Matcher.AllOf(Matcher.Energy));
     _observer = _energy.CreateObserver(GroupEventType.OnEntityAdded);
 }
Exemplo n.º 9
0
    void when_created()
    {
        Group         groupA    = null;
        GroupObserver observerA = null;

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

        before = () => {
            _pool  = new Pool(CID.TotalComponents);
            groupA = _pool.GetGroup(matcherA);
        };

        context["when observing with eventType OnEntityAdded"] = () => {
            before = () => {
                observerA = new GroupObserver(groupA, GroupEventType.OnEntityAdded);
            };

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

            context["when entity collected"] = () => {
                Entity e = null;

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

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

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

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

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

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

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

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

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

                    observerA.Activate();

                    var e2 = createEA();

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

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

                it["can ToString"] = () => {
                    observerA.ToString().should_be("GroupObserver(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; };
                    _pool.DestroyEntity(e);
                    e.retainCount.should_be(1);
                    didExecute.should_be(0);
                };

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

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

        context["when observing with eventType OnEntityRemoved"] = () => {
            before = () => {
                observerA = new GroupObserver(groupA, GroupEventType.OnEntityRemoved);
            };

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

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

        context["when observing with eventType OnEntityAddedOrRemoved"] = () => {
            before = () => {
                observerA = new GroupObserver(groupA, GroupEventType.OnEntityAddedOrRemoved);
            };

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

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

        context["when observing multiple groups"] = () => {
            Group groupB = null;

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

            it["throws when group count != eventType count"] = expect <GroupObserverException>(() => {
                observerA = new GroupObserver(
                    new [] { groupA },
                    new [] {
                    GroupEventType.OnEntityAdded,
                    GroupEventType.OnEntityAdded
                }
                    );
            });

            context["when observing with eventType OnEntityAdded"] = () => {
                before = () => {
                    observerA = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEventType.OnEntityAdded,
                        GroupEventType.OnEntityAdded
                    }
                        );
                };

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

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

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

            context["when observing with eventType OnEntityRemoved"] = () => {
                before = () => {
                    observerA = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEventType.OnEntityRemoved,
                        GroupEventType.OnEntityRemoved
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = createEA();
                    var eB = createEB();
                    observerA.collectedEntities.should_be_empty();

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

            context["when observing with eventType OnEntityAddedOrRemoved"] = () => {
                before = () => {
                    observerA = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEventType.OnEntityAddedOrRemoved,
                        GroupEventType.OnEntityAddedOrRemoved
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA       = createEA();
                    var eB       = createEB();
                    var entities = observerA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                    observerA.ClearCollectedEntities();

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

            context["when observing with mixed eventTypes"] = () => {
                before = () => {
                    observerA = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEventType.OnEntityAdded,
                        GroupEventType.OnEntityRemoved
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA       = createEA();
                    var eB       = createEB();
                    var entities = observerA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eA);
                    observerA.ClearCollectedEntities();

                    eA.RemoveComponentA();
                    eB.RemoveComponentB();
                    entities = observerA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eB);
                };
            };
        };
    }
Exemplo n.º 10
0
 public ReactiveSystem(Pool pool, IReactiveSystem subSystem)
 {
     _subsystem = subSystem;
     _observer  = new GroupObserver(pool.GetGroup(subSystem.GetTriggeringMatcher()), subSystem.GetEventType());
 }
    void when_created()
    {
        Pool                 pool           = null;
        ReactiveSystem       reactiveSystem = null;
        ReactiveSubSystemSpy subSystem      = null;

        before = () => {
            pool = new Pool(CID.NumComponents);
        };

        context["OnEntityAdded"] = () => {
            before = () => {
                subSystem      = getSubSystemSypWithOnEntityAdded();
                reactiveSystem = new ReactiveSystem(pool, subSystem);
            };

            it["does not execute its subsystem when no entities were collected"] = () => {
                reactiveSystem.Execute();
                subSystem.didExecute.should_be(0);
            };

            it["executes when trigger matches"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(1);
                subSystem.entities.Length.should_be(1);
                subSystem.entities.should_contain(e);
            };

            it["executes only once when trigger matches"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Execute();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(1);
                subSystem.entities.Length.should_be(1);
                subSystem.entities.should_contain(e);
            };

            it["collects changed entities in execute"] = () => {
                subSystem.replaceComponentAOnExecute = true;
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Execute();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(2);
            };

            it["doesn't execute when trigger doesn't match"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                reactiveSystem.Execute();
                subSystem.didExecute.should_be(0);
                subSystem.entities.should_be_null();
            };

            it["deactivates and will not be triggered"] = () => {
                reactiveSystem.Deactivate();
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(0);
                subSystem.entities.should_be_null();
            };

            it["activates and will be triggered again"] = () => {
                reactiveSystem.Deactivate();
                reactiveSystem.Activate();
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(1);
                subSystem.entities.Length.should_be(1);
                subSystem.entities.should_contain(e);
            };

            it["clears"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Clear();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(0);
            };

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

        context["OnEntityRemoved"] = () => {
            before = () => {
                subSystem      = getSubSystemSypWithOnEntityRemoved();
                reactiveSystem = new ReactiveSystem(pool, subSystem);
            };

            it["executes when trigger matches"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                e.RemoveComponentA();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(1);
                subSystem.entities.Length.should_be(1);
                subSystem.entities.should_contain(e);
            };

            it["executes only once when trigger matches"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                e.RemoveComponentA();
                reactiveSystem.Execute();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(1);
                subSystem.entities.Length.should_be(1);
                subSystem.entities.should_contain(e);
            };

            it["doesn't execute when trigger doesn't match"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                e.AddComponentC();
                e.RemoveComponentC();
                reactiveSystem.Execute();
                subSystem.didExecute.should_be(0);
                subSystem.entities.should_be_null();
            };

            it["retains entities until execute completed"] = () => {
                var    didExecute     = 0;
                Entity providedEntity = null;
                subSystem.executeAction = entities => {
                    didExecute    += 1;
                    providedEntity = entities[0];
                    providedEntity.retainCount.should_be(1);
                };

                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                pool.DestroyEntity(e);
                reactiveSystem.Execute();
                didExecute.should_be(1);
                providedEntity.retainCount.should_be(0);
            };
        };

        context["OnEntityAddedOrRemoved"] = () => {
            it["executes when added"] = () => {
                subSystem      = getSubSystemSypWithOnEntityAddedOrRemoved();
                reactiveSystem = new ReactiveSystem(pool, subSystem);
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(1);
                subSystem.entities.Length.should_be(1);
                subSystem.entities.should_contain(e);
            };

            it["executes when removed"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                subSystem      = getSubSystemSypWithOnEntityAddedOrRemoved();
                reactiveSystem = new ReactiveSystem(pool, subSystem);
                e.RemoveComponentA();
                reactiveSystem.Execute();

                subSystem.didExecute.should_be(1);
                subSystem.entities.Length.should_be(1);
                subSystem.entities.should_contain(e);
            };

            it["collects matching entities created or modified in the subsystem"] = () => {
                var sys = new EntityEmittingSubSystem(pool);
                reactiveSystem = new ReactiveSystem(pool, sys);
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Execute();
                sys.entities.Length.should_be(1);
                sys.didExecute.should_be(1);
                reactiveSystem.Execute();
                sys.entities.Length.should_be(1);
                sys.didExecute.should_be(2);
            };
        };

        context["MultiReactiveSystem"] = () => {
            MultiReactiveSubSystemSpy multiSubSystem = null;

            before = () => {
                var triggers = new [] {
                    Matcher.AllOf(new [] { CID.ComponentA }).OnEntityAdded(),
                    Matcher.AllOf(new [] { CID.ComponentB }).OnEntityRemoved()
                };
                multiSubSystem = new MultiReactiveSubSystemSpy(triggers);
                reactiveSystem = new ReactiveSystem(pool, multiSubSystem);
            };

            it["executes when a triggering matcher matches"] = () => {
                var eA = pool.CreateEntity();
                eA.AddComponentA();
                var eB = pool.CreateEntity();
                eB.AddComponentB();
                reactiveSystem.Execute();

                multiSubSystem.didExecute.should_be(1);
                multiSubSystem.entities.Length.should_be(1);
                multiSubSystem.entities.should_contain(eA);

                eB.RemoveComponentB();
                reactiveSystem.Execute();

                multiSubSystem.didExecute.should_be(2);
                multiSubSystem.entities.Length.should_be(1);
                multiSubSystem.entities.should_contain(eB);
            };
        };

        context["GroupObserverSystem"] = () => {
            GroupObserverSubSystemSpy groupObserverSubSystem = null;
            Pool pool1 = null;
            Pool pool2 = null;

            before = () => {
                pool1 = new Pool(CID.NumComponents);
                pool2 = new Pool(CID.NumComponents);

                var groupA = pool1.GetGroup(Matcher.AllOf(new [] { CID.ComponentA }));
                var groupB = pool2.GetGroup(Matcher.AllOf(new [] { CID.ComponentB }));

                var groups        = new [] { groupA, groupB };
                var eventTypes    = new [] { GroupEventType.OnEntityAdded, GroupEventType.OnEntityRemoved };
                var groupObserver = new GroupObserver(groups, eventTypes);

                groupObserverSubSystem = new GroupObserverSubSystemSpy(groupObserver);
                reactiveSystem         = new ReactiveSystem(groupObserverSubSystem);
            };

            it["executes when a triggered by groupObserver"] = () => {
                var eA1 = pool1.CreateEntity();
                eA1.AddComponentA();
                var eA2 = pool2.CreateEntity();
                eA2.AddComponentA();

                var eB1 = pool1.CreateEntity();
                eB1.AddComponentB();
                var eB2 = pool2.CreateEntity();
                eB2.AddComponentB();

                reactiveSystem.Execute();

                groupObserverSubSystem.didExecute.should_be(1);
                groupObserverSubSystem.entities.Length.should_be(1);
                groupObserverSubSystem.entities.should_contain(eA1);

                eB1.RemoveComponentB();
                eB2.RemoveComponentB();

                reactiveSystem.Execute();

                groupObserverSubSystem.didExecute.should_be(2);
                groupObserverSubSystem.entities.Length.should_be(1);
                groupObserverSubSystem.entities.should_contain(eB2);
            };
        };

        context["ensure components matcher"] = () => {
            context["single reactive system"] = () => {
                ReactiveEnsureSubSystemSpy ensureSubSystem = null;
                Entity eAB  = null;
                Entity eABC = null;
                before = () => {
                    ensureSubSystem = new ReactiveEnsureSubSystemSpy(_matcherAB, GroupEventType.OnEntityAdded, Matcher.AllOf(new[] {
                        CID.ComponentA,
                        CID.ComponentB,
                        CID.ComponentC
                    }));
                    reactiveSystem = new ReactiveSystem(pool, ensureSubSystem);

                    eAB = pool.CreateEntity();
                    eAB.AddComponentA();
                    eAB.AddComponentB();
                    eABC = pool.CreateEntity();
                    eABC.AddComponentA();
                    eABC.AddComponentB();
                    eABC.AddComponentC();
                };

                it["only passes in entities matching required matcher"] = () => {
                    reactiveSystem.Execute();
                    ensureSubSystem.didExecute.should_be(1);
                    ensureSubSystem.entities.Length.should_be(1);
                    ensureSubSystem.entities.should_contain(eABC);
                };

                it["retains included entities until execute completed"] = () => {
                    eABC.retainCount.should_be(3); // retained by pool, group and group observer
                    var didExecute = 0;
                    ensureSubSystem.executeAction = entities => {
                        didExecute += 1;
                        eABC.retainCount.should_be(3);
                    };
                    reactiveSystem.Execute();
                    didExecute.should_be(1);
                    eABC.retainCount.should_be(2); // retained by pool and group
                };

                it["doesn't retain not included entities until execute completed"] = () => {
                    eAB.retainCount.should_be(3); // retained by pool, group and group observer
                    var didExecute = 0;
                    ensureSubSystem.executeAction = entity => {
                        didExecute += 1;
                        eAB.retainCount.should_be(2);
                    };
                    reactiveSystem.Execute();
                    didExecute.should_be(1);
                    eABC.retainCount.should_be(2); // retained by pool and group
                    eAB.retainCount.should_be(2);  // retained by pool and group
                };
            };

            it["only passes in entities matching required matcher (multi reactive)"] = () => {
                var triggers = new [] {
                    Matcher.AllOf(new [] { CID.ComponentA }).OnEntityAdded(),
                    Matcher.AllOf(new [] { CID.ComponentB }).OnEntityRemoved()
                };
                var ensure = Matcher.AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC
                });

                var ensureSubSystem = new MultiReactiveEnsureSubSystemSpy(triggers, ensure);
                reactiveSystem = new ReactiveSystem(pool, ensureSubSystem);

                var eAB = pool.CreateEntity();
                eAB.AddComponentA();
                eAB.AddComponentB();
                var eABC = pool.CreateEntity();
                eABC.AddComponentA();
                eABC.AddComponentB();
                eABC.AddComponentC();
                reactiveSystem.Execute();

                ensureSubSystem.didExecute.should_be(1);
                ensureSubSystem.entities.Length.should_be(1);
                ensureSubSystem.entities.should_contain(eABC);
            };

            it["doesn't call execute when no entities left after filtering"] = () => {
                var ensureSubSystem = new ReactiveEnsureSubSystemSpy(
                    _matcherAB,
                    GroupEventType.OnEntityAdded,
                    Matcher.AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC,
                    CID.ComponentD
                })
                    );
                reactiveSystem = new ReactiveSystem(pool, ensureSubSystem);

                var eAB = pool.CreateEntity();
                eAB.AddComponentA();
                eAB.AddComponentB();
                var eABC = pool.CreateEntity();
                eABC.AddComponentA();
                eABC.AddComponentB();
                eABC.AddComponentC();
                reactiveSystem.Execute();

                ensureSubSystem.didExecute.should_be(0);
            };
        };

        context["exlude components"] = () => {
            context["single reactive system"] = () => {
                ReactiveExcludeSubSystemSpy excludeSubSystem = null;
                Entity eAB  = null;
                Entity eABC = null;
                before = () => {
                    excludeSubSystem = new ReactiveExcludeSubSystemSpy(_matcherAB,
                                                                       GroupEventType.OnEntityAdded,
                                                                       Matcher.AllOf(new[] { CID.ComponentC })
                                                                       );

                    reactiveSystem = new ReactiveSystem(pool, excludeSubSystem);
                    eAB            = pool.CreateEntity();
                    eAB.AddComponentA();
                    eAB.AddComponentB();
                    eABC = pool.CreateEntity();
                    eABC.AddComponentA();
                    eABC.AddComponentB();
                    eABC.AddComponentC();
                };

                it["only passes in entities not matching matcher"] = () => {
                    reactiveSystem.Execute();
                    excludeSubSystem.didExecute.should_be(1);
                    excludeSubSystem.entities.Length.should_be(1);
                    excludeSubSystem.entities.should_contain(eAB);
                };

                it["retains included entities until execute completed"] = () => {
                    var didExecute = 0;
                    excludeSubSystem.executeAction = entities => {
                        didExecute += 1;
                        eAB.retainCount.should_be(3);
                    };

                    reactiveSystem.Execute();
                    didExecute.should_be(1);
                };

                it["doesn't retain not included entities until execute completed"] = () => {
                    var didExecute = 0;
                    excludeSubSystem.executeAction = entities => {
                        didExecute += 1;
                        eABC.retainCount.should_be(2);
                    };

                    reactiveSystem.Execute();
                    didExecute.should_be(1);
                };
            };

            it["only passes in entities not matching required matcher (multi reactive)"] = () => {
                var triggers = new [] {
                    Matcher.AllOf(new [] { CID.ComponentA }).OnEntityAdded(),
                    Matcher.AllOf(new [] { CID.ComponentB }).OnEntityRemoved()
                };
                var exclude = Matcher.AllOf(new [] {
                    CID.ComponentC
                });

                var excludeSubSystem = new MultiReactiveExcludeSubSystemSpy(triggers, exclude);
                reactiveSystem = new ReactiveSystem(pool, excludeSubSystem);

                var eAB = pool.CreateEntity();
                eAB.AddComponentA();
                eAB.AddComponentB();
                var eABC = pool.CreateEntity();
                eABC.AddComponentA();
                eABC.AddComponentB();
                eABC.AddComponentC();
                reactiveSystem.Execute();

                excludeSubSystem.didExecute.should_be(1);
                excludeSubSystem.entities.Length.should_be(1);
                excludeSubSystem.entities.should_contain(eAB);
            };
        };

        context["ensure / exlude components mix"] = () => {
            ReactiveEnsureExcludeSubSystemSpy ensureExcludeSystem = null;
            Entity eAB  = null;
            Entity eAC  = null;
            Entity eABC = null;
            before = () => {
                ensureExcludeSystem = new ReactiveEnsureExcludeSubSystemSpy(_matcherAB, GroupEventType.OnEntityAdded,
                                                                            Matcher.AllOf(new[] {
                    CID.ComponentA,
                    CID.ComponentB
                }), Matcher.AllOf(new[] {
                    CID.ComponentC
                }));
                reactiveSystem = new ReactiveSystem(pool, ensureExcludeSystem);

                eAB = pool.CreateEntity();
                eAB.AddComponentA();
                eAB.AddComponentB();
                eAC = pool.CreateEntity();
                eAC.AddComponentA();
                eAC.AddComponentC();
                eABC = pool.CreateEntity();
                eABC.AddComponentA();
                eABC.AddComponentB();
                eABC.AddComponentC();
            };

            it["only passes in entities"] = () => {
                reactiveSystem.Execute();
                ensureExcludeSystem.didExecute.should_be(1);
                ensureExcludeSystem.entities.Length.should_be(1);
                ensureExcludeSystem.entities.should_contain(eAB);
            };

            it["retains included entities until execute completed"] = () => {
                var didExecute = 0;
                ensureExcludeSystem.executeAction = entities => {
                    didExecute += 1;
                    eAB.retainCount.should_be(3);
                };

                reactiveSystem.Execute();
                didExecute.should_be(1);
            };

            it["doesn't retain not included entities until execute completed"] = () => {
                var didExecute = 0;
                ensureExcludeSystem.executeAction = entities => {
                    didExecute += 1;
                    eAC.retainCount.should_be(1);
                    eABC.retainCount.should_be(2);
                };

                reactiveSystem.Execute();
                didExecute.should_be(1);
            };
        };

        context["IClearReactiveSystem"] = () => {
            ClearReactiveSubSystemSpy clearSubSystem = null;
            before = () => {
                clearSubSystem = getClearSubSystemSypWithOnEntityAdded();
                reactiveSystem = new ReactiveSystem(pool, clearSubSystem);
            };

            it["clears reactive system after execute when implementing IClearReactiveSystem"] = () => {
                clearSubSystem.replaceComponentAOnExecute = true;
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Execute();
                reactiveSystem.Execute();
                clearSubSystem.didExecute.should_be(1);
            };
        };
    }
Exemplo n.º 12
0
    void when_created()
    {
        Pool          pool     = null;
        Group         groupA   = null;
        GroupObserver observer = null;

        before = () => {
            pool   = new Pool(CID.NumComponents);
            groupA = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA }));
        };

        context["when observing with eventType OnEntityAdded"] = () => {
            before = () => {
                observer = new GroupObserver(groupA, GroupEventType.OnEntityAdded);
            };

            it["returns collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();

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

            it["only returns matching collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                var e2 = pool.CreateEntity();
                e2.AddComponentB();

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

            it["collects entites only once"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.RemoveComponentA();
                e.AddComponentA();

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

            it["returns empty list when no entities were collected"] = () => {
                observer.collectedEntities.should_be_empty();
            };

            it["clears collected entities on deactivation"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();

                observer.Deactivate();
                observer.collectedEntities.should_be_empty();
            };

            it["doesn't collect entities when deactivated"] = () => {
                observer.Deactivate();
                var e = pool.CreateEntity();
                e.AddComponentA();
                observer.collectedEntities.should_be_empty();
            };

            it["continues collecting when activated"] = () => {
                observer.Deactivate();
                var e1 = pool.CreateEntity();
                e1.AddComponentA();

                observer.Activate();

                var e2 = pool.CreateEntity();
                e2.AddComponentA();

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

            it["clears collected entites"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();

                observer.ClearCollectedEntities();
                observer.collectedEntities.should_be_empty();
            };
        };

        context["when observing with eventType OnEntityRemoved"] = () => {
            before = () => {
                observer = new GroupObserver(groupA, GroupEventType.OnEntityRemoved);
            };

            it["returns collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                observer.collectedEntities.should_be_empty();

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

        context["when observing with eventType OnEntityAddedOrRemoved"] = () => {
            before = () => {
                observer = new GroupObserver(groupA, GroupEventType.OnEntityAddedOrRemoved);
            };

            it["returns collected entities"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                var entities = observer.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
                observer.ClearCollectedEntities();

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

        context["when observing multiple groups"] = () => {
            Group groupB = null;
            before = () => {
                groupB = pool.GetGroup(Matcher.AllOf(new[] { CID.ComponentB }));
            };

            it["throws when goup count != eventType count"] = expect <GroupObserverException>(() => {
                observer = new GroupObserver(
                    new [] { groupA },
                    new [] { GroupEventType.OnEntityAdded, GroupEventType.OnEntityAdded }
                    );
            });

            context["when observing with eventType OnEntityAdded"] = () => {
                before = () => {
                    observer = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] { GroupEventType.OnEntityAdded, GroupEventType.OnEntityAdded }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = pool.CreateEntity();
                    eA.AddComponentA();
                    var eB = pool.CreateEntity();
                    eB.AddComponentB();

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

            context["when observing with eventType OnEntityRemoved"] = () => {
                before = () => {
                    observer = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] { GroupEventType.OnEntityRemoved, GroupEventType.OnEntityRemoved }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = pool.CreateEntity();
                    eA.AddComponentA();
                    var eB = pool.CreateEntity();
                    eB.AddComponentB();
                    observer.collectedEntities.should_be_empty();

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

            context["when observing with eventType OnEntityAddedOrRemoved"] = () => {
                before = () => {
                    observer = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] { GroupEventType.OnEntityAddedOrRemoved, GroupEventType.OnEntityAddedOrRemoved }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = pool.CreateEntity();
                    eA.AddComponentA();
                    var eB = pool.CreateEntity();
                    eB.AddComponentB();
                    var entities = observer.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                    observer.ClearCollectedEntities();

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

            context["when observing with mixed eventTypes"] = () => {
                before = () => {
                    observer = new GroupObserver(
                        new [] { groupA, groupB },
                        new [] { GroupEventType.OnEntityAdded, GroupEventType.OnEntityRemoved }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = pool.CreateEntity();
                    eA.AddComponentA();
                    var eB = pool.CreateEntity();
                    eB.AddComponentB();
                    var entities = observer.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eA);
                    observer.ClearCollectedEntities();

                    eA.RemoveComponentA();
                    eB.RemoveComponentB();
                    entities = observer.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eB);
                };
            };
        };
    }
 public GroupObserverSubSystemSpy(GroupObserver groupObserver)
 {
     _groupObserver = groupObserver;
 }