encompass-cs/test/OrderedRendererTest.cs

110 lines
3.1 KiB
C#
Raw Normal View History

using System;
using NUnit.Framework;
using FluentAssertions;
using Encompass;
using System.Collections.Generic;
namespace Tests
{
public class OrderedRendererTest
{
2020-03-20 22:45:58 +00:00
struct AComponent { }
struct BComponent { }
struct CComponent { }
2020-03-20 22:45:58 +00:00
struct TestDrawComponent : IDrawableComponent
{
public int Layer { get; set; }
}
class TestRenderer : OrderedRenderer<TestDrawComponent>
{
2020-03-20 21:13:26 +00:00
public override void Render(Entity entity, in TestDrawComponent testDrawComponent) { }
}
static bool called = false;
class DeactivatedRenderer : TestRenderer
{
2020-03-20 21:13:26 +00:00
public override void Render(Entity entity, in TestDrawComponent testDrawComponent)
{
called = true;
}
}
static bool calledOnDraw = false;
2019-11-21 03:01:29 +00:00
static (TestDrawComponent, Entity) resultComponent;
class CalledRenderer : OrderedRenderer<TestDrawComponent>
{
2020-03-20 21:13:26 +00:00
public override void Render(Entity entity, in TestDrawComponent testDrawComponent)
{
2019-11-21 03:01:29 +00:00
resultComponent = (testDrawComponent, entity);
calledOnDraw = true;
}
}
[Test]
public void RenderMethodCalledOnWorldDraw()
{
var worldBuilder = new WorldBuilder();
2019-12-17 02:51:45 +00:00
worldBuilder.RegisterDrawLayer(2);
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
AComponent aComponent;
CComponent cComponent;
var testDrawComponent = new TestDrawComponent { Layer = 2 };
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, aComponent);
worldBuilder.SetComponent(entity, cComponent);
worldBuilder.SetComponent(entity, testDrawComponent);
var world = worldBuilder.Build();
world.Update(0.01f);
world.Draw();
Assert.IsTrue(calledOnDraw);
2019-11-21 03:01:29 +00:00
resultComponent.Should().BeEquivalentTo((testDrawComponent, entity));
}
[Reads(typeof(TestDrawComponent))]
class DestroyerEngine : Engine
{
public override void Update(double dt)
{
2019-11-21 03:01:29 +00:00
foreach (var entity in ReadEntities<TestDrawComponent>())
{
2019-11-21 03:01:29 +00:00
Destroy(entity);
}
}
}
[Test]
public void RenderMethodNotCalledAfterDestroy()
{
calledOnDraw = false;
var worldBuilder = new WorldBuilder();
2019-12-17 02:51:45 +00:00
worldBuilder.RegisterDrawLayer(1);
worldBuilder.AddEngine(new DestroyerEngine());
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
TestDrawComponent testDrawComponent = new TestDrawComponent { Layer = 1 };
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, testDrawComponent);
var world = worldBuilder.Build();
world.Update(0.01);
world.Draw();
Assert.IsFalse(calledOnDraw);
}
}
}