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.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #if UNITY_NATIVE
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using Svelto.Common;
  5. using Svelto.DataStructures;
  6. namespace Svelto.ECS
  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 && !PROFILE_SVELTO
  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. throw new ECSException("Entity not found");
  53. }
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. public bool TryGetArrayAndEntityIndex(uint entityID, out uint index, out NB<T> array)
  56. {
  57. index = 0;
  58. if (_map.count > 0 && _map.TryFindIndex(entityID, out index))
  59. {
  60. array = new NB<T>(_map.GetValues(out var count).ToNativeArray(out _), count);
  61. return true;
  62. }
  63. array = default;
  64. return false;
  65. }
  66. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  67. public bool Exists(uint idEntityId)
  68. {
  69. return _map.count > 0 && _map.TryFindIndex(idEntityId, out _);
  70. }
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. public uint GetIndex(uint entityID)
  73. {
  74. return _map.GetIndex(entityID);
  75. }
  76. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  77. public bool FindIndex(uint valueKey, out uint index)
  78. {
  79. return _map.TryFindIndex(valueKey, out index);
  80. }
  81. readonly ReadonlySveltoDictionaryNative<uint, T> _map;
  82. }
  83. }
  84. #endif