add some convenience methods to Engines

pull/5/head
Evan Hemsley 2019-06-21 17:44:07 -07:00
parent fde3cfe9e3
commit e964055ec1
3 changed files with 115 additions and 0 deletions

View File

@ -68,6 +68,23 @@ namespace Encompass
return componentManager.GetEntityIDFromComponentID(componentID);
}
protected Entity GetEntityFromComponentID(Guid componentID)
{
return GetEntity(GetEntityIDFromComponentID(componentID));
}
protected TComponent GetComponentByID<TComponent>(Guid componentID) where TComponent : struct, IComponent
{
if (componentManager.GetComponentTypeByID(componentID) == typeof(TComponent))
{
return (TComponent)componentManager.GetComponentByID(componentID);
}
else
{
throw new ComponentTypeMismatchException("Expected Component to be of type {0} but was actually of type {1}", typeof(TComponent).Name, componentManager.GetComponentTypeByID(componentID).Name);
}
}
protected IEnumerable<KeyValuePair<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
return componentManager.GetActiveComponentsByType<TComponent>();

View File

@ -0,0 +1,12 @@
using System;
namespace Encompass
{
public class ComponentTypeMismatchException : Exception
{
public ComponentTypeMismatchException(
string format,
params object[] args
) : base(string.Format(format, args)) { }
}
}

View File

@ -435,5 +435,91 @@ namespace Tests
Assert.That(results, Does.Not.Contain(new KeyValuePair<Guid, MockComponent>(componentID, mockComponent)));
Assert.That(results, Does.Not.Contain(new KeyValuePair<Guid, MockComponent>(componentBID, mockComponent)));
}
static Entity entityFromComponentIDResult;
class GetEntityFromComponentIDEngine : Engine
{
public override void Update(float dt)
{
var componentID = ReadComponent<MockComponent>().Key;
entityFromComponentIDResult = GetEntityFromComponentID(componentID);
}
}
[Test]
public void GetEntityFromComponentID()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<GetEntityFromComponentIDEngine>();
MockComponent component;
component.myInt = 2;
component.myString = "howdy";
var entity = worldBuilder.CreateEntity();
entity.AddComponent(component);
var world = worldBuilder.Build();
world.Update(0.01f);
Assert.That(entity, Is.EqualTo(entityFromComponentIDResult));
}
static MockComponent mockComponentByIDResult;
class GetComponentByIDEngine : Engine
{
public override void Update(float dt)
{
var componentID = ReadComponent<MockComponent>().Key;
mockComponentByIDResult = GetComponentByID<MockComponent>(componentID);
}
}
[Test]
public void GetComponentByID()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<GetComponentByIDEngine>();
MockComponent component;
component.myInt = 2;
component.myString = "howdy";
var entity = worldBuilder.CreateEntity();
entity.AddComponent(component);
var world = worldBuilder.Build();
world.Update(0.01f);
Assert.That(component, Is.EqualTo(mockComponentByIDResult));
}
struct OtherComponent : IComponent { }
class GetComponentByIDWithTypeMismatchEngine : Engine
{
public override void Update(float dt)
{
var componentID = ReadComponent<MockComponent>().Key;
GetComponentByID<OtherComponent>(componentID);
}
}
[Test]
public void GetComponentByIDWithTypeMismatch()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<GetComponentByIDWithTypeMismatchEngine>();
MockComponent component;
component.myInt = 2;
component.myString = "howdy";
var entity = worldBuilder.CreateEntity();
entity.AddComponent(component);
var world = worldBuilder.Build();
Assert.Throws<ComponentTypeMismatchException>(() => world.Update(0.01f));
}
}
}