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); entitiesWithAddedComponents.Add(entityID);
if (component is IDrawComponent) return componentID;
{
drawLayerManager.RegisterComponentWithLayer(componentID, (component as IDrawComponent).Layer);
} }
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; return componentID;
} }
@ -236,10 +238,7 @@ namespace Encompass
componentIDToEntityID.Remove(componentID); componentIDToEntityID.Remove(componentID);
typeToComponentIDs[type].Remove(componentID); typeToComponentIDs[type].Remove(componentID);
if (component is IDrawComponent) drawLayerManager.UnRegisterComponentWithLayer(componentID);
{
drawLayerManager.UnRegisterComponentWithLayer(componentID, (component as IDrawComponent).Layer);
}
} }
componentsToRemove.Clear(); 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<Guid>> layerIndexToComponentIDs = new Dictionary<int, HashSet<Guid>>();
private Dictionary<int, HashSet<GeneralRenderer>> layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>(); 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 IEnumerable<int> LayerOrder { get { return layerOrder.Values; } }
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer) public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
@ -61,24 +63,27 @@ namespace Encompass
set.Add(id); set.Add(id);
} }
componentIDToLayerIndex[id] = layer;
if (!layerOrder.ContainsKey(layer)) if (!layerOrder.ContainsKey(layer))
{ {
layerOrder.Add(layer, 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); layerIndexToComponentIDs[layer].Remove(id);
} }
} }
public void AdjustComponentLayer(Guid id, int oldLayer, int newLayer) public void AdjustComponentLayer(Guid id, int layer)
{ {
UnRegisterComponentWithLayer(id, oldLayer); UnRegisterComponentWithLayer(id);
RegisterComponentWithLayer(id, newLayer); RegisterComponentWithLayer(id, layer);
} }
public IEnumerable<Guid> ComponentIDsByLayer(int layer) public IEnumerable<Guid> ComponentIDsByLayer(int layer)

View File

@ -21,6 +21,11 @@ namespace Encompass
return componentManager.AddComponent(id, component); 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 public IEnumerable<KeyValuePair<Guid, TComponent>> GetComponents<TComponent>() where TComponent : struct, IComponent
{ {
return componentManager.GetComponentsByEntityAndType<TComponent>(id); 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; return engine;
} }
public TRenderer AddRenderer<TRenderer>() where TRenderer : Renderer, new() public TRenderer AddEntityRenderer<TRenderer>() where TRenderer : Renderer, new()
{ {
var renderer = new TRenderer(); var renderer = new TRenderer();
renderer.AssignEntityManager(entityManager); renderer.AssignEntityManager(entityManager);
@ -110,12 +110,18 @@ namespace Encompass
entityManager.RegisterEntityTracker(renderer as IEntityTracker); entityManager.RegisterEntityTracker(renderer as IEntityTracker);
renderManager.RegisterEntityRenderer(renderer as EntityRenderer); renderManager.RegisterEntityRenderer(renderer as EntityRenderer);
} }
else if (renderer is GeneralRenderer)
{ return renderer;
var generalRenderer = renderer as GeneralRenderer;
renderManager.RegisterGeneralRendererWithLayer(generalRenderer, generalRenderer.Layer);
} }
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; return renderer;
} }

View File

@ -1,8 +1,7 @@
namespace Encompass namespace Encompass
{ {
public abstract class GeneralRenderer : Renderer, IRenderable public abstract class GeneralRenderer : Renderer
{ {
public int Layer { get; set; }
public abstract void Render(); public abstract void Render();
} }
} }

View File

@ -11,10 +11,7 @@ namespace Tests
struct BComponent : IComponent { } struct BComponent : IComponent { }
struct CComponent : IComponent { } struct CComponent : IComponent { }
struct TestDrawComponent : IDrawComponent struct TestDrawComponent : IComponent { }
{
public int Layer { get; set; }
}
[Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(BComponent))] [Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(BComponent))]
class TestRenderer : EntityRenderer class TestRenderer : EntityRenderer
@ -26,7 +23,7 @@ namespace Tests
public void CheckAndTrackEntities() public void CheckAndTrackEntities()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
var renderer = worldBuilder.AddRenderer<TestRenderer>(); var renderer = worldBuilder.AddEntityRenderer<TestRenderer>();
AComponent aComponent; AComponent aComponent;
BComponent bComponent; BComponent bComponent;
@ -69,17 +66,16 @@ namespace Tests
public void InactiveDrawComponent() public void InactiveDrawComponent()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
var renderer = worldBuilder.AddRenderer<TestRenderer>(); var renderer = worldBuilder.AddEntityRenderer<TestRenderer>();
AComponent aComponent; AComponent aComponent;
BComponent bComponent; BComponent bComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent); TestDrawComponent testDrawComponent = default(TestDrawComponent);
testDrawComponent.Layer = 0;
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
entity.AddComponent(aComponent); entity.AddComponent(aComponent);
entity.AddComponent(bComponent); entity.AddComponent(bComponent);
var testDrawComponentID = entity.AddComponent(testDrawComponent); var testDrawComponentID = entity.AddDrawComponent(testDrawComponent, 1);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -110,7 +106,7 @@ namespace Tests
public void RenderMethodCalledOnWorldDraw() public void RenderMethodCalledOnWorldDraw()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
var renderer = worldBuilder.AddRenderer<CalledRenderer>(); var renderer = worldBuilder.AddEntityRenderer<CalledRenderer>();
AComponent aComponent; AComponent aComponent;
CComponent cComponent; CComponent cComponent;
@ -119,7 +115,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
entity.AddComponent(aComponent); entity.AddComponent(aComponent);
entity.AddComponent(cComponent); entity.AddComponent(cComponent);
entity.AddComponent(testDrawComponent); entity.AddDrawComponent(testDrawComponent, 2);
var world = worldBuilder.Build(); var world = worldBuilder.Build();

View File

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

View File

@ -12,10 +12,7 @@ namespace Tests
public class WorldTest public class WorldTest
{ {
struct TestComponent : IComponent { } struct TestComponent : IComponent { }
struct TestDrawComponent : IDrawComponent struct TestDrawComponent : IComponent { }
{
public int Layer { get ; set; }
}
static List<object> drawOrder = new List<object>(); static List<object> drawOrder = new List<object>();
@ -30,8 +27,6 @@ namespace Tests
class TestGeneralRenderer : GeneralRenderer class TestGeneralRenderer : GeneralRenderer
{ {
public new int Layer { get { return 9; } }
public override void Render() public override void Render()
{ {
drawOrder.Add(this); drawOrder.Add(this);
@ -42,37 +37,33 @@ namespace Tests
public void DrawOrder() public void DrawOrder()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddRenderer<TestEntityRenderer>(); worldBuilder.AddEntityRenderer<TestEntityRenderer>();
var testGeneralRenderer = worldBuilder.AddRenderer<TestGeneralRenderer>(); var testGeneralRenderer = worldBuilder.AddGeneralRenderer<TestGeneralRenderer>(7);
TestComponent testComponent; TestComponent testComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent); TestDrawComponent testDrawComponent = default(TestDrawComponent);
testDrawComponent.Layer = 3;
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
entity.AddComponent(testComponent); entity.AddComponent(testComponent);
entity.AddComponent(testDrawComponent); entity.AddDrawComponent(testDrawComponent, 3);
TestDrawComponent testDrawComponentTwo = default(TestDrawComponent); TestDrawComponent testDrawComponentTwo = default(TestDrawComponent);
testDrawComponentTwo.Layer = 1;
var entityTwo = worldBuilder.CreateEntity(); var entityTwo = worldBuilder.CreateEntity();
entityTwo.AddComponent(testComponent); entityTwo.AddComponent(testComponent);
entityTwo.AddComponent(testDrawComponentTwo); entityTwo.AddDrawComponent(testDrawComponentTwo, 1);
TestDrawComponent testDrawComponentThree = default(TestDrawComponent); TestDrawComponent testDrawComponentThree = default(TestDrawComponent);
testDrawComponentThree.Layer = 5;
var entityThree = worldBuilder.CreateEntity(); var entityThree = worldBuilder.CreateEntity();
entityThree.AddComponent(testComponent); entityThree.AddComponent(testComponent);
entityThree.AddComponent(testDrawComponentThree); entityThree.AddDrawComponent(testDrawComponentThree, 5);
TestDrawComponent testDrawComponentFour = default(TestDrawComponent); TestDrawComponent testDrawComponentFour = default(TestDrawComponent);
testDrawComponentFour.Layer = -5;
var entityFour = worldBuilder.CreateEntity(); var entityFour = worldBuilder.CreateEntity();
entityFour.AddComponent(testComponent); entityFour.AddComponent(testComponent);
entityFour.AddComponent(testDrawComponentFour); entityFour.AddDrawComponent(testDrawComponentFour, -5);
var world = worldBuilder.Build(); var world = worldBuilder.Build();