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.AreEqual(2, entityManager.Count); Assert.AreEqual(1, transformProcessor.TransformationRoots.Count); Assert.True(transformProcessor.TransformationRoots.Contains(entity.Transform)); // ================================================================ // 2) Reset the manager // ================================================================ entityManager.Reset(); Assert.AreEqual(0, entityManager.Count); Assert.AreEqual(0, entityManager.MapComponentTypeToProcessors.Count); Assert.AreEqual(0, entityManager.Processors.Count); Assert.AreEqual(0, transformProcessor.TransformationRoots.Count); Assert.Null(transformProcessor.EntityManager); Assert.Null(entity.Owner); }
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); }
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.AreEqual(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.AreEqual(1, processorsForTransform.Count); Assert.NotNull(processorsForTransform.Dependencies); Assert.AreEqual(1, processorsForTransform.Dependencies.Count); Assert.AreEqual(customProcessor, processorsForTransform.Dependencies[0]); // Check that the custom processor is empty var processorsForCustom = entityManager.MapComponentTypeToProcessors[typeof(CustomEntityComponentWithDependency).GetTypeInfo()]; Assert.AreEqual(1, processorsForCustom.Count); 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.AreEqual(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.AreEqual(null, customComponent.Link); Assert.AreEqual(0, customProcessor.CurrentComponentDatas.Count); }
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.AreEqual(1, entityManager.Count); Assert.AreEqual(2, entityManager.Processors.Count); // Verify that the processor has correctly registered the component var customProcessor = entityManager.GetProcessor <CustomEntityComponentProcessor>(); Assert.NotNull(customProcessor); Assert.AreEqual(1, customProcessor.CurrentComponentDatas.Count); 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.AreEqual(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.AreEqual(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.AreEqual(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.AreEqual(1, customProcessor.CurrentComponentDatas.Count); Assert.False(customProcessor.CurrentComponentDatas.ContainsKey(customComponent)); Assert.AreEqual(null, customComponent.Entity); expectedEvents = new List <CustomEntityComponentEventArgs>() { new CustomEntityComponentEventArgs(CustomEntityComponentEventType.EntityComponentRemoved, entity, customComponent), }; Assert.AreEqual(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.AreEqual(0, customProcessor.CurrentComponentDatas.Count); Assert.False(customProcessor.CurrentComponentDatas.ContainsKey(customComponent2)); Assert.AreEqual(null, customComponent2.Entity); expectedEvents = new List <CustomEntityComponentEventArgs>() { new CustomEntityComponentEventArgs(CustomEntityComponentEventType.EntityComponentRemoved, entity, customComponent2), }; Assert.AreEqual(expectedEvents, events); events.Clear(); // The processor is still registered but is not running on any component Assert.AreEqual(2, entityManager.Processors.Count); Assert.NotNull(entityManager.GetProcessor <CustomEntityComponentProcessor>()); }