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.

91 lines
2.7KB

  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()
  24. {
  25. _group = ExclusiveGroupStruct.Generate();
  26. }
  27. public ExclusiveGroup(string recognizeAs)
  28. {
  29. _group = ExclusiveGroupStruct.Generate();
  30. _knownGroups.Add(recognizeAs, _group);
  31. }
  32. public ExclusiveGroup(ExclusiveGroupBitmask bitmask)
  33. {
  34. _group = ExclusiveGroupStruct.Generate((byte) bitmask);
  35. }
  36. public ExclusiveGroup(ushort range)
  37. {
  38. _group = ExclusiveGroupStruct.GenerateWithRange(range);
  39. #if DEBUG
  40. _range = range;
  41. #endif
  42. }
  43. public static implicit operator ExclusiveGroupStruct(ExclusiveGroup group)
  44. {
  45. return group._group;
  46. }
  47. public static ExclusiveGroupStruct operator+(ExclusiveGroup @group, uint b)
  48. {
  49. #if DEBUG
  50. if (@group._range == 0)
  51. throw new ECSException($"Adding values to a not ranged ExclusiveGroup: {@group.id}");
  52. if (b >= @group._range)
  53. throw new ECSException($"Using out of range group: {@group.id} + {b}");
  54. #endif
  55. return group._group + b;
  56. }
  57. public uint id => _group.id;
  58. //todo document the use case for this method
  59. public static ExclusiveGroupStruct Search(string holderGroupName)
  60. {
  61. if (_knownGroups.ContainsKey(holderGroupName) == false)
  62. throw new Exception("Named Group Not Found ".FastConcat(holderGroupName));
  63. return _knownGroups[holderGroupName];
  64. }
  65. public override string ToString()
  66. {
  67. return _group.ToString();
  68. }
  69. static readonly Dictionary<string, ExclusiveGroupStruct> _knownGroups =
  70. new Dictionary<string, ExclusiveGroupStruct>();
  71. #if DEBUG
  72. readonly ushort _range;
  73. #endif
  74. readonly ExclusiveGroupStruct _group;
  75. }
  76. }