encompass-cs/encompass-cs/UberEngine.cs

63 lines
2.7 KiB
C#
Raw Normal View History

using System;
2019-12-17 02:51:45 +00:00
using System.Collections.Generic;
using System.Reflection;
namespace Encompass
{
internal class UberEngine : Engine
{
2019-12-17 02:51:45 +00:00
private IEnumerable<Type> _componentTypes;
private Entity _entity;
public UberEngine(Entity entity, IEnumerable<Type> componentTypes)
{
_componentTypes = componentTypes;
readTypes.UnionWith(componentTypes);
writeTypes.UnionWith(componentTypes);
_entity = entity;
}
public void Write()
{
2019-12-17 02:51:45 +00:00
foreach (var type in _componentTypes)
{
2019-12-17 02:51:45 +00:00
var instanceParam = new object[] { _entity, Activator.CreateInstance(type) };
var setComponentMethod = typeof(Engine).GetMethod("SetComponent", BindingFlags.NonPublic | BindingFlags.Instance);
var genericSetComponentMethod = setComponentMethod.MakeGenericMethod(type);
genericSetComponentMethod.Invoke(this, instanceParam);
}
}
2019-12-17 02:51:45 +00:00
public override void Update(double dt)
{
foreach (var type in _componentTypes)
{
CallGenericMethod(type, "ReadComponent", null);
CallGenericMethod(type, "ReadComponentIncludingEntity", null);
CallGenericMethod(type, "ReadComponents", null);
CallGenericMethod(type, "ReadComponentsIncludingEntity", null);
CallGenericMethod(type, "GetComponent", new Type[] { typeof(Entity) }, new object[] { _entity });
CallGenericMethod(type, "HasComponent", new Type[] { typeof(Entity) }, new object[] { _entity });
CallGenericMethod(type, "SomeComponent", null);
CallGenericMethod(type, "RemoveComponent", new Type[] { typeof(Entity) }, new object[] { _entity });
CallGenericMethod(type, "DestroyWith", null);
CallGenericMethod(type, "DestroyAllWith", null);
}
}
2019-12-17 02:51:45 +00:00
private void CallGenericMethod(Type type, string methodName, object[] parameters)
{
var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters);
}
private void CallGenericMethod(Type type, string methodName, Type[] types, object[] parameters)
{
2019-12-17 02:51:45 +00:00
var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters);
}
}
}