add Spawner engine type

pull/5/head
Evan Hemsley 2019-06-28 22:07:48 -07:00
parent 53ec91d64f
commit a9d774a43f
5 changed files with 84 additions and 4 deletions

View File

@ -8,9 +8,9 @@ namespace Encompass
{ {
public abstract class Engine public abstract class Engine
{ {
private readonly List<Type> mutateComponentTypes = new List<Type>(); internal readonly List<Type> mutateComponentTypes = new List<Type>();
private readonly List<Type> emitMessageTypes = new List<Type>(); internal readonly List<Type> emitMessageTypes = new List<Type>();
private readonly List<Type> readMessageTypes = new List<Type>(); internal readonly List<Type> readMessageTypes = new List<Type>();
private EntityManager entityManager; private EntityManager entityManager;
private ComponentManager componentManager; private ComponentManager componentManager;

View File

@ -12,7 +12,7 @@ namespace Encompass.Engines
public IEnumerable<Type> ComponentTypes { get { return componentTypes; } } public IEnumerable<Type> ComponentTypes { get { return componentTypes; } }
protected Detector() protected Detector() : base()
{ {
var detectsAttribute = GetType().GetCustomAttribute<Detects>(false); var detectsAttribute = GetType().GetCustomAttribute<Detects>(false);
if (detectsAttribute != null) if (detectsAttribute != null)

View File

@ -0,0 +1,28 @@
using System.Reflection;
namespace Encompass.Engines
{
public abstract class Spawner<TMessage> : Engine where TMessage : struct, IMessage
{
protected Spawner() : base()
{
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
if (readsAttribute != null)
{
readsAttribute.readMessageTypes.Add(typeof(TMessage));
}
readMessageTypes.Add(typeof(TMessage));
}
public override void Update(double dt)
{
foreach (var message in ReadMessages<TMessage>())
{
Spawn(message);
}
}
protected abstract void Spawn(TMessage message);
}
}

View File

@ -36,6 +36,11 @@ namespace Encompass
return entityManager.CreateEntity(); return entityManager.CreateEntity();
} }
public void EmitMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
{
messageManager.AddMessage(message);
}
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
{ {
engine.AssignEntityManager(entityManager); engine.AssignEntityManager(entityManager);

47
test/SpawnerTest.cs Normal file
View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Text;
using Encompass;
using Encompass.Engines;
using NUnit.Framework;
namespace Tests
{
public class SpawnerTest
{
struct TestComponent : IComponent { }
struct SpawnMessageA : IMessage { }
static Entity resultEntity;
class TestSpawner : Spawner<SpawnMessageA>
{
protected override void Spawn(SpawnMessageA message)
{
resultEntity = CreateEntity();
resultEntity.AddComponent(new TestComponent());
}
}
[Test]
public void RunsSpawnMethodOnMessageRead()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new TestSpawner());
worldBuilder.EmitMessage(new SpawnMessageA());
var world = worldBuilder.Build();
world.Update(0.01);
Assert.That(resultEntity.HasComponent<TestComponent>(), Is.True);
var id = resultEntity.id;
world.Update(0.01);
Assert.That(resultEntity.id, Is.EqualTo(id));
}
}
}