コード例 #1
0
        public void TestEntityAndChildren()
        {
            var registry      = new ServiceRegistry();
            var entityManager = new CustomEntityManager(registry);

            // Entity with a sub-Entity
            var childEntity0 = new Entity();
            var entity       = new Entity();

            entity.AddChild(childEntity0);

            // ================================================================
            // 1) Add entity with sub-entity and check EntityManager and TransformProcessor
            // ================================================================
            entityManager.Add(entity);
            var transformProcessor = entityManager.GetProcessor <TransformProcessor>();

            Assert.NotNull(transformProcessor);

            Assert.AreEqual(2, entityManager.Count);
            Assert.AreEqual(1, transformProcessor.TransformationRoots.Count);
            Assert.True(transformProcessor.TransformationRoots.Contains(entity.Transform));

            // ================================================================
            // 2) Remove child from entity while the Entity is still in the EntityManager
            // ================================================================
            entity.Transform.Children.RemoveAt(0);

            Assert.AreEqual(1, entityManager.Count);
            Assert.AreEqual(1, transformProcessor.TransformationRoots.Count);
            Assert.True(transformProcessor.TransformationRoots.Contains(entity.Transform));

            // ================================================================
            // 3) Add a child to the root entity while the Entity is still in the EntityManager
            // ================================================================
            var childEntity = new Entity();

            entity.AddChild(childEntity);

            Assert.AreEqual(2, entityManager.Count);
            Assert.AreEqual(1, transformProcessor.TransformationRoots.Count);
            Assert.True(transformProcessor.TransformationRoots.Contains(entity.Transform));

            // ================================================================
            // 3) Remove top level entity
            // ================================================================
            entityManager.Remove(entity);

            Assert.AreEqual(0, entityManager.Count);
            Assert.AreEqual(0, transformProcessor.TransformationRoots.Count);
        }
コード例 #2
0
        public void TestDefaultProcessors()
        {
            var registry      = new ServiceRegistry();
            var entityManager = new CustomEntityManager(registry);

            // Create events collector to check fired events on EntityManager
            var componentTypes = new List <Type>();
            var entityAdded    = new List <Entity>();
            var entityRemoved  = new List <Entity>();

            entityManager.ComponentTypeAdded += (sender, type) => componentTypes.Add(type);
            entityManager.EntityAdded        += (sender, entity1) => entityAdded.Add(entity1);
            entityManager.EntityRemoved      += (sender, entity1) => entityRemoved.Add(entity1);

            // No processors registered by default
            Assert.AreEqual(0, entityManager.Processors.Count);

            // ================================================================
            // 1) Add an entity with the default TransformComponent to the Entity Manager
            // ================================================================

            var entity = new Entity();

            entityManager.Add(entity);

            // Check types are correctly registered
            Assert.AreEqual(1, componentTypes.Count);
            Assert.AreEqual(typeof(TransformComponent), componentTypes[0]);

            // Check entity correctly added
            Assert.AreEqual(1, entityManager.Count);
            Assert.True(entityManager.Contains(entity));
            Assert.AreEqual(1, entityAdded.Count);
            Assert.AreEqual(entity, entityAdded[0]);
            Assert.AreEqual(0, entityRemoved.Count);

            // We should have 1 processor
            Assert.AreEqual(1, entityManager.Processors.Count);

            var transformProcessor = entityManager.Processors[0] as TransformProcessor;

            Assert.NotNull(transformProcessor);

            Assert.AreEqual(1, transformProcessor.TransformationRoots.Count);
            // TODO: Check the root entity.Transform

            // Check internal mapping of component types => EntityProcessor
            Assert.AreEqual(1, entityManager.MapComponentTypeToProcessors.Count);
            Assert.True(entityManager.MapComponentTypeToProcessors.ContainsKey(typeof(TransformComponent).GetTypeInfo()));

            var processorListForTransformComponentType = entityManager.MapComponentTypeToProcessors[typeof(TransformComponent).GetTypeInfo()];

            Assert.AreEqual(1, processorListForTransformComponentType.Count);
            Assert.True(processorListForTransformComponentType[0] is TransformProcessor);

            // clear events collector
            componentTypes.Clear();
            entityAdded.Clear();
            entityRemoved.Clear();

            // ================================================================
            // 2) Add another empty entity
            // ================================================================

            var newEntity = new Entity();

            entityManager.Add(newEntity);

            // We should not have new component types registered
            Assert.AreEqual(0, componentTypes.Count);

            // Check entity correctly added
            Assert.AreEqual(2, entityManager.Count);
            Assert.True(entityManager.Contains(newEntity));
            Assert.AreEqual(1, entityAdded.Count);
            Assert.AreEqual(newEntity, entityAdded[0]);
            Assert.AreEqual(0, entityRemoved.Count);

            // We should still have 2 processors
            Assert.AreEqual(1, entityManager.Processors.Count);
            Assert.AreEqual(2, transformProcessor.TransformationRoots.Count);

            componentTypes.Clear();
            entityAdded.Clear();
            entityRemoved.Clear();

            // ================================================================
            // 3) Remove previous entity
            // ================================================================

            entityManager.Remove(newEntity);

            // Check entity correctly removed
            Assert.AreEqual(1, entityManager.Count);
            Assert.False(entityManager.Contains(newEntity));
            Assert.AreEqual(0, entityAdded.Count);
            Assert.AreEqual(1, entityRemoved.Count);
            Assert.AreEqual(newEntity, entityRemoved[0]);

            Assert.AreEqual(1, transformProcessor.TransformationRoots.Count);

            componentTypes.Clear();
            entityAdded.Clear();
            entityRemoved.Clear();
        }