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.

ExclusiveGroupStruct.cs 3.3KB

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