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.

97 lines
2.6KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. #pragma warning disable 660,661
  5. namespace Svelto.ECS
  6. {
  7. [Serialization.DoNotSerialize]
  8. [Serializable]
  9. [StructLayout(LayoutKind.Explicit)]
  10. public struct EGID : IEquatable<EGID>, IComparable<EGID>
  11. {
  12. [FieldOffset(0)] public readonly uint entityID;
  13. [FieldOffset(4)] public readonly ExclusiveGroupStruct groupID;
  14. [FieldOffset(0)] readonly ulong _GID;
  15. public static bool operator ==(EGID obj1, EGID obj2)
  16. {
  17. return obj1._GID == obj2._GID;
  18. }
  19. public static bool operator !=(EGID obj1, EGID obj2)
  20. {
  21. return obj1._GID != obj2._GID;
  22. }
  23. public EGID(uint entityID, ExclusiveGroupStruct groupID) : this()
  24. {
  25. #if DEBUG && !PROFILE_SVELTO
  26. if (groupID == (ExclusiveGroupStruct) default)
  27. throw new Exception("Trying to use a not initialised group ID");
  28. #endif
  29. _GID = MAKE_GLOBAL_ID(entityID, groupID.ToIDAndBitmask());
  30. }
  31. static ulong MAKE_GLOBAL_ID(uint entityId, uint groupId)
  32. {
  33. return (ulong) groupId << 32 | ((ulong) entityId & 0xFFFFFFFF);
  34. }
  35. public static explicit operator uint(EGID id)
  36. {
  37. return id.entityID;
  38. }
  39. //in the way it's used, ulong must be always the same for each id/group
  40. public static explicit operator ulong(EGID id)
  41. {
  42. return id._GID;
  43. }
  44. public bool Equals(EGID other)
  45. {
  46. return _GID == other._GID;
  47. }
  48. public bool Equals(EGID x, EGID y)
  49. {
  50. return x == y;
  51. }
  52. public override int GetHashCode()
  53. {
  54. return _GID.GetHashCode();
  55. }
  56. public int GetHashCode(EGID egid)
  57. {
  58. return egid.GetHashCode();
  59. }
  60. public int CompareTo(EGID other)
  61. {
  62. return _GID.CompareTo(other._GID);
  63. }
  64. internal EGID(uint entityID, uint groupID) : this()
  65. {
  66. _GID = MAKE_GLOBAL_ID(entityID, groupID);
  67. }
  68. public override string ToString()
  69. {
  70. var value = groupID.ToName();
  71. return "id ".FastConcat(entityID).FastConcat(" group ").FastConcat(value);
  72. }
  73. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  74. public EntityReference ToEntityReference(EntitiesDB entitiesDB)
  75. {
  76. return entitiesDB.GetEntityReference(this);
  77. }
  78. }
  79. }