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(); 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(new 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; static IEnumerable> resultComponents; [Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(CComponent))] class CalledRenderer : EntityRenderer { public override void Render(Entity entity) { resultComponents = entity.GetComponents(); 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(); entity.AddComponent(aComponent); entity.AddComponent(cComponent); var testDrawComponentID = entity.AddDrawComponent(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 KeyValuePair(testDrawComponentID, testDrawComponent)); } } }