Пример #1
0
        public void IsMatched_MultipleComponents_AllMatchedNotFound()
        {
            var target = new Matcher().All(typeof(Test1Component), typeof(Test2Component));

            var entity = new TestEntity();

            entity.AddComponent <Test1Component>();
            entity.AddComponent <Test3Component>();
            entity.Components.CommitChanges();

            var result = target.IsMatched(entity);

            Assert.IsFalse(result);
        }
Пример #2
0
        public void IsMatched_MultipleComponents_MatchedSingleFilter()
        {
            var target = new Matcher().All(typeof(Test1Component));

            var entity = new TestEntity();

            entity.AddComponent <Test1Component>();
            entity.AddComponent <Test2Component>();
            entity.Components.CommitChanges();

            var result = target.IsMatched(entity);

            Assert.IsTrue(result);
        }
Пример #3
0
        public void IsMatched_ComplexTest3_Matched()
        {
            var target = new Matcher().All(typeof(Test3Component), typeof(Test2Component)).One(typeof(Test2Component), typeof(Test1Component));

            var entity = new TestEntity();

            entity.AddComponent <Test2Component>();
            entity.AddComponent <Test3Component>();
            entity.Components.CommitChanges();

            var result = target.IsMatched(entity);

            Assert.IsTrue(result);
        }
Пример #4
0
        public void IsMatched_SingleComponent_NotMatchedDifferentFilter()
        {
            var target = new Matcher().All(typeof(Test1Component));

            var entity = new TestEntity();

            entity.AddComponent <Test2Component>();
            entity.Components.CommitChanges();

            var result = target.IsMatched(entity);

            Assert.IsFalse(result);
        }
Пример #5
0
        public void IsMatched_OneComponentNotFound_NotMatched()
        {
            var target = new Matcher().One(typeof(Test1Component));

            var entity = new TestEntity();

            entity.AddComponent <Test2Component>();
            entity.Components.CommitChanges();

            var result = target.IsMatched(entity);

            Assert.IsFalse(result);
        }
Пример #6
0
    void when_primary_index()
    {
        context["single key"] = () => {
            PrimaryEntityIndex <TestEntity, string> index = null;
            IContext <TestEntity> ctx   = null;
            IGroup <TestEntity>   group = null;

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

            context["when entity for key doesn't exist"] = () => {
                it["returns null when getting entity for unknown key"] = () => {
                    index.GetEntity("unknownKey").should_be_null();
                };
            };

            context["when entity for key exists"] = () => {
                const string name   = "Max";
                TestEntity   entity = null;

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

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

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

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

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

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

                it["can ToString"] = () => {
                    index.ToString().should_be("PrimaryEntityIndex(TestIndex)");
                };

                context["when deactivated"] = () => {
                    before = () => {
                        index.Deactivate();
                    };

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

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

                    context["when activated"] = () => {
                        before = () => {
                            index.Activate();
                        };

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

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

                            index.GetEntity("Jack").should_be_same(entity);
                        };
                    };
                };
            };
        };

        context["multiple keys"] = () => {
            PrimaryEntityIndex <TestEntity, string> index = null;
            IContext <TestEntity> ctx   = null;
            IGroup <TestEntity>   group = null;

            before = () => {
                ctx   = new MyTestContext();
                group = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA));
                index = new PrimaryEntityIndex <TestEntity, string>("TestIndex", group, (e, c) => {
                    var nameAge = c as NameAgeComponent;
                    return(nameAge != null
                        ? new [] { nameAge.name + "1", nameAge.name + "2" }
                        : new [] { ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name + "1", ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name + "2" });
                });
            };

            context["when entity for key exists"] = () => {
                const string name   = "Max";
                TestEntity   entity = null;

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

                it["retains entity"] = () => {
                    entity.retainCount.should_be(3);

                    var safeAerc = entity.aerc as SafeAERC;
                    if (safeAerc != null)
                    {
                        safeAerc.owners.should_contain(index);
                    }
                };

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

                it["releases and removes entity from index when component gets removed"] = () => {
                    entity.RemoveComponent(CID.ComponentA);
                    index.GetEntity(name + "1").should_be_null();
                    index.GetEntity(name + "2").should_be_null();
                    entity.retainCount.should_be(1);

                    var safeAerc = entity.aerc as SafeAERC;
                    if (safeAerc != null)
                    {
                        safeAerc.owners.should_not_contain(index);
                    }
                };

                it["has existing entity"] = () => {
                    index.Deactivate();
                    index.Activate();
                    index.GetEntity(name + "1").should_be_same(entity);
                    index.GetEntity(name + "2").should_be_same(entity);
                };
            };
        };
    }
Пример #7
0
    void when_index()
    {
        context["single key"] = () => {
            EntityIndex <TestEntity, string> index = null;
            IContext <TestEntity>            ctx   = null;
            IGroup <TestEntity> group = null;

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

            context["when entity for key doesn't exist"] = () => {
                it["has no entities"] = () => {
                    index.GetEntities("unknownKey").should_be_empty();
                };
            };

            context["when entity for key exists"] = () => {
                const string     name             = "Max";
                NameAgeComponent nameAgeComponent = null;
                TestEntity       entity1          = null;
                TestEntity       entity2          = null;

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

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

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

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

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

                it["can ToString"] = () => {
                    index.ToString().should_be("EntityIndex(TestIndex)");
                };

                context["when deactivated"] = () => {
                    before = () => {
                        index.Deactivate();
                    };

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

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

                    context["when activated"] = () => {
                        before = () => {
                            index.Activate();
                        };

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

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

                            var entities = index.GetEntities(name);
                            entities.Count.should_be(3);
                            entities.should_contain(entity1);
                            entities.should_contain(entity2);
                            entities.should_contain(entity3);
                        };
                    };
                };
            };
        };

        context["multiple keys"] = () => {
            EntityIndex <TestEntity, string> index = null;
            IContext <TestEntity>            ctx   = null;
            IGroup <TestEntity> group   = null;
            TestEntity          entity1 = null;
            TestEntity          entity2 = null;

            before = () => {
                ctx   = new MyTestContext();
                group = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA));
                index = new EntityIndex <TestEntity, string>("TestIndex", group, (e, c) => {
                    return(e == entity1
                        ? new [] { "1", "2" }
                        : new [] { "2", "3" });
                });
            };

            context["when entity for key exists"] = () => {
                before = () => {
                    entity1 = ctx.CreateEntity();
                    entity1.AddComponentA();
                    entity2 = ctx.CreateEntity();
                    entity2.AddComponentA();
                };

                it["retains entity"] = () => {
                    entity1.retainCount.should_be(3);
                    entity2.retainCount.should_be(3);

                    var safeAerc1 = entity1.aerc as SafeAERC;
                    if (safeAerc1 != null)
                    {
                        safeAerc1.owners.should_contain(index);
                    }

                    var safeAerc2 = entity1.aerc as SafeAERC;
                    if (safeAerc2 != null)
                    {
                        safeAerc2.owners.should_contain(index);
                    }
                };

                it["has entity"] = () => {
                    index.GetEntities("1").Count.should_be(1);
                    index.GetEntities("2").Count.should_be(2);
                    index.GetEntities("3").Count.should_be(1);
                };

                it["gets entity for key"] = () => {
                    index.GetEntities("1").First().should_be_same(entity1);
                    index.GetEntities("2").should_contain(entity1);
                    index.GetEntities("2").should_contain(entity2);
                    index.GetEntities("3").First().should_be_same(entity2);
                };

                it["releases and removes entity from index when component gets removed"] = () => {
                    entity1.RemoveComponent(CID.ComponentA);
                    index.GetEntities("1").Count.should_be(0);
                    index.GetEntities("2").Count.should_be(1);
                    index.GetEntities("3").Count.should_be(1);

                    entity1.retainCount.should_be(1);
                    entity2.retainCount.should_be(3);

                    var safeAerc1 = entity1.aerc as SafeAERC;
                    if (safeAerc1 != null)
                    {
                        safeAerc1.owners.should_not_contain(index);
                    }

                    var safeAerc2 = entity2.aerc as SafeAERC;
                    if (safeAerc2 != null)
                    {
                        safeAerc2.owners.should_contain(index);
                    }
                };

                it["has existing entities"] = () => {
                    index.Deactivate();
                    index.Activate();
                    index.GetEntities("1").First().should_be_same(entity1);
                    index.GetEntities("2").should_contain(entity1);
                    index.GetEntities("2").should_contain(entity2);
                    index.GetEntities("3").First().should_be_same(entity2);
                };
            };
        };
    }
    void when_extending()
    {
        context["when copying components"] = () => {
            IContext <TestEntity> ctx     = null;
            TestEntity            entity  = null;
            TestEntity            target  = null;
            NameAgeComponent      nameAge = null;

            before = () => {
                ctx     = new MyTestContext();
                entity  = ctx.CreateEntity();
                target  = ctx.CreateEntity();
                nameAge = new NameAgeComponent {
                    name = "Max", age = 42
                };
            };

            it["doesn't change entity if original doesn't have any components"] = () => {
                entity.CopyTo(target);

                entity.creationIndex.should_be(0);
                target.creationIndex.should_be(1);

                target.GetComponents().Length.should_be(0);
            };

            it["adds copies of all components to target entity"] = () => {
                entity.AddComponentA();
                entity.AddComponent(CID.ComponentB, nameAge);

                entity.CopyTo(target);

                target.GetComponents().Length.should_be(2);
                target.HasComponentA().should_be_true();
                target.HasComponentB().should_be_true();
                target.GetComponentA().should_not_be_same(Component.A);
                target.GetComponent(CID.ComponentB).should_not_be_same(nameAge);

                var clonedComponent = (NameAgeComponent)target.GetComponent(CID.ComponentB);

                clonedComponent.name.should_be(nameAge.name);
                clonedComponent.age.should_be(nameAge.age);
            };

            it["throws when target already has a component at index"] = base.expect <EntityAlreadyHasComponentException>(() => {
                entity.AddComponentA();
                entity.AddComponent(CID.ComponentB, nameAge);
                var component = new NameAgeComponent();
                target.AddComponent(CID.ComponentB, component);

                entity.CopyTo(target);
            });

            it["replaces existing components when overwrite is set"] = () => {
                entity.AddComponentA();
                entity.AddComponent(CID.ComponentB, nameAge);
                var component = new NameAgeComponent();
                target.AddComponent(CID.ComponentB, component);

                entity.CopyTo(target, true);

                var copy = target.GetComponent(CID.ComponentB);
                copy.should_not_be_same(nameAge);
                copy.should_not_be_same(component);
                ((NameAgeComponent)copy).name.should_be(nameAge.name);
                ((NameAgeComponent)copy).age.should_be(nameAge.age);
            };

            it["only adds copies of specified components to target entity"] = () => {
                entity.AddComponentA();
                entity.AddComponentB();
                entity.AddComponentC();

                entity.CopyTo(target, false, CID.ComponentB, CID.ComponentC);

                target.GetComponents().Length.should_be(2);
                target.HasComponentB().should_be_true();
                target.HasComponentC().should_be_true();
            };

            it["uses component pool"] = () => {
                entity.AddComponentA();

                var component = new ComponentA();
                target.GetComponentPool(CID.ComponentA).Push(component);

                entity.CopyTo(target);

                target.GetComponentA().should_be_same(component);
            };
        };
    }
Пример #9
0
    void when_creating()
    {
        IContext <TestEntity> ctx    = null;
        TestEntity            entity = null;

        before = () => {
            ctx    = new MyTestContext();
            entity = ctx.CreateEntity();
        };

        context["ComponentBlueprint"] = () => {
            it["creates a component blueprint from a component without members"] = () => {
                var component = new ComponentA();

                const int index = 42;

                var componentBlueprint = new ComponentBlueprint(index, component);
                componentBlueprint.index.should_be(index);
                componentBlueprint.fullTypeName.should_be(component.GetType().FullName);
                componentBlueprint.members.Length.should_be(0);
            };

            it["throws when unknown type"] = expect <ComponentBlueprintException>(() => {
                var componentBlueprint          = new ComponentBlueprint();
                componentBlueprint.fullTypeName = "UnknownType";
                componentBlueprint.CreateComponent(null);
            });

            it["throws when type doesn't implement IComponent"] = expect <ComponentBlueprintException>(() => {
                var componentBlueprint          = new ComponentBlueprint();
                componentBlueprint.fullTypeName = "string";
                componentBlueprint.CreateComponent(null);
            });

            it["creates a component blueprint from a component with members"] = () => {
                var component = new NameAgeComponent();
                component.name = "Max";
                component.age  = 42;

                const int index = 24;

                var componentBlueprint = new ComponentBlueprint(index, component);
                componentBlueprint.index.should_be(index);
                componentBlueprint.fullTypeName.should_be(component.GetType().FullName);
                componentBlueprint.members.Length.should_be(2);

                componentBlueprint.members[0].name.should_be("name");
                componentBlueprint.members[0].value.should_be(component.name);

                componentBlueprint.members[1].name.should_be("age");
                componentBlueprint.members[1].value.should_be(component.age);
            };

            it["creates a component and sets members values"] = () => {
                var componentBlueprint = new ComponentBlueprint();
                componentBlueprint.fullTypeName = typeof(ComponentWithFieldsAndProperties).FullName;
                componentBlueprint.index        = CID.ComponentB;
                componentBlueprint.members      = new [] {
                    new SerializableMember("publicField", "publicFieldValue"),
                    new SerializableMember("publicProperty", "publicPropertyValue")
                };

                var component = (ComponentWithFieldsAndProperties)componentBlueprint.CreateComponent(entity);
                component.publicField.should_be("publicFieldValue");
                component.publicProperty.should_be("publicPropertyValue");
            };

            it["ignores invalid member name"] = () => {
                var componentBlueprint = new ComponentBlueprint();
                componentBlueprint.index        = 0;
                componentBlueprint.fullTypeName = typeof(NameAgeComponent).FullName;
                componentBlueprint.members      = new [] {
                    new SerializableMember("xxx", "publicFieldValue"),
                    new SerializableMember("publicProperty", "publicPropertyValue")
                };
                componentBlueprint.CreateComponent(entity);
            };
        };

        context["Blueprint"] = () => {
            it["creates an empty blueprint from a null entity"] = () => {
                var blueprint = new Blueprint("My Context", "Hero", null);
                blueprint.contextIdentifier.should_be("My Context");
                blueprint.name.should_be("Hero");
                blueprint.components.Length.should_be(0);
            };

            it["creates a blueprint from an entity"] = () => {
                entity.AddComponentA();

                var component = new NameAgeComponent();
                component.name = "Max";
                component.age  = 42;

                entity.AddComponent(CID.ComponentB, component);

                var blueprint = new Blueprint("My Context", "Hero", entity);
                blueprint.contextIdentifier.should_be("My Context");
                blueprint.name.should_be("Hero");
                blueprint.components.Length.should_be(2);

                blueprint.components[0].index.should_be(CID.ComponentA);
                blueprint.components[0].fullTypeName.should_be(Component.A.GetType().FullName);

                blueprint.components[1].index.should_be(CID.ComponentB);
                blueprint.components[1].fullTypeName.should_be(component.GetType().FullName);
            };

            context["when applying blueprint"] = () => {
                Blueprint blueprint = null;

                before = () => {
                    var component1 = new ComponentBlueprint();
                    component1.index        = CID.ComponentA;
                    component1.fullTypeName = typeof(ComponentA).FullName;
                    component1.members      = new SerializableMember[0];

                    var component2 = new ComponentBlueprint();
                    component2.index        = CID.ComponentB;
                    component2.fullTypeName = typeof(NameAgeComponent).FullName;
                    component2.members      = new [] {
                        new SerializableMember("name", "Max"),
                        new SerializableMember("age", 42)
                    };

                    blueprint            = new Blueprint();
                    blueprint.name       = "Hero";
                    blueprint.components = new [] { component1, component2 };
                };

                it["applies blueprint to entity"] = () => {
                    entity.ApplyBlueprint(blueprint);
                    entity.GetComponents().Length.should_be(2);

                    entity.GetComponent(CID.ComponentA).GetType().should_be(typeof(ComponentA));

                    var nameAgeComponent = (NameAgeComponent)entity.GetComponent(CID.ComponentB);
                    nameAgeComponent.GetType().should_be(typeof(NameAgeComponent));
                    nameAgeComponent.name.should_be("Max");
                    nameAgeComponent.age.should_be(42);
                };

                it["throws when entity already has a component which should be added from blueprint"] = expect <EntityAlreadyHasComponentException>(() => {
                    entity.AddComponentA();
                    entity.ApplyBlueprint(blueprint);
                });

                it["can overwrite existing components"] = () => {
                    var nameAgeComponent = new NameAgeComponent();
                    nameAgeComponent.name = "Jack";
                    nameAgeComponent.age  = 24;
                    entity.AddComponent(CID.ComponentB, nameAgeComponent);

                    entity.ApplyBlueprint(blueprint, true);
                };

                it["uses component from componentPool"] = () => {
                    var component = new ComponentBlueprint();
                    component.index        = CID.ComponentA;
                    component.fullTypeName = typeof(ComponentA).FullName;
                    component.members      = new SerializableMember[0];

                    blueprint            = new Blueprint();
                    blueprint.name       = "Hero";
                    blueprint.components = new [] { component };

                    var componentA = entity.CreateComponent <ComponentA>(CID.ComponentA);
                    entity.AddComponent(CID.ComponentA, componentA);
                    entity.RemoveComponentA();

                    entity.ApplyBlueprint(blueprint);

                    entity.GetComponentA().should_be_same(componentA);
                };
            };
        };
    }