Esempio n. 1
0
        public void TestMultipleComponents()
        {
            // Check that TransformComponent cannot be added multiple times
            Assert.False(EntityComponentAttributes.Get <TransformComponent>().AllowMultipleComponents);

            // Check that CustomEntityComponent can be added multiple times
            Assert.True(EntityComponentAttributes.Get <CustomEntityComponent>().AllowMultipleComponents);

            // Check that DerivedEntityComponentBase can be added multiple times
            Assert.True(EntityComponentAttributes.Get <DerivedEntityComponent>().AllowMultipleComponents);

            var entity = new Entity();

            var transform = entity.Get <TransformComponent>();

            Assert.NotNull(transform);
            Assert.Equal(entity.Transform, transform);

            var custom = entity.GetOrCreate <CustomEntityComponent>();

            Assert.NotNull(custom);

            var custom2 = new CustomEntityComponent();

            entity.Components.Add(custom2);
            Assert.Equal(custom, entity.Get <CustomEntityComponent>());

            var allComponents = entity.GetAll <CustomEntityComponent>().ToList();

            Assert.Equal(new List <EntityComponent>()
            {
                custom, custom2
            }, allComponents);
        }
Esempio n. 2
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>());
        }
Esempio n. 3
0
        public void TestComponents()
        {
            var entity = new Entity();

            // Plug an event handler to track events
            var events = new List <EntityComponentEvent>();

            entity.EntityManager = new DelegateEntityComponentNotify(evt => events.Add(evt));

            // Make sure that an entity has a transform component
            Assert.NotNull(entity.Transform);
            Assert.Single(entity.Components);
            Assert.Equal(entity.Transform, entity.Components[0]);

            // Remove Transform
            var oldTransform = entity.Transform;

            entity.Components.RemoveAt(0);
            Assert.Null(entity.Transform);

            // Check that events is correctly propagated
            Assert.Equal(new List <EntityComponentEvent>()
            {
                new EntityComponentEvent(entity, 0, oldTransform, null)
            }, events);
            events.Clear();

            // Re-add transform
            var transform = new TransformComponent();

            entity.Components.Add(transform);
            Assert.NotNull(entity.Transform);

            // Check that events is correctly propagated
            Assert.Equal(new List <EntityComponentEvent>()
            {
                new EntityComponentEvent(entity, 0, null, transform)
            }, events);
            events.Clear();

            // We cannot add a single component
            var invalidOpException = Assert.Throws <InvalidOperationException>(() => entity.Components.Add(new TransformComponent()));

            Assert.Equal($"Cannot add a component of type [{typeof(TransformComponent)}] multiple times", invalidOpException.Message);

            invalidOpException = Assert.Throws <InvalidOperationException>(() => entity.Components.Add(transform));
            Assert.Equal("Cannot add a same component multiple times. Already set at index [0]", invalidOpException.Message);

            // We cannot add a null component
            Assert.Throws <ArgumentNullException>(() => entity.Components.Add(null));

            // Replace Transform
            var custom = new CustomEntityComponent();

            entity.Components[0] = custom;
            Assert.Null(entity.Transform);

            // Check that events is correctly propagated
            Assert.Equal(new List <EntityComponentEvent>()
            {
                new EntityComponentEvent(entity, 0, transform, custom)
            }, events);
            events.Clear();

            // Add again transform component
            transform = new TransformComponent();
            entity.Components.Add(transform);
            Assert.NotNull(entity.Transform);
            Assert.Equal(transform, entity.Components[1]);

            // Check that TransformComponent is on index 1 now
            Assert.Equal(new List <EntityComponentEvent>()
            {
                new EntityComponentEvent(entity, 1, null, transform)
            }, events);
            events.Clear();

            // Clear components and check that Transform is also removed
            entity.Components.Clear();
            Assert.Empty(entity.Components);
            Assert.Null(entity.Transform);

            // Check that events is correctly propagated
            Assert.Equal(new List <EntityComponentEvent>()
            {
                new EntityComponentEvent(entity, 1, transform, null),
                new EntityComponentEvent(entity, 0, custom, null),
            }, events);
            events.Clear();
        }