diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 8231340..2b2f32c 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -64,11 +64,13 @@ namespace Encompass entitiesWithAddedComponents.Add(entityID); - if (component is IDrawComponent) - { - drawLayerManager.RegisterComponentWithLayer(componentID, (component as IDrawComponent).Layer); - } + return componentID; + } + internal Guid AddDrawComponent(Guid entityID, TComponent component, int layer = 0) where TComponent : struct, IComponent + { + var componentID = AddComponent(entityID, component); + drawLayerManager.RegisterComponentWithLayer(componentID, layer); return componentID; } @@ -236,10 +238,7 @@ namespace Encompass componentIDToEntityID.Remove(componentID); typeToComponentIDs[type].Remove(componentID); - if (component is IDrawComponent) - { - drawLayerManager.UnRegisterComponentWithLayer(componentID, (component as IDrawComponent).Layer); - } + drawLayerManager.UnRegisterComponentWithLayer(componentID); } componentsToRemove.Clear(); diff --git a/encompass-cs/DrawLayerManager.cs b/encompass-cs/DrawLayerManager.cs index bec81b6..36a75ab 100644 --- a/encompass-cs/DrawLayerManager.cs +++ b/encompass-cs/DrawLayerManager.cs @@ -11,6 +11,8 @@ namespace Encompass private Dictionary> layerIndexToComponentIDs = new Dictionary>(); private Dictionary> layerIndexToGeneralRenderers = new Dictionary>(); + private Dictionary componentIDToLayerIndex = new Dictionary(); + public IEnumerable LayerOrder { get { return layerOrder.Values; } } public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer) @@ -61,24 +63,27 @@ namespace Encompass set.Add(id); } + componentIDToLayerIndex[id] = layer; + if (!layerOrder.ContainsKey(layer)) { layerOrder.Add(layer, layer); } } - public void UnRegisterComponentWithLayer(Guid id, int layer) + public void UnRegisterComponentWithLayer(Guid id) { - if (layerIndexToComponentIDs.ContainsKey(layer)) + if (componentIDToLayerIndex.ContainsKey(id)) { + var layer = componentIDToLayerIndex[id]; layerIndexToComponentIDs[layer].Remove(id); } } - public void AdjustComponentLayer(Guid id, int oldLayer, int newLayer) + public void AdjustComponentLayer(Guid id, int layer) { - UnRegisterComponentWithLayer(id, oldLayer); - RegisterComponentWithLayer(id, newLayer); + UnRegisterComponentWithLayer(id); + RegisterComponentWithLayer(id, layer); } public IEnumerable ComponentIDsByLayer(int layer) diff --git a/encompass-cs/Entity.cs b/encompass-cs/Entity.cs index 12055ef..d2e336e 100644 --- a/encompass-cs/Entity.cs +++ b/encompass-cs/Entity.cs @@ -21,6 +21,11 @@ namespace Encompass return componentManager.AddComponent(id, component); } + public Guid AddDrawComponent(TComponent component, int layer = 0) where TComponent : struct, IComponent + { + return componentManager.AddDrawComponent(id, component, layer); + } + public IEnumerable> GetComponents() where TComponent : struct, IComponent { return componentManager.GetComponentsByEntityAndType(id); diff --git a/encompass-cs/IDrawComponent.cs b/encompass-cs/IDrawComponent.cs deleted file mode 100644 index fa7c1c7..0000000 --- a/encompass-cs/IDrawComponent.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Encompass -{ - public interface IDrawComponent : IComponent, IRenderable - { - } -} diff --git a/encompass-cs/IRenderable.cs b/encompass-cs/IRenderable.cs deleted file mode 100644 index cc32e4f..0000000 --- a/encompass-cs/IRenderable.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Encompass -{ - public interface IRenderable - { - int Layer { get; set; } - } -} diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index f3e6ad3..4606093 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -99,7 +99,7 @@ namespace Encompass return engine; } - public TRenderer AddRenderer() where TRenderer : Renderer, new() + public TRenderer AddEntityRenderer() where TRenderer : Renderer, new() { var renderer = new TRenderer(); renderer.AssignEntityManager(entityManager); @@ -110,11 +110,17 @@ namespace Encompass entityManager.RegisterEntityTracker(renderer as IEntityTracker); renderManager.RegisterEntityRenderer(renderer as EntityRenderer); } - else if (renderer is GeneralRenderer) - { - var generalRenderer = renderer as GeneralRenderer; - renderManager.RegisterGeneralRendererWithLayer(generalRenderer, generalRenderer.Layer); - } + + return renderer; + } + + public TRenderer AddGeneralRenderer(int layer) where TRenderer : GeneralRenderer, new() + { + var renderer = new TRenderer(); + renderer.AssignEntityManager(entityManager); + renderer.AssignComponentManager(componentManager); + + renderManager.RegisterGeneralRendererWithLayer(renderer, layer); return renderer; } diff --git a/encompass-cs/renderers/GeneralRenderer.cs b/encompass-cs/renderers/GeneralRenderer.cs index 6046146..e408d8f 100644 --- a/encompass-cs/renderers/GeneralRenderer.cs +++ b/encompass-cs/renderers/GeneralRenderer.cs @@ -1,8 +1,7 @@ namespace Encompass { - public abstract class GeneralRenderer : Renderer, IRenderable + public abstract class GeneralRenderer : Renderer { - public int Layer { get; set; } public abstract void Render(); } } diff --git a/test/EntityRendererTest.cs b/test/EntityRendererTest.cs index 3b21fa6..6577eff 100644 --- a/test/EntityRendererTest.cs +++ b/test/EntityRendererTest.cs @@ -11,10 +11,7 @@ namespace Tests struct BComponent : IComponent { } struct CComponent : IComponent { } - struct TestDrawComponent : IDrawComponent - { - public int Layer { get; set; } - } + struct TestDrawComponent : IComponent { } [Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(BComponent))] class TestRenderer : EntityRenderer @@ -26,7 +23,7 @@ namespace Tests public void CheckAndTrackEntities() { var worldBuilder = new WorldBuilder(); - var renderer = worldBuilder.AddRenderer(); + var renderer = worldBuilder.AddEntityRenderer(); AComponent aComponent; BComponent bComponent; @@ -69,17 +66,16 @@ namespace Tests public void InactiveDrawComponent() { var worldBuilder = new WorldBuilder(); - var renderer = worldBuilder.AddRenderer(); + var renderer = worldBuilder.AddEntityRenderer(); AComponent aComponent; BComponent bComponent; TestDrawComponent testDrawComponent = default(TestDrawComponent); - testDrawComponent.Layer = 0; var entity = worldBuilder.CreateEntity(); entity.AddComponent(aComponent); entity.AddComponent(bComponent); - var testDrawComponentID = entity.AddComponent(testDrawComponent); + var testDrawComponentID = entity.AddDrawComponent(testDrawComponent, 1); var world = worldBuilder.Build(); @@ -110,7 +106,7 @@ namespace Tests public void RenderMethodCalledOnWorldDraw() { var worldBuilder = new WorldBuilder(); - var renderer = worldBuilder.AddRenderer(); + var renderer = worldBuilder.AddEntityRenderer(); AComponent aComponent; CComponent cComponent; @@ -119,7 +115,7 @@ namespace Tests var entity = worldBuilder.CreateEntity(); entity.AddComponent(aComponent); entity.AddComponent(cComponent); - entity.AddComponent(testDrawComponent); + entity.AddDrawComponent(testDrawComponent, 2); var world = worldBuilder.Build(); diff --git a/test/GeneralRendererTest.cs b/test/GeneralRendererTest.cs index 817b4a1..3ada80c 100644 --- a/test/GeneralRendererTest.cs +++ b/test/GeneralRendererTest.cs @@ -17,8 +17,6 @@ namespace Tests class TestRenderer : GeneralRenderer { - public new int Layer { get { return 1; } } - public override void Render() { result = ReadComponent(); @@ -29,7 +27,7 @@ namespace Tests public void SingletonComponent() { var worldBuilder = new WorldBuilder(); - worldBuilder.AddRenderer(); + worldBuilder.AddGeneralRenderer(1); AComponent aComponent; @@ -48,7 +46,7 @@ namespace Tests public void MultipleComponents() { var worldBuilder = new WorldBuilder(); - worldBuilder.AddRenderer(); + worldBuilder.AddGeneralRenderer(1); AComponent aComponent; AComponent aComponentTwo; diff --git a/test/WorldTest.cs b/test/WorldTest.cs index bcfe040..f403608 100644 --- a/test/WorldTest.cs +++ b/test/WorldTest.cs @@ -12,10 +12,7 @@ namespace Tests public class WorldTest { struct TestComponent : IComponent { } - struct TestDrawComponent : IDrawComponent - { - public int Layer { get ; set; } - } + struct TestDrawComponent : IComponent { } static List drawOrder = new List(); @@ -30,8 +27,6 @@ namespace Tests class TestGeneralRenderer : GeneralRenderer { - public new int Layer { get { return 9; } } - public override void Render() { drawOrder.Add(this); @@ -42,37 +37,33 @@ namespace Tests public void DrawOrder() { var worldBuilder = new WorldBuilder(); - worldBuilder.AddRenderer(); - var testGeneralRenderer = worldBuilder.AddRenderer(); + worldBuilder.AddEntityRenderer(); + var testGeneralRenderer = worldBuilder.AddGeneralRenderer(7); TestComponent testComponent; TestDrawComponent testDrawComponent = default(TestDrawComponent); - testDrawComponent.Layer = 3; var entity = worldBuilder.CreateEntity(); entity.AddComponent(testComponent); - entity.AddComponent(testDrawComponent); + entity.AddDrawComponent(testDrawComponent, 3); TestDrawComponent testDrawComponentTwo = default(TestDrawComponent); - testDrawComponentTwo.Layer = 1; var entityTwo = worldBuilder.CreateEntity(); entityTwo.AddComponent(testComponent); - entityTwo.AddComponent(testDrawComponentTwo); + entityTwo.AddDrawComponent(testDrawComponentTwo, 1); TestDrawComponent testDrawComponentThree = default(TestDrawComponent); - testDrawComponentThree.Layer = 5; var entityThree = worldBuilder.CreateEntity(); entityThree.AddComponent(testComponent); - entityThree.AddComponent(testDrawComponentThree); + entityThree.AddDrawComponent(testDrawComponentThree, 5); TestDrawComponent testDrawComponentFour = default(TestDrawComponent); - testDrawComponentFour.Layer = -5; var entityFour = worldBuilder.CreateEntity(); entityFour.AddComponent(testComponent); - entityFour.AddComponent(testDrawComponentFour); + entityFour.AddDrawComponent(testDrawComponentFour, -5); var world = worldBuilder.Build();