2019-06-24 19:14:37 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2019-06-24 19:26:19 +00:00
|
|
|
|
Action addEngine = () => worldBuilder.AddEngine(new NoComponentTypesDetector());
|
|
|
|
|
addEngine.Should().Throw<DetectorWithoutComponentTypesException>();
|
2019-06-24 19:14:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2019-06-24 19:26:19 +00:00
|
|
|
|
var detector = worldBuilder.AddEngine(new TestDetector());
|
2019-06-24 19:14:37 +00:00
|
|
|
|
|
|
|
|
|
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();
|
2019-06-24 19:26:19 +00:00
|
|
|
|
worldBuilder.AddEngine(new TestDetector());
|
2019-06-24 19:14:37 +00:00
|
|
|
|
|
|
|
|
|
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();
|
2019-06-24 19:26:19 +00:00
|
|
|
|
worldBuilder.AddEngine(new TestDetector());
|
2019-06-24 19:14:37 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|