encompass-cs/test/EntityRendererTest.cs

130 lines
4.0 KiB
C#

using System;
using NUnit.Framework;
using Encompass;
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<TestRenderer>();
AComponent aComponent;
BComponent bComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent);
var entityToTrack = worldBuilder.CreateEntity();
entityToTrack.AddComponent(aComponent);
entityToTrack.AddComponent(bComponent);
entityToTrack.AddComponent(testDrawComponent);
var entityNotToTrack = worldBuilder.CreateEntity();
entityNotToTrack.AddComponent(aComponent);
entityNotToTrack.AddComponent(testDrawComponent);
var entityWithoutDrawComponent = worldBuilder.CreateEntity();
entityWithoutDrawComponent.AddComponent(aComponent);
entityWithoutDrawComponent.AddComponent(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<TestRenderer>();
AComponent aComponent;
BComponent bComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent);
var entity = worldBuilder.CreateEntity();
entity.AddComponent(aComponent);
entity.AddComponent(bComponent);
var testDrawComponentID = entity.AddDrawComponent(testDrawComponent, 1);
var world = worldBuilder.Build();
world.Update(0.01f);
entity.DeactivateComponent(testDrawComponentID);
world.Update(0.01f);
Assert.IsFalse(renderer.IsTracking(entity.id));
world.Draw();
Assert.IsFalse(called);
}
static bool calledOnDraw = false;
[Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(CComponent))]
class CalledRenderer : EntityRenderer
{
public override void Render(Entity entity)
{
calledOnDraw = true;
}
}
[Test]
public void RenderMethodCalledOnWorldDraw()
{
var worldBuilder = new WorldBuilder();
var renderer = worldBuilder.AddEntityRenderer<CalledRenderer>();
AComponent aComponent;
CComponent cComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent);
var entity = worldBuilder.CreateEntity();
entity.AddComponent(aComponent);
entity.AddComponent(cComponent);
entity.AddDrawComponent(testDrawComponent, 2);
var world = worldBuilder.Build();
world.Update(0.01f);
world.Draw();
Assert.IsTrue(renderer.IsTracking(entity.id));
Assert.IsTrue(calledOnDraw);
}
}
}