encompass-cs/test/ComponentTest.cs

422 lines
14 KiB
C#

using System.ComponentModel;
using NUnit.Framework;
using FluentAssertions;
using Encompass;
using System.Collections.Generic;
using System;
using System.Linq;
using Encompass.Exceptions;
namespace Tests
{
public class ComponentTests
{
struct MockComponent : Encompass.IComponent
{
public string myString;
public int myInt;
}
struct EntityMessage : IMessage
{
public Entity entity;
}
static (Guid, MockComponent) gottenMockComponentIDPair;
[Receives(typeof(EntityMessage))]
[Reads(typeof(MockComponent))]
class GetMockComponentEngine : Engine
{
public override void Update(double dt)
{
foreach (var entityMessage in ReadMessages<EntityMessage>())
{
gottenMockComponentIDPair = GetComponent<MockComponent>(entityMessage.entity);
}
}
}
struct AddComponentTestMessage : IMessage
{
public Entity entity;
public MockComponent mockComponent;
}
[Receives(typeof(AddComponentTestMessage))]
[Reads(typeof(MockComponent))]
class AddComponentTestEngine : Engine
{
public override void Update(double dt)
{
foreach (var addComponentTestMessage in ReadMessages<AddComponentTestMessage>())
{
Assert.IsTrue(HasComponent<MockComponent>(addComponentTestMessage.entity));
Assert.That(GetComponent<MockComponent>(addComponentTestMessage.entity).Item2, Is.EqualTo(addComponentTestMessage.mockComponent));
}
}
}
[Test]
public void AddComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AddComponentTestEngine());
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent);
AddComponentTestMessage addComponentTestMessage;
addComponentTestMessage.entity = entity;
addComponentTestMessage.mockComponent = mockComponent;
worldBuilder.SendMessage(addComponentTestMessage);
var world = worldBuilder.Build();
world.Update(0.01);
}
[Test]
public void SetMultipleComponentOfSameTypeOnEntity()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new ReadMockComponentEngine());
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent { myInt = 20, myString = "what" }, 2);
worldBuilder.SetComponent(entity, new MockComponent { myInt = 50, myString = "hi" }, 0);
worldBuilder.SetComponent(entity, new MockComponent { myInt = 40, myString = "wassup" }, 1);
var world = worldBuilder.Build();
world.Update(0.01);
Assert.That(gottenMockComponentIDPair.Item2.myInt, Is.EqualTo(50));
Assert.That(gottenMockComponentIDPair.Item2.myString, Is.EqualTo("hi"));
}
[Reads(typeof(MockComponent))]
[WritesPending(typeof(MockComponent))]
[Writes(typeof(MockComponent))]
class OverwriteEngine : Engine
{
public override void Update(double dt)
{
foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>())
{
var entity = GetEntityByComponentID<MockComponent>(mockComponentID);
SetComponent(entity, new MockComponent { myInt = 420 });
}
}
}
[ReadsPending(typeof(MockComponent))]
[Reads(typeof(MockComponent))]
class ReadMockComponentEngine : Engine
{
public override void Update(double dt)
{
gottenMockComponentIDPair = ReadComponent<MockComponent>();
}
}
[Test]
public void EngineOverwriteComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new OverwriteEngine());
worldBuilder.AddEngine(new ReadMockComponentEngine());
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent {});
var world = worldBuilder.Build();
world.Update(0.01);
Assert.That(gottenMockComponentIDPair.Item2.myInt, Is.EqualTo(420));
}
[Reads(typeof(MockComponent))]
[Writes(typeof(MockComponent))]
class AddAndRemoveComponentEngine : Engine
{
public override void Update(double dt)
{
foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>())
{
var entity = GetEntityByComponentID<MockComponent>(mockComponentID);
SetComponent(entity, mockComponent);
RemoveComponent(mockComponentID);
}
}
}
[Test]
public void AddMultipleComponentSameFrameAsRemove()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AddAndRemoveComponentEngine());
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent());
var world = worldBuilder.Build();
Assert.DoesNotThrow(() => world.Update(0.01));
}
struct AddMockComponentMessage : IMessage
{
public Entity entity;
public MockComponent mockComponent;
}
[Sends(typeof(AddMockComponentMessage))]
class EmitMockComponentMessageEngine : Engine
{
private Entity entity;
public EmitMockComponentMessageEngine(Entity entity)
{
this.entity = entity;
}
public override void Update(double dt)
{
MockComponent mockComponent;
mockComponent.myInt = 10;
mockComponent.myString = "four";
AddMockComponentMessage addMockComponentMessage;
addMockComponentMessage.entity = entity;
addMockComponentMessage.mockComponent = mockComponent;
SendMessage(addMockComponentMessage);
}
}
[WritesPending(typeof(MockComponent))]
[Receives(typeof(AddMockComponentMessage))]
[Writes(typeof(MockComponent))]
class AddMockComponentEngine : Engine
{
public override void Update(double dt)
{
foreach (var message in ReadMessages<AddMockComponentMessage>())
{
SetComponent(message.entity, message.mockComponent);
}
}
}
[ReadsPending(typeof(MockComponent))]
class HasMockComponentEngine : Engine
{
private Entity entity;
public HasMockComponentEngine(Entity entity)
{
this.entity = entity;
}
public override void Update(double dt)
{
Assert.IsTrue(HasComponent<MockComponent>(entity));
}
}
[Test]
public void AddComponentAndReadSameFrame()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
worldBuilder.AddEngine(new EmitMockComponentMessageEngine(entity));
worldBuilder.AddEngine(new AddMockComponentEngine());
worldBuilder.AddEngine(new HasMockComponentEngine(entity));
var world = worldBuilder.Build();
world.Update(0.01);
}
[Test]
public void GetComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new GetMockComponentEngine());
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
var componentID = worldBuilder.SetComponent<MockComponent>(entity, mockComponent);
EntityMessage entityMessage;
entityMessage.entity = entity;
worldBuilder.SendMessage(entityMessage);
var world = worldBuilder.Build();
world.Update(0.01);
Assert.AreEqual((componentID, mockComponent), gottenMockComponentIDPair);
}
struct HasComponentTestMessage : IMessage
{
public Entity entity;
}
[Receives(typeof(HasComponentTestMessage))]
[Reads(typeof(MockComponent))]
class HasComponentTestEngine : Engine
{
public override void Update(double dt)
{
foreach (var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>())
{
Assert.IsTrue(HasComponent<MockComponent>(hasComponentTestEngine.entity));
}
}
}
[Test]
public void HasComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new HasComponentTestEngine());
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent);
HasComponentTestMessage hasComponentTestMessage;
hasComponentTestMessage.entity = entity;
worldBuilder.SendMessage(hasComponentTestMessage);
var world = worldBuilder.Build();
world.Update(0.01);
}
struct RemoveComponentTestMessage : IMessage
{
public Entity entity;
public Guid componentID;
}
[Receives(typeof(RemoveComponentTestMessage))]
class RemoveComponentTestEngine : Engine
{
public override void Update(double dt)
{
foreach (var removeComponentMessage in ReadMessages<RemoveComponentTestMessage>())
{
RemoveComponent(removeComponentMessage.componentID);
}
}
}
[Receives(typeof(RemoveComponentTestMessage))]
[Sends(typeof(CheckHasMockComponentMessage))]
class DoRemoveCheckEngine : Engine
{
private Entity entity;
public DoRemoveCheckEngine(Entity entity)
{
this.entity = entity;
}
public override void Update(double dt)
{
if (SomeMessage<RemoveComponentTestMessage>())
{
CheckHasMockComponentMessage checkHasMockComponentMessage;
checkHasMockComponentMessage.entity = entity;
checkHasMockComponentMessage.shouldHaveComponent = true;
SendMessage(checkHasMockComponentMessage);
}
else
{
CheckHasMockComponentMessage checkHasMockComponentMessage;
checkHasMockComponentMessage.entity = entity;
checkHasMockComponentMessage.shouldHaveComponent = false;
SendMessage(checkHasMockComponentMessage);
}
}
}
[Receives(typeof(CheckHasMockComponentMessage))]
[Reads(typeof(MockComponent))]
class CheckHasMockComponentEngine : Engine
{
public override void Update(double dt)
{
foreach (var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>())
{
Assert.IsTrue(HasComponent<MockComponent>(checkHasMockComponentMessage.entity));
}
}
}
[Test]
public void RemoveComponent()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
worldBuilder.AddEngine(new RemoveComponentTestEngine());
worldBuilder.AddEngine(new CheckHasMockComponentEngine());
worldBuilder.AddEngine(new DoRemoveCheckEngine(entity));
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
var componentID = worldBuilder.SetComponent(entity, mockComponent);
RemoveComponentTestMessage removeComponentMessage;
removeComponentMessage.entity = entity;
removeComponentMessage.componentID = componentID;
worldBuilder.SendMessage(removeComponentMessage);
var world = worldBuilder.Build();
world.Update(0.01f);
}
struct CheckHasMockComponentMessage : IMessage
{
public Entity entity;
public bool shouldHaveComponent;
}
[Receives(typeof(CheckHasMockComponentMessage))]
[ReadsPending(typeof(MockComponent))]
class CheckHasPendingMockComponentEngine : Engine
{
public override void Update(double dt)
{
foreach (var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>())
{
Assert.IsTrue(HasComponent<MockComponent>(checkHasMockComponentMessage.entity));
}
}
}
}
}