diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index c7f6f9b..9501c18 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -161,7 +161,7 @@ namespace Encompass throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name); } - return componentManager.GetComponentsByEntityAndType(entity.ID); + return ReadMessages>().Where((message) => message.entity == entity).Select((message) => (message.componentID, message.component)); } protected ValueTuple GetComponent(Entity entity) where TComponent : struct, IComponent @@ -176,7 +176,7 @@ namespace Encompass throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name); } - return messageManager.GetMessagesByType>().Where((message) => message.entity == entity).Any(); + return ReadMessages>().Where((message) => message.entity == entity).Any(); } internal void UpdateComponentInWorld(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent @@ -241,7 +241,7 @@ namespace Encompass throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", GetType().Name, typeof(TMessage).Name); } - return messageManager.GetMessagesByType().Any(); + return ReadMessages().Any(); } protected bool SomeComponent() where TComponent : struct, IComponent @@ -251,7 +251,7 @@ namespace Encompass throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name); } - return componentManager.GetActiveComponentsByType().Any(); + return ReadMessages>().Any(); } protected void Destroy(Guid entityID) diff --git a/test/ComponentTest.cs b/test/ComponentTest.cs index c8aa299..98204a8 100644 --- a/test/ComponentTest.cs +++ b/test/ComponentTest.cs @@ -24,20 +24,6 @@ namespace Tests static IEnumerable<(Guid, MockComponent)> gottenMockComponentIDPairs = Enumerable.Empty<(Guid, MockComponent)>(); static (Guid, MockComponent) gottenMockComponentIDPair; - [Reads(typeof(EntityMessage), typeof(MockComponent))] - class GetMockComponentsEngine : Engine - { - public override void Update(double dt) - { - gottenMockComponentIDPairs = Enumerable.Empty<(Guid, MockComponent)>(); - - foreach (var entityMessage in ReadMessages()) - { - gottenMockComponentIDPairs = GetComponents(entityMessage.entity); - } - } - } - [Reads(typeof(EntityMessage), typeof(MockComponent))] class GetMockComponentEngine : Engine { @@ -166,12 +152,51 @@ namespace Tests world.Update(0.01); } + [Reads(typeof(EntityMessage), typeof(MockComponent))] + class GetMockComponentsEngine : Engine + { + private Entity entity; + private Guid componentAID; + private Guid componentBID; + private Guid componentCID; + private MockComponent componentA; + private MockComponent componentB; + private MockComponent componentC; + + public GetMockComponentsEngine( + Entity entity, + Guid componentAID, + MockComponent componentA, + Guid componentBID, + MockComponent componentB, + Guid componentCID, + MockComponent componentC + ) { + this.entity = entity; + this.componentAID = componentAID; + this.componentA = componentA; + this.componentBID = componentBID; + this.componentB = componentB; + this.componentCID = componentCID; + this.componentC = componentC; + } + + public override void Update(double dt) + { + foreach (var entityMessage in ReadMessages()) + { + var results = GetComponents(entityMessage.entity); + results.Should().Contain((componentAID, componentA)); + results.Should().Contain((componentBID, componentB)); + results.Should().Contain((componentCID, componentC)); + } + } + } + [Test] public void GetComponents() { var worldBuilder = new WorldBuilder(); - worldBuilder.AddEngine(new GetMockComponentsEngine()); - var entity = worldBuilder.CreateEntity(); MockComponent mockComponentA; @@ -190,6 +215,16 @@ namespace Tests var componentBID = worldBuilder.AddComponent(entity, mockComponentB); var componentCID = worldBuilder.AddComponent(entity, mockComponentC); + worldBuilder.AddEngine(new GetMockComponentsEngine( + entity, + componentAID, + mockComponentA, + componentBID, + mockComponentB, + componentCID, + mockComponentC + )); + EntityMessage entityMessage; entityMessage.entity = entity; worldBuilder.EmitMessage(entityMessage); @@ -197,10 +232,6 @@ namespace Tests var world = worldBuilder.Build(); world.Update(0.01); - - gottenMockComponentIDPairs.Should().Contain((componentAID, mockComponentA)); - gottenMockComponentIDPairs.Should().Contain((componentBID, mockComponentB)); - gottenMockComponentIDPairs.Should().Contain((componentCID, mockComponentC)); } [Test] @@ -422,13 +453,6 @@ namespace Tests public override void Update(double dt) { if (SomeMessage()) - { - CheckHasMockComponentMessage checkHasMockComponentMessage; - checkHasMockComponentMessage.entity = entity; - checkHasMockComponentMessage.shouldHaveComponent = false; - EmitMessage(checkHasMockComponentMessage); - } - else { CheckHasMockComponentMessage checkHasMockComponentMessage; checkHasMockComponentMessage.entity = entity; @@ -445,20 +469,11 @@ namespace Tests { foreach (var checkHasMockComponentMessage in ReadMessages()) { - if (checkHasMockComponentMessage.shouldHaveComponent) - { - Assert.IsTrue(HasComponent(checkHasMockComponentMessage.entity)); - } - else - { - Assert.IsFalse(HasComponent(checkHasMockComponentMessage.entity)); - } + Assert.IsTrue(HasComponent(checkHasMockComponentMessage.entity)); } } } - // TODO: need to rethink this test because ActivateComponent is instant now - [Test] public void ActivateComponent() { @@ -485,7 +500,6 @@ namespace Tests var world = worldBuilder.Build(); world.Update(0.01); - world.Update(0.01); } struct DeactivateComponentMessage : IMessage diff --git a/test/EntityTest.cs b/test/EntityTest.cs new file mode 100644 index 0000000..89b0520 --- /dev/null +++ b/test/EntityTest.cs @@ -0,0 +1,24 @@ +using NUnit.Framework; +using FluentAssertions; + +using Encompass; + +namespace Tests +{ + public class EntityTests + { + [Test] + public void Equals() + { + var worldBuilder = new WorldBuilder(); + var entity = worldBuilder.CreateEntity(); + var entityTwo = worldBuilder.CreateEntity(); + + var copyEntity = entity; + + Assert.AreNotEqual(entity, entityTwo); + Assert.AreEqual(entity, entity); + Assert.IsTrue(entity == copyEntity); + } + } +} \ No newline at end of file