encompass-cs/encompass-cs/UberRenderer.cs

62 lines
2.2 KiB
C#
Raw Normal View History

2019-12-17 04:16:46 +00:00
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Encompass
{
class UberRenderer : Renderer
{
2020-03-20 07:09:57 +00:00
private readonly IEnumerable<Type> _componentTypes;
private Entity _entity;
2019-12-17 04:16:46 +00:00
public UberRenderer(IEnumerable<Type> componentTypes)
2019-12-17 04:16:46 +00:00
{
_componentTypes = componentTypes;
}
public void SetEntity(Entity entity)
{
_entity = entity;
}
2020-03-23 02:10:28 +00:00
// can't reflect invoke on Span returns...
2019-12-17 04:16:46 +00:00
public void Render()
{
foreach (var type in _componentTypes)
{
2020-03-25 05:19:24 +00:00
CallGenericWrappedMethod(type, "ReadEntitiesWrapper", null);
2019-12-17 04:16:46 +00:00
CallGenericMethod(type, "ReadEntity", null);
2020-03-25 05:19:24 +00:00
CallGenericWrappedMethod(type, "ReadComponentsWrapper", null);
2019-12-17 04:16:46 +00:00
CallGenericMethod(type, "ReadComponent", null);
CallGenericMethod(type, "GetComponent", new object[] { _entity });
CallGenericMethod(type, "HasComponent", new object[] { _entity });
CallGenericMethod(type, "SomeComponent", null);
}
}
2020-03-25 05:19:24 +00:00
protected void ReadEntitiesWrapper<TComponent>() where TComponent : struct, IComponent
{
ReadEntities<TComponent>();
}
protected void ReadComponentsWrapper<TComponent>() where TComponent : struct, IComponent
{
ReadComponents<TComponent>();
}
2019-12-17 04:16:46 +00:00
private void CallGenericMethod(Type type, string methodName, object[] parameters)
{
var readComponentMethod = typeof(Renderer).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters);
}
2020-03-25 05:19:24 +00:00
private void CallGenericWrappedMethod(Type type, string methodName, object[] parameters)
{
var readComponentMethod = typeof(UberRenderer).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters);
}
2019-12-17 04:16:46 +00:00
}
}