fix IDrawableComponent throwing if non auto property used for Layer (#9)
continuous-integration/drone/push Build is passing Details

pull/10/head
cosmonaut 2020-07-16 07:10:21 +00:00
parent 59014c7a9e
commit 703e088430
3 changed files with 30 additions and 5 deletions

View File

@ -26,7 +26,8 @@ namespace Encompass
foreach (var type in _componentTypes)
{
var instanceParam = new object[] { Entity, Activator.CreateInstance(type) };
var instance = Activator.CreateInstance(type);
var instanceParam = new object[] { Entity, instance };
var setComponentMethod = typeof(Engine).GetMethod("SetComponent", BindingFlags.NonPublic | BindingFlags.Instance);
var genericSetComponentMethod = setComponentMethod.MakeGenericMethod(type);
genericSetComponentMethod.Invoke(this, instanceParam);

View File

@ -418,6 +418,11 @@ namespace Encompass
if (componentType.GetInterface("IDrawableComponent") != null)
{
// register draw layer using property value
var instance = Activator.CreateInstance(componentType);
var layerPropertyInfo = componentType.GetProperty("Layer");
dummyDrawLayerManager.RegisterDrawLayer((int)layerPropertyInfo.GetValue(instance));
var drawLayerManagerRegisterMethod = typeof(DrawLayerManager).GetMethod("RegisterOrderedDrawable");
var drawLayerManagerRegisterGenericMethod = drawLayerManagerRegisterMethod.MakeGenericMethod(componentType);
drawLayerManagerRegisterGenericMethod.Invoke(dummyDrawLayerManager, null);

View File

@ -563,15 +563,24 @@ namespace Tests
{
struct AComponent : IComponent, IDrawableComponent
{
public int Layer { get; set; }
public int Layer { get; }
}
struct BComponent : IComponent, IDrawableComponent
{
public int Layer { get => 3; }
}
class ARenderer : OrderedRenderer<AComponent>
{
public override void Render(Entity entity, in AComponent drawComponent) { }
}
class BRenderer : OrderedRenderer<BComponent>
{
public override void Render(Entity entity, in BComponent drawComponent) { }
}
[Test]
public void DrawLayerRegisterAfterOrderedRendererRegisterThrows()
{
@ -590,6 +599,16 @@ namespace Tests
Assert.DoesNotThrow(() => worldBuilder.RegisterDrawLayer(1));
Assert.DoesNotThrow(() => worldBuilder.AddOrderedRenderer(new ARenderer()));
}
[Test]
public void DrawLayerWithProperty()
{
var worldBuilder = new WorldBuilder();
var rendererB = worldBuilder.AddOrderedRenderer(new BRenderer());
Assert.DoesNotThrow(() => worldBuilder.Build());
}
}
}
}