2019-06-15 00:51:06 +00:00
|
|
|
using NUnit.Framework;
|
2019-06-17 18:33:38 +00:00
|
|
|
using FluentAssertions;
|
2019-06-15 00:51:06 +00:00
|
|
|
|
|
|
|
using Encompass;
|
2019-06-17 18:33:38 +00:00
|
|
|
|
2019-06-15 01:13:24 +00:00
|
|
|
using System;
|
2019-06-17 18:33:38 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Collections.Generic;
|
2019-06-24 19:14:37 +00:00
|
|
|
using Encompass.Exceptions;
|
2019-06-15 00:51:06 +00:00
|
|
|
|
2019-06-15 19:32:56 +00:00
|
|
|
namespace Tests
|
|
|
|
{
|
2019-07-17 18:24:21 +00:00
|
|
|
struct MockComponent : IComponent
|
|
|
|
{
|
|
|
|
public int myInt;
|
|
|
|
public string myString;
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:51:06 +00:00
|
|
|
public class EngineTest
|
|
|
|
{
|
2019-06-27 23:55:12 +00:00
|
|
|
static List<ValueTuple<Guid, MockComponent>> resultComponents;
|
2019-06-15 18:40:42 +00:00
|
|
|
static MockComponent resultComponent;
|
2019-06-15 00:51:06 +00:00
|
|
|
|
2019-06-17 01:03:57 +00:00
|
|
|
static List<MockMessage> resultMessages;
|
2019-06-16 01:55:35 +00:00
|
|
|
|
2019-06-15 01:13:24 +00:00
|
|
|
public class ReadComponentsTestEngine : Engine
|
2019-06-15 00:51:06 +00:00
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-15 00:51:06 +00:00
|
|
|
{
|
2019-06-27 23:55:12 +00:00
|
|
|
resultComponents = ReadComponents<MockComponent>().ToList();
|
2019-06-15 00:51:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 01:13:24 +00:00
|
|
|
public class ReadComponentTestEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-15 01:13:24 +00:00
|
|
|
{
|
2019-06-27 23:55:12 +00:00
|
|
|
resultComponent = ReadComponent<MockComponent>().Item2;
|
2019-06-15 01:13:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:51:06 +00:00
|
|
|
[Test]
|
|
|
|
public void ReadComponents()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new ReadComponentsTestEngine());
|
2019-06-15 00:51:06 +00:00
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
|
|
|
|
MockComponent mockComponent;
|
|
|
|
mockComponent.myInt = 0;
|
|
|
|
mockComponent.myString = "hello";
|
|
|
|
|
|
|
|
MockComponent mockComponentB;
|
|
|
|
mockComponentB.myInt = 1;
|
|
|
|
mockComponentB.myString = "howdy";
|
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
var componentAID = worldBuilder.AddComponent(entity, mockComponent);
|
|
|
|
var componentBID = worldBuilder.AddComponent(entity, mockComponentB);
|
|
|
|
var inactiveComponentAID = worldBuilder.AddComponent(entity, mockComponent);
|
2019-07-10 16:53:38 +00:00
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.DeactivateComponent(inactiveComponentAID);
|
2019-06-15 00:51:06 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
2019-06-27 23:55:12 +00:00
|
|
|
var resultComponentValues = resultComponents.Select((kv) => kv.Item2);
|
2019-06-17 18:33:38 +00:00
|
|
|
resultComponentValues.Should().Contain(mockComponent);
|
|
|
|
resultComponentValues.Should().Contain(mockComponentB);
|
2019-07-10 16:53:38 +00:00
|
|
|
resultComponents.Should().NotContain((inactiveComponentAID, mockComponent));
|
2019-06-15 00:51:06 +00:00
|
|
|
}
|
2019-06-15 01:13:24 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void ReadComponent()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new ReadComponentTestEngine());
|
2019-06-15 01:13:24 +00:00
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
|
|
|
|
MockComponent mockComponent;
|
|
|
|
mockComponent.myInt = 0;
|
|
|
|
mockComponent.myString = "hello";
|
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, mockComponent);
|
2019-06-15 01:13:24 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
2019-06-15 18:40:42 +00:00
|
|
|
Assert.AreEqual(mockComponent, resultComponent);
|
2019-06-15 01:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void ReadComponentWhenMultipleComponents()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new ReadComponentTestEngine());
|
2019-06-15 01:13:24 +00:00
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
|
|
|
|
MockComponent mockComponent;
|
|
|
|
mockComponent.myInt = 0;
|
|
|
|
mockComponent.myString = "hello";
|
|
|
|
|
|
|
|
MockComponent mockComponentB;
|
|
|
|
mockComponentB.myInt = 1;
|
|
|
|
mockComponentB.myString = "howdy";
|
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, mockComponent);
|
|
|
|
worldBuilder.AddComponent(entity, mockComponentB);
|
2019-06-15 01:13:24 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
Assert.Throws<InvalidOperationException>(() => world.Update(0.01f));
|
|
|
|
}
|
2019-06-15 07:39:08 +00:00
|
|
|
|
2019-07-16 16:47:58 +00:00
|
|
|
[Writes(typeof(MockComponent))]
|
2019-06-15 07:39:08 +00:00
|
|
|
public class UpdateComponentTestEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-15 07:39:08 +00:00
|
|
|
{
|
2019-06-27 23:55:12 +00:00
|
|
|
(var componentID, var component) = ReadComponent<MockComponent>();
|
2019-06-17 18:33:38 +00:00
|
|
|
|
|
|
|
component.myInt = 420;
|
|
|
|
component.myString = "blaze it";
|
2019-06-27 23:55:12 +00:00
|
|
|
UpdateComponent(componentID, component);
|
2019-06-17 18:33:38 +00:00
|
|
|
|
2019-06-27 23:55:12 +00:00
|
|
|
resultComponent = ReadComponent<MockComponent>().Item2;
|
2019-06-15 07:39:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateComponent()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new UpdateComponentTestEngine());
|
2019-06-15 07:39:08 +00:00
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
|
|
|
|
MockComponent mockComponent;
|
|
|
|
mockComponent.myInt = 0;
|
|
|
|
mockComponent.myString = "hello";
|
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, mockComponent);
|
2019-06-15 07:39:08 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
2019-06-15 18:40:42 +00:00
|
|
|
Assert.AreEqual(420, resultComponent.myInt);
|
|
|
|
Assert.AreEqual("blaze it", resultComponent.myString);
|
2019-06-15 07:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class UndeclaredUpdateComponentTestEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-15 07:39:08 +00:00
|
|
|
{
|
2019-06-17 18:33:38 +00:00
|
|
|
(var componentID, var component) = this.ReadComponent<MockComponent>();
|
|
|
|
|
|
|
|
component.myInt = 420;
|
|
|
|
component.myString = "blaze it";
|
2019-06-27 23:55:12 +00:00
|
|
|
UpdateComponent(componentID, component);
|
2019-06-17 18:33:38 +00:00
|
|
|
|
2019-06-27 23:55:12 +00:00
|
|
|
component = ReadComponent<MockComponent>().Item2;
|
2019-06-15 07:39:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateUndeclaredComponent()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new UndeclaredUpdateComponentTestEngine());
|
2019-06-15 07:39:08 +00:00
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
|
|
|
|
MockComponent mockComponent;
|
|
|
|
mockComponent.myInt = 0;
|
|
|
|
mockComponent.myString = "hello";
|
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, mockComponent);
|
2019-06-15 07:39:08 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
2019-07-16 18:17:07 +00:00
|
|
|
var ex = Assert.Throws<IllegalWriteException>(() => world.Update(0.01f));
|
2019-07-16 16:47:58 +00:00
|
|
|
Assert.That(ex.Message, Is.EqualTo("Engine UndeclaredUpdateComponentTestEngine tried to write undeclared Component MockComponent"));
|
2019-06-15 07:39:08 +00:00
|
|
|
}
|
2019-06-16 01:55:35 +00:00
|
|
|
|
|
|
|
struct MockMessage : IMessage
|
|
|
|
{
|
|
|
|
public string myString;
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:47:58 +00:00
|
|
|
[Writes(typeof(MockMessage))]
|
2019-06-16 01:55:35 +00:00
|
|
|
public class MessageEmitEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-16 01:55:35 +00:00
|
|
|
{
|
|
|
|
MockMessage message;
|
|
|
|
message.myString = "howdy";
|
|
|
|
|
|
|
|
this.EmitMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Reads(typeof(MockMessage))]
|
|
|
|
public class MessageReadEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-16 01:55:35 +00:00
|
|
|
{
|
2019-06-17 01:03:57 +00:00
|
|
|
resultMessages = this.ReadMessages<MockMessage>().ToList();
|
2019-06-16 01:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void EmitAndReadMessage()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new MessageEmitEngine());
|
|
|
|
worldBuilder.AddEngine(new MessageReadEngine());
|
2019-06-16 01:55:35 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
|
|
|
Assert.AreEqual(resultMessages.First().myString, "howdy");
|
|
|
|
}
|
|
|
|
|
|
|
|
public class UndeclaredMessageEmitEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-16 01:55:35 +00:00
|
|
|
{
|
|
|
|
MockMessage message;
|
|
|
|
message.myString = "howdy";
|
|
|
|
|
|
|
|
this.EmitMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 00:23:52 +00:00
|
|
|
static IEnumerable<MockMessage> emptyReadMessagesResult;
|
2019-07-18 01:12:29 +00:00
|
|
|
|
2019-06-22 00:23:52 +00:00
|
|
|
[Reads(typeof(MockMessage))]
|
|
|
|
class ReadMessagesWhenNoneExistEngine : Engine
|
|
|
|
{
|
2019-06-24 19:40:40 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-22 00:23:52 +00:00
|
|
|
{
|
|
|
|
emptyReadMessagesResult = ReadMessages<MockMessage>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void ReadMessagesWhenNoneHaveBeenEmitted()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:40:40 +00:00
|
|
|
worldBuilder.AddEngine(new ReadMessagesWhenNoneExistEngine());
|
2019-06-22 00:23:52 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
|
|
|
emptyReadMessagesResult.Should().BeEmpty();
|
|
|
|
}
|
|
|
|
|
2019-06-16 01:55:35 +00:00
|
|
|
[Test]
|
|
|
|
public void EmitUndeclaredMessage()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new UndeclaredMessageEmitEngine());
|
2019-06-16 01:55:35 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
2019-07-16 18:17:07 +00:00
|
|
|
var ex = Assert.Throws<IllegalWriteException>(() => world.Update(0.01f));
|
2019-07-18 01:12:29 +00:00
|
|
|
Assert.That(ex.Message, Is.EqualTo("Engine UndeclaredMessageEmitEngine tried to write undeclared Message MockMessage"));
|
2019-06-16 01:55:35 +00:00
|
|
|
}
|
2019-06-17 01:11:35 +00:00
|
|
|
|
|
|
|
static bool someTest;
|
|
|
|
|
2019-07-16 16:47:58 +00:00
|
|
|
[Writes(typeof(MockMessage))]
|
2019-06-17 01:11:35 +00:00
|
|
|
class EmitMockMessageEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-17 01:11:35 +00:00
|
|
|
{
|
|
|
|
MockMessage message;
|
|
|
|
message.myString = "howdy";
|
|
|
|
|
|
|
|
this.EmitMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Reads(typeof(MockMessage))]
|
2019-07-17 18:46:54 +00:00
|
|
|
class SomeMessageTestEngine : Engine
|
2019-06-17 01:11:35 +00:00
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-17 01:11:35 +00:00
|
|
|
{
|
2019-07-17 18:46:54 +00:00
|
|
|
someTest = this.SomeMessage<MockMessage>();
|
2019-06-17 01:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2019-07-17 18:46:54 +00:00
|
|
|
public void SomeMessage()
|
2019-06-17 01:11:35 +00:00
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new EmitMockMessageEngine());
|
2019-07-17 18:46:54 +00:00
|
|
|
worldBuilder.AddEngine(new SomeMessageTestEngine());
|
2019-06-17 01:11:35 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
|
|
|
Assert.That(someTest, Is.True);
|
|
|
|
}
|
2019-06-17 01:16:45 +00:00
|
|
|
|
2019-07-17 18:46:54 +00:00
|
|
|
class UndeclaredSomeMessageEngine : Engine
|
2019-06-17 01:16:45 +00:00
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-17 01:16:45 +00:00
|
|
|
{
|
2019-07-17 18:46:54 +00:00
|
|
|
someTest = this.SomeMessage<MockMessage>();
|
2019-06-17 01:16:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2019-07-17 18:46:54 +00:00
|
|
|
public void UndeclaredSomeMessage()
|
2019-06-17 01:16:45 +00:00
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new EmitMockMessageEngine());
|
2019-07-17 18:46:54 +00:00
|
|
|
worldBuilder.AddEngine(new UndeclaredSomeMessageEngine());
|
2019-06-17 01:16:45 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
2019-07-16 18:17:07 +00:00
|
|
|
Assert.Throws<IllegalReadException>(() => world.Update(0.01f));
|
2019-06-17 01:16:45 +00:00
|
|
|
}
|
2019-06-17 19:12:07 +00:00
|
|
|
|
2019-07-17 18:46:54 +00:00
|
|
|
class SomeComponentTestEngine : Engine
|
|
|
|
{
|
|
|
|
public override void Update(double dt)
|
|
|
|
{
|
|
|
|
Assert.IsTrue(SomeComponent<MockComponent>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void SomeComponent()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
worldBuilder.AddComponent(entity, new MockComponent());
|
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
world.Update(0.01);
|
|
|
|
}
|
|
|
|
|
2019-06-27 23:55:12 +00:00
|
|
|
static ValueTuple<Guid, MockComponent> pairA;
|
|
|
|
static ValueTuple<Guid, MockComponent> pairB;
|
2019-06-17 19:12:07 +00:00
|
|
|
|
|
|
|
class SameValueComponentReadEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-17 19:12:07 +00:00
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
var components = ReadComponents<MockComponent>();
|
2019-06-17 19:12:07 +00:00
|
|
|
|
|
|
|
pairA = components.First();
|
|
|
|
pairB = components.Last();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that components with identical values should be distinguishable by ID
|
|
|
|
[Test]
|
|
|
|
public void SameValueComponents()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new SameValueComponentReadEngine());
|
2019-06-17 19:12:07 +00:00
|
|
|
|
|
|
|
MockComponent componentA;
|
|
|
|
componentA.myInt = 20;
|
|
|
|
componentA.myString = "hello";
|
|
|
|
|
|
|
|
MockComponent componentB;
|
|
|
|
componentB.myInt = 20;
|
|
|
|
componentB.myString = "hello";
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, componentA);
|
|
|
|
worldBuilder.AddComponent(entity, componentB);
|
2019-06-17 19:12:07 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
|
|
|
Assert.That(pairA, Is.Not.EqualTo(pairB));
|
2019-06-27 23:55:12 +00:00
|
|
|
Assert.That(pairA.Item2, Is.EqualTo(pairB.Item2));
|
2019-06-17 19:12:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 23:55:12 +00:00
|
|
|
static IEnumerable<ValueTuple<Guid, MockComponent>> emptyComponentReadResult;
|
2019-06-17 19:12:07 +00:00
|
|
|
|
|
|
|
class ReadEmptyMockComponentsEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-17 19:12:07 +00:00
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
emptyComponentReadResult = ReadComponents<MockComponent>();
|
2019-06-17 19:12:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void ReadComponentsOfTypeWhereNoneExist()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new ReadEmptyMockComponentsEngine());
|
2019-06-17 19:12:07 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
|
|
|
Assert.That(emptyComponentReadResult, Is.Empty);
|
|
|
|
}
|
2019-06-20 03:37:46 +00:00
|
|
|
|
|
|
|
struct DestroyerComponent : IComponent { }
|
|
|
|
|
|
|
|
class DestroyerEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-20 03:37:46 +00:00
|
|
|
{
|
2019-06-27 23:55:12 +00:00
|
|
|
foreach (var componentPair in ReadComponents<DestroyerComponent>())
|
2019-06-20 03:37:46 +00:00
|
|
|
{
|
2019-06-27 23:55:12 +00:00
|
|
|
var componentID = componentPair.Item1;
|
2019-06-22 00:50:01 +00:00
|
|
|
var entityID = GetEntityIDByComponentID(componentID);
|
2019-06-20 03:37:46 +00:00
|
|
|
Destroy(entityID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 23:55:12 +00:00
|
|
|
static IEnumerable<ValueTuple<Guid, MockComponent>> results;
|
2019-07-16 18:17:07 +00:00
|
|
|
|
2019-06-20 03:37:46 +00:00
|
|
|
class ReaderEngine : Engine
|
|
|
|
{
|
2019-06-24 19:14:37 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-20 03:37:46 +00:00
|
|
|
{
|
|
|
|
results = ReadComponents<MockComponent>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void DestroyEntity()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:26:19 +00:00
|
|
|
worldBuilder.AddEngine(new DestroyerEngine());
|
|
|
|
worldBuilder.AddEngine(new ReaderEngine());
|
2019-06-20 03:37:46 +00:00
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
var entityB = worldBuilder.CreateEntity();
|
|
|
|
|
|
|
|
DestroyerComponent destroyerComponent;
|
|
|
|
MockComponent mockComponent;
|
|
|
|
mockComponent.myInt = 2;
|
|
|
|
mockComponent.myString = "blah";
|
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, destroyerComponent);
|
|
|
|
var componentID = worldBuilder.AddComponent(entity, mockComponent);
|
2019-06-20 03:37:46 +00:00
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entityB, destroyerComponent);
|
|
|
|
var componentBID = worldBuilder.AddComponent(entityB, mockComponent);
|
2019-06-20 03:37:46 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
2019-07-11 01:22:08 +00:00
|
|
|
Assert.That(results, Does.Not.Contain((componentID, mockComponent)));
|
|
|
|
Assert.That(results, Does.Not.Contain((componentBID, mockComponent)));
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
[Writes(typeof(MockComponent))]
|
2019-07-11 01:22:08 +00:00
|
|
|
class DestroyAndAddComponentEngine : Engine
|
|
|
|
{
|
|
|
|
public override void Update(double dt)
|
|
|
|
{
|
|
|
|
foreach (var componentPair in ReadComponents<DestroyerComponent>())
|
|
|
|
{
|
|
|
|
var componentID = componentPair.Item1;
|
|
|
|
var entity = GetEntityByComponentID(componentID);
|
2019-07-17 18:24:21 +00:00
|
|
|
var (id, _) = GetComponent<MockComponent>(entity);
|
|
|
|
RemoveComponent(id);
|
|
|
|
Destroy(entity.ID);
|
2019-07-11 01:22:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void DestroyEntityWhileRemovingComponent()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
|
|
|
worldBuilder.AddEngine(new DestroyAndAddComponentEngine());
|
|
|
|
worldBuilder.AddEngine(new ReaderEngine());
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, new DestroyerComponent());
|
|
|
|
worldBuilder.AddComponent(entity, new MockComponent());
|
2019-07-11 01:22:08 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
Assert.DoesNotThrow(() => world.Update(0.01));
|
2019-06-20 03:37:46 +00:00
|
|
|
}
|
2019-06-22 00:44:07 +00:00
|
|
|
|
|
|
|
static Entity entityFromComponentIDResult;
|
2019-07-16 18:17:07 +00:00
|
|
|
|
2019-06-22 00:44:07 +00:00
|
|
|
class GetEntityFromComponentIDEngine : Engine
|
|
|
|
{
|
2019-06-24 19:40:40 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-22 00:44:07 +00:00
|
|
|
{
|
2019-06-27 23:55:12 +00:00
|
|
|
var componentID = ReadComponent<MockComponent>().Item1;
|
2019-06-22 00:50:01 +00:00
|
|
|
entityFromComponentIDResult = GetEntityByComponentID(componentID);
|
2019-06-22 00:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetEntityFromComponentID()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:40:40 +00:00
|
|
|
worldBuilder.AddEngine(new GetEntityFromComponentIDEngine());
|
2019-06-22 00:44:07 +00:00
|
|
|
|
|
|
|
MockComponent component;
|
|
|
|
component.myInt = 2;
|
|
|
|
component.myString = "howdy";
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, component);
|
2019-06-22 00:44:07 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
|
|
|
Assert.That(entity, Is.EqualTo(entityFromComponentIDResult));
|
|
|
|
}
|
|
|
|
|
|
|
|
static MockComponent mockComponentByIDResult;
|
2019-07-16 18:17:07 +00:00
|
|
|
|
2019-06-22 00:44:07 +00:00
|
|
|
class GetComponentByIDEngine : Engine
|
|
|
|
{
|
2019-06-24 19:40:40 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-22 00:44:07 +00:00
|
|
|
{
|
2019-06-27 23:55:12 +00:00
|
|
|
var componentID = ReadComponent<MockComponent>().Item1;
|
2019-06-22 00:44:07 +00:00
|
|
|
mockComponentByIDResult = GetComponentByID<MockComponent>(componentID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[Test]
|
|
|
|
public void GetComponentByID()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:40:40 +00:00
|
|
|
worldBuilder.AddEngine(new GetComponentByIDEngine());
|
2019-06-22 00:44:07 +00:00
|
|
|
|
|
|
|
MockComponent component;
|
|
|
|
component.myInt = 2;
|
|
|
|
component.myString = "howdy";
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, component);
|
2019-06-22 00:44:07 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
world.Update(0.01f);
|
|
|
|
|
|
|
|
Assert.That(component, Is.EqualTo(mockComponentByIDResult));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OtherComponent : IComponent { }
|
|
|
|
|
|
|
|
class GetComponentByIDWithTypeMismatchEngine : Engine
|
|
|
|
{
|
2019-06-24 19:40:40 +00:00
|
|
|
public override void Update(double dt)
|
2019-06-22 00:44:07 +00:00
|
|
|
{
|
2019-06-27 23:55:12 +00:00
|
|
|
var componentID = ReadComponent<MockComponent>().Item1;
|
2019-06-22 00:44:07 +00:00
|
|
|
GetComponentByID<OtherComponent>(componentID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetComponentByIDWithTypeMismatch()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-24 19:40:40 +00:00
|
|
|
worldBuilder.AddEngine(new GetComponentByIDWithTypeMismatchEngine());
|
2019-06-22 00:44:07 +00:00
|
|
|
|
|
|
|
MockComponent component;
|
|
|
|
component.myInt = 2;
|
|
|
|
component.myString = "howdy";
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, component);
|
2019-06-22 00:44:07 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
Assert.Throws<ComponentTypeMismatchException>(() => world.Update(0.01f));
|
|
|
|
}
|
2019-07-13 00:37:31 +00:00
|
|
|
|
|
|
|
struct EntityIDComponent : IComponent { public Guid entityID; }
|
|
|
|
static bool hasEntity;
|
2019-07-16 18:17:07 +00:00
|
|
|
|
2019-07-13 00:37:31 +00:00
|
|
|
class HasEntityTestEngine : Engine
|
|
|
|
{
|
|
|
|
public override void Update(double dt)
|
|
|
|
{
|
|
|
|
foreach (var (mockComponentID, mockComponent) in ReadComponents<EntityIDComponent>())
|
|
|
|
{
|
|
|
|
hasEntity = EntityExists(mockComponent.entityID);
|
|
|
|
if (hasEntity) { Destroy(mockComponent.entityID); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void EntityExists()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
|
|
|
worldBuilder.AddEngine(new HasEntityTestEngine());
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
var entityTwo = worldBuilder.CreateEntity();
|
|
|
|
|
|
|
|
EntityIDComponent entityIDComponent;
|
2019-07-17 18:24:21 +00:00
|
|
|
entityIDComponent.entityID = entityTwo.ID;
|
2019-07-13 00:37:31 +00:00
|
|
|
|
2019-07-17 18:24:21 +00:00
|
|
|
worldBuilder.AddComponent(entity, entityIDComponent);
|
2019-07-13 00:37:31 +00:00
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
world.Update(0.01);
|
|
|
|
|
|
|
|
Assert.IsTrue(hasEntity);
|
|
|
|
|
|
|
|
world.Update(0.01);
|
|
|
|
|
|
|
|
Assert.IsFalse(hasEntity);
|
|
|
|
}
|
2019-07-18 01:12:29 +00:00
|
|
|
|
|
|
|
[Writes(typeof(MockComponent), typeof(MockMessage))]
|
|
|
|
class EngineThatWritesComponentAndMessage : Engine
|
|
|
|
{
|
|
|
|
public override void Update(double dt) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void EngineWritesComponentAndMessage()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
|
|
|
|
|
|
|
Assert.Throws<ComponentAndMessageWriteException>(() => worldBuilder.AddEngine(new EngineThatWritesComponentAndMessage()));
|
|
|
|
}
|
2019-06-15 00:51:06 +00:00
|
|
|
}
|
|
|
|
}
|