replace IDrawComponent with IDrawableComponent and remove SetDrawComponent

pull/5/head
Evan Hemsley 2019-11-20 19:16:44 -08:00
parent 62dfdb758a
commit d53bf043db
8 changed files with 44 additions and 73 deletions

View File

@ -79,6 +79,11 @@ namespace Encompass
return id;
}
internal void RegisterDrawableComponent<TComponent>(Guid componentID, TComponent component) where TComponent : IDrawableComponent
{
drawLayerManager.RegisterComponentWithLayer(componentID, component.Layer);
}
internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component)
{
IDToComponent[componentID] = component;
@ -104,7 +109,7 @@ namespace Encompass
{
var (entity, type) = keyValuePair.Key;
var (componentID, component) = keyValuePair.Value;
if (!componentIDsMarkedForWrite.Contains(componentID) || !entityIDToComponentTypeToComponentID.ContainsKey(entity.ID)) { continue; }
if (entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type))
@ -222,7 +227,7 @@ namespace Encompass
{
componentIDsMarkedForWrite.Remove(componentID);
}
if (IDToComponent.ContainsKey(componentID))
{
Remove(componentID);

View File

@ -503,43 +503,10 @@ namespace Encompass
newComponentMessage.priority = priority;
SendPendingComponentMessage(newComponentMessage);
}
}
/// <summary>
/// Overwrites Component struct data associated with the specified Component ID.
/// </summary>
internal void SetComponent<TComponent>(Guid componentID, TComponent component) where TComponent : struct, IComponent
{
SetComponent(GetEntityByComponentID<TComponent>(componentID), component);
}
/// <summary>
/// Sets Draw Component data for the specified Component Type on the specified Entity.
/// This method must be used for the Draw Component to be readable by an OrderedRenderer.
/// If Component data for this Type already existed on the Entity, the component data is overwritten.
/// </summary>
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
/// Thrown when the Engine does not declare that it Writes the given Component Type.
/// </exception>
protected void SetDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent, IDrawComponent
{
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : 0;
var componentID = componentManager.MarkDrawComponentForWrite(entity, component, priority, layer);
if (!sendTypes.Contains(typeof(ComponentWriteMessage<TComponent>)))
if (component is IDrawableComponent drawableComponent)
{
throw new IllegalWriteException("Engine {0} tried to write undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
if (sendTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
{
PendingComponentMessage<TComponent> newComponentMessage;
newComponentMessage.entity = entity;
newComponentMessage.componentID = componentID;
newComponentMessage.component = component;
newComponentMessage.priority = priority;
SendPendingComponentMessage(newComponentMessage);
componentManager.RegisterDrawableComponent(componentID, drawableComponent);
}
}

View File

@ -1,7 +0,0 @@
namespace Encompass
{
/// <summary>
/// Structs that implement IDrawComponent are considered to be DrawComponents.
/// </summary>
public interface IDrawComponent { }
}

View File

@ -0,0 +1,7 @@
namespace Encompass
{
public interface IDrawableComponent
{
int Layer { get; set; }
}
}

View File

@ -5,7 +5,7 @@ namespace Encompass
/// <summary>
/// OrdereredRenderer provides a structure for the common pattern of wishing to draw a specific DrawComponent at a specific layer.
/// </summary>
public abstract class OrderedRenderer<TComponent> : Renderer where TComponent : struct, IComponent, IDrawComponent
public abstract class OrderedRenderer<TComponent> : Renderer where TComponent : struct, IComponent, IDrawableComponent
{
public abstract void Render(Entity entity, TComponent drawComponent);

View File

@ -74,16 +74,11 @@ namespace Encompass
/// </summary>
public void SetComponent<TComponent>(Entity entity, TComponent component, int priority = 0) where TComponent : struct, IComponent
{
componentManager.MarkComponentForWrite(entity, component, priority);
}
/// <summary>
/// Sets Draw Component data for the specified Component Type on the specified Entity.
/// This method must be used for the Draw Component to be readable by an OrderedRenderer.
/// </summary>
public void SetDrawComponent<TComponent>(Entity entity, TComponent component, int priority = 0, int layer = 0) where TComponent : struct, IComponent, IDrawComponent
{
componentManager.MarkDrawComponentForWrite(entity, component, priority, layer);
var componentID = componentManager.MarkComponentForWrite(entity, component, priority);
if (component is IDrawableComponent drawableComponent)
{
componentManager.RegisterDrawableComponent(componentID, drawableComponent);
}
}
internal void RegisterComponent(Type componentType)
@ -159,7 +154,7 @@ namespace Encompass
/// <summary>
/// Adds the specified OrderedRenderer to the World.
/// </summary>
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawComponent
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawableComponent
{
renderer.AssignEntityManager(entityManager);
renderer.AssignComponentManager(componentManager);

View File

@ -13,7 +13,10 @@ namespace Tests
struct BComponent : IComponent { }
struct CComponent : IComponent { }
struct TestDrawComponent : IComponent, IDrawComponent { }
struct TestDrawComponent : IComponent, IDrawableComponent
{
public int Layer { get; set; }
}
class TestRenderer : OrderedRenderer<TestDrawComponent>
{
@ -49,12 +52,13 @@ namespace Tests
AComponent aComponent;
CComponent cComponent;
TestDrawComponent testDrawComponent;
var testDrawComponent = new TestDrawComponent { Layer = 2 };
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, aComponent);
worldBuilder.SetComponent(entity, cComponent);
worldBuilder.SetDrawComponent(entity, testDrawComponent, 2);
worldBuilder.SetComponent(entity, testDrawComponent);
var world = worldBuilder.Build();
@ -85,10 +89,10 @@ namespace Tests
worldBuilder.AddEngine(new DestroyerEngine());
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
TestDrawComponent testDrawComponent;
TestDrawComponent testDrawComponent = new TestDrawComponent { Layer = 1 };
var entity = worldBuilder.CreateEntity();
worldBuilder.SetDrawComponent(entity, testDrawComponent, 1);
worldBuilder.SetComponent(entity, testDrawComponent);
var world = worldBuilder.Build();

View File

@ -12,7 +12,10 @@ namespace Tests
public class WorldTest
{
struct TestComponent : IComponent { }
struct TestDrawComponent : IComponent, IDrawComponent { }
struct TestDrawComponent : IComponent, IDrawableComponent
{
public int Layer { get; set; }
}
static List<object> drawOrder = new List<object>();
@ -40,29 +43,26 @@ namespace Tests
var testGeneralRenderer = worldBuilder.AddGeneralRenderer(new TestGeneralRenderer(), 7);
TestComponent testComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent);
TestDrawComponent drawComponentThree = new TestDrawComponent { Layer = 3 };
var drawComponentTwo = new TestDrawComponent { Layer = 2 };
var drawComponentOne = new TestDrawComponent { Layer = 1 };
var drawComponentFour = new TestDrawComponent { Layer = 4 };
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, testComponent);
worldBuilder.SetDrawComponent(entity, testDrawComponent, 3);
TestDrawComponent testDrawComponentTwo = default(TestDrawComponent);
worldBuilder.SetComponent(entity, drawComponentThree);
var entityTwo = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entityTwo, testComponent);
worldBuilder.SetDrawComponent(entityTwo, testDrawComponentTwo, 1);
TestDrawComponent testDrawComponentThree = default(TestDrawComponent);
worldBuilder.SetComponent(entityTwo, drawComponentTwo);
var entityThree = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entityThree, testComponent);
worldBuilder.SetDrawComponent(entityThree, testDrawComponentThree, 5);
TestDrawComponent testDrawComponentFour = default(TestDrawComponent);
worldBuilder.SetComponent(entityThree, drawComponentThree);
var entityFour = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entityFour, testComponent);
worldBuilder.SetDrawComponent(entityFour, testDrawComponentFour, -5);
worldBuilder.SetComponent(entityFour, drawComponentFour);
var world = worldBuilder.Build();