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.

145 lines
4.4KB

  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. ///
  19. public class ExclusiveGroup
  20. {
  21. public ExclusiveGroup()
  22. {
  23. _group = ExclusiveGroupStruct.Generate();
  24. }
  25. public ExclusiveGroup(string recognizeAs)
  26. {
  27. _group = ExclusiveGroupStruct.Generate();
  28. _serialisedGroups.Add(recognizeAs, _group);
  29. }
  30. public ExclusiveGroup(ushort range)
  31. {
  32. _group = new ExclusiveGroupStruct(range);
  33. }
  34. public static implicit operator ExclusiveGroupStruct(ExclusiveGroup group)
  35. {
  36. return group._group;
  37. }
  38. public static explicit operator uint(ExclusiveGroup group)
  39. {
  40. return group._group;
  41. }
  42. public static ExclusiveGroupStruct operator+(ExclusiveGroup a, uint b)
  43. {
  44. return a._group + b;
  45. }
  46. readonly ExclusiveGroupStruct _group;
  47. //I use this as parameter because it must not be possible to pass null Exclusive Groups.
  48. public struct ExclusiveGroupStruct : IEquatable<ExclusiveGroupStruct>, IComparable<ExclusiveGroupStruct>,
  49. IEqualityComparer<ExclusiveGroupStruct>
  50. {
  51. public static bool operator ==(ExclusiveGroupStruct c1, ExclusiveGroupStruct c2)
  52. {
  53. return c1.Equals(c2);
  54. }
  55. public static bool operator !=(ExclusiveGroupStruct c1, ExclusiveGroupStruct c2)
  56. {
  57. return c1.Equals(c2) == false;
  58. }
  59. public bool Equals(ExclusiveGroupStruct other)
  60. {
  61. return other._id == _id;
  62. }
  63. public int CompareTo(ExclusiveGroupStruct other)
  64. {
  65. return other._id.CompareTo(_id);
  66. }
  67. public bool Equals(ExclusiveGroupStruct x, ExclusiveGroupStruct y)
  68. {
  69. return x._id == y._id;
  70. }
  71. public int GetHashCode(ExclusiveGroupStruct obj)
  72. {
  73. return _id.GetHashCode();
  74. }
  75. internal static ExclusiveGroupStruct Generate()
  76. {
  77. ExclusiveGroupStruct groupStruct;
  78. groupStruct._id = _globalId;
  79. DBC.ECS.Check.Require(_globalId + 1 < ushort.MaxValue, "too many exclusive groups created");
  80. _globalId++;
  81. return groupStruct;
  82. }
  83. /// <summary>
  84. /// Use this constructor to reserve N groups
  85. /// </summary>
  86. internal ExclusiveGroupStruct(ushort range)
  87. {
  88. _id = _globalId;
  89. DBC.ECS.Check.Require(_globalId + range < ushort.MaxValue, "too many exclusive groups created");
  90. _globalId += range;
  91. }
  92. internal ExclusiveGroupStruct(uint groupID)
  93. {
  94. _id = groupID;
  95. }
  96. public static implicit operator uint(ExclusiveGroupStruct groupStruct)
  97. {
  98. return groupStruct._id;
  99. }
  100. public static ExclusiveGroupStruct operator+(ExclusiveGroupStruct a, uint b)
  101. {
  102. var group = new ExclusiveGroupStruct();
  103. group._id = a._id + b;
  104. return group;
  105. }
  106. uint _id;
  107. static uint _globalId;
  108. }
  109. public static ExclusiveGroupStruct Search(string holderGroupName)
  110. {
  111. if (_serialisedGroups.ContainsKey(holderGroupName) == false)
  112. throw new Exception("Serialized Group Not Found ".FastConcat(holderGroupName));
  113. return _serialisedGroups[holderGroupName];
  114. }
  115. static readonly Dictionary<string, ExclusiveGroupStruct> _serialisedGroups = new Dictionary<string,
  116. ExclusiveGroupStruct>();
  117. }
  118. }