Example #1
0
        public void has_existing_entity()
        {
            var newIndex = new PrimaryEntityIndex <ITestSecondPool, string>(Group, (e, c) => {
                var nameAge = c as NameTestComponent;
                return(nameAge != null
                        ? nameAge.Name
                        : e.Get <NameTestComponent>().Name);
            });

            Assert.IsTrue(newIndex.HasEntity(Key));
        }
    void when_primary_index()
    {
        PrimaryEntityIndex<string> index = null;
        Context ctx = null;
        Group group = null;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                        index.HasEntity("Jack").should_be_true();
                    };
                };
            };
        };
    }
Example #3
0
    void when_primary_index()
    {
        PrimaryEntityIndex <string> index = null;
        Pool  pool  = null;
        Group group = null;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                        index.HasEntity("Jack").should_be_true();
                    };
                };
            };
        };
    }
Example #4
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>(group, (e, c) => {
                    var nameAge = c as NameAgeComponent;
                    return(nameAge != null
                        ? nameAge.name
                        : ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name);
                });
            };

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                            index.HasEntity("Jack").should_be_true();
                        };
                    };
                };
            };
        };

        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>(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);
                    #if !ENTITAS_FAST_AND_UNSAFE
                    entity.owners.should_contain(index);
                    #endif
                };

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

                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.HasEntity(name + "1").should_be_false();
                    index.HasEntity(name + "2").should_be_false();
                    entity.retainCount.should_be(1);
                    #if !ENTITAS_FAST_AND_UNSAFE
                    entity.owners.should_not_contain(index);
                    #endif
                };

                it["has existing entity"] = () => {
                    index.Deactivate();
                    index.Activate();
                    index.GetEntity(name + "1").should_be_same(entity);
                    index.GetEntity(name + "2").should_be_same(entity);
                };
            };
        };
    }