change WorldBuilder.AddEngine to take an instantiated Engine

pull/5/head
Evan Hemsley 2019-06-24 12:26:19 -07:00
parent b4347f4085
commit 9bb8fbda25
4 changed files with 34 additions and 39 deletions

View File

@ -36,16 +36,13 @@ namespace Encompass
return entityManager.CreateEntity(); return entityManager.CreateEntity();
} }
public Engine AddEngine<TEngine>() where TEngine : Engine, new() public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
{ {
var engine = new TEngine();
engine.AssignEntityManager(entityManager); engine.AssignEntityManager(entityManager);
engine.AssignComponentManager(componentManager); engine.AssignComponentManager(componentManager);
engine.AssignMessageManager(messageManager); engine.AssignMessageManager(messageManager);
engines.Add(engine); engines.Add(engine);
engineGraph.AddVertex(engine); engineGraph.AddVertex(engine);
if (engine is IEntityTracker) if (engine is IEntityTracker)

View File

@ -21,10 +21,8 @@ namespace Tests
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
Action addEngine = () => worldBuilder.AddEngine<NoComponentTypesDetector>(); Action addEngine = () => worldBuilder.AddEngine(new NoComponentTypesDetector());
addEngine.Should() addEngine.Should().Throw<DetectorWithoutComponentTypesException>();
.Throw<System.Reflection.TargetInvocationException>()
.WithInnerException<DetectorWithoutComponentTypesException>();
} }
struct AComponent : IComponent { } struct AComponent : IComponent { }
@ -46,7 +44,7 @@ namespace Tests
public void CheckAndTrackEntities() public void CheckAndTrackEntities()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
var detector = worldBuilder.AddEngine<TestDetector>(); var detector = worldBuilder.AddEngine(new TestDetector());
var entityToTrack = worldBuilder.CreateEntity(); var entityToTrack = worldBuilder.CreateEntity();
entityToTrack.AddComponent(new AComponent()); entityToTrack.AddComponent(new AComponent());
@ -80,7 +78,7 @@ namespace Tests
public void EntityUntrackedWhenComponentRemoved() public void EntityUntrackedWhenComponentRemoved()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<TestDetector>(); worldBuilder.AddEngine(new TestDetector());
var entityToUntrack = worldBuilder.CreateEntity(); var entityToUntrack = worldBuilder.CreateEntity();
entityToUntrack.AddComponent(new AComponent()); entityToUntrack.AddComponent(new AComponent());
@ -102,7 +100,7 @@ namespace Tests
public void DetectCalledPerTrackedEntityOnWorldUpdat() public void DetectCalledPerTrackedEntityOnWorldUpdat()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<TestDetector>(); worldBuilder.AddEngine(new TestDetector());
var entityOne = worldBuilder.CreateEntity(); var entityOne = worldBuilder.CreateEntity();
entityOne.AddComponent(new AComponent()); entityOne.AddComponent(new AComponent());

View File

@ -37,7 +37,7 @@ namespace Tests
public void ReadComponents() public void ReadComponents()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<ReadComponentsTestEngine>(); worldBuilder.AddEngine(new ReadComponentsTestEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
@ -65,7 +65,7 @@ namespace Tests
public void ReadComponent() public void ReadComponent()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<ReadComponentTestEngine>(); worldBuilder.AddEngine(new ReadComponentTestEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
@ -86,7 +86,7 @@ namespace Tests
public void ReadComponentWhenMultipleComponents() public void ReadComponentWhenMultipleComponents()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<ReadComponentTestEngine>(); worldBuilder.AddEngine(new ReadComponentTestEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
@ -125,7 +125,7 @@ namespace Tests
public void UpdateComponent() public void UpdateComponent()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<UpdateComponentTestEngine>(); worldBuilder.AddEngine(new UpdateComponentTestEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
@ -161,7 +161,7 @@ namespace Tests
public void UpdateUndeclaredComponent() public void UpdateUndeclaredComponent()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<UndeclaredUpdateComponentTestEngine>(); worldBuilder.AddEngine(new UndeclaredUpdateComponentTestEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
@ -207,8 +207,8 @@ namespace Tests
public void EmitAndReadMessage() public void EmitAndReadMessage()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<MessageEmitEngine>(); worldBuilder.AddEngine(new MessageEmitEngine());
worldBuilder.AddEngine<MessageReadEngine>(); worldBuilder.AddEngine(new MessageReadEngine());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -232,7 +232,7 @@ namespace Tests
public void EmitUndeclaredMessage() public void EmitUndeclaredMessage()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<UndeclaredMessageEmitEngine>(); worldBuilder.AddEngine(new UndeclaredMessageEmitEngine());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -267,8 +267,8 @@ namespace Tests
public void Some() public void Some()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<EmitMockMessageEngine>(); worldBuilder.AddEngine(new EmitMockMessageEngine());
worldBuilder.AddEngine<SomeTestEngine>(); worldBuilder.AddEngine(new SomeTestEngine());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -289,8 +289,8 @@ namespace Tests
public void IllegalSome() public void IllegalSome()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<EmitMockMessageEngine>(); worldBuilder.AddEngine(new EmitMockMessageEngine());
worldBuilder.AddEngine<UndeclaredSomeEngine>(); worldBuilder.AddEngine(new UndeclaredSomeEngine());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -316,7 +316,7 @@ namespace Tests
public void SameValueComponents() public void SameValueComponents()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<SameValueComponentReadEngine>(); worldBuilder.AddEngine(new SameValueComponentReadEngine());
MockComponent componentA; MockComponent componentA;
componentA.myInt = 20; componentA.myInt = 20;
@ -351,7 +351,7 @@ namespace Tests
public void ReadComponentsOfTypeWhereNoneExist() public void ReadComponentsOfTypeWhereNoneExist()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<ReadEmptyMockComponentsEngine>(); worldBuilder.AddEngine(new ReadEmptyMockComponentsEngine());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01f); world.Update(0.01f);
@ -389,8 +389,8 @@ namespace Tests
public void DestroyEntity() public void DestroyEntity()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<DestroyerEngine>(); worldBuilder.AddEngine(new DestroyerEngine());
worldBuilder.AddEngine<ReaderEngine>(); worldBuilder.AddEngine(new ReaderEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
var entityB = worldBuilder.CreateEntity(); var entityB = worldBuilder.CreateEntity();

View File

@ -39,8 +39,8 @@ namespace Tests
public void EngineCycle() public void EngineCycle()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<AEngine>(); worldBuilder.AddEngine(new AEngine());
worldBuilder.AddEngine<BEngine>(); worldBuilder.AddEngine(new BEngine());
Assert.Throws<EngineCycleException>(() => worldBuilder.Build()); Assert.Throws<EngineCycleException>(() => worldBuilder.Build());
} }
@ -101,10 +101,10 @@ namespace Tests
public void EngineCycle() public void EngineCycle()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<AEngine>(); worldBuilder.AddEngine(new AEngine());
worldBuilder.AddEngine<BEngine>(); worldBuilder.AddEngine(new BEngine());
worldBuilder.AddEngine<CEngine>(); worldBuilder.AddEngine(new CEngine());
worldBuilder.AddEngine<DEngine>(); worldBuilder.AddEngine(new DEngine());
Assert.Throws<EngineCycleException>(() => worldBuilder.Build()); Assert.Throws<EngineCycleException>(() => worldBuilder.Build());
} }
@ -130,8 +130,8 @@ namespace Tests
public void MutationConflictException() public void MutationConflictException()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<AEngine>(); worldBuilder.AddEngine(new AEngine());
worldBuilder.AddEngine<BEngine>(); worldBuilder.AddEngine(new BEngine());
Assert.Throws<EngineMutationConflictException>(() => worldBuilder.Build()); Assert.Throws<EngineMutationConflictException>(() => worldBuilder.Build());
} }
@ -193,10 +193,10 @@ namespace Tests
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
var engineA = worldBuilder.AddEngine<AEngine>(); var engineA = worldBuilder.AddEngine(new AEngine());
var engineB = worldBuilder.AddEngine<BEngine>(); var engineB = worldBuilder.AddEngine(new BEngine());
var engineC = worldBuilder.AddEngine<CEngine>(); var engineC = worldBuilder.AddEngine(new CEngine());
var engineD = worldBuilder.AddEngine<DEngine>(); var engineD = worldBuilder.AddEngine(new DEngine());
Assert.DoesNotThrow(() => worldBuilder.Build()); Assert.DoesNotThrow(() => worldBuilder.Build());