Mirror of Svelto.ECS because we're a fan of it
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

88 行
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. /// <summary>
  8. /// Todo: EntityReference shouldn't map EGIDs as dictionaries keys but directly the indices in the EntityDB arrays
  9. /// </summary>
  10. [Serialization.DoNotSerialize] //it's not a serializable field for svelto serializable system
  11. [Serializable]
  12. [StructLayout(LayoutKind.Explicit)]
  13. public struct EntityReference : IEquatable<EntityReference>
  14. {
  15. [FieldOffset(0)] public readonly uint uniqueID;
  16. [FieldOffset(4)] public readonly uint version;
  17. [FieldOffset(0)] readonly ulong _GID;
  18. internal uint index => uniqueID - 1;
  19. public static bool operator ==(EntityReference obj1, EntityReference obj2)
  20. {
  21. return obj1._GID == obj2._GID;
  22. }
  23. public static bool operator !=(EntityReference obj1, EntityReference obj2)
  24. {
  25. return obj1._GID != obj2._GID;
  26. }
  27. public override int GetHashCode() { return _GID.GetHashCode(); }
  28. public EntityReference(uint uniqueId) : this(uniqueId, 0) {}
  29. public EntityReference(ulong GID):this() { _GID = GID; }
  30. public EntityReference(uint uniqueId, uint version) : this()
  31. {
  32. _GID = MAKE_GLOBAL_ID(uniqueId, version);
  33. }
  34. public bool Equals(EntityReference other)
  35. {
  36. return _GID == other._GID;
  37. }
  38. public bool Equals(EntityReference x, EntityReference y)
  39. {
  40. return x._GID == y._GID;
  41. }
  42. public override string ToString()
  43. {
  44. return "id:".FastConcat(uniqueID).FastConcat(" version:").FastConcat(version);
  45. }
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public EGID ToEGID(EntitiesDB entitiesDB)
  48. {
  49. DBC.ECS.Check.Require(this != Invalid, "Invalid Reference Used");
  50. return entitiesDB.GetEGID(this);
  51. }
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. public bool ToEGID(EntitiesDB entitiesDB, out EGID egid)
  54. {
  55. DBC.ECS.Check.Require(this != Invalid, "Invalid Reference Used");
  56. return entitiesDB.TryGetEGID(this, out egid);
  57. }
  58. public ulong ToULong()
  59. {
  60. return _GID;
  61. }
  62. static ulong MAKE_GLOBAL_ID(uint uniqueId, uint version)
  63. {
  64. return (ulong)version << 32 | ((ulong)uniqueId & 0xFFFFFFFF);
  65. }
  66. public static EntityReference Invalid => default;
  67. }
  68. }