implement Detector and change World and Engine update to use double
parent
74ec2f6320
commit
b4347f4085
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)) { }
|
||||
}
|
||||
}
|
|
@ -25,7 +25,7 @@ namespace Encompass
|
|||
this.renderManager = renderManager;
|
||||
}
|
||||
|
||||
public void Update(float dt)
|
||||
public void Update(double dt)
|
||||
{
|
||||
foreach (var engine in enginesInOrder)
|
||||
{
|
||||
|
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Linq;
|
||||
using Encompass.Exceptions;
|
||||
|
||||
namespace Encompass
|
||||
{
|
||||
|
@ -39,9 +40,9 @@ namespace Encompass
|
|||
{
|
||||
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);
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Encompass
|
||||
{
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
using System;
|
||||
|
||||
namespace Encompass
|
||||
namespace Encompass.Exceptions
|
||||
{
|
||||
public class EngineCycleException : Exception
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
|
||||
namespace Encompass
|
||||
namespace Encompass.Exceptions
|
||||
{
|
||||
public class EngineMutationConflictException : Exception
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
|
||||
namespace Encompass
|
||||
namespace Encompass.Exceptions
|
||||
{
|
||||
public class IllegalComponentMutationException : Exception
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
|
||||
namespace Encompass
|
||||
namespace Encompass.Exceptions
|
||||
{
|
||||
public class IllegalMessageEmitException : Exception
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
|
||||
namespace Encompass
|
||||
namespace Encompass.Exceptions
|
||||
{
|
||||
public class IllegalMessageReadException : Exception
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
@ -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>();
|
||||
|
||||
|
@ -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>();
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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";
|
||||
|
@ -244,7 +245,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";
|
||||
|
@ -256,7 +257,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>();
|
||||
}
|
||||
|
@ -278,7 +279,7 @@ namespace Tests
|
|||
|
||||
class UndeclaredSomeEngine : Engine
|
||||
{
|
||||
public override void Update(float dt)
|
||||
public override void Update(double dt)
|
||||
{
|
||||
someTest = this.Some<MockMessage>();
|
||||
}
|
||||
|
@ -301,9 +302,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();
|
||||
|
@ -340,9 +341,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>();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -362,7 +363,7 @@ namespace Tests
|
|||
|
||||
class DestroyerEngine : Engine
|
||||
{
|
||||
public override void Update(float dt)
|
||||
public override void Update(double dt)
|
||||
{
|
||||
var componentPairs = ReadComponents<DestroyerComponent>();
|
||||
|
||||
|
@ -378,7 +379,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>();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -116,13 +117,13 @@ 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]
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue