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.

139 lines
4.0KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. namespace Svelto.ECS
  5. {
  6. public readonly struct BuildGroup
  7. {
  8. internal BuildGroup(ExclusiveGroupStruct group)
  9. {
  10. this.group = group;
  11. }
  12. public static implicit operator BuildGroup(ExclusiveGroupStruct group)
  13. {
  14. return new BuildGroup(group);
  15. }
  16. public static implicit operator BuildGroup(ExclusiveGroup group)
  17. {
  18. return new BuildGroup(group);
  19. }
  20. public static implicit operator uint(BuildGroup groupStruct)
  21. {
  22. return groupStruct.group;
  23. }
  24. internal ExclusiveGroupStruct @group { get; }
  25. }
  26. [StructLayout(LayoutKind.Explicit, Size = 4)]
  27. public struct ExclusiveGroupStruct : IEquatable<ExclusiveGroupStruct>, IComparable<ExclusiveGroupStruct>,
  28. IEqualityComparer<ExclusiveGroupStruct>
  29. {
  30. public override bool Equals(object obj)
  31. {
  32. return obj is ExclusiveGroupStruct other && Equals(other);
  33. }
  34. public override int GetHashCode()
  35. {
  36. return (int) _id;
  37. }
  38. public static bool operator ==(ExclusiveGroupStruct c1, ExclusiveGroupStruct c2)
  39. {
  40. return c1.Equals(c2);
  41. }
  42. public static bool operator !=(ExclusiveGroupStruct c1, ExclusiveGroupStruct c2)
  43. {
  44. return c1.Equals(c2) == false;
  45. }
  46. public bool Equals(ExclusiveGroupStruct other)
  47. {
  48. return other._id == _id;
  49. }
  50. public int CompareTo(ExclusiveGroupStruct other)
  51. {
  52. return other._id.CompareTo(_id);
  53. }
  54. public bool Equals(ExclusiveGroupStruct x, ExclusiveGroupStruct y)
  55. {
  56. return x._id == y._id;
  57. }
  58. public int GetHashCode(ExclusiveGroupStruct obj)
  59. {
  60. return _id.GetHashCode();
  61. }
  62. public override string ToString()
  63. {
  64. return this.ToName();
  65. }
  66. internal static ExclusiveGroupStruct Generate(byte bitmask = 0)
  67. {
  68. ExclusiveGroupStruct groupStruct;
  69. groupStruct._id = _globalId;
  70. groupStruct._bytemask = bitmask;
  71. DBC.ECS.Check.Require(_globalId + 1 < ExclusiveGroup.MaxNumberOfExclusiveGroups, "too many exclusive groups created");
  72. _globalId++;
  73. return groupStruct;
  74. }
  75. internal ExclusiveGroupStruct(ExclusiveGroupStruct @group):this() { this = group; }
  76. /// <summary>
  77. /// Use this constructor to reserve N groups
  78. /// </summary>
  79. internal ExclusiveGroupStruct(ushort range):this()
  80. {
  81. _id = _globalId;
  82. DBC.ECS.Check.Require(_globalId + range < ExclusiveGroup.MaxNumberOfExclusiveGroups, "too many exclusive groups created");
  83. _globalId += range;
  84. }
  85. internal ExclusiveGroupStruct(uint groupID):this()
  86. {
  87. _id = groupID;
  88. }
  89. public ExclusiveGroupStruct(byte[] data, uint pos):this()
  90. {
  91. _id = (uint)(
  92. data[pos]
  93. | data[++pos] << 8
  94. | data[++pos] << 16
  95. );
  96. _bytemask = (byte) (data[++pos] << 24);
  97. DBC.ECS.Check.Ensure(_id < _globalId, "Invalid group ID deserialiased");
  98. }
  99. public static implicit operator uint(ExclusiveGroupStruct groupStruct)
  100. {
  101. return groupStruct._id;
  102. }
  103. public static ExclusiveGroupStruct operator+(ExclusiveGroupStruct a, uint b)
  104. {
  105. var group = new ExclusiveGroupStruct {_id = a._id + b};
  106. return @group;
  107. }
  108. [FieldOffset(0)] uint _id;
  109. [FieldOffset(3)] byte _bytemask;
  110. static uint _globalId = 1; //it starts from 1 because default EGID is considered not initalized value
  111. }
  112. }