From a9d774a43f01ea6346adc1d49a650a5bd145290d Mon Sep 17 00:00:00 2001 From: Evan Hemsley <2342303+ehemsley@users.noreply.github.com> Date: Fri, 28 Jun 2019 22:07:48 -0700 Subject: [PATCH] add Spawner engine type --- encompass-cs/Engine.cs | 6 ++-- encompass-cs/Engines/Detector.cs | 2 +- encompass-cs/Engines/Spawner.cs | 28 +++++++++++++++++++ encompass-cs/WorldBuilder.cs | 5 ++++ test/SpawnerTest.cs | 47 ++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 encompass-cs/Engines/Spawner.cs create mode 100644 test/SpawnerTest.cs diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 80c9b30..2257b39 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -8,9 +8,9 @@ namespace Encompass { public abstract class Engine { - private readonly List mutateComponentTypes = new List(); - private readonly List emitMessageTypes = new List(); - private readonly List readMessageTypes = new List(); + internal readonly List mutateComponentTypes = new List(); + internal readonly List emitMessageTypes = new List(); + internal readonly List readMessageTypes = new List(); private EntityManager entityManager; private ComponentManager componentManager; diff --git a/encompass-cs/Engines/Detector.cs b/encompass-cs/Engines/Detector.cs index 4cbd64c..4ead6ac 100644 --- a/encompass-cs/Engines/Detector.cs +++ b/encompass-cs/Engines/Detector.cs @@ -12,7 +12,7 @@ namespace Encompass.Engines public IEnumerable ComponentTypes { get { return componentTypes; } } - protected Detector() + protected Detector() : base() { var detectsAttribute = GetType().GetCustomAttribute(false); if (detectsAttribute != null) diff --git a/encompass-cs/Engines/Spawner.cs b/encompass-cs/Engines/Spawner.cs new file mode 100644 index 0000000..c91e4bf --- /dev/null +++ b/encompass-cs/Engines/Spawner.cs @@ -0,0 +1,28 @@ +using System.Reflection; + +namespace Encompass.Engines +{ + public abstract class Spawner : Engine where TMessage : struct, IMessage + { + protected Spawner() : base() + { + var readsAttribute = GetType().GetCustomAttribute(false); + if (readsAttribute != null) + { + readsAttribute.readMessageTypes.Add(typeof(TMessage)); + } + + readMessageTypes.Add(typeof(TMessage)); + } + + public override void Update(double dt) + { + foreach (var message in ReadMessages()) + { + Spawn(message); + } + } + + protected abstract void Spawn(TMessage message); + } +} diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index 68f1560..1c18b9b 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -36,6 +36,11 @@ namespace Encompass return entityManager.CreateEntity(); } + public void EmitMessage(TMessage message) where TMessage : struct, IMessage + { + messageManager.AddMessage(message); + } + public Engine AddEngine(TEngine engine) where TEngine : Engine { engine.AssignEntityManager(entityManager); diff --git a/test/SpawnerTest.cs b/test/SpawnerTest.cs new file mode 100644 index 0000000..d4a3ce0 --- /dev/null +++ b/test/SpawnerTest.cs @@ -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 + { + 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(), Is.True); + + var id = resultEntity.id; + + world.Update(0.01); + + Assert.That(resultEntity.id, Is.EqualTo(id)); + } + } +}