コード例 #1
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);
            };
        };
    }
コード例 #2
0
    void when_created()
    {
        Pool                      pool           = null;
        ReactiveSystem            reactiveSystem = null;
        ReactiveSubSystemSpy      subSystem      = null;
        MultiReactiveSubSystemSpy multiSubSystem = 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"] = () => {
                reactiveSystem.Deactivate();
                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                reactiveSystem.Execute();

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

            it["activates"] = () => {
                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);
            };
        };

        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.RefCount().should_be(1);
                };

                var e = pool.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                pool.DestroyEntity(e);
                reactiveSystem.Execute();
                didExecute.should_be(1);
                providedEntity.RefCount().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"] = () => {
            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["ensure components matcher"] = () => {
            context["single reactive system"] = () => {
                ReactiveEnsureSubSystemSpy ensureSubSystem = null;
                Entity eAB  = null;
                Entity eABC = null;
                before = () => {
                    ensureSubSystem = new ReactiveEnsureSubSystemSpy(_mactherAB, 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.RefCount().should_be(3); // retained by pool, group and group observer
                    var didExecute = 0;
                    ensureSubSystem.executeAction = entities => {
                        didExecute += 1;
                        eABC.RefCount().should_be(3);
                    };
                    reactiveSystem.Execute();
                    didExecute.should_be(1);
                    eABC.RefCount().should_be(2); // retained by pool and group
                };

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

                reactiveSystem.Execute();
                didExecute.should_be(1);
            };
        };
    }
コード例 #3
0
    void when_created()
    {
        Pool pool = null;
        ReactiveSystem reactiveSystem = null;
        ReactiveSubSystemSpy subSystem = null;
        MultiReactiveSubSystemSpy multiSubSystem = 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"] = () => {
            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["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);
            };
        };
    }
コード例 #4
0
    void when_created()
    {
        Pool                      pool           = null;
        ReactiveSystem            reactiveSystem = null;
        ReactiveSubSystemSpy      subSystem      = null;
        MultiReactiveSubSystemSpy multiSubSystem = 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 triggeringMatcher 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 triggeringMatcher 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["doesn't execute when triggeringMatcher doesn't match"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                reactiveSystem.Execute();
                subSystem.didExecute.should_be(0);
                subSystem.entities.should_be_null();
            };

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

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

            it["activates"] = () => {
                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);
            };
        };

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

            it["executes when triggeringMatcher 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 triggeringMatcher 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 triggeringMatcher 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();
            };
        };

        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);
                reactiveSystem.Execute();
                sys.entities.Length.should_be(1);
            };
        };

        context["MultiReactiveSystem"] = () => {
            before = () => {
                var matchers = new IMatcher[] {
                    Matcher.AllOf(new [] { CID.ComponentA }),
                    Matcher.AllOf(new [] { CID.ComponentB })
                };
                var eventTypes = new [] {
                    GroupEventType.OnEntityAdded,
                    GroupEventType.OnEntityRemoved,
                };
                multiSubSystem = new MultiReactiveSubSystemSpy(matchers, eventTypes);
                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["ensure components matcher"] = () => {
            it["only passes in entities matching required matcher"] = () => {
                var ensureSubSystem = new ReactiveEnsureSubSystemSpy(
                    allOfAB(),
                    GroupEventType.OnEntityAdded,
                    Matcher.AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC
                })
                    );
                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["only passes in entities matching required matcher (multi reactive)"] = () => {
                var matchers = new IMatcher[] {
                    Matcher.AllOf(new [] { CID.ComponentA }),
                    Matcher.AllOf(new [] { CID.ComponentB })
                };
                var eventTypes = new [] {
                    GroupEventType.OnEntityAdded,
                    GroupEventType.OnEntityRemoved,
                };

                var ensure = Matcher.AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC
                });

                var ensureSubSystem = new MultiReactiveEnsureSubSystemSpy(matchers, eventTypes, 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(
                    allOfAB(),
                    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"] = () => {
            it["only passes in entities not matching matcher"] = () => {
                var excludeSubSystem = new ReactiveExcludeSubSystemSpy(
                    allOfAB(),
                    GroupEventType.OnEntityAdded,
                    Matcher.AllOf(new [] { CID.ComponentC })
                    );
                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);
            };

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

                var exclude = Matcher.AllOf(new [] {
                    CID.ComponentC
                });

                var excludeSubSystem = new MultiReactiveExcludeSubSystemSpy(matchers, eventTypes, 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"] = () => {
            it["only passes in entities"] = () => {
                var ensureExcludeSystem = new ReactiveEnsureExcludeSubSystemSpy(
                    allOfAB(),
                    GroupEventType.OnEntityAdded,
                    Matcher.AllOf(new [] { CID.ComponentA, CID.ComponentB }),
                    Matcher.AllOf(new [] { CID.ComponentC })
                    );
                reactiveSystem = new ReactiveSystem(pool, ensureExcludeSystem);

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

                ensureExcludeSystem.didExecute.should_be(1);
                ensureExcludeSystem.entities.Length.should_be(1);
                ensureExcludeSystem.entities.should_contain(eAB);
            };
        };
    }
コード例 #5
0
    void when_created()
    {
        Pool pool = null;
        ReactiveSystem reactiveSystem = null;
        ReactiveSubSystemSpy subSystem = null;
        MultiReactiveSubSystemSpy multiSubSystem = 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 triggeringMatcher 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 triggeringMatcher 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["doesn't execute when triggeringMatcher doesn't match"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                reactiveSystem.Execute();
                subSystem.didExecute.should_be(0);
                subSystem.entities.should_be_null();
            };

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

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

            it["activates"] = () => {
                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);
            };
        };

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

            it["executes when triggeringMatcher 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 triggeringMatcher 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 triggeringMatcher 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();
            };
        };

        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.entites.Length.should_be(1);
                reactiveSystem.Execute();
                sys.entites.Length.should_be(1);
            };
        };

        context["MultiReactiveSystem"] = () => {
            before = () => {
                var matchers = new IMatcher[] {
                    Matcher.AllOf(new [] { CID.ComponentA }),
                    Matcher.AllOf(new [] { CID.ComponentB })
                };
                var eventTypes = new [] {
                    GroupEventType.OnEntityAdded,
                    GroupEventType.OnEntityRemoved,
                };
                multiSubSystem = new MultiReactiveSubSystemSpy(matchers, eventTypes);
                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);
            };
        };
    }