Merge pull request #1 from encompass-ecs/special_engines

Detectors and some API Changes
pull/5/head
thatcosmonaut 2019-06-24 12:35:32 -07:00 committed by GitHub
commit c7a4297cff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 281 additions and 71 deletions

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
namespace Encompass
{
[AttributeUsage(AttributeTargets.Class)]
public class Detects : Attribute
{
public readonly List<Type> componentTypes;
public Detects(params Type[] componentTypes)
{
this.componentTypes = new List<Type>(componentTypes);
}
}
}

View File

@ -2,6 +2,7 @@ using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
@ -51,7 +52,7 @@ namespace Encompass
this.messageManager = messageManager;
}
public abstract void Update(float dt);
public abstract void Update(double dt);
protected Entity CreateEntity()
{

View File

@ -0,0 +1,59 @@
using System;
using System.Reflection;
using System.Collections.Generic;
using Encompass.Exceptions;
namespace Encompass.Engines
{
public abstract class Detector : Engine, IEntityTracker
{
private readonly List<Type> componentTypes = new List<Type>();
private readonly EntityTracker entityTracker = new EntityTracker();
public IEnumerable<Type> ComponentTypes { get { return componentTypes; } }
protected Detector()
{
var detectsAttribute = GetType().GetCustomAttribute<Detects>(false);
if (detectsAttribute != null)
{
componentTypes = detectsAttribute.componentTypes;
}
else
{
throw new DetectorWithoutComponentTypesException("Detector {0} does not have any component types declared. Use the Detects attribute to declare component types", GetType().Name);
}
}
public override void Update(double dt)
{
foreach (var id in entityTracker.TrackedEntityIDs)
{
Detect(GetEntity(id), dt);
}
}
public abstract void Detect(Entity entity, double dt);
public bool CheckAndTrackEntity(Guid entityID)
{
var entity = GetEntity(entityID);
var shouldTrack = CheckEntity(entity);
if (shouldTrack) { entityTracker.TrackEntity(entityID); }
return shouldTrack;
}
public bool CheckAndUntrackEntity(Guid entityID)
{
var entity = GetEntity(entityID);
var shouldUntrack = !CheckEntity(entity);
if (shouldUntrack) { entityTracker.UntrackEntity(entityID); }
return shouldUntrack;
}
private bool CheckEntity(Entity entity)
{
return EntityChecker.CheckEntity(entity, componentTypes);
}
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace Encompass.Exceptions
{
public class DetectorWithoutComponentTypesException : Exception
{
public DetectorWithoutComponentTypesException(
string format,
params object[] args
) : base(string.Format(format, args)) { }
}
}

View File

@ -25,7 +25,7 @@ namespace Encompass
this.renderManager = renderManager;
}
public void Update(float dt)
public void Update(double dt)
{
foreach (var engine in enginesInOrder)
{

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
@ -35,16 +36,13 @@ namespace Encompass
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(this.entityManager);
engine.AssignComponentManager(this.componentManager);
engine.AssignMessageManager(this.messageManager);
engine.AssignEntityManager(entityManager);
engine.AssignComponentManager(componentManager);
engine.AssignMessageManager(messageManager);
engines.Add(engine);
engineGraph.AddVertex(engine);
if (engine is IEntityTracker)

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Encompass
{

View File

@ -1,7 +1,7 @@
using System;
namespace Encompass
namespace Encompass.Exceptions
{
public class EngineCycleException : Exception
{

View File

@ -1,6 +1,6 @@
using System;
namespace Encompass
namespace Encompass.Exceptions
{
public class EngineMutationConflictException : Exception
{

View File

@ -1,6 +1,6 @@
using System;
namespace Encompass
namespace Encompass.Exceptions
{
public class IllegalComponentMutationException : Exception
{

View File

@ -1,6 +1,6 @@
using System;
namespace Encompass
namespace Encompass.Exceptions
{
public class IllegalMessageEmitException : Exception
{

View File

@ -1,6 +1,6 @@
using System;
namespace Encompass
namespace Encompass.Exceptions
{
public class IllegalMessageReadException : Exception
{

123
test/DetectorTest.cs Normal file
View File

@ -0,0 +1,123 @@
using NUnit.Framework;
using FluentAssertions;
using Encompass;
using Encompass.Engines;
using Encompass.Exceptions;
using System;
using System.Collections.Generic;
namespace Tests
{
class DetectorTest
{
class NoComponentTypesDetector : Detector
{
public override void Detect(Entity entity, double dt) { }
}
[Test]
public void DetectorWithNoComponentTypes()
{
var worldBuilder = new WorldBuilder();
Action addEngine = () => worldBuilder.AddEngine(new NoComponentTypesDetector());
addEngine.Should().Throw<DetectorWithoutComponentTypesException>();
}
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct CComponent : IComponent { }
static List<Entity> trackedEntities = new List<Entity>();
[Detects(typeof(AComponent), typeof(BComponent))]
class TestDetector : Detector
{
public override void Detect(Entity entity, double dt)
{
trackedEntities.Add(entity);
}
}
[Test]
public void CheckAndTrackEntities()
{
var worldBuilder = new WorldBuilder();
var detector = worldBuilder.AddEngine(new TestDetector());
var entityToTrack = worldBuilder.CreateEntity();
entityToTrack.AddComponent(new AComponent());
entityToTrack.AddComponent(new BComponent());
var entityNotToTrack = worldBuilder.CreateEntity();
entityNotToTrack.AddComponent(new AComponent());
entityNotToTrack.AddComponent(new CComponent());
var entityWithDeactivatedComponents = worldBuilder.CreateEntity();
var aComponent = entityWithDeactivatedComponents.AddComponent(new AComponent());
entityWithDeactivatedComponents.AddComponent(new BComponent());
entityWithDeactivatedComponents.DeactivateComponent(aComponent);
var entityWithOneDeactivatedComponent = worldBuilder.CreateEntity();
var inactiveComponent = entityWithOneDeactivatedComponent.AddComponent(new AComponent());
entityWithOneDeactivatedComponent.AddComponent(new AComponent());
entityWithOneDeactivatedComponent.AddComponent(new BComponent());
entityWithOneDeactivatedComponent.DeactivateComponent(inactiveComponent);
var world = worldBuilder.Build();
world.Update(0.01);
trackedEntities.Should().Contain(entityToTrack);
trackedEntities.Should().NotContain(entityNotToTrack);
trackedEntities.Should().NotContain(entityWithDeactivatedComponents);
trackedEntities.Should().Contain(entityWithOneDeactivatedComponent);
}
[Test]
public void EntityUntrackedWhenComponentRemoved()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new TestDetector());
var entityToUntrack = worldBuilder.CreateEntity();
entityToUntrack.AddComponent(new AComponent());
var bComponent = entityToUntrack.AddComponent(new BComponent());
var world = worldBuilder.Build();
// have to update twice because we are updating from outside the world
entityToUntrack.RemoveComponent(bComponent);
world.Update(0.01);
trackedEntities.Clear();
world.Update(0.01);
trackedEntities.Should().NotContain(entityToUntrack);
}
[Test]
public void DetectCalledPerTrackedEntityOnWorldUpdat()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new TestDetector());
var entityOne = worldBuilder.CreateEntity();
entityOne.AddComponent(new AComponent());
entityOne.AddComponent(new BComponent());
var entityTwo = worldBuilder.CreateEntity();
entityTwo.AddComponent(new AComponent());
entityTwo.AddComponent(new BComponent());
trackedEntities.Clear();
var world = worldBuilder.Build();
world.Update(0.01);
trackedEntities.Should().Contain(entityOne);
trackedEntities.Should().Contain(entityTwo);
}
}
}

View File

@ -6,6 +6,7 @@ using Encompass;
using System;
using System.Linq;
using System.Collections.Generic;
using Encompass.Exceptions;
namespace Tests
{
@ -18,7 +19,7 @@ namespace Tests
public class ReadComponentsTestEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
resultComponents = this.ReadComponents<MockComponent>().ToList();
}
@ -26,7 +27,7 @@ namespace Tests
public class ReadComponentTestEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
resultComponent = this.ReadComponent<MockComponent>().Value;
}
@ -36,7 +37,7 @@ namespace Tests
public void ReadComponents()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<ReadComponentsTestEngine>();
worldBuilder.AddEngine(new ReadComponentsTestEngine());
var entity = worldBuilder.CreateEntity();
@ -64,7 +65,7 @@ namespace Tests
public void ReadComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<ReadComponentTestEngine>();
worldBuilder.AddEngine(new ReadComponentTestEngine());
var entity = worldBuilder.CreateEntity();
@ -85,7 +86,7 @@ namespace Tests
public void ReadComponentWhenMultipleComponents()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<ReadComponentTestEngine>();
worldBuilder.AddEngine(new ReadComponentTestEngine());
var entity = worldBuilder.CreateEntity();
@ -108,7 +109,7 @@ namespace Tests
[Mutates(typeof(MockComponent))]
public class UpdateComponentTestEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
(var componentID, var component) = this.ReadComponent<MockComponent>();
@ -124,7 +125,7 @@ namespace Tests
public void UpdateComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<UpdateComponentTestEngine>();
worldBuilder.AddEngine(new UpdateComponentTestEngine());
var entity = worldBuilder.CreateEntity();
@ -144,7 +145,7 @@ namespace Tests
public class UndeclaredUpdateComponentTestEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
(var componentID, var component) = this.ReadComponent<MockComponent>();
@ -160,7 +161,7 @@ namespace Tests
public void UpdateUndeclaredComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<UndeclaredUpdateComponentTestEngine>();
worldBuilder.AddEngine(new UndeclaredUpdateComponentTestEngine());
var entity = worldBuilder.CreateEntity();
@ -184,7 +185,7 @@ namespace Tests
[Emits(typeof(MockMessage))]
public class MessageEmitEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
MockMessage message;
message.myString = "howdy";
@ -196,7 +197,7 @@ namespace Tests
[Reads(typeof(MockMessage))]
public class MessageReadEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
resultMessages = this.ReadMessages<MockMessage>().ToList();
}
@ -206,8 +207,8 @@ namespace Tests
public void EmitAndReadMessage()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<MessageEmitEngine>();
worldBuilder.AddEngine<MessageReadEngine>();
worldBuilder.AddEngine(new MessageEmitEngine());
worldBuilder.AddEngine(new MessageReadEngine());
var world = worldBuilder.Build();
@ -218,7 +219,7 @@ namespace Tests
public class UndeclaredMessageEmitEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
MockMessage message;
message.myString = "howdy";
@ -254,7 +255,7 @@ namespace Tests
public void EmitUndeclaredMessage()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<UndeclaredMessageEmitEngine>();
worldBuilder.AddEngine(new UndeclaredMessageEmitEngine());
var world = worldBuilder.Build();
@ -267,7 +268,7 @@ namespace Tests
[Emits(typeof(MockMessage))]
class EmitMockMessageEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
MockMessage message;
message.myString = "howdy";
@ -279,7 +280,7 @@ namespace Tests
[Reads(typeof(MockMessage))]
class SomeTestEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
someTest = this.Some<MockMessage>();
}
@ -289,8 +290,8 @@ namespace Tests
public void Some()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<EmitMockMessageEngine>();
worldBuilder.AddEngine<SomeTestEngine>();
worldBuilder.AddEngine(new EmitMockMessageEngine());
worldBuilder.AddEngine(new SomeTestEngine());
var world = worldBuilder.Build();
@ -301,7 +302,7 @@ namespace Tests
class UndeclaredSomeEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
someTest = this.Some<MockMessage>();
}
@ -311,8 +312,8 @@ namespace Tests
public void IllegalSome()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<EmitMockMessageEngine>();
worldBuilder.AddEngine<UndeclaredSomeEngine>();
worldBuilder.AddEngine(new EmitMockMessageEngine());
worldBuilder.AddEngine(new UndeclaredSomeEngine());
var world = worldBuilder.Build();
@ -324,9 +325,9 @@ namespace Tests
class SameValueComponentReadEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
var components = this.ReadComponents<MockComponent>();
var components = ReadComponents<MockComponent>();
pairA = components.First();
pairB = components.Last();
@ -338,7 +339,7 @@ namespace Tests
public void SameValueComponents()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<SameValueComponentReadEngine>();
worldBuilder.AddEngine(new SameValueComponentReadEngine());
MockComponent componentA;
componentA.myInt = 20;
@ -363,9 +364,9 @@ namespace Tests
class ReadEmptyMockComponentsEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
emptyComponentReadResult = this.ReadComponents<MockComponent>();
emptyComponentReadResult = ReadComponents<MockComponent>();
}
}
@ -373,7 +374,7 @@ namespace Tests
public void ReadComponentsOfTypeWhereNoneExist()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<ReadEmptyMockComponentsEngine>();
worldBuilder.AddEngine(new ReadEmptyMockComponentsEngine());
var world = worldBuilder.Build();
world.Update(0.01f);
@ -385,7 +386,7 @@ namespace Tests
class DestroyerEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
var componentPairs = ReadComponents<DestroyerComponent>();
@ -401,7 +402,7 @@ namespace Tests
static IEnumerable<KeyValuePair<Guid, MockComponent>> results;
class ReaderEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
results = ReadComponents<MockComponent>();
}
@ -411,8 +412,8 @@ namespace Tests
public void DestroyEntity()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<DestroyerEngine>();
worldBuilder.AddEngine<ReaderEngine>();
worldBuilder.AddEngine(new DestroyerEngine());
worldBuilder.AddEngine(new ReaderEngine());
var entity = worldBuilder.CreateEntity();
var entityB = worldBuilder.CreateEntity();

View File

@ -2,6 +2,7 @@ using NUnit.Framework;
using Encompass;
using System.Collections.Generic;
using Encompass.Exceptions;
namespace Tests
{
@ -16,7 +17,7 @@ namespace Tests
[Emits(typeof(BMessage))]
class AEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
BMessage message;
this.EmitMessage(message);
@ -27,7 +28,7 @@ namespace Tests
[Emits(typeof(AMessage))]
class BEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
AMessage message;
this.EmitMessage(message);
@ -38,8 +39,8 @@ namespace Tests
public void EngineCycle()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<AEngine>();
worldBuilder.AddEngine<BEngine>();
worldBuilder.AddEngine(new AEngine());
worldBuilder.AddEngine(new BEngine());
Assert.Throws<EngineCycleException>(() => worldBuilder.Build());
}
@ -56,7 +57,7 @@ namespace Tests
[Emits(typeof(BMessage))]
class AEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
BMessage message;
this.EmitMessage(message);
@ -67,7 +68,7 @@ namespace Tests
[Emits(typeof(CMessage))]
class BEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
CMessage message;
this.EmitMessage(message);
@ -78,7 +79,7 @@ namespace Tests
[Emits(typeof(DMessage))]
class CEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
DMessage message;
this.EmitMessage(message);
@ -89,7 +90,7 @@ namespace Tests
[Emits(typeof(AMessage))]
class DEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
AMessage message;
this.EmitMessage(message);
@ -100,10 +101,10 @@ namespace Tests
public void EngineCycle()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<AEngine>();
worldBuilder.AddEngine<BEngine>();
worldBuilder.AddEngine<CEngine>();
worldBuilder.AddEngine<DEngine>();
worldBuilder.AddEngine(new AEngine());
worldBuilder.AddEngine(new BEngine());
worldBuilder.AddEngine(new CEngine());
worldBuilder.AddEngine(new DEngine());
Assert.Throws<EngineCycleException>(() => worldBuilder.Build());
}
@ -116,21 +117,21 @@ namespace Tests
[Mutates(typeof(AComponent))]
class AEngine : Engine
{
public override void Update(float dt) { }
public override void Update(double dt) { }
}
[Mutates(typeof(AComponent))]
class BEngine : Engine
{
public override void Update(float dt) { }
public override void Update(double dt) { }
}
[Test]
public void MutationConflictException()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<AEngine>();
worldBuilder.AddEngine<BEngine>();
worldBuilder.AddEngine(new AEngine());
worldBuilder.AddEngine(new BEngine());
Assert.Throws<EngineMutationConflictException>(() => worldBuilder.Build());
}
@ -152,7 +153,7 @@ namespace Tests
[Emits(typeof(AMessage))]
class AEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
order.Add(this);
}
@ -162,7 +163,7 @@ namespace Tests
[Emits(typeof(BMessage))]
class BEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
order.Add(this);
}
@ -172,7 +173,7 @@ namespace Tests
[Emits(typeof(DMessage))]
class CEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
order.Add(this);
}
@ -181,7 +182,7 @@ namespace Tests
[Reads(typeof(DMessage))]
class DEngine : Engine
{
public override void Update(float dt)
public override void Update(double dt)
{
order.Add(this);
}
@ -192,10 +193,10 @@ namespace Tests
{
var worldBuilder = new WorldBuilder();
var engineA = worldBuilder.AddEngine<AEngine>();
var engineB = worldBuilder.AddEngine<BEngine>();
var engineC = worldBuilder.AddEngine<CEngine>();
var engineD = worldBuilder.AddEngine<DEngine>();
var engineA = worldBuilder.AddEngine(new AEngine());
var engineB = worldBuilder.AddEngine(new BEngine());
var engineC = worldBuilder.AddEngine(new CEngine());
var engineD = worldBuilder.AddEngine(new DEngine());
Assert.DoesNotThrow(() => worldBuilder.Build());