AspectTests.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Specialized;
  2. using MonoGame.Extended.Graphics;
  3. using Xunit;
  4. namespace MonoGame.Extended.ECS.Tests
  5. {
  6. public class DummyComponent
  7. {
  8. }
  9. public class AspectTests
  10. {
  11. private readonly ComponentManager _componentManager;
  12. private readonly BitVector32 _entityA;
  13. private readonly BitVector32 _entityB;
  14. public AspectTests()
  15. {
  16. _componentManager = new ComponentManager();
  17. _entityA = new BitVector32
  18. {
  19. [1 << _componentManager.GetComponentTypeId(typeof(Transform2))] = true,
  20. [1 << _componentManager.GetComponentTypeId(typeof(Sprite))] = true,
  21. [1 << _componentManager.GetComponentTypeId(typeof(DummyComponent))] = true
  22. };
  23. _entityB = new BitVector32
  24. {
  25. [1 << _componentManager.GetComponentTypeId(typeof(Transform2))] = true,
  26. [1 << _componentManager.GetComponentTypeId(typeof(Sprite))] = true,
  27. };
  28. }
  29. [Fact]
  30. public void EmptyAspectMatchesAllComponents()
  31. {
  32. var componentManager = new ComponentManager();
  33. var emptyAspect = Aspect.All()
  34. .Build(componentManager);
  35. Assert.True(emptyAspect.IsInterested(_entityA));
  36. Assert.True(emptyAspect.IsInterested(_entityB));
  37. }
  38. [Fact]
  39. public void IsInterestedInAllComponents()
  40. {
  41. var allAspect = Aspect
  42. .All(typeof(Sprite), typeof(Transform2), typeof(DummyComponent))
  43. .Build(_componentManager);
  44. Assert.True(allAspect.IsInterested(_entityA));
  45. Assert.False(allAspect.IsInterested(_entityB));
  46. }
  47. [Fact]
  48. public void IsInterestedInEitherOneOfTheComponents()
  49. {
  50. var eitherOneAspect = Aspect
  51. .One(typeof(Transform2), typeof(DummyComponent))
  52. .Build(_componentManager);
  53. Assert.True(eitherOneAspect.IsInterested(_entityA));
  54. Assert.True(eitherOneAspect.IsInterested(_entityB));
  55. }
  56. [Fact]
  57. public void IsInterestedInJustOneComponent()
  58. {
  59. var oneAspect = Aspect
  60. .One(typeof(DummyComponent))
  61. .Build(_componentManager);
  62. Assert.True(oneAspect.IsInterested(_entityA));
  63. Assert.False(oneAspect.IsInterested(_entityB));
  64. }
  65. [Fact]
  66. public void IsInterestedInExcludingOneComponent()
  67. {
  68. var oneAspect = Aspect
  69. .Exclude(typeof(DummyComponent))
  70. .Build(_componentManager);
  71. Assert.False(oneAspect.IsInterested(_entityA));
  72. Assert.True(oneAspect.IsInterested(_entityB));
  73. }
  74. }
  75. }