Mirror of Svelto.ECS because we're a fan of it
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

90 wiersze
2.7KB

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