Mirror of Svelto.ECS because we're a fan of it
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

93 satır
2.7KB

  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. return entitiesDB.TryGetEGID(this, out egid);
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public bool Exists(EntitiesDB entitiesDB)
  59. {
  60. return this != Invalid && entitiesDB.TryGetEGID(this, out _);
  61. }
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public ulong ToULong()
  64. {
  65. return _GID;
  66. }
  67. static ulong MAKE_GLOBAL_ID(uint uniqueId, uint version)
  68. {
  69. return (ulong)version << 32 | ((ulong)uniqueId & 0xFFFFFFFF);
  70. }
  71. public static EntityReference Invalid => default;
  72. }
  73. }