encompass-cs/test/EntityRendererTest.cs

131 lines
4.4 KiB
C#
Raw Normal View History

2019-06-19 21:14:44 +00:00
using System;
using NUnit.Framework;
2019-06-20 19:38:19 +00:00
using FluentAssertions;
2019-06-19 21:14:44 +00:00
using Encompass;
2019-06-20 19:38:19 +00:00
using System.Collections.Generic;
2019-06-19 21:14:44 +00:00
namespace Tests
{
public class EntityRendererTest
{
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct CComponent : IComponent { }
struct TestDrawComponent : IComponent { }
2019-06-19 21:14:44 +00:00
[Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(BComponent))]
class TestRenderer : EntityRenderer
{
public override void Render(Entity entity) { }
}
[Test]
public void CheckAndTrackEntities()
{
var worldBuilder = new WorldBuilder();
var renderer = worldBuilder.AddEntityRenderer(new TestRenderer());
2019-06-19 21:14:44 +00:00
AComponent aComponent;
BComponent bComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent);
var entityToTrack = worldBuilder.CreateEntity();
2019-07-17 18:24:21 +00:00
worldBuilder.AddComponent(entityToTrack, aComponent);
worldBuilder.AddComponent(entityToTrack, bComponent);
worldBuilder.AddComponent(entityToTrack, testDrawComponent);
2019-06-19 21:14:44 +00:00
var entityNotToTrack = worldBuilder.CreateEntity();
2019-07-17 18:24:21 +00:00
worldBuilder.AddComponent(entityNotToTrack, aComponent);
worldBuilder.AddComponent(entityNotToTrack, testDrawComponent);
2019-06-19 21:14:44 +00:00
var entityWithoutDrawComponent = worldBuilder.CreateEntity();
2019-07-17 18:24:21 +00:00
worldBuilder.AddComponent(entityWithoutDrawComponent, aComponent);
worldBuilder.AddComponent(entityWithoutDrawComponent, bComponent);
2019-06-19 21:14:44 +00:00
var world = worldBuilder.Build();
world.Update(0.01f);
2019-07-17 18:24:21 +00:00
Assert.IsTrue(renderer.IsTracking(entityToTrack.ID));
Assert.IsFalse(renderer.IsTracking(entityNotToTrack.ID));
Assert.IsFalse(renderer.IsTracking(entityWithoutDrawComponent.ID));
2019-06-19 21:14:44 +00:00
}
static bool called = false;
class DeactivatedRenderer : TestRenderer
{
public override void Render(Entity entity)
{
called = true;
}
}
[Test]
public void InactiveDrawComponent()
{
var worldBuilder = new WorldBuilder();
var renderer = worldBuilder.AddEntityRenderer(new TestRenderer());
AComponent aComponent;
BComponent bComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent);
var entity = worldBuilder.CreateEntity();
2019-07-17 18:24:21 +00:00
worldBuilder.AddComponent(entity, aComponent);
worldBuilder.AddComponent(entity, bComponent);
var testDrawComponentID = worldBuilder.AddDrawComponent(entity, testDrawComponent, 1);
2019-07-17 18:24:21 +00:00
worldBuilder.DeactivateComponent(testDrawComponentID);
2019-07-17 18:24:21 +00:00
var world = worldBuilder.Build();
world.Update(0.01f);
2019-07-17 18:24:21 +00:00
Assert.IsFalse(renderer.IsTracking(entity.ID));
world.Draw();
Assert.IsFalse(called);
}
static bool calledOnDraw = false;
static IEnumerable<ValueTuple<Guid, TestDrawComponent>> resultComponents;
[Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(CComponent))]
class CalledRenderer : EntityRenderer
{
public override void Render(Entity entity)
{
2019-07-17 18:24:21 +00:00
resultComponents = GetComponents<TestDrawComponent>(entity);
calledOnDraw = true;
}
}
[Test]
public void RenderMethodCalledOnWorldDraw()
{
var worldBuilder = new WorldBuilder();
var renderer = worldBuilder.AddEntityRenderer(new CalledRenderer());
AComponent aComponent;
CComponent cComponent;
2019-06-20 19:38:19 +00:00
TestDrawComponent testDrawComponent;
var entity = worldBuilder.CreateEntity();
2019-07-17 18:24:21 +00:00
worldBuilder.AddComponent(entity, aComponent);
worldBuilder.AddComponent(entity, cComponent);
var testDrawComponentID = worldBuilder.AddDrawComponent(entity, testDrawComponent, 2);
var world = worldBuilder.Build();
world.Update(0.01f);
world.Draw();
2019-07-17 18:24:21 +00:00
Assert.IsTrue(renderer.IsTracking(entity.ID));
Assert.IsTrue(calledOnDraw);
resultComponents.Should().Contain(new ValueTuple<Guid, TestDrawComponent>(testDrawComponentID, testDrawComponent));
}
2019-06-19 21:14:44 +00:00
}
}