add Spawner engine type
parent
53ec91d64f
commit
a9d774a43f
|
@ -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;
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue