void when_index()
    {
        EntityIndex<string> index = null;
        Context ctx = null;
        Group group = null;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                        var entities = index.GetEntities(name);
                        entities.Count.should_be(3);
                        entities.should_contain(entity1);
                        entities.should_contain(entity2);
                        entities.should_contain(entity3);
                    };
                };
            };
        };
    }
    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);
                };
            };
        };
    }
Beispiel #3
0
    void when_index()
    {
        EntityIndex <string> index = null;
        Pool  pool  = null;
        Group group = null;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                        var entities = index.GetEntities(name);
                        entities.Count.should_be(3);
                        entities.should_contain(entity1);
                        entities.should_contain(entity2);
                        entities.should_contain(entity3);
                    };
                };
            };
        };
    }
        public void SingleKeyEntityIndexReturnsNullEntityForUnknownKey()
        {
            SetupActivatedSingleKeyEntityIndex();

            Assert.IsEmpty(_index.GetEntities("unknownKey"));
        }