diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 4c7cc60..ea6d841 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -79,6 +79,11 @@ namespace Encompass return id; } + internal void RegisterDrawableComponent(Guid componentID, TComponent component) where TComponent : IDrawableComponent + { + drawLayerManager.RegisterComponentWithLayer(componentID, component.Layer); + } + internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component) { IDToComponent[componentID] = component; @@ -104,7 +109,7 @@ namespace Encompass { var (entity, type) = keyValuePair.Key; var (componentID, component) = keyValuePair.Value; - + if (!componentIDsMarkedForWrite.Contains(componentID) || !entityIDToComponentTypeToComponentID.ContainsKey(entity.ID)) { continue; } if (entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type)) @@ -222,7 +227,7 @@ namespace Encompass { componentIDsMarkedForWrite.Remove(componentID); } - + if (IDToComponent.ContainsKey(componentID)) { Remove(componentID); diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 0cc8d45..ae7f256 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -503,43 +503,10 @@ namespace Encompass newComponentMessage.priority = priority; SendPendingComponentMessage(newComponentMessage); } - } - /// - /// Overwrites Component struct data associated with the specified Component ID. - /// - internal void SetComponent(Guid componentID, TComponent component) where TComponent : struct, IComponent - { - SetComponent(GetEntityByComponentID(componentID), component); - } - - /// - /// Sets Draw Component data for the specified Component Type on the specified Entity. - /// This method must be used for the Draw Component to be readable by an OrderedRenderer. - /// If Component data for this Type already existed on the Entity, the component data is overwritten. - /// - /// - /// Thrown when the Engine does not declare that it Writes the given Component Type. - /// - protected void SetDrawComponent(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent, IDrawComponent - { - var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : 0; - - var componentID = componentManager.MarkDrawComponentForWrite(entity, component, priority, layer); - - if (!sendTypes.Contains(typeof(ComponentWriteMessage))) + if (component is IDrawableComponent drawableComponent) { - throw new IllegalWriteException("Engine {0} tried to write undeclared Component {1}", GetType().Name, typeof(TComponent).Name); - } - - if (sendTypes.Contains(typeof(PendingComponentMessage))) - { - PendingComponentMessage newComponentMessage; - newComponentMessage.entity = entity; - newComponentMessage.componentID = componentID; - newComponentMessage.component = component; - newComponentMessage.priority = priority; - SendPendingComponentMessage(newComponentMessage); + componentManager.RegisterDrawableComponent(componentID, drawableComponent); } } diff --git a/encompass-cs/IDrawComponent.cs b/encompass-cs/IDrawComponent.cs deleted file mode 100644 index b77bf94..0000000 --- a/encompass-cs/IDrawComponent.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Encompass -{ - /// - /// Structs that implement IDrawComponent are considered to be DrawComponents. - /// - public interface IDrawComponent { } -} diff --git a/encompass-cs/Interfaces/IDrawableComponent.cs b/encompass-cs/Interfaces/IDrawableComponent.cs new file mode 100644 index 0000000..e292d90 --- /dev/null +++ b/encompass-cs/Interfaces/IDrawableComponent.cs @@ -0,0 +1,7 @@ +namespace Encompass +{ + public interface IDrawableComponent + { + int Layer { get; set; } + } +} \ No newline at end of file diff --git a/encompass-cs/Renderers/OrderedRenderer.cs b/encompass-cs/Renderers/OrderedRenderer.cs index 12f650f..20ca636 100644 --- a/encompass-cs/Renderers/OrderedRenderer.cs +++ b/encompass-cs/Renderers/OrderedRenderer.cs @@ -5,7 +5,7 @@ namespace Encompass /// /// OrdereredRenderer provides a structure for the common pattern of wishing to draw a specific DrawComponent at a specific layer. /// - public abstract class OrderedRenderer : Renderer where TComponent : struct, IComponent, IDrawComponent + public abstract class OrderedRenderer : Renderer where TComponent : struct, IComponent, IDrawableComponent { public abstract void Render(Entity entity, TComponent drawComponent); diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index 97433f0..b214475 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -74,16 +74,11 @@ namespace Encompass /// public void SetComponent(Entity entity, TComponent component, int priority = 0) where TComponent : struct, IComponent { - componentManager.MarkComponentForWrite(entity, component, priority); - } - - /// - /// Sets Draw Component data for the specified Component Type on the specified Entity. - /// This method must be used for the Draw Component to be readable by an OrderedRenderer. - /// - public void SetDrawComponent(Entity entity, TComponent component, int priority = 0, int layer = 0) where TComponent : struct, IComponent, IDrawComponent - { - componentManager.MarkDrawComponentForWrite(entity, component, priority, layer); + var componentID = componentManager.MarkComponentForWrite(entity, component, priority); + if (component is IDrawableComponent drawableComponent) + { + componentManager.RegisterDrawableComponent(componentID, drawableComponent); + } } internal void RegisterComponent(Type componentType) @@ -159,7 +154,7 @@ namespace Encompass /// /// Adds the specified OrderedRenderer to the World. /// - public OrderedRenderer AddOrderedRenderer(OrderedRenderer renderer) where TComponent : struct, IComponent, IDrawComponent + public OrderedRenderer AddOrderedRenderer(OrderedRenderer renderer) where TComponent : struct, IComponent, IDrawableComponent { renderer.AssignEntityManager(entityManager); renderer.AssignComponentManager(componentManager); diff --git a/test/OrderedRendererTest.cs b/test/OrderedRendererTest.cs index c113988..4dfa315 100644 --- a/test/OrderedRendererTest.cs +++ b/test/OrderedRendererTest.cs @@ -13,7 +13,10 @@ namespace Tests struct BComponent : IComponent { } struct CComponent : IComponent { } - struct TestDrawComponent : IComponent, IDrawComponent { } + struct TestDrawComponent : IComponent, IDrawableComponent + { + public int Layer { get; set; } + } class TestRenderer : OrderedRenderer { @@ -49,12 +52,13 @@ namespace Tests AComponent aComponent; CComponent cComponent; - TestDrawComponent testDrawComponent; + + var testDrawComponent = new TestDrawComponent { Layer = 2 }; var entity = worldBuilder.CreateEntity(); worldBuilder.SetComponent(entity, aComponent); worldBuilder.SetComponent(entity, cComponent); - worldBuilder.SetDrawComponent(entity, testDrawComponent, 2); + worldBuilder.SetComponent(entity, testDrawComponent); var world = worldBuilder.Build(); @@ -85,10 +89,10 @@ namespace Tests worldBuilder.AddEngine(new DestroyerEngine()); var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer()); - TestDrawComponent testDrawComponent; + TestDrawComponent testDrawComponent = new TestDrawComponent { Layer = 1 }; var entity = worldBuilder.CreateEntity(); - worldBuilder.SetDrawComponent(entity, testDrawComponent, 1); + worldBuilder.SetComponent(entity, testDrawComponent); var world = worldBuilder.Build(); diff --git a/test/WorldTest.cs b/test/WorldTest.cs index abfe7c3..c02a38f 100644 --- a/test/WorldTest.cs +++ b/test/WorldTest.cs @@ -12,7 +12,10 @@ namespace Tests public class WorldTest { struct TestComponent : IComponent { } - struct TestDrawComponent : IComponent, IDrawComponent { } + struct TestDrawComponent : IComponent, IDrawableComponent + { + public int Layer { get; set; } + } static List drawOrder = new List(); @@ -40,29 +43,26 @@ namespace Tests var testGeneralRenderer = worldBuilder.AddGeneralRenderer(new TestGeneralRenderer(), 7); TestComponent testComponent; - TestDrawComponent testDrawComponent = default(TestDrawComponent); + TestDrawComponent drawComponentThree = new TestDrawComponent { Layer = 3 }; + var drawComponentTwo = new TestDrawComponent { Layer = 2 }; + var drawComponentOne = new TestDrawComponent { Layer = 1 }; + var drawComponentFour = new TestDrawComponent { Layer = 4 }; var entity = worldBuilder.CreateEntity(); worldBuilder.SetComponent(entity, testComponent); - worldBuilder.SetDrawComponent(entity, testDrawComponent, 3); - - TestDrawComponent testDrawComponentTwo = default(TestDrawComponent); + worldBuilder.SetComponent(entity, drawComponentThree); var entityTwo = worldBuilder.CreateEntity(); worldBuilder.SetComponent(entityTwo, testComponent); - worldBuilder.SetDrawComponent(entityTwo, testDrawComponentTwo, 1); - - TestDrawComponent testDrawComponentThree = default(TestDrawComponent); + worldBuilder.SetComponent(entityTwo, drawComponentTwo); var entityThree = worldBuilder.CreateEntity(); worldBuilder.SetComponent(entityThree, testComponent); - worldBuilder.SetDrawComponent(entityThree, testDrawComponentThree, 5); - - TestDrawComponent testDrawComponentFour = default(TestDrawComponent); + worldBuilder.SetComponent(entityThree, drawComponentThree); var entityFour = worldBuilder.CreateEntity(); worldBuilder.SetComponent(entityFour, testComponent); - worldBuilder.SetDrawComponent(entityFour, testDrawComponentFour, -5); + worldBuilder.SetComponent(entityFour, drawComponentFour); var world = worldBuilder.Build();