diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 5853d61..30cd58d 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -68,6 +68,23 @@ namespace Encompass return componentManager.GetEntityIDFromComponentID(componentID); } + protected Entity GetEntityFromComponentID(Guid componentID) + { + return GetEntity(GetEntityIDFromComponentID(componentID)); + } + + protected TComponent GetComponentByID(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> ReadComponents() where TComponent : struct, IComponent { return componentManager.GetActiveComponentsByType(); diff --git a/encompass-cs/exceptions/ComponentTypeMismatchException.cs b/encompass-cs/exceptions/ComponentTypeMismatchException.cs new file mode 100644 index 0000000..d28e7c4 --- /dev/null +++ b/encompass-cs/exceptions/ComponentTypeMismatchException.cs @@ -0,0 +1,12 @@ +using System; + +namespace Encompass +{ + public class ComponentTypeMismatchException : Exception + { + public ComponentTypeMismatchException( + string format, + params object[] args + ) : base(string.Format(format, args)) { } + } +} diff --git a/test/EngineTest.cs b/test/EngineTest.cs index c38a0ae..c8d85d8 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -435,5 +435,91 @@ namespace Tests Assert.That(results, Does.Not.Contain(new KeyValuePair(componentID, mockComponent))); Assert.That(results, Does.Not.Contain(new KeyValuePair(componentBID, mockComponent))); } + + static Entity entityFromComponentIDResult; + class GetEntityFromComponentIDEngine : Engine + { + public override void Update(float dt) + { + var componentID = ReadComponent().Key; + entityFromComponentIDResult = GetEntityFromComponentID(componentID); + } + } + + [Test] + public void GetEntityFromComponentID() + { + var worldBuilder = new WorldBuilder(); + worldBuilder.AddEngine(); + + 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().Key; + mockComponentByIDResult = GetComponentByID(componentID); + } + } + [Test] + public void GetComponentByID() + { + var worldBuilder = new WorldBuilder(); + worldBuilder.AddEngine(); + + 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().Key; + GetComponentByID(componentID); + } + } + + [Test] + public void GetComponentByIDWithTypeMismatch() + { + var worldBuilder = new WorldBuilder(); + worldBuilder.AddEngine(); + + MockComponent component; + component.myInt = 2; + component.myString = "howdy"; + + var entity = worldBuilder.CreateEntity(); + entity.AddComponent(component); + + var world = worldBuilder.Build(); + + Assert.Throws(() => world.Update(0.01f)); + } } }