Mirror of Svelto.ECS because we're a fan of it
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

112 líneas
4.1KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Svelto.Common;
  4. using Svelto.DataStructures;
  5. using Svelto.DataStructures.Native;
  6. using Svelto.ECS.Internal;
  7. namespace Svelto.ECS.Native
  8. {
  9. /// <summary>
  10. /// Note: this class should really be ref struct by design. It holds the reference of a dictionary that can become
  11. /// invalid. Unfortunately it can be a ref struct, because Jobs needs to hold if by paramater. So the deal is
  12. /// that a job can use it as long as nothing else is modifying the entities database and the NativeEGIDMapper
  13. /// is disposed right after the use.
  14. /// </summary>
  15. public readonly struct NativeEGIDMapper<T> : IEGIDMapper where T : unmanaged, _IInternalEntityComponent
  16. {
  17. public static readonly NativeEGIDMapper<T> empty = new NativeEGIDMapper<T>
  18. (default, new SharedSveltoDictionaryNative<uint, T>(0, Allocator.Persistent));
  19. public NativeEGIDMapper(ExclusiveGroupStruct groupStructId,
  20. in SharedSveltoDictionaryNative<uint, T> toDisposableNative) : this()
  21. {
  22. groupID = groupStructId;
  23. _map = toDisposableNative;
  24. }
  25. public int count => _map.dictionary.count;
  26. public Type entityType => TypeCache<T>.type;
  27. public ExclusiveGroupStruct groupID { get; }
  28. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  29. public ref T Entity(uint entityID)
  30. {
  31. var sveltoDictionary = _map.dictionary;
  32. #if DEBUG && !PROFILE_SVELTO
  33. if (sveltoDictionary.TryFindIndex(entityID, out var findIndex) == false)
  34. throw new Exception($"Entity {entityID} not found in this group {groupID} - {typeof(T).Name}");
  35. #else
  36. sveltoDictionary.TryFindIndex(entityID, out var findIndex);
  37. #endif
  38. return ref sveltoDictionary.GetDirectValueByRef(findIndex);
  39. }
  40. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41. public bool TryGetEntity(uint entityID, out T value)
  42. {
  43. var sveltoDictionary = _map.dictionary;
  44. if (sveltoDictionary.count > 0 && sveltoDictionary.TryFindIndex(entityID, out var index))
  45. {
  46. var values = sveltoDictionary.unsafeValues.ToRealBuffer();
  47. value = values[index];
  48. return true;
  49. }
  50. value = default;
  51. return false;
  52. }
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public NB<T> GetArrayAndEntityIndex(uint entityID, out uint index)
  55. {
  56. var sveltoDictionary = _map.dictionary;
  57. if (sveltoDictionary.TryFindIndex(entityID, out index))
  58. return sveltoDictionary.unsafeValues.ToRealBuffer();
  59. #if DEBUG && !PROFILE_SVELTO
  60. throw new ECSException("Entity not found");
  61. #else
  62. return default;
  63. #endif
  64. }
  65. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66. public bool TryGetArrayAndEntityIndex(uint entityID, out uint index, out NB<T> array)
  67. {
  68. index = 0;
  69. var sveltoDictionary = _map.dictionary;
  70. if (sveltoDictionary.count > 0 && sveltoDictionary.TryFindIndex(entityID, out index))
  71. {
  72. array = sveltoDictionary.unsafeValues.ToRealBuffer();
  73. return true;
  74. }
  75. array = default;
  76. return false;
  77. }
  78. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. public bool Exists(uint idEntityId)
  80. {
  81. var sveltoDictionary = _map.dictionary;
  82. return sveltoDictionary.count > 0 && sveltoDictionary.TryFindIndex(idEntityId, out _);
  83. }
  84. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  85. public uint GetIndex(uint entityID)
  86. {
  87. return _map.dictionary.GetIndex(entityID);
  88. }
  89. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  90. public bool FindIndex(uint valueKey, out uint index)
  91. {
  92. return _map.dictionary.TryFindIndex(valueKey, out index);
  93. }
  94. readonly SharedSveltoDictionaryNative<uint, T> _map;
  95. }
  96. }