예제 #1
0
        public void TestReset()
        {
            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) Reset the manager
            // ================================================================
            entityManager.Reset();

            Assert.Empty(entityManager);
            Assert.Empty(entityManager.MapComponentTypeToProcessors);
            Assert.Empty(entityManager.Processors);
            Assert.Empty(transformProcessor.TransformationRoots);
            Assert.Null(transformProcessor.EntityManager);
        }
예제 #2
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);
        }
예제 #3
0
        public void TestProcessorWithRequiredTypes()
        {
            var registry      = new ServiceRegistry();
            var entityManager = new CustomEntityManager(registry);

            var events = new List <CustomEntityComponentEventArgs>();
            var entity = new Entity()
            {
                new CustomEntityComponentWithDependency()
                {
                    Changed = evt => events.Add(evt)
                }
            };
            var customComponent = entity.Get <CustomEntityComponentWithDependency>();

            // ================================================================
            // 1) Add entity, check that processors and required processors are correctly in EntityManager
            // ================================================================

            entityManager.Add(entity);

            // Check internal processors
            Assert.Equal(2, entityManager.MapComponentTypeToProcessors.Count);
            Assert.True(entityManager.MapComponentTypeToProcessors.ContainsKey(typeof(TransformComponent).GetTypeInfo()));
            Assert.True(entityManager.MapComponentTypeToProcessors.ContainsKey(typeof(CustomEntityComponentWithDependency).GetTypeInfo()));

            var customProcessor = entityManager.GetProcessor <CustomEntityComponentProcessorWithDependency>();

            // Because the custom processor has a dependency on TransformComponent, we are checking that the dependencies is correctly registered back in the
            // list of processors for TransformComponent that should have a link to the custom processor
            var processorsForTransform = entityManager.MapComponentTypeToProcessors[typeof(TransformComponent).GetTypeInfo()];

            // there is the HierarchicalProcessor and TransformProcessor
            Assert.Single(processorsForTransform);
            Assert.NotNull(processorsForTransform.Dependencies);
            Assert.Single(processorsForTransform.Dependencies);
            Assert.Equal(customProcessor, processorsForTransform.Dependencies[0]);

            // Check that the custom processor is empty
            var processorsForCustom = entityManager.MapComponentTypeToProcessors[typeof(CustomEntityComponentWithDependency).GetTypeInfo()];

            Assert.Single(processorsForCustom);
            Assert.Null(processorsForCustom.Dependencies);

            // ================================================================
            // 2) Override the TransformComponent with a new TransformComponent and check that required Processor are called and updated
            // ================================================================

            var previousTransform = entity.Transform;
            var newTransform      = new TransformComponent();

            entity.Components[0] = newTransform;

            // If the entity manager is working property, because the TransformComponent is updated, all processor depending on it
            // will be called on the entity
            // We are checking here that the new transform is correctly copied to the custom component by the custom processor.
            Assert.Equal(newTransform, customComponent.Link);

            // ================================================================
            // 3) Remove TransformComponent
            // ================================================================

            entity.Components.RemoveAt(0);

            // The link is not updated, but it is ok, as it is an associated data that is no longer part of the processor
            Assert.Null(customComponent.Link);
            Assert.Empty(customProcessor.CurrentComponentDatas);
        }
예제 #4
0
        public void TestMultipleComponents()
        {
            var registry      = new ServiceRegistry();
            var entityManager = new CustomEntityManager(registry);

            var events = new List <CustomEntityComponentEventArgs>();

            var entity = new Entity
            {
                new CustomEntityComponent()
                {
                    Changed = e => events.Add(e)
                }
            };
            var customComponent = entity.Get <CustomEntityComponent>();

            // ================================================================
            // 1) Add an entity with a component to the Entity Manager
            // ================================================================

            // Add component
            entityManager.Add(entity);

            // Check that component was correctly processed when first adding the entity
            Assert.Single(entityManager);
            Assert.Equal(2, entityManager.Processors.Count);

            // Verify that the processor has correctly registered the component
            var customProcessor = entityManager.GetProcessor <CustomEntityComponentProcessor>();

            Assert.NotNull(customProcessor);

            Assert.Single(customProcessor.CurrentComponentDatas);
            Assert.True(customProcessor.CurrentComponentDatas.ContainsKey(customComponent));

            // Verify that events are correctly propagated
            var expectedEvents = new List <CustomEntityComponentEventArgs>()
            {
                new CustomEntityComponentEventArgs(CustomEntityComponentEventType.GenerateComponentData, entity, customComponent),
                new CustomEntityComponentEventArgs(CustomEntityComponentEventType.EntityComponentAdding, entity, customComponent),
            };

            Assert.Equal(expectedEvents, events);
            events.Clear();

            // ================================================================
            // 2) Add a component to the entity that is already handled by the Entity Manager
            // ================================================================

            // Check that component is correctly processed when adding it after the entity is already into the EntityManager
            var customComponent2 = new CustomEntityComponent()
            {
                Changed = e => events.Add(e)
            };

            entity.Components.Add(customComponent2);

            // Verify that the processor has correctly registered the component
            Assert.Equal(2, customProcessor.CurrentComponentDatas.Count);
            Assert.True(customProcessor.CurrentComponentDatas.ContainsKey(customComponent2));

            expectedEvents = new List <CustomEntityComponentEventArgs>()
            {
                new CustomEntityComponentEventArgs(CustomEntityComponentEventType.GenerateComponentData, entity, customComponent2),
                new CustomEntityComponentEventArgs(CustomEntityComponentEventType.EntityComponentAdding, entity, customComponent2),
            };
            Assert.Equal(expectedEvents, events);
            events.Clear();

            // ================================================================
            // 3) Remove the 1st CustomEntityComponent from the entity
            // ================================================================

            // Test remove first component
            entity.Components.Remove(customComponent);

            // Verify that the processor has correctly removed the component
            Assert.Single(customProcessor.CurrentComponentDatas);
            Assert.False(customProcessor.CurrentComponentDatas.ContainsKey(customComponent));

            Assert.Null(customComponent.Entity);
            expectedEvents = new List <CustomEntityComponentEventArgs>()
            {
                new CustomEntityComponentEventArgs(CustomEntityComponentEventType.EntityComponentRemoved, entity, customComponent),
            };
            Assert.Equal(expectedEvents, events);
            events.Clear();

            // ================================================================
            // 4) Remove the 2nd CustomEntityComponent from the entity
            // ================================================================

            // Test remove second component
            entity.Components.Remove(customComponent2);

            // Verify that the processor has correctly removed the component
            Assert.Empty(customProcessor.CurrentComponentDatas);
            Assert.False(customProcessor.CurrentComponentDatas.ContainsKey(customComponent2));

            Assert.Null(customComponent2.Entity);
            expectedEvents = new List <CustomEntityComponentEventArgs>()
            {
                new CustomEntityComponentEventArgs(CustomEntityComponentEventType.EntityComponentRemoved, entity, customComponent2),
            };
            Assert.Equal(expectedEvents, events);
            events.Clear();

            // The processor is still registered but is not running on any component
            Assert.Equal(2, entityManager.Processors.Count);
            Assert.NotNull(entityManager.GetProcessor <CustomEntityComponentProcessor>());
        }