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; 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) internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component)
{ {
IDToComponent[componentID] = component; IDToComponent[componentID] = component;
@ -104,7 +109,7 @@ namespace Encompass
{ {
var (entity, type) = keyValuePair.Key; var (entity, type) = keyValuePair.Key;
var (componentID, component) = keyValuePair.Value; var (componentID, component) = keyValuePair.Value;
if (!componentIDsMarkedForWrite.Contains(componentID) || !entityIDToComponentTypeToComponentID.ContainsKey(entity.ID)) { continue; } if (!componentIDsMarkedForWrite.Contains(componentID) || !entityIDToComponentTypeToComponentID.ContainsKey(entity.ID)) { continue; }
if (entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type)) if (entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type))
@ -222,7 +227,7 @@ namespace Encompass
{ {
componentIDsMarkedForWrite.Remove(componentID); componentIDsMarkedForWrite.Remove(componentID);
} }
if (IDToComponent.ContainsKey(componentID)) if (IDToComponent.ContainsKey(componentID))
{ {
Remove(componentID); Remove(componentID);

View File

@ -503,43 +503,10 @@ namespace Encompass
newComponentMessage.priority = priority; newComponentMessage.priority = priority;
SendPendingComponentMessage(newComponentMessage); SendPendingComponentMessage(newComponentMessage);
} }
}
/// <summary> if (component is IDrawableComponent drawableComponent)
/// 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>)))
{ {
throw new IllegalWriteException("Engine {0} tried to write undeclared Component {1}", GetType().Name, typeof(TComponent).Name); componentManager.RegisterDrawableComponent(componentID, drawableComponent);
}
if (sendTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
{
PendingComponentMessage<TComponent> newComponentMessage;
newComponentMessage.entity = entity;
newComponentMessage.componentID = componentID;
newComponentMessage.component = component;
newComponentMessage.priority = priority;
SendPendingComponentMessage(newComponentMessage);
} }
} }

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> /// <summary>
/// OrdereredRenderer provides a structure for the common pattern of wishing to draw a specific DrawComponent at a specific layer. /// OrdereredRenderer provides a structure for the common pattern of wishing to draw a specific DrawComponent at a specific layer.
/// </summary> /// </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); public abstract void Render(Entity entity, TComponent drawComponent);

View File

@ -74,16 +74,11 @@ namespace Encompass
/// </summary> /// </summary>
public void SetComponent<TComponent>(Entity entity, TComponent component, int priority = 0) where TComponent : struct, IComponent public void SetComponent<TComponent>(Entity entity, TComponent component, int priority = 0) where TComponent : struct, IComponent
{ {
componentManager.MarkComponentForWrite(entity, component, priority); var componentID = componentManager.MarkComponentForWrite(entity, component, priority);
} if (component is IDrawableComponent drawableComponent)
{
/// <summary> componentManager.RegisterDrawableComponent(componentID, drawableComponent);
/// 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);
} }
internal void RegisterComponent(Type componentType) internal void RegisterComponent(Type componentType)
@ -159,7 +154,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Adds the specified OrderedRenderer to the World. /// Adds the specified OrderedRenderer to the World.
/// </summary> /// </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.AssignEntityManager(entityManager);
renderer.AssignComponentManager(componentManager); renderer.AssignComponentManager(componentManager);

View File

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

View File

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