AspectBuilder.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Specialized;
  3. using MonoGame.Extended.Collections;
  4. namespace MonoGame.Extended.ECS
  5. {
  6. public class AspectBuilder
  7. {
  8. public AspectBuilder()
  9. {
  10. AllTypes = new Bag<Type>();
  11. ExclusionTypes = new Bag<Type>();
  12. OneTypes = new Bag<Type>();
  13. }
  14. public Bag<Type> AllTypes { get; }
  15. public Bag<Type> ExclusionTypes { get; }
  16. public Bag<Type> OneTypes { get; }
  17. public AspectBuilder All(params Type[] types)
  18. {
  19. foreach (var type in types)
  20. AllTypes.Add(type);
  21. return this;
  22. }
  23. public AspectBuilder One(params Type[] types)
  24. {
  25. foreach (var type in types)
  26. OneTypes.Add(type);
  27. return this;
  28. }
  29. public AspectBuilder Exclude(params Type[] types)
  30. {
  31. foreach (var type in types)
  32. ExclusionTypes.Add(type);
  33. return this;
  34. }
  35. public Aspect Build(ComponentManager componentManager)
  36. {
  37. var aspect = new Aspect();
  38. Associate(componentManager, AllTypes, ref aspect.AllSet);
  39. Associate(componentManager, OneTypes, ref aspect.OneSet);
  40. Associate(componentManager, ExclusionTypes, ref aspect.ExclusionSet);
  41. return aspect;
  42. }
  43. // ReSharper disable once ParameterTypeCanBeEnumerable.Local
  44. private static void Associate(ComponentManager componentManager, Bag<Type> types, ref BitVector32 bits)
  45. {
  46. foreach (var type in types)
  47. {
  48. var id = componentManager.GetComponentTypeId(type);
  49. bits[1 << id] = true;
  50. }
  51. }
  52. }
  53. }