示例#1
0
        public Pool(int totalComponents, int startCreationIndex, PoolMetaData metaData) {
            _totalComponents = totalComponents;
            _creationIndex = startCreationIndex;

            if (metaData != null) {
                _metaData = metaData;

                if (metaData.componentNames.Length != totalComponents) {
                    throw new PoolMetaDataException(this, metaData);
                }
            } else {
                var componentNames = new string[totalComponents];
                const string suffix = "Index ";
                for (int i = 0, componentNamesLength = componentNames.Length; i < componentNamesLength; i++) {
                    componentNames[i] = suffix + i;
                }
                _metaData = new PoolMetaData("Unnamed Pool", componentNames);
            }

            _groupsForIndex = new List<Group>[totalComponents];

            // Cache delegates to avoid gc allocations
            _cachedUpdateGroupsComponentAddedOrRemoved = updateGroupsComponentAddedOrRemoved;
            _cachedUpdateGroupsComponentReplaced = updateGroupsComponentReplaced;
            _cachedOnEntityReleased = onEntityReleased;
        }
示例#2
0
        /// The prefered way is to use the generated methods from the code generator to create a Pool, e.g. var pool = Pools.pool;
        public Pool(int totalComponents, int startCreationIndex, PoolMetaData metaData)
        {
            _totalComponents = totalComponents;
            _componentPools = new Stack<IComponent>[totalComponents];
            _creationIndex = startCreationIndex;

            if (metaData != null) {
                _metaData = metaData;

                if (metaData.componentNames.Length != totalComponents) {
                    throw new PoolMetaDataException(this, metaData);
                }
            } else {

                // If Pools.pool was used to create the pool, we will never end up here.
                // This is a fallback when the pool is created manually.

                var componentNames = new string[totalComponents];
                const string prefix = "Index ";
                for (int i = 0, componentNamesLength = componentNames.Length; i < componentNamesLength; i++) {
                    componentNames[i] = prefix + i;
                }
                _metaData = new PoolMetaData("Unnamed Pool", componentNames, null);
            }

            _groupsForIndex = new List<Group>[totalComponents];

            // Cache delegates to avoid gc allocations
            _cachedUpdateGroupsComponentAddedOrRemoved = updateGroupsComponentAddedOrRemoved;
            _cachedUpdateGroupsComponentReplaced = updateGroupsComponentReplaced;
            _cachedOnEntityReleased = onEntityReleased;
        }
示例#3
0
文件: Pool.cs 项目: soulhez/AMazeBoss
        /// The prefered way is to use the generated methods from the code generator to create a Pool, e.g. var pool = Pools.pool;
        public Pool(int totalComponents, int startCreationIndex, PoolMetaData metaData)
        {
            _totalComponents = totalComponents;
            _componentPools  = new Stack <IComponent> [totalComponents];
            _creationIndex   = startCreationIndex;

            if (metaData != null)
            {
                _metaData = metaData;

                if (metaData.componentNames.Length != totalComponents)
                {
                    throw new PoolMetaDataException(this, metaData);
                }
            }
            else
            {
                var          componentNames = new string[totalComponents];
                const string prefix         = "Index ";
                for (int i = 0, componentNamesLength = componentNames.Length; i < componentNamesLength; i++)
                {
                    componentNames[i] = prefix + i;
                }
                _metaData = new PoolMetaData("Unnamed Pool", componentNames);
            }

            _groupsForIndex = new List <Group> [totalComponents];

            // Cache delegates to avoid gc allocations
            _cachedUpdateGroupsComponentAddedOrRemoved = updateGroupsComponentAddedOrRemoved;
            _cachedUpdateGroupsComponentReplaced       = updateGroupsComponentReplaced;
            _cachedOnEntityReleased = onEntityReleased;
        }
        /// Use pool.CreateEntity() to create a new entity and
        /// pool.DestroyEntity() to destroy it.
        public Entity(int totalComponents,
                      Stack <IComponent>[] componentPools,
                      PoolMetaData poolMetaData = null)
        {
            _totalComponents = totalComponents;
            _components      = new IComponent[totalComponents];
            _componentPools  = componentPools;

            if (poolMetaData != null)
            {
                _poolMetaData = poolMetaData;
            }
            else
            {
                // If pool.CreateEntity() was used to create the entity,
                // we will never end up here.
                // This is a fallback when entities are created manually.

                var componentNames = new string[totalComponents];
                for (int i = 0; i < componentNames.Length; i++)
                {
                    componentNames[i] = i.ToString();
                }
                _poolMetaData = new PoolMetaData(
                    "No Pool", componentNames, null
                    );
            }
        }
示例#5
0
 public PoolMetaDataException(Pool pool, PoolMetaData poolMetaData) :
     base("Invalid PoolMetaData for '" + pool + "'!\nExpected " +
          pool.totalComponents + " componentName(s) but got " +
          poolMetaData.componentNames.Length + ":",
          string.Join("\n", poolMetaData.componentNames))
 {
 }
    void when_created()
    {
        it["has values"] = () => {

            var componentNames = new [] { "Health", "Position", "View" };
            const string poolName = "My Pool";

            var data = new PoolMetaData(poolName, componentNames);

            data.poolName.should_be(poolName);
            data.componentNames.should_be_same(componentNames);
        };
    }
示例#7
0
        public Entity(int totalComponents, PoolMetaData poolMetaData = null)
        {
            _components = new IComponent[totalComponents];

            if (poolMetaData != null)
            {
                _poolMetaData = poolMetaData;
            }
            else
            {
                var componentNames = new string[totalComponents];
                for (int i = 0, componentNamesLength = componentNames.Length; i < componentNamesLength; i++)
                {
                    componentNames[i] = i.ToString();
                }
                _poolMetaData = new PoolMetaData("No Pool", componentNames);
            }
        }
示例#8
0
        /// The prefered way is to use the generated methods from the
        /// code generator to create a Pool,
        /// e.g. Pools.sharedInstance.pool = Pools.CreatePool();
        public Pool(int totalComponents,
                    int startCreationIndex,
                    PoolMetaData metaData)
        {
            _totalComponents = totalComponents;
            _creationIndex   = startCreationIndex;

            if (metaData != null)
            {
                _metaData = metaData;

                if (metaData.componentNames.Length != totalComponents)
                {
                    throw new PoolMetaDataException(this, metaData);
                }
            }
            else
            {
                // If Pools.CreatePool() was used to create the pool,
                // we will never end up here.
                // This is a fallback when the pool is created manually.

                var          componentNames = new string[totalComponents];
                const string prefix         = "Index ";
                for (int i = 0; i < componentNames.Length; i++)
                {
                    componentNames[i] = prefix + i;
                }
                _metaData = new PoolMetaData(
                    "Unnamed Pool", componentNames, null
                    );
            }

            _groupsForIndex = new List <Group> [totalComponents];
            _componentPools = new Stack <IComponent> [totalComponents];
            _entityIndices  = new Dictionary <string, IEntityIndex>();

            // Cache delegates to avoid gc allocations
            _cachedEntityChanged     = updateGroupsComponentAddedOrRemoved;
            _cachedComponentReplaced = updateGroupsComponentReplaced;
            _cachedEntityReleased    = onEntityReleased;
        }
示例#9
0
 public PoolMetaDataException(Pool pool, PoolMetaData poolMetaData) :
     base("Invalid PoolMetaData for '" + pool + "'!\nExpected " + pool.totalComponents + " componentName(s) but got " + poolMetaData.componentNames.Length + ":",
         string.Join("\n", poolMetaData.componentNames)) {
 }
    void when_throwing()
    {
        Pool pool = null;
        Entity entity = null;
        before = () => {
            var componentNames = new [] { "Health", "Position", "View" };
            var metaData = new PoolMetaData("My Pool", componentNames, null);
            pool = new Pool(componentNames.Length, 42, metaData);
            entity = pool.CreateEntity();
        };

        it["creates exception with hint separated by newLine"] = () => {
            var msg = "Message";
            var hint = "Hint";
            var ex = new EntitasException(msg, hint);
            ex.Message.should_be(msg + "\n" + hint);
        };

        it["ignores hint when null"] = () => {
            var msg = "Message";
            string hint = null;
            var ex = new EntitasException(msg, hint);
            ex.Message.should_be(msg);
        };

        context["Entity"] = () => {

            context["when not enabled"] = () => {

                before = () => {
                    pool.DestroyEntity(entity);
                };

                it["add a component"] = () => printErrorMessage(() => entity.AddComponentA());
                it["remove a component"] = () => printErrorMessage(() => entity.RemoveComponentA());
                it["replace a component"] = () => printErrorMessage(() => entity.ReplaceComponentA(Component.A));
            };

            context["when enabled"] = () => {
                it["add a component twice"] = () => printErrorMessage(() => {
                    entity.AddComponentA();
                    entity.AddComponentA();
                });

                it["remove a component that doesn't exist"] = () => printErrorMessage(() => {
                    entity.RemoveComponentA();
                });

                it["get a component that doesn't exist"] = () => printErrorMessage(() => {
                    entity.GetComponentA();
                });

                it["retain an entity twice"] = () => printErrorMessage(() => {
                    var owner = new object();
                    entity.Retain(owner);
                    entity.Retain(owner);
                });

                it["release an entity with wrong owner"] = () => printErrorMessage(() => {
                    var owner = new object();
                    entity.Release(owner);
                });
            };
        };

        context["Group"] = () => {

            it["get single entity when multiple exist"] = () => printErrorMessage(() => {
                pool.CreateEntity().AddComponentA();
                pool.CreateEntity().AddComponentA();
                var matcher = (Matcher)Matcher.AllOf(CID.ComponentA);
                matcher.componentNames = new [] { "Health", "Position", "View" };
                var group = pool.GetGroup(matcher);
                group.GetSingleEntity();
            });
        };

        context["GroupObserver"] = () => {

            it["unbalanced goups"] = () => printErrorMessage(() => {
                var g1 = new Group(Matcher.AllOf(CID.ComponentA));
                var g2 = new Group(Matcher.AllOf(CID.ComponentB));
                var e1 = GroupEventType.OnEntityAdded;

                new GroupObserver(new [] { g1, g2 }, new [] { e1 });
            });
        };

        context["Pool"] = () => {

            it["wrong PoolMetaData componentNames count"] = () => printErrorMessage(() => {
                var componentNames = new [] { "Health", "Position", "View" };
                var metaData = new PoolMetaData("My Pool", componentNames, null);
                new Pool(1, 0, metaData);
            });

            it["destroy entity which is not in pool"] = () => printErrorMessage(() => {
                pool.DestroyEntity(new Entity(0, null));
            });

            it["destroy retained entities"] = () => printErrorMessage(() => {
                pool.CreateEntity().Retain(this);
                pool.DestroyAllEntities();
            });

            it["releases entity before destroy"] = () => printErrorMessage(() => {
                entity.Release(pool);
            });
        };

        context["CollectionExtension"] = () => {

            it["get single entity when more than one exist"] = () => printErrorMessage(() => {
                new Entity[2].SingleEntity();
            });
        };

        context["ComponentBlueprint"] = () => {

            it["type doesn't implement IComponent"] = () => printErrorMessage(() => {
                var componentBlueprint = new ComponentBlueprint();
                componentBlueprint.fullTypeName = "string";
                componentBlueprint.CreateComponent(entity);
            });

            it["type doesn't exist"] = () => printErrorMessage(() => {
                var componentBlueprint = new ComponentBlueprint();
                componentBlueprint.fullTypeName = "UnknownType";
                componentBlueprint.CreateComponent(entity);
                componentBlueprint.CreateComponent(entity);
            });

            it["invalid field name"] = () => printErrorMessage(() => {
                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);
            });
        };
    }
    void when_created()
    {
        Entity e = null;
        before = () => {
            e = this.CreateEntity();
        };

        context["initial state"] = () => {

            it["has default PoolMetaData"] = () => {
                e.poolMetaData.poolName.should_be("No Pool");
                e.poolMetaData.componentNames.Length.should_be(CID.NumComponents);
                for (int i = 0; i < e.poolMetaData.componentNames.Length; i++) {
                    e.poolMetaData.componentNames[i].should_be(i.ToString());
                }
            };

            it["has custom PoolMetaData when set"] = () => {
                var poolMetaData = new PoolMetaData(null, null);
                e = new Entity(0, poolMetaData);
                e.poolMetaData.should_be_same(poolMetaData);
            };

            it["throws when attempting to get component of type which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => {
                e.GetComponentA();
            });

            it["gets empty array of components when no components were added"] = () => {
                e.GetComponents().should_be_empty();
            };

            it["gets empty array of component indices when no components were added"] = () => {
                e.GetComponentIndices().should_be_empty();
            };

            it["doesn't have component of type when no component of that type was added"] = () => {
                e.HasComponentA().should_be_false();
            };

            it["doesn't have components of types when no components of these types were added"] = () => {
                e.HasComponents(_indicesA).should_be_false();
            };

            it["doesn't have any components of types when no components of these types were added"] = () => {
                e.HasAnyComponent(_indicesA).should_be_false();
            };

            it["returns entity when adding a component"] = () => {
                e.AddComponent(0, null).should_be_same(e);
            };

            it["adds a component"] = () => {
                e.AddComponentA();
                assertHasComponentA(e);
            };

            it["throws when attempting to remove a component of type which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => {
                e.RemoveComponentA();
            });

            it["replacing a non existing component adds component"] = () => {
                e.ReplaceComponentA(Component.A);
                assertHasComponentA(e);
            };
        };

        context["when component added"] = () => {
            before = () => {
                e.AddComponentA();
            };

            it["throws when adding a component of the same type twice"] = expect<EntityAlreadyHasComponentException>(() => {
                e.AddComponentA();
                e.AddComponentA();
            });

            it["returns entity when removing a component"] = () => {
                e.RemoveComponent(CID.ComponentA).should_be_same(e);
            };

            it["removes a component of type"] = () => {
                e.RemoveComponentA();
                assertHasNotComponentA(e);
            };

            it["returns entity when replacing a component"] = () => {
                e.ReplaceComponent(CID.ComponentA, null).should_be_same(e);
            };

            it["replaces existing component"] = () => {
                var newComponentA = new ComponentA();
                e.ReplaceComponentA(newComponentA);
                assertHasComponentA(e, newComponentA);
            };

            it["doesn't have components of types when not all components of these types were added"] = () => {
                e.HasComponents(_indicesAB).should_be_false();
            };

            it["has any components of types when any component of these types was added"] = () => {
                e.HasAnyComponent(_indicesAB).should_be_true();
            };

            context["when adding another component"] = () => {
                before = () => {
                    e.AddComponentB();
                };

                it["gets all components"] = () => {
                    var components = e.GetComponents();
                    components.Length.should_be(2);
                    components.should_contain(Component.A);
                    components.should_contain(Component.B);
                };

                it["gets all component indices"] = () => {
                    var componentIndices = e.GetComponentIndices();
                    componentIndices.Length.should_be(2);
                    componentIndices.should_contain(CID.ComponentA);
                    componentIndices.should_contain(CID.ComponentB);
                };

                it["has other component"] = () => {
                    e.HasComponentB().should_be_true();
                };

                it["has components of types when all components of these types were added"] = () => {
                    e.HasComponents(_indicesAB).should_be_true();
                };

                it["removes all components"] = () => {
                    e.RemoveAllComponents();
                    e.HasComponentA().should_be_false();
                    e.HasComponentB().should_be_false();
                    e.GetComponents().should_be_empty();
                    e.GetComponentIndices().should_be_empty();
                };

                it["can ToString"] = () => {
                    e.AddComponent(0, new SomeComponent());
                    e.Retain(this);
                    e.ToString().should_be("Entity_0(1)(Some, ComponentA, ComponentB)");
                };
            };
        };

        context["events"] = () => {
            int didDispatch = 0;

            before = () => {
                didDispatch = 0;
            };

            it["dispatches OnComponentAdded when adding a component"] = () => {
                e.OnComponentAdded += (entity, index, component) => {
                    didDispatch += 1;
                    entity.should_be_same(e);
                    index.should_be(CID.ComponentA);
                    component.should_be_same(Component.A);
                };
                e.OnComponentRemoved += (entity, index, component) => this.Fail();
                e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail();

                e.AddComponentA();
                didDispatch.should_be(1);
            };

            it["dispatches OnComponentRemoved when removing a component"] = () => {
                e.AddComponentA();
                e.OnComponentRemoved += (entity, index, component) => {
                    didDispatch += 1;
                    entity.should_be_same(e);
                    index.should_be(CID.ComponentA);
                    component.should_be_same(Component.A);
                };
                e.OnComponentAdded += (entity, index, component) => this.Fail();
                e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail();

                e.RemoveComponentA();
                didDispatch.should_be(1);
            };

            it["dispatches OnComponentReplaced when replacing a component"] = () => {
                e.AddComponentA();
                var newComponentA = new ComponentA();
                e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => {
                    didDispatch += 1;
                    entity.should_be_same(e);
                    index.should_be(CID.ComponentA);
                    previousComponent.should_be_same(Component.A);
                    newComponent.should_be_same(newComponentA);
                };
                e.OnComponentAdded += (entity, index, component) => this.Fail();
                e.OnComponentRemoved += (entity, index, component) => this.Fail();

                e.ReplaceComponentA(newComponentA);
                didDispatch.should_be(1);
            };

            it["provides previous and new component OnComponentReplaced when replacing with different component"] = () => {
                var prevComp = new ComponentA();
                var newComp = new ComponentA();
                e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => {
                    didDispatch += 1;
                    entity.should_be_same(e);
                    previousComponent.should_be_same(prevComp);
                    newComponent.should_be_same(newComp);
                };

                e.AddComponent(CID.ComponentA, prevComp);
                e.ReplaceComponent(CID.ComponentA, newComp);
                didDispatch.should_be(1);
            };

            it["provides previous and new component OnComponentReplaced when replacing with same component"] = () => {
                e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => {
                    didDispatch += 1;
                    entity.should_be_same(e);
                    previousComponent.should_be_same(Component.A);
                    newComponent.should_be_same(Component.A);
                };

                e.AddComponentA();
                e.ReplaceComponentA(Component.A);
                didDispatch.should_be(1);
            };

            it["doesn't dispatch anything when replacing a non existing component with null"] = () => {
                e.OnComponentAdded += (entity, index, component) => this.Fail();
                e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail();
                e.OnComponentRemoved += (entity, index, component) => this.Fail();

                e.ReplaceComponentA(null);
            };

            it["dispatches OnComponentAdded when attempting to replace a component which hasn't been added"] = () => {
                var newComponentA = new ComponentA();
                e.OnComponentAdded += (entity, index, component) => {
                    didDispatch += 1;
                    entity.should_be_same(e);
                    index.should_be(CID.ComponentA);
                    component.should_be_same(newComponentA);
                };
                e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail();
                e.OnComponentRemoved += (entity, index, component) => this.Fail();

                e.ReplaceComponentA(newComponentA);
                didDispatch.should_be(1);
            };

            it["dispatches OnComponentRemoved when replacing a component with null"] = () => {
                e.AddComponentA();
                e.OnComponentRemoved += (entity, index, component) => {
                    didDispatch += 1;
                };
                e.OnComponentAdded += (entity, index, component) => this.Fail();
                e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail();

                e.ReplaceComponentA(null);
                didDispatch.should_be(1);
            };

            it["dispatches OnComponentRemoved when removing all components"] = () => {
                e.AddComponentA();
                e.AddComponentB();
                e.OnComponentRemoved += (entity, index, component) => didDispatch += 1;
                e.RemoveAllComponents();
                didDispatch.should_be(2);
            };
        };

        context["reference counting"] = () => {
            it["retains entity"] = () => {
                e.retainCount.should_be(0);
                e.Retain(this);
                e.retainCount.should_be(1);
            };

            it["releases entity"] = () => {
                e.Retain(this);
                e.Release(this);
                e.retainCount.should_be(0);
            };

            it["throws when releasing more than it has been retained"] = expect<EntityIsNotRetainedByOwnerException>(() => {
                e.Retain(this);
                e.Release(this);
                e.Release(this);
            });

            it["throws when retaining twice with same owner"] = expect<EntityIsAlreadyRetainedByOwnerException>(() => {
                var owner1 = new object();
                e.Retain(owner1);
                e.Retain(owner1);
            });

            it["throws when releasing with unknown owner"] = expect<EntityIsNotRetainedByOwnerException>(() => {
                var owner = new object();
                var unknownOwner = new object();
                e.Retain(owner);
                e.Release(unknownOwner);
            });

            it["throws when releasing with owner which doesn't retain entity anymore"] = expect<EntityIsNotRetainedByOwnerException>(() => {
                var owner1 = new object();
                var owner2 = new object();
                e.Retain(owner1);
                e.Retain(owner2);
                e.Release(owner2);
                e.Release(owner2);
            });

            context["events"] = () => {
                it["doesn't dispatch OnEntityReleased when retaining"] = () => {
                    e.OnEntityReleased += entity => this.Fail();
                    e.Retain(this);
                };

                it["dispatches OnEntityReleased when retain and release"] = () => {
                    var didDispatch = 0;
                    e.OnEntityReleased += entity => {
                        didDispatch += 1;
                        entity.should_be_same(e);
                    };
                    e.Retain(this);
                    e.Release(this);
                };
            };
        };

        context["internal caching"] = () => {
            context["components"] = () => {

                IComponent[] cache = null;
                before = () => {
                    e.AddComponentA();
                    cache = e.GetComponents();
                };

                it["caches components"] = () => {
                    e.GetComponents().should_be_same(cache);
                };

                it["updates cache when a new component was added"] = () => {
                    e.AddComponentB();
                    e.GetComponents().should_not_be_same(cache);
                };

                it["updates cache when a component was removed"] = () => {
                    e.RemoveComponentA();
                    e.GetComponents().should_not_be_same(cache);
                };

                it["updates cache when a component was replaced"] = () => {
                    e.ReplaceComponentA(new ComponentA());
                    e.GetComponents().should_not_be_same(cache);
                };

                it["doesn't update cache when a component was replaced with same component"] = () => {
                    e.ReplaceComponentA(Component.A);
                    e.GetComponents().should_be_same(cache);
                };

                it["updates cache when all components were removed"] = () => {
                    e.RemoveAllComponents();
                    e.GetComponents().should_not_be_same(cache);
                };
            };

            context["component indices"] = () => {

                int[] cache = null;
                before = () => {
                    e.AddComponentA();
                    cache = e.GetComponentIndices();
                };

                it["caches component indices"] = () => {
                    e.GetComponentIndices().should_be_same(cache);
                };

                it["updates cache when a new component was added"] = () => {
                    e.AddComponentB();
                    e.GetComponentIndices().should_not_be_same(cache);
                };

                it["updates cache when a component was removed"] = () => {
                    e.RemoveComponentA();
                    e.GetComponentIndices().should_not_be_same(cache);
                };

                it["doesn't update cache when a component was replaced"] = () => {
                    e.ReplaceComponentA(new ComponentA());
                    e.GetComponentIndices().should_be_same(cache);
                };

                it["updates cache when adding a new component with ReplaceComponent"] = () => {
                    e.ReplaceComponentC(Component.C);
                    e.GetComponentIndices().should_not_be_same(cache);
                };

                it["updates cache when all components were removed"] = () => {
                    e.RemoveAllComponents();
                    e.GetComponentIndices().should_not_be_same(cache);
                };
            };

            context["ToString"] = () => {

                context["when component was added"] = () => {

                    string cache = null;
                    before = () => {
                        e.AddComponentA();
                        cache = e.ToString();
                    };

                    it["caches entity description"] = () => {
                        e.ToString().should_be_same(cache);
                    };

                    it["updates cache when a new component was added"] = () => {
                        e.AddComponentB();
                        e.ToString().should_not_be_same(cache);
                    };

                    it["updates cache when a component was removed"] = () => {
                        e.RemoveComponentA();
                        e.ToString().should_not_be_same(cache);
                    };

                    it["doesn't update cache when a component was replaced"] = () => {
                        e.ReplaceComponentA(new ComponentA());
                        e.ToString().should_be_same(cache);
                    };

                    it["updates cache when all components were removed"] = () => {
                        e.RemoveAllComponents();
                        e.ToString().should_not_be_same(cache);
                    };
                };

                it["updates cache when RemoveAllComponents is called, even if entity has no components"] = () => {
                    var str = e.ToString();
                    e.RemoveAllComponents();
                    e.ToString().should_not_be_same(str);
                };
            };
        };
    }
示例#12
0
    void when_created()
    {
        Pool pool = null;
        before = () => {
            pool = new Pool(CID.NumComponents);
        };

        it["increments creationIndex"] = () => {
            pool.CreateEntity().creationIndex.should_be(0);
            pool.CreateEntity().creationIndex.should_be(1);
        };

        it["starts with given creationIndex"] = () => {
            new Pool(CID.NumComponents, 42, null).CreateEntity().creationIndex.should_be(42);
        };

        it["has no entities when no entities were created"] = () => {
            pool.GetEntities().should_be_empty();
        };

        it["gets total entity count"] = () => {
            pool.count.should_be(0);
        };

        it["creates entity"] = () => {
            var e = pool.CreateEntity();
            e.should_not_be_null();
            e.GetType().should_be(typeof(Entity));
        };

        it["has default PoolMetaData"] = () => {
            pool.metaData.poolName.should_be("Unnamed Pool");
            pool.metaData.componentNames.Length.should_be(CID.NumComponents);
            for (int i = 0; i < pool.metaData.componentNames.Length; i++) {
                pool.metaData.componentNames[i].should_be("Index " + i);
            }
        };

        it["creates component pools"] = () => {
            pool.componentPools.should_not_be_null();
            pool.componentPools.Length.should_be(CID.NumComponents);
        };

        it["creates entity with component pools"] = () => {
            var e = pool.CreateEntity();
            e.componentPools.should_be_same(pool.componentPools);
        };

        it["throws when destroying an entity the pool doesn't contain"] = expect<PoolDoesNotContainEntityException>(() => {
            var e = pool.CreateEntity();
            pool.DestroyEntity(e);
            pool.DestroyEntity(e);
        });

        it["can ToString"] = () => {
            pool.ToString().should_be("Unnamed Pool");
        };

        context["when PoolMetaData set"] = () => {

            PoolMetaData metaData = null;
            before = () => {
                var componentNames = new [] { "Health", "Position", "View" };
                metaData = new PoolMetaData("My Pool", componentNames);
                pool = new Pool(componentNames.Length, 0, metaData);
            };

            it["has custom PoolMetaData"] = () => {
                pool.metaData.should_be_same(metaData);
            };

            it["creates entity with same PoolMetaData"] = () => {
                pool.CreateEntity().poolMetaData.should_be_same(metaData);
            };

            it["throws when componentNames is not same length as totalComponents"] = expect<PoolMetaDataException>(() => {
                new Pool(metaData.componentNames.Length + 1, 0, metaData);
            });
        };

        context["when entity created"] = () => {

            Entity e = null;
            before = () => {
                e = pool.CreateEntity();
                e.AddComponentA();
            };

            it["gets total entity count"] = () => {
                pool.count.should_be(1);
            };

            it["has entities that were created with CreateEntity()"] = () => {
                pool.HasEntity(e).should_be_true();
            };

            it["doesn't have entities that were not created with CreateEntity()"] = () => {
                pool.HasEntity(this.CreateEntity()).should_be_false();
            };

            it["returns all created entities"] = () => {
                var e2 = pool.CreateEntity();
                var entities = pool.GetEntities();
                entities.Length.should_be(2);
                entities.should_contain(e);
                entities.should_contain(e2);
            };

            it["destroys entity and removes it"] = () => {
                pool.DestroyEntity(e);
                pool.HasEntity(e).should_be_false();
                pool.count.should_be(0);
                pool.GetEntities().should_be_empty();
            };

            it["destroys an entity and removes all its components"] = () => {
                pool.DestroyEntity(e);
                e.GetComponents().should_be_empty();
            };

            it["destroys all entities"] = () => {
                pool.CreateEntity();
                pool.DestroyAllEntities();
                pool.HasEntity(e).should_be_false();
                pool.count.should_be(0);
                pool.GetEntities().should_be_empty();
                e.GetComponents().should_be_empty();
            };

            it["ensures same deterministic order when getting entities after destroying all entities"] = () => {

                // This is a Unity specific problem. Run Unity Test Tools with in the Entitas.Unity project

                const int numEntities = 10;
                for (int i = 0; i < numEntities; i++) {
                    pool.CreateEntity();
                }

                var order1 = new int[numEntities];
                var entities1 = pool.GetEntities();
                for (int i = 0; i < numEntities; i++) {
                    order1[i] = entities1[i].creationIndex;
                }

                pool.DestroyAllEntities();
                pool.ResetCreationIndex();

                for (int i = 0; i < numEntities; i++) {
                    pool.CreateEntity();
                }

                var order2 = new int[numEntities];
                var entities2 = pool.GetEntities();
                for (int i = 0; i < numEntities; i++) {
                    order2[i] = entities2[i].creationIndex;
                }

                for (int i = 0; i < numEntities; i++) {
                    var index1 = order1[i];
                    var index2 = order2[i];
                    index1.should_be(index2);
                }
            };

            it["throws when destroying all entities and there are still entities retained"] = expect<PoolStillHasRetainedEntitiesException>(() => {
                pool.CreateEntity().Retain(new object());
                pool.DestroyAllEntities();
            });
        };

        context["internal caching"] = () => {
            it["caches entities"] = () => {
                var entities = pool.GetEntities();
                pool.GetEntities().should_be_same(entities);
            };

            it["updates entities cache when creating an entity"] = () => {
                var entities = pool.GetEntities();
                pool.CreateEntity();
                pool.GetEntities().should_not_be_same(entities);
            };

            it["updates entities cache when destroying an entity"] = () => {
                var e = pool.CreateEntity();
                var entities = pool.GetEntities();
                pool.DestroyEntity(e);
                pool.GetEntities().should_not_be_same(entities);
            };
        };

        context["events"] = () => {

            var didDispatch = 0;
            before = () => {
                didDispatch = 0;
            };

            it["dispatches OnEntityCreated when creating a new entity"] = () => {
                Entity eventEntity = null;
                pool.OnEntityCreated += (p, entity) => {
                    didDispatch += 1;
                    eventEntity = entity;
                    p.should_be_same(p);
                };

                var e = pool.CreateEntity();
                didDispatch.should_be(1);
                eventEntity.should_be_same(e);
            };

            it["dispatches OnEntityWillBeDestroyed when destroying an entity"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                pool.OnEntityWillBeDestroyed += (p, entity) => {
                    didDispatch += 1;
                    p.should_be_same(pool);
                    entity.should_be_same(e);
                    entity.HasComponentA().should_be_true();
                    entity.IsEnabled().should_be_true();
                };
                pool.DestroyEntity(e);
                didDispatch.should_be(1);
            };

            it["dispatches OnEntityDestroyed when destroying an entity"] = () => {
                var e = pool.CreateEntity();
                pool.OnEntityDestroyed += (p, entity) => {
                    didDispatch += 1;
                    p.should_be_same(pool);
                    entity.should_be_same(e);
                    entity.HasComponentA().should_be_false();
                    entity.IsEnabled().should_be_false();
                };
                pool.DestroyEntity(e);
                didDispatch.should_be(1);
            };

            it["Entity is released after OnEntityDestroyed"] = () => {
                var e = pool.CreateEntity();
                pool.OnEntityDestroyed += (p, entity) => {
                    didDispatch += 1;
                    p.should_be_same(pool);
                    entity.should_be_same(e);
                    entity.retainCount.should_be(1);
                    var newEntity = pool.CreateEntity();
                    newEntity.should_not_be_null();
                    newEntity.should_not_be_same(entity);
                };
                pool.DestroyEntity(e);
                var reusedEntity = pool.CreateEntity();
                reusedEntity.should_be_same(e);
                didDispatch.should_be(1);
            };

            it["throws if entity is released before it is destroyed"] = expect<EntityIsNotDestroyedException>(() => {
                var e = pool.CreateEntity();
                e.Release(pool);
            });

            it["dispatches OnGroupCreated when creating a new group"] = () => {
                Group eventGroup = null;
                pool.OnGroupCreated += (p, g) => {
                    didDispatch += 1;
                    p.should_be_same(pool);
                    eventGroup = g;
                };
                var group = pool.GetGroup(Matcher.AllOf(0));
                didDispatch.should_be(1);
                eventGroup.should_be_same(group);
            };

            it["doesn't dispatch OnGroupCreated when group alredy exists"] = () => {
                pool.GetGroup(Matcher.AllOf(0));
                pool.OnGroupCreated += (p, g) => this.Fail();
                pool.GetGroup(Matcher.AllOf(0));
            };

            it["dispatches OnGroupCleared when clearing groups"] = () => {
                Group eventGroup = null;
                pool.OnGroupCleared += (p, g) => {
                    didDispatch += 1;
                    p.should_be_same(pool);
                    eventGroup = g;
                };
                pool.GetGroup(Matcher.AllOf(0));
                var group2 = pool.GetGroup(Matcher.AllOf(1));
                pool.ClearGroups();

                didDispatch.should_be(2);
                eventGroup.should_be_same(group2);
            };

            it["removes all external delegates when destroying an entity"] = () => {
                var e = pool.CreateEntity();
                e.OnComponentAdded += (entity, index, component) => this.Fail();
                e.OnComponentRemoved += (entity, index, component) => this.Fail();
                e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail();
                pool.DestroyEntity(e);
                var e2 = pool.CreateEntity();
                e2.should_be_same(e);
                e2.AddComponentA();
                e2.ReplaceComponentA(Component.A);
                e2.RemoveComponentA();
            };
        };

        context["entity pool"] = () => {

            it["gets entity from object pool"] = () => {
                var e = pool.CreateEntity();
                e.should_not_be_null();
                e.GetType().should_be(typeof(Entity));
            };

            it["destroys entity when pushing back to object pool"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                pool.DestroyEntity(e);
                e.HasComponent(CID.ComponentA).should_be_false();
            };

            it["returns pushed entity"] = () => {
                var e = pool.CreateEntity();
                e.AddComponentA();
                pool.DestroyEntity(e);
                var entity = pool.CreateEntity();
                entity.HasComponent(CID.ComponentA).should_be_false();
                entity.should_be_same(e);
            };

            it["only returns released entities"] = () => {
                var e1 = pool.CreateEntity();
                e1.Retain(this);
                pool.DestroyEntity(e1);
                var e2 = pool.CreateEntity();
                e2.should_not_be_same(e1);
                e1.Release(this);
                var e3 = pool.CreateEntity();
                e3.should_be_same(e1);
            };

            it["returns new entity"] = () => {
                var e1 = pool.CreateEntity();
                e1.AddComponentA();
                pool.DestroyEntity(e1);
                pool.CreateEntity();
                var e2 = pool.CreateEntity();
                e2.HasComponent(CID.ComponentA).should_be_false();
                e2.should_not_be_same(e1);
            };

            it["sets up entity from pool"] = () => {
                pool.DestroyEntity(pool.CreateEntity());
                var g = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA }));
                var e = pool.CreateEntity();
                e.AddComponentA();
                g.GetEntities().should_contain(e);
            };

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

                it["throws when adding component"] = expect<EntityIsNotEnabledException>(() => e.AddComponentA());
                it["throws when removing component"] = expect<EntityIsNotEnabledException>(() => e.RemoveComponentA());
                it["throws when replacing component"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(new ComponentA()));
                it["throws when replacing component with null"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(null));
            };
        };

        context["groups"] = () => {

            it["gets empty group for matcher when no entities were created"] = () => {
                var g = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA }));
                g.should_not_be_null();
                g.GetEntities().should_be_empty();
            };

            context["when entities created"] = () => {
                Entity eAB1 = null;
                Entity eAB2 = null;
                Entity eA = null;

                IMatcher matcherAB = Matcher.AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB
                });

                before = () => {
                    eAB1 = pool.CreateEntity();
                    eAB1.AddComponentA();
                    eAB1.AddComponentB();
                    eAB2 = pool.CreateEntity();
                    eAB2.AddComponentA();
                    eAB2.AddComponentB();
                    eA = pool.CreateEntity();
                    eA.AddComponentA();
                };

                it["gets group with matching entities"] = () => {
                    var g = pool.GetGroup(matcherAB).GetEntities();
                    g.Length.should_be(2);
                    g.should_contain(eAB1);
                    g.should_contain(eAB2);
                };

                it["gets cached group"] = () => {
                    pool.GetGroup(matcherAB).should_be_same(pool.GetGroup(matcherAB));
                };

                it["cached group contains newly created matching entity"] = () => {
                    var g = pool.GetGroup(matcherAB);
                    eA.AddComponentB();
                    g.GetEntities().should_contain(eA);
                };

                it["cached group doesn't contain entity which are not matching anymore"] = () => {
                    var g = pool.GetGroup(matcherAB);
                    eAB1.RemoveComponentA();
                    g.GetEntities().should_not_contain(eAB1);
                };

                it["removes destroyed entity"] = () => {
                    var g = pool.GetGroup(matcherAB);
                    pool.DestroyEntity(eAB1);
                    g.GetEntities().should_not_contain(eAB1);
                };

                it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => {
                    var g = pool.GetGroup(matcherAB);
                    var didDispatchRemoved = 0;
                    var didDispatchAdded = 0;
                    var componentA = new ComponentA();
                    g.OnEntityRemoved += (group, entity, index, component) => {
                        group.should_be_same(g);
                        entity.should_be_same(eAB1);
                        index.should_be(CID.ComponentA);
                        component.should_be_same(Component.A);
                        didDispatchRemoved++;
                    };
                    g.OnEntityAdded += (group, entity, index, component) => {
                        group.should_be_same(g);
                        entity.should_be_same(eAB1);
                        index.should_be(CID.ComponentA);
                        component.should_be_same(componentA);
                        didDispatchAdded++;
                    };
                    eAB1.ReplaceComponentA(componentA);

                    didDispatchRemoved.should_be(1);
                    didDispatchAdded.should_be(1);
                };

                it["group dispatches OnEntityUpdated with previous and current component when replacing a component"] = () => {
                    var updated = 0;
                    var prevComp = eA.GetComponent(CID.ComponentA);
                    var newComp = new ComponentA();
                    var g = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA }));
                    g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => {
                        updated += 1;
                        group.should_be_same(g);
                        entity.should_be_same(eA);
                        index.should_be(CID.ComponentA);
                        previousComponent.should_be_same(prevComp);
                        newComponent.should_be_same(newComp);
                    };

                    eA.ReplaceComponent(CID.ComponentA, newComp);

                    updated.should_be(1);
                };

                context["event timing"] = () => {

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

                    it["dispatches group.OnEntityAdded events after all groups are updated"] = () => {
                        var groupA = pool.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB));
                        var groupB = pool.GetGroup(Matcher.AllOf(CID.ComponentB));

                        groupA.OnEntityAdded += delegate {
                            groupB.count.should_be(1);
                        };

                        var entity = pool.CreateEntity();
                        entity.AddComponentA();
                        entity.AddComponentB();
                    };

                    it["dispatches group.OnEntityRemoved events after all groups are updated"] = () => {
                        pool = new Pool(CID.NumComponents);
                        var groupB = pool.GetGroup(Matcher.AllOf(CID.ComponentB));
                        var groupA = pool.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB));

                        groupB.OnEntityRemoved += delegate {
                            groupA.count.should_be(0);
                        };

                        var entity = pool.CreateEntity();
                        entity.AddComponentA();
                        entity.AddComponentB();

                        entity.RemoveComponentB();
                    };
                };
            };
        };

        context["reset"] = () => {

            context["groups"] = () => {

                it["resets and removes groups from pool"] = () => {
                    var m = Matcher.AllOf(CID.ComponentA);
                    var groupsCreated = 0;
                    Group createdGroup = null;
                    pool.OnGroupCreated += (p, g) => {
                        groupsCreated += 1;
                        createdGroup = g;
                    };

                    var initialGroup = pool.GetGroup(m);

                    pool.ClearGroups();

                    pool.GetGroup(m);

                    pool.CreateEntity().AddComponentA();

                    groupsCreated.should_be(2);
                    createdGroup.should_not_be_same(initialGroup);

                    initialGroup.count.should_be(0);
                    createdGroup.count.should_be(1);
                };

                it["removes all event handlers from groups"] = () => {
                    var m = Matcher.AllOf(CID.ComponentA);
                    var group = pool.GetGroup(m);

                    group.OnEntityAdded += (g, entity, index, component) => this.Fail();

                    pool.ClearGroups();

                    var e = pool.CreateEntity();
                    e.AddComponentA();
                    group.HandleEntity(e, CID.ComponentA, Component.A);
                };

                it["releases entities in groups"] = () => {
                    var m = Matcher.AllOf(CID.ComponentA);
                    pool.GetGroup(m);
                    var entity = pool.CreateEntity();
                    entity.AddComponentA();

                    pool.ClearGroups();

                    entity.retainCount.should_be(1);
                };
            };

            context["pool"] = () => {

                it["resets creation index"] = () => {
                    pool.CreateEntity();

                    pool.ResetCreationIndex();

                    pool.CreateEntity().creationIndex.should_be(0);
                };
            };

            context["component pools"] = () => {

                before = () => {
                    var entity = pool.CreateEntity();
                    entity.AddComponentA();
                    entity.AddComponentB();
                    entity.RemoveComponentA();
                    entity.RemoveComponentB();
                };

                it["clears all component pools"] = () => {
                    pool.componentPools[CID.ComponentA].Count.should_be(1);
                    pool.componentPools[CID.ComponentB].Count.should_be(1);

                    pool.ClearComponentPools();

                    pool.componentPools[CID.ComponentA].Count.should_be(0);
                    pool.componentPools[CID.ComponentB].Count.should_be(0);
                };

                it["clears a specific component pool"] = () => {
                    pool.ClearComponentPool(CID.ComponentB);

                    pool.componentPools[CID.ComponentA].Count.should_be(1);
                    pool.componentPools[CID.ComponentB].Count.should_be(0);
                };

                it["only clears existing component pool"] = () => {
                    pool.ClearComponentPool(CID.ComponentC);
                };
            };
        };
    }