all component reads use ComponentMessage now

pull/5/head
Evan Hemsley 2019-07-18 17:50:38 -07:00
parent a42123f58d
commit 3deff94dfe
3 changed files with 80 additions and 42 deletions

View File

@ -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<TComponent>(entity.ID);
return ReadMessages<ComponentMessage<TComponent>>().Where((message) => message.entity == entity).Select((message) => (message.componentID, message.component));
}
protected ValueTuple<Guid, TComponent> GetComponent<TComponent>(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<ComponentMessage<TComponent>>().Where((message) => message.entity == entity).Any();
return ReadMessages<ComponentMessage<TComponent>>().Where((message) => message.entity == entity).Any();
}
internal void UpdateComponentInWorld<TComponent>(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<TMessage>().Any();
return ReadMessages<TMessage>().Any();
}
protected bool SomeComponent<TComponent>() 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<TComponent>().Any();
return ReadMessages<ComponentMessage<TComponent>>().Any();
}
protected void Destroy(Guid entityID)

View File

@ -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<EntityMessage>())
{
gottenMockComponentIDPairs = GetComponents<MockComponent>(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<EntityMessage>())
{
var results = GetComponents<MockComponent>(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<ActivateComponentMessage>())
{
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<CheckHasMockComponentMessage>())
{
if (checkHasMockComponentMessage.shouldHaveComponent)
{
Assert.IsTrue(HasComponent<MockComponent>(checkHasMockComponentMessage.entity));
}
else
{
Assert.IsFalse(HasComponent<MockComponent>(checkHasMockComponentMessage.entity));
}
Assert.IsTrue(HasComponent<MockComponent>(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

24
test/EntityTest.cs Normal file
View File

@ -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);
}
}
}