implement Detector and change World and Engine update to use double

pull/5/head
Evan Hemsley 2019-06-24 12:14:37 -07:00
parent 74ec2f6320
commit b4347f4085
15 changed files with 254 additions and 39 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.Reflection;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Encompass.Exceptions;
namespace Encompass namespace Encompass
{ {
@ -51,7 +52,7 @@ namespace Encompass
this.messageManager = messageManager; this.messageManager = messageManager;
} }
public abstract void Update(float dt); public abstract void Update(double dt);
protected Entity CreateEntity() 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; this.renderManager = renderManager;
} }
public void Update(float dt) public void Update(double dt)
{ {
foreach (var engine in enginesInOrder) foreach (var engine in enginesInOrder)
{ {

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Linq; using System.Linq;
using Encompass.Exceptions;
namespace Encompass namespace Encompass
{ {
@ -39,9 +40,9 @@ namespace Encompass
{ {
var engine = new TEngine(); var engine = new TEngine();
engine.AssignEntityManager(this.entityManager); engine.AssignEntityManager(entityManager);
engine.AssignComponentManager(this.componentManager); engine.AssignComponentManager(componentManager);
engine.AssignMessageManager(this.messageManager); engine.AssignMessageManager(messageManager);
engines.Add(engine); engines.Add(engine);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

125
test/DetectorTest.cs Normal file
View File

@ -0,0 +1,125 @@
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<NoComponentTypesDetector>();
addEngine.Should()
.Throw<System.Reflection.TargetInvocationException>()
.WithInnerException<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<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<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<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;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using Encompass.Exceptions;
namespace Tests namespace Tests
{ {
@ -18,7 +19,7 @@ namespace Tests
public class ReadComponentsTestEngine : Engine public class ReadComponentsTestEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
resultComponents = this.ReadComponents<MockComponent>().ToList(); resultComponents = this.ReadComponents<MockComponent>().ToList();
} }
@ -26,7 +27,7 @@ namespace Tests
public class ReadComponentTestEngine : Engine public class ReadComponentTestEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
resultComponent = this.ReadComponent<MockComponent>().Value; resultComponent = this.ReadComponent<MockComponent>().Value;
} }
@ -108,7 +109,7 @@ namespace Tests
[Mutates(typeof(MockComponent))] [Mutates(typeof(MockComponent))]
public class UpdateComponentTestEngine : Engine public class UpdateComponentTestEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
(var componentID, var component) = this.ReadComponent<MockComponent>(); (var componentID, var component) = this.ReadComponent<MockComponent>();
@ -144,7 +145,7 @@ namespace Tests
public class UndeclaredUpdateComponentTestEngine : Engine public class UndeclaredUpdateComponentTestEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
(var componentID, var component) = this.ReadComponent<MockComponent>(); (var componentID, var component) = this.ReadComponent<MockComponent>();
@ -184,7 +185,7 @@ namespace Tests
[Emits(typeof(MockMessage))] [Emits(typeof(MockMessage))]
public class MessageEmitEngine : Engine public class MessageEmitEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
MockMessage message; MockMessage message;
message.myString = "howdy"; message.myString = "howdy";
@ -196,7 +197,7 @@ namespace Tests
[Reads(typeof(MockMessage))] [Reads(typeof(MockMessage))]
public class MessageReadEngine : Engine public class MessageReadEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
resultMessages = this.ReadMessages<MockMessage>().ToList(); resultMessages = this.ReadMessages<MockMessage>().ToList();
} }
@ -218,7 +219,7 @@ namespace Tests
public class UndeclaredMessageEmitEngine : Engine public class UndeclaredMessageEmitEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
MockMessage message; MockMessage message;
message.myString = "howdy"; message.myString = "howdy";
@ -244,7 +245,7 @@ namespace Tests
[Emits(typeof(MockMessage))] [Emits(typeof(MockMessage))]
class EmitMockMessageEngine : Engine class EmitMockMessageEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
MockMessage message; MockMessage message;
message.myString = "howdy"; message.myString = "howdy";
@ -256,7 +257,7 @@ namespace Tests
[Reads(typeof(MockMessage))] [Reads(typeof(MockMessage))]
class SomeTestEngine : Engine class SomeTestEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
someTest = this.Some<MockMessage>(); someTest = this.Some<MockMessage>();
} }
@ -278,7 +279,7 @@ namespace Tests
class UndeclaredSomeEngine : Engine class UndeclaredSomeEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
someTest = this.Some<MockMessage>(); someTest = this.Some<MockMessage>();
} }
@ -301,9 +302,9 @@ namespace Tests
class SameValueComponentReadEngine : Engine 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(); pairA = components.First();
pairB = components.Last(); pairB = components.Last();
@ -340,9 +341,9 @@ namespace Tests
class ReadEmptyMockComponentsEngine : Engine class ReadEmptyMockComponentsEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
emptyComponentReadResult = this.ReadComponents<MockComponent>(); emptyComponentReadResult = ReadComponents<MockComponent>();
} }
} }
@ -362,7 +363,7 @@ namespace Tests
class DestroyerEngine : Engine class DestroyerEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
var componentPairs = ReadComponents<DestroyerComponent>(); var componentPairs = ReadComponents<DestroyerComponent>();
@ -378,7 +379,7 @@ namespace Tests
static IEnumerable<KeyValuePair<Guid, MockComponent>> results; static IEnumerable<KeyValuePair<Guid, MockComponent>> results;
class ReaderEngine : Engine class ReaderEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
results = ReadComponents<MockComponent>(); results = ReadComponents<MockComponent>();
} }

View File

@ -2,6 +2,7 @@ using NUnit.Framework;
using Encompass; using Encompass;
using System.Collections.Generic; using System.Collections.Generic;
using Encompass.Exceptions;
namespace Tests namespace Tests
{ {
@ -16,7 +17,7 @@ namespace Tests
[Emits(typeof(BMessage))] [Emits(typeof(BMessage))]
class AEngine : Engine class AEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
BMessage message; BMessage message;
this.EmitMessage(message); this.EmitMessage(message);
@ -27,7 +28,7 @@ namespace Tests
[Emits(typeof(AMessage))] [Emits(typeof(AMessage))]
class BEngine : Engine class BEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
AMessage message; AMessage message;
this.EmitMessage(message); this.EmitMessage(message);
@ -56,7 +57,7 @@ namespace Tests
[Emits(typeof(BMessage))] [Emits(typeof(BMessage))]
class AEngine : Engine class AEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
BMessage message; BMessage message;
this.EmitMessage(message); this.EmitMessage(message);
@ -67,7 +68,7 @@ namespace Tests
[Emits(typeof(CMessage))] [Emits(typeof(CMessage))]
class BEngine : Engine class BEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
CMessage message; CMessage message;
this.EmitMessage(message); this.EmitMessage(message);
@ -78,7 +79,7 @@ namespace Tests
[Emits(typeof(DMessage))] [Emits(typeof(DMessage))]
class CEngine : Engine class CEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
DMessage message; DMessage message;
this.EmitMessage(message); this.EmitMessage(message);
@ -89,7 +90,7 @@ namespace Tests
[Emits(typeof(AMessage))] [Emits(typeof(AMessage))]
class DEngine : Engine class DEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
AMessage message; AMessage message;
this.EmitMessage(message); this.EmitMessage(message);
@ -116,13 +117,13 @@ namespace Tests
[Mutates(typeof(AComponent))] [Mutates(typeof(AComponent))]
class AEngine : Engine class AEngine : Engine
{ {
public override void Update(float dt) { } public override void Update(double dt) { }
} }
[Mutates(typeof(AComponent))] [Mutates(typeof(AComponent))]
class BEngine : Engine class BEngine : Engine
{ {
public override void Update(float dt) { } public override void Update(double dt) { }
} }
[Test] [Test]
@ -152,7 +153,7 @@ namespace Tests
[Emits(typeof(AMessage))] [Emits(typeof(AMessage))]
class AEngine : Engine class AEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
order.Add(this); order.Add(this);
} }
@ -162,7 +163,7 @@ namespace Tests
[Emits(typeof(BMessage))] [Emits(typeof(BMessage))]
class BEngine : Engine class BEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
order.Add(this); order.Add(this);
} }
@ -172,7 +173,7 @@ namespace Tests
[Emits(typeof(DMessage))] [Emits(typeof(DMessage))]
class CEngine : Engine class CEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
order.Add(this); order.Add(this);
} }
@ -181,7 +182,7 @@ namespace Tests
[Reads(typeof(DMessage))] [Reads(typeof(DMessage))]
class DEngine : Engine class DEngine : Engine
{ {
public override void Update(float dt) public override void Update(double dt)
{ {
order.Add(this); order.Add(this);
} }