encompass-cs/test/EntityRendererTest.cs

133 lines
4.5 KiB
C#

using System;
using NUnit.Framework;
using FluentAssertions;
using Encompass;
using System.Collections.Generic;
namespace Tests
{
public class EntityRendererTest
{
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct CComponent : IComponent { }
struct TestDrawComponent : IComponent { }
[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());
AComponent aComponent;
BComponent bComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent);
var entityToTrack = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entityToTrack, aComponent);
worldBuilder.AddComponent(entityToTrack, bComponent);
worldBuilder.AddComponent(entityToTrack, testDrawComponent);
var entityNotToTrack = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entityNotToTrack, aComponent);
worldBuilder.AddComponent(entityNotToTrack, testDrawComponent);
var entityWithoutDrawComponent = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entityWithoutDrawComponent, aComponent);
worldBuilder.AddComponent(entityWithoutDrawComponent, bComponent);
var world = worldBuilder.Build();
world.Update(0.01f);
Console.WriteLine(renderer.IsTracking(entityNotToTrack.ID));
Assert.IsTrue(renderer.IsTracking(entityToTrack.ID));
Assert.IsFalse(renderer.IsTracking(entityNotToTrack.ID));
Assert.IsFalse(renderer.IsTracking(entityWithoutDrawComponent.ID));
}
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();
worldBuilder.AddComponent(entity, aComponent);
worldBuilder.AddComponent(entity, bComponent);
var testDrawComponentID = worldBuilder.AddDrawComponent(entity, testDrawComponent, 1);
worldBuilder.DeactivateComponent(testDrawComponentID);
var world = worldBuilder.Build();
world.Update(0.01f);
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)
{
resultComponents = GetComponents<TestDrawComponent>(entity);
calledOnDraw = true;
}
}
[Test]
public void RenderMethodCalledOnWorldDraw()
{
var worldBuilder = new WorldBuilder();
var renderer = worldBuilder.AddEntityRenderer(new CalledRenderer());
AComponent aComponent;
CComponent cComponent;
TestDrawComponent testDrawComponent;
var entity = worldBuilder.CreateEntity();
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();
Assert.IsTrue(renderer.IsTracking(entity.ID));
Assert.IsTrue(calledOnDraw);
resultComponents.Should().Contain(new ValueTuple<Guid, TestDrawComponent>(testDrawComponentID, testDrawComponent));
}
}
}