Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.9KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. #pragma warning disable 660,661
  5. namespace Svelto.ECS
  6. {
  7. /// <summary>
  8. /// Exclusive Groups guarantee that the GroupID is unique.
  9. ///
  10. /// The best way to use it is like:
  11. ///
  12. /// public static class MyExclusiveGroups //(can be as many as you want)
  13. /// {
  14. /// public static ExclusiveGroup MyExclusiveGroup1 = new ExclusiveGroup();
  15. ///
  16. /// public static ExclusiveGroup[] GroupOfGroups = { MyExclusiveGroup1, ...}; //for each on this!
  17. /// }
  18. /// </summary>
  19. ///To debug it use in your debug window: Svelto.ECS.Debugger.EGID.GetGroupNameFromId(groupID)
  20. public sealed class ExclusiveGroup
  21. {
  22. public const uint MaxNumberOfExclusiveGroups = 2 << 20;
  23. public ExclusiveGroup(ExclusiveGroupBitmask bitmask = 0)
  24. {
  25. _group = ExclusiveGroupStruct.Generate((byte) bitmask);
  26. }
  27. public ExclusiveGroup(string recognizeAs, ExclusiveGroupBitmask bitmask = 0)
  28. {
  29. _group = ExclusiveGroupStruct.Generate((byte) bitmask);
  30. _knownGroups.Add(recognizeAs, _group);
  31. }
  32. public ExclusiveGroup(ushort range)
  33. {
  34. _group = new ExclusiveGroupStruct(range);
  35. #if DEBUG
  36. _range = range;
  37. #endif
  38. }
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public void Disable()
  41. {
  42. _group.Disable();
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public void Enable()
  46. {
  47. _group.Enable();
  48. }
  49. public static implicit operator ExclusiveGroupStruct(ExclusiveGroup group)
  50. {
  51. return group._group;
  52. }
  53. public static explicit operator uint(ExclusiveGroup group)
  54. {
  55. return (uint) @group._group;
  56. }
  57. public static ExclusiveGroupStruct operator +(ExclusiveGroup a, uint b)
  58. {
  59. #if DEBUG
  60. if (a._range == 0)
  61. throw new ECSException($"Adding values to a not ranged ExclusiveGroup: {(uint) a}");
  62. if (b >= a._range)
  63. throw new ECSException($"Using out of range group: {(uint) a} + {b}");
  64. #endif
  65. return a._group + b;
  66. }
  67. //todo document the use case for this method
  68. public static ExclusiveGroupStruct Search(string holderGroupName)
  69. {
  70. if (_knownGroups.ContainsKey(holderGroupName) == false)
  71. throw new Exception("Named Group Not Found ".FastConcat(holderGroupName));
  72. return _knownGroups[holderGroupName];
  73. }
  74. public override string ToString()
  75. {
  76. return _group.ToString();
  77. }
  78. static readonly Dictionary<string, ExclusiveGroupStruct> _knownGroups =
  79. new Dictionary<string, ExclusiveGroupStruct>();
  80. #if DEBUG
  81. readonly ushort _range;
  82. #endif
  83. ExclusiveGroupStruct _group;
  84. }
  85. }