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.

95 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, (uint) groupID);
  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) { return id._GID; }
  41. public bool Equals(EGID other)
  42. {
  43. return _GID == other._GID;
  44. }
  45. public bool Equals(EGID x, EGID y)
  46. {
  47. return x == y;
  48. }
  49. public override int GetHashCode()
  50. {
  51. return _GID.GetHashCode();
  52. }
  53. public int GetHashCode(EGID egid)
  54. {
  55. return egid.GetHashCode();
  56. }
  57. public int CompareTo(EGID other)
  58. {
  59. return _GID.CompareTo(other._GID);
  60. }
  61. internal EGID(uint entityID, uint groupID) : this()
  62. {
  63. _GID = MAKE_GLOBAL_ID(entityID, groupID);
  64. }
  65. public override string ToString()
  66. {
  67. var value = groupID.ToName();
  68. return "id ".FastConcat(entityID).FastConcat(" group ").FastConcat(value);
  69. }
  70. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  71. public EntityReference ToEntityReference(EntitiesDB entitiesDB)
  72. {
  73. return entitiesDB.GetEntityReference(this);
  74. }
  75. }
  76. }