2019-08-01 22:06:19 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using Encompass;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Tests
|
|
|
|
|
{
|
|
|
|
|
public class OrderedRendererTest
|
|
|
|
|
{
|
|
|
|
|
struct AComponent : IComponent { }
|
|
|
|
|
struct BComponent : IComponent { }
|
|
|
|
|
struct CComponent : IComponent { }
|
|
|
|
|
|
2019-11-21 03:16:44 +00:00
|
|
|
|
struct TestDrawComponent : IComponent, IDrawableComponent
|
|
|
|
|
{
|
|
|
|
|
public int Layer { get; set; }
|
|
|
|
|
}
|
2019-08-01 22:06:19 +00:00
|
|
|
|
|
|
|
|
|
class TestRenderer : OrderedRenderer<TestDrawComponent>
|
|
|
|
|
{
|
2019-11-21 03:01:29 +00:00
|
|
|
|
public override void Render(Entity entity, TestDrawComponent testDrawComponent) { }
|
2019-08-01 22:06:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool called = false;
|
|
|
|
|
class DeactivatedRenderer : TestRenderer
|
|
|
|
|
{
|
2019-11-21 03:01:29 +00:00
|
|
|
|
public override void Render(Entity entity, TestDrawComponent testDrawComponent)
|
2019-08-01 22:06:19 +00:00
|
|
|
|
{
|
|
|
|
|
called = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool calledOnDraw = false;
|
2019-11-21 03:01:29 +00:00
|
|
|
|
static (TestDrawComponent, Entity) resultComponent;
|
2019-08-01 22:06:19 +00:00
|
|
|
|
|
|
|
|
|
class CalledRenderer : OrderedRenderer<TestDrawComponent>
|
|
|
|
|
{
|
2019-11-21 03:01:29 +00:00
|
|
|
|
public override void Render(Entity entity, TestDrawComponent testDrawComponent)
|
2019-08-01 22:06:19 +00:00
|
|
|
|
{
|
2019-11-21 03:01:29 +00:00
|
|
|
|
resultComponent = (testDrawComponent, entity);
|
2019-08-01 22:06:19 +00:00
|
|
|
|
calledOnDraw = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void RenderMethodCalledOnWorldDraw()
|
|
|
|
|
{
|
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-12-17 02:51:45 +00:00
|
|
|
|
worldBuilder.RegisterDrawLayer(2);
|
|
|
|
|
|
2019-08-01 22:06:19 +00:00
|
|
|
|
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
|
|
|
|
|
|
|
|
|
|
AComponent aComponent;
|
|
|
|
|
CComponent cComponent;
|
2019-11-21 03:16:44 +00:00
|
|
|
|
|
|
|
|
|
var testDrawComponent = new TestDrawComponent { Layer = 2 };
|
2019-08-01 22:06:19 +00:00
|
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
2019-08-21 22:13:48 +00:00
|
|
|
|
worldBuilder.SetComponent(entity, aComponent);
|
|
|
|
|
worldBuilder.SetComponent(entity, cComponent);
|
2019-11-21 03:16:44 +00:00
|
|
|
|
worldBuilder.SetComponent(entity, testDrawComponent);
|
2019-08-01 22:06:19 +00:00
|
|
|
|
|
|
|
|
|
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));
|
2019-08-01 22:06:19 +00:00
|
|
|
|
}
|
2019-08-02 06:09:41 +00:00
|
|
|
|
|
|
|
|
|
[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-08-02 06:09:41 +00:00
|
|
|
|
{
|
2019-11-21 03:01:29 +00:00
|
|
|
|
Destroy(entity);
|
2019-08-02 06:09:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[Test]
|
|
|
|
|
public void RenderMethodNotCalledAfterDestroy()
|
|
|
|
|
{
|
|
|
|
|
calledOnDraw = false;
|
|
|
|
|
|
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-12-17 02:51:45 +00:00
|
|
|
|
worldBuilder.RegisterDrawLayer(1);
|
|
|
|
|
|
2019-08-02 06:09:41 +00:00
|
|
|
|
worldBuilder.AddEngine(new DestroyerEngine());
|
|
|
|
|
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
|
|
|
|
|
|
2019-11-21 03:16:44 +00:00
|
|
|
|
TestDrawComponent testDrawComponent = new TestDrawComponent { Layer = 1 };
|
2019-08-02 06:09:41 +00:00
|
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
2019-11-21 03:16:44 +00:00
|
|
|
|
worldBuilder.SetComponent(entity, testDrawComponent);
|
2019-08-02 06:09:41 +00:00
|
|
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
|
|
world.Update(0.01);
|
|
|
|
|
world.Draw();
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(calledOnDraw);
|
|
|
|
|
}
|
2019-08-01 22:06:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|