refactor rendering to not use layer property

pull/5/head
Evan Hemsley 2019-06-19 22:38:56 -07:00
parent dd75a94d18
commit 4a54a5544c
10 changed files with 50 additions and 64 deletions

View File

@ -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<TComponent>(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();

View File

@ -11,6 +11,8 @@ namespace Encompass
private Dictionary<int, HashSet<Guid>> layerIndexToComponentIDs = new Dictionary<int, HashSet<Guid>>();
private Dictionary<int, HashSet<GeneralRenderer>> layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>();
private Dictionary<Guid, int> componentIDToLayerIndex = new Dictionary<Guid, int>();
public IEnumerable<int> 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<Guid> ComponentIDsByLayer(int layer)

View File

@ -21,6 +21,11 @@ namespace Encompass
return componentManager.AddComponent(id, component);
}
public Guid AddDrawComponent<TComponent>(TComponent component, int layer = 0) where TComponent : struct, IComponent
{
return componentManager.AddDrawComponent(id, component, layer);
}
public IEnumerable<KeyValuePair<Guid, TComponent>> GetComponents<TComponent>() where TComponent : struct, IComponent
{
return componentManager.GetComponentsByEntityAndType<TComponent>(id);

View File

@ -1,6 +0,0 @@
namespace Encompass
{
public interface IDrawComponent : IComponent, IRenderable
{
}
}

View File

@ -1,7 +0,0 @@
namespace Encompass
{
public interface IRenderable
{
int Layer { get; set; }
}
}

View File

@ -99,7 +99,7 @@ namespace Encompass
return engine;
}
public TRenderer AddRenderer<TRenderer>() where TRenderer : Renderer, new()
public TRenderer AddEntityRenderer<TRenderer>() 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<TRenderer>(int layer) where TRenderer : GeneralRenderer, new()
{
var renderer = new TRenderer();
renderer.AssignEntityManager(entityManager);
renderer.AssignComponentManager(componentManager);
renderManager.RegisterGeneralRendererWithLayer(renderer, layer);
return renderer;
}

View File

@ -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();
}
}

View File

@ -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<TestRenderer>();
var renderer = worldBuilder.AddEntityRenderer<TestRenderer>();
AComponent aComponent;
BComponent bComponent;
@ -69,17 +66,16 @@ namespace Tests
public void InactiveDrawComponent()
{
var worldBuilder = new WorldBuilder();
var renderer = worldBuilder.AddRenderer<TestRenderer>();
var renderer = worldBuilder.AddEntityRenderer<TestRenderer>();
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<CalledRenderer>();
var renderer = worldBuilder.AddEntityRenderer<CalledRenderer>();
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();

View File

@ -17,8 +17,6 @@ namespace Tests
class TestRenderer : GeneralRenderer
{
public new int Layer { get { return 1; } }
public override void Render()
{
result = ReadComponent<AComponent>();
@ -29,7 +27,7 @@ namespace Tests
public void SingletonComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddRenderer<TestRenderer>();
worldBuilder.AddGeneralRenderer<TestRenderer>(1);
AComponent aComponent;
@ -48,7 +46,7 @@ namespace Tests
public void MultipleComponents()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddRenderer<TestRenderer>();
worldBuilder.AddGeneralRenderer<TestRenderer>(1);
AComponent aComponent;
AComponent aComponentTwo;

View File

@ -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<object> drawOrder = new List<object>();
@ -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<TestEntityRenderer>();
var testGeneralRenderer = worldBuilder.AddRenderer<TestGeneralRenderer>();
worldBuilder.AddEntityRenderer<TestEntityRenderer>();
var testGeneralRenderer = worldBuilder.AddGeneralRenderer<TestGeneralRenderer>(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();