Mirror of Svelto.ECS because we're a fan of it
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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