diff --git a/encompass-cs/UberEngine.cs b/encompass-cs/UberEngine.cs
index bc7c299..604b337 100644
--- a/encompass-cs/UberEngine.cs
+++ b/encompass-cs/UberEngine.cs
@@ -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);
diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs
index e4b3cce..7da9ced 100644
--- a/encompass-cs/WorldBuilder.cs
+++ b/encompass-cs/WorldBuilder.cs
@@ -175,8 +175,8 @@ namespace Encompass
/// The draw layer to register.
public void RegisterDrawLayer(int layer)
{
- if (_rendererRegistered)
- {
+ if (_rendererRegistered)
+ {
throw new IllegalDrawLayerException("Cannot register a draw layer after a Renderer has been registered.");
}
_drawLayerManager.RegisterDrawLayer(layer);
@@ -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);
diff --git a/test/WorldBuilderTest.cs b/test/WorldBuilderTest.cs
index b404e77..530e120 100644
--- a/test/WorldBuilderTest.cs
+++ b/test/WorldBuilderTest.cs
@@ -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
{
-
public override void Render(Entity entity, in AComponent drawComponent) { }
}
+ class BRenderer : OrderedRenderer
+ {
+ 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());
+ }
}
}
}