mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 12:06:37 +00:00
implemented aspect filtering builder
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended.Sprites;
|
||||
using Xunit;
|
||||
|
||||
namespace MonoGame.Extended.Entities.Tests
|
||||
{
|
||||
public class AspectBuilderTests
|
||||
{
|
||||
[Fact]
|
||||
public void MatchAllTypes()
|
||||
{
|
||||
var builder = new Aspect.Builder()
|
||||
.All(typeof(Transform2), typeof(Sprite));
|
||||
|
||||
Assert.Equal(2, builder.AllTypes.Count);
|
||||
Assert.Contains(typeof(Transform2), builder.AllTypes);
|
||||
Assert.Contains(typeof(Sprite), builder.AllTypes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchAllTypesIsEmpty()
|
||||
{
|
||||
var builder = new Aspect.Builder()
|
||||
.All();
|
||||
|
||||
Assert.Empty(builder.AllTypes);
|
||||
Assert.Empty(builder.OneTypes);
|
||||
Assert.Empty(builder.ExclusionTypes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchOneOfType()
|
||||
{
|
||||
var builder = new Aspect.Builder()
|
||||
.One(typeof(Transform2), typeof(Sprite));
|
||||
|
||||
Assert.Equal(2, builder.OneTypes.Count);
|
||||
Assert.Contains(typeof(Transform2), builder.OneTypes);
|
||||
Assert.Contains(typeof(Sprite), builder.OneTypes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExcludeTypes()
|
||||
{
|
||||
var builder = new Aspect.Builder()
|
||||
.Exclude(typeof(Transform2), typeof(Sprite));
|
||||
|
||||
Assert.Equal(2, builder.ExclusionTypes.Count);
|
||||
Assert.Contains(typeof(Transform2), builder.ExclusionTypes);
|
||||
Assert.Contains(typeof(Sprite), builder.ExclusionTypes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildAspect()
|
||||
{
|
||||
var componentManager = new ComponentManager();
|
||||
var builder = new Aspect.Builder()
|
||||
.All(typeof(Transform2), typeof(Sprite))
|
||||
.One(typeof(string))
|
||||
.Exclude(typeof(Texture2D));
|
||||
|
||||
var aspect = builder.Build(componentManager);
|
||||
|
||||
Assert.NotEmpty(aspect.AllSet);
|
||||
Assert.NotEmpty(aspect.OneSet);
|
||||
Assert.NotEmpty(aspect.ExclusionSet);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System.Collections;
|
||||
using MonoGame.Extended.Sprites;
|
||||
using Xunit;
|
||||
|
||||
namespace MonoGame.Extended.Entities.Tests
|
||||
{
|
||||
public class DummyComponent
|
||||
{
|
||||
}
|
||||
|
||||
public class AspectTests
|
||||
{
|
||||
private readonly ComponentManager _componentManager;
|
||||
private readonly BitArray _entityA;
|
||||
private readonly BitArray _entityB;
|
||||
|
||||
public AspectTests()
|
||||
{
|
||||
_componentManager = new ComponentManager();
|
||||
_entityA = new BitArray(3)
|
||||
{
|
||||
[_componentManager.GetComponentTypeId(typeof(Transform2))] = true,
|
||||
[_componentManager.GetComponentTypeId(typeof(Sprite))] = true,
|
||||
[_componentManager.GetComponentTypeId(typeof(DummyComponent))] = true
|
||||
};
|
||||
_entityB = new BitArray(3)
|
||||
{
|
||||
[_componentManager.GetComponentTypeId(typeof(Transform2))] = true,
|
||||
[_componentManager.GetComponentTypeId(typeof(Sprite))] = true,
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmptyAspectMatchesAllComponents()
|
||||
{
|
||||
var componentManager = new ComponentManager();
|
||||
var emptyAspect = Aspect.All()
|
||||
.Build(componentManager);
|
||||
|
||||
Assert.True(emptyAspect.IsInterested(_entityA));
|
||||
Assert.True(emptyAspect.IsInterested(_entityB));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsInterestedInAllComponents()
|
||||
{
|
||||
var allAspect = Aspect
|
||||
.All(typeof(Sprite), typeof(Transform2), typeof(DummyComponent))
|
||||
.Build(_componentManager);
|
||||
|
||||
Assert.True(allAspect.IsInterested(_entityA));
|
||||
Assert.False(allAspect.IsInterested(_entityB));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsInterestedInEitherOneOfTheComponents()
|
||||
{
|
||||
var eitherOneAspect = Aspect
|
||||
.One(typeof(Transform2), typeof(DummyComponent))
|
||||
.Build(_componentManager);
|
||||
|
||||
Assert.True(eitherOneAspect.IsInterested(_entityA));
|
||||
Assert.True(eitherOneAspect.IsInterested(_entityB));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsInterestedInJustOneComponent()
|
||||
{
|
||||
var oneAspect = Aspect
|
||||
.One(typeof(DummyComponent))
|
||||
.Build(_componentManager);
|
||||
|
||||
Assert.True(oneAspect.IsInterested(_entityA));
|
||||
Assert.False(oneAspect.IsInterested(_entityB));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsInterestedInExcludingOneComponent()
|
||||
{
|
||||
var oneAspect = Aspect
|
||||
.Exclude(typeof(DummyComponent))
|
||||
.Build(_componentManager);
|
||||
|
||||
Assert.False(oneAspect.IsInterested(_entityA));
|
||||
Assert.True(oneAspect.IsInterested(_entityB));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using MonoGame.Extended.Sprites;
|
||||
using Xunit;
|
||||
|
||||
namespace MonoGame.Extended.Entities.Tests
|
||||
{
|
||||
public class ComponentManagerTests
|
||||
{
|
||||
[Fact]
|
||||
public void GetMapperForType()
|
||||
{
|
||||
var componentManager = new ComponentManager();
|
||||
var transformMapper = componentManager.GetMapper<Transform2>();
|
||||
var spriteMapper = componentManager.GetMapper<Sprite>();
|
||||
|
||||
Assert.IsType<ComponentMapper<Transform2>>(transformMapper);
|
||||
Assert.IsType<ComponentMapper<Sprite>>(spriteMapper);
|
||||
Assert.Equal(0, transformMapper.Id);
|
||||
Assert.Equal(1, spriteMapper.Id);
|
||||
Assert.Same(spriteMapper, componentManager.GetMapper<Sprite>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetComponentTypeId()
|
||||
{
|
||||
var componentManager = new ComponentManager();
|
||||
|
||||
Assert.Equal(0, componentManager.GetComponentTypeId(typeof(Transform2)));
|
||||
Assert.Equal(1, componentManager.GetComponentTypeId(typeof(Sprite)));
|
||||
Assert.Equal(0, componentManager.GetComponentTypeId(typeof(Transform2)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,7 @@
|
||||
using System;
|
||||
using MonoGame.Extended.Entities.Systems;
|
||||
using Xunit;
|
||||
using Xunit;
|
||||
|
||||
namespace MonoGame.Extended.Entities.Tests
|
||||
{
|
||||
public class ComponentManagerTests
|
||||
{
|
||||
[Fact]
|
||||
public void CreateComponentManager()
|
||||
{
|
||||
var manager = new ComponentManager();
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class ComponentMapperTests
|
||||
{
|
||||
[Fact]
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using MonoGame.Extended.Entities;
|
||||
using MonoGame.Extended.Entities.Legacy;
|
||||
using MonoGame.Extended.Entities.Legacy;
|
||||
|
||||
namespace MonoGame.Extended.Gui.Tests.Implementation
|
||||
{
|
||||
|
||||
+1
-2
@@ -1,5 +1,4 @@
|
||||
using MonoGame.Extended.Entities;
|
||||
using MonoGame.Extended.Entities.Legacy;
|
||||
using MonoGame.Extended.Entities.Legacy;
|
||||
|
||||
namespace MonoGame.Extended.Gui.Tests.Implementation
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user