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.

NativeEGIDMapper.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #if UNITY_NATIVE
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using Svelto.Common;
  5. using Svelto.DataStructures;
  6. namespace Svelto.ECS.Native
  7. {
  8. public readonly struct NativeEGIDMapper<T>:IEGIDMapper where T : unmanaged, IEntityComponent
  9. {
  10. public NativeEGIDMapper
  11. (ExclusiveGroupStruct groupStructId, SveltoDictionaryNative<uint, T> toNative) : this()
  12. {
  13. groupID = groupStructId;
  14. _map = toNative;
  15. }
  16. public int count => _map.count;
  17. public Type entityType => TypeCache<T>.type;
  18. public ExclusiveGroupStruct groupID { get; }
  19. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  20. public ref T Entity(uint entityID)
  21. {
  22. #if DEBUG
  23. if (_map.TryFindIndex(entityID, out var findIndex) == false)
  24. throw new Exception("Entity not found in this group ".FastConcat(typeof(T).ToString()));
  25. #else
  26. _map.TryFindIndex(entityID, out var findIndex);
  27. #endif
  28. return ref _map.GetDirectValueByRef(findIndex);
  29. }
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. public bool TryGetEntity(uint entityID, out T value)
  32. {
  33. if (_map.count > 0 && _map.TryFindIndex(entityID, out var index))
  34. {
  35. unsafe
  36. {
  37. value = Unsafe.AsRef<T>(Unsafe.Add<T>((void*) _map.GetValues(out _).ToNativeArray(out _)
  38. , (int) index));
  39. return true;
  40. }
  41. }
  42. value = default;
  43. return false;
  44. }
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public NB<T> GetArrayAndEntityIndex(uint entityID, out uint index)
  47. {
  48. if (_map.TryFindIndex(entityID, out index))
  49. {
  50. return new NB<T>(_map.GetValues(out var count).ToNativeArray(out _), count);
  51. }
  52. #if DEBUG
  53. throw new ECSException("Entity not found");
  54. #else
  55. return default;
  56. #endif
  57. }
  58. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  59. public bool TryGetArrayAndEntityIndex(uint entityID, out uint index, out NB<T> array)
  60. {
  61. index = 0;
  62. if (_map.count > 0 && _map.TryFindIndex(entityID, out index))
  63. {
  64. array = new NB<T>(_map.GetValues(out var count).ToNativeArray(out _), count);
  65. return true;
  66. }
  67. array = default;
  68. return false;
  69. }
  70. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  71. public bool Exists(uint idEntityId)
  72. {
  73. return _map.count > 0 && _map.TryFindIndex(idEntityId, out _);
  74. }
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. public uint GetIndex(uint entityID)
  77. {
  78. return _map.GetIndex(entityID);
  79. }
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public bool FindIndex(uint valueKey, out uint index)
  82. {
  83. return _map.TryFindIndex(valueKey, out index);
  84. }
  85. readonly ReadonlySveltoDictionaryNative<uint, T> _map;
  86. }
  87. }
  88. #endif