Beispiel #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.Equal(2, entityManager.Count);
            Assert.Single(transformProcessor.TransformationRoots);
            Assert.Contains(entity.Transform, transformProcessor.TransformationRoots);

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

            Assert.Single(entityManager);
            Assert.Single(transformProcessor.TransformationRoots);
            Assert.Contains(entity.Transform, transformProcessor.TransformationRoots);

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

            entity.AddChild(childEntity);

            Assert.Equal(2, entityManager.Count);
            Assert.Single(transformProcessor.TransformationRoots);
            Assert.Contains(entity.Transform, transformProcessor.TransformationRoots);

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

            Assert.Empty(entityManager);
            Assert.Empty(transformProcessor.TransformationRoots);
        }
Beispiel #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.Empty(entityManager.Processors);

            // ================================================================
            // 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.Single(componentTypes);
            Assert.Equal(typeof(TransformComponent), componentTypes[0]);

            // Check entity correctly added
            Assert.Single(entityManager);
            Assert.True(entityManager.Contains(entity));
            Assert.Single(entityAdded);
            Assert.Equal(entity, entityAdded[0]);
            Assert.Empty(entityRemoved);

            // We should have 1 processor
            Assert.Single(entityManager.Processors);

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

            Assert.NotNull(transformProcessor);

            Assert.Single(transformProcessor.TransformationRoots);
            // TODO: Check the root entity.Transform

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

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

            Assert.Single(processorListForTransformComponentType);
            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.Empty(componentTypes);

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

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

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

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

            entityManager.Remove(newEntity);

            // Check entity correctly removed
            Assert.Single(entityManager);
            Assert.False(entityManager.Contains(newEntity));
            Assert.Empty(entityAdded);
            Assert.Single(entityRemoved);
            Assert.Equal(newEntity, entityRemoved[0]);

            Assert.Single(transformProcessor.TransformationRoots);

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