encompass-cs/encompass-cs/RenderManager.cs

73 lines
2.6 KiB
C#

using System;
using System.Reflection;
using System.Collections.Generic;
namespace Encompass
{
internal class RenderManager
{
private readonly EntityManager entityManager;
private readonly ComponentManager componentManager;
private readonly DrawLayerManager drawLayerManager;
private readonly Dictionary<Type, EntityRenderer> drawComponentTypeToEntityRenderer = new Dictionary<Type, EntityRenderer>();
public RenderManager(
EntityManager entityManager,
ComponentManager componentManager,
DrawLayerManager drawLayerManager
)
{
this.entityManager = entityManager;
this.componentManager = componentManager;
this.drawLayerManager = drawLayerManager;
}
public void RegisterEntityRenderer(EntityRenderer renderer)
{
var rendersAttribute = renderer.GetType().GetCustomAttribute<Renders>(false);
if (rendersAttribute != null)
{
drawComponentTypeToEntityRenderer.Add(rendersAttribute.drawComponentType, renderer);
}
}
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 (drawComponentTypeToEntityRenderer.ContainsKey(componentType))
{
var renderer = drawComponentTypeToEntityRenderer[componentType];
var entityID = componentManager.GetEntityIDByComponentID(componentID);
if (renderer.IsTracking(entityID) && entityManager.EntityExists(entityID))
{
var entity = entityManager.GetEntity(entityID);
renderer.Render(entity);
}
}
}
foreach (var generalRenderer in generalRendererSet)
{
generalRenderer.Render();
}
}
}
}
}