Mirror of Svelto.ECS because we're a fan of it
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.0KB

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