encompass-cs/encompass-cs/RenderManager.cs

60 lines
2.1 KiB
C#

using System;
using System.Reflection;
using System.Collections.Generic;
namespace Encompass
{
internal class RenderManager
{
private readonly ComponentManager componentManager;
private readonly DrawLayerManager drawLayerManager;
private readonly Dictionary<Type, Action<Guid, IComponent>> drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Guid, IComponent>>();
public RenderManager(
ComponentManager componentManager,
DrawLayerManager drawLayerManager
)
{
this.componentManager = componentManager;
this.drawLayerManager = drawLayerManager;
}
public void RegisterOrderedRenderer<TComponent>(Action<Guid, IComponent> renderAction) where TComponent : struct, IComponent
{
drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction);
}
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
{
drawLayerManager.RegisterGeneralRendererWithLayer(renderer, layer);
}
public void Draw()
{
foreach (var layer in drawLayerManager.LayerOrder)
{
var componentIDSet = drawLayerManager.ComponentIDsByLayer(layer);
var generalRendererSet = drawLayerManager.GeneralRenderersByLayer(layer);
foreach (var componentID in componentIDSet)
{
var component = componentManager.GetComponentByID(componentID);
var componentType = componentManager.GetComponentTypeByID(componentID);
if (drawComponentTypeToOrderedRenderer.ContainsKey(componentType))
{
var internalRenderAction = drawComponentTypeToOrderedRenderer[componentType];
internalRenderAction(componentID, component);
}
}
foreach (var generalRenderer in generalRendererSet)
{
generalRenderer.Render();
}
}
}
}
}