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.

149 lines
6.1KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Svelto.Common;
  4. using Svelto.DataStructures;
  5. using Svelto.ECS.Hybrid;
  6. namespace Svelto.ECS
  7. {
  8. namespace Native
  9. {
  10. public struct EGIDMultiMapper<T> where T : unmanaged, IEntityComponent
  11. {
  12. public EGIDMultiMapper
  13. (SveltoDictionary<ExclusiveGroupStruct,
  14. SveltoDictionary<uint, T, NativeStrategy<
  15. SveltoDictionaryNode<uint>>, NativeStrategy<T>, NativeStrategy<int>>,
  16. ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  17. ManagedStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>
  18. , NativeStrategy<int>>>, NativeStrategy<int>> dictionary)
  19. {
  20. _dic = dictionary;
  21. }
  22. public int count
  23. {
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. get => _dic.count;
  26. }
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public ref T Entity(EGID entity)
  29. {
  30. #if DEBUG && !PROFILE_SVELTO
  31. if (Exists(entity) == false)
  32. throw new Exception("NativeEGIDMultiMapper: Entity not found");
  33. #endif
  34. ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
  35. return ref sveltoDictionary.GetValueByRef(entity.entityID);
  36. }
  37. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  38. public bool Exists(EGID entity)
  39. {
  40. return _dic.TryFindIndex(entity.groupID, out var index)
  41. && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public bool TryGetEntity(EGID entity, out T component)
  45. {
  46. component = default;
  47. return _dic.TryFindIndex(entity.groupID, out var index)
  48. && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
  49. }
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public bool FindIndex(ExclusiveGroupStruct group, uint entityID, out uint index)
  52. {
  53. index = 0;
  54. return _dic.TryFindIndex(group, out var groupIndex) &&
  55. _dic.GetDirectValueByRef(groupIndex).TryFindIndex(entityID, out index);
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public uint GetIndex(ExclusiveGroupStruct group, uint entityID)
  59. {
  60. uint groupIndex = _dic.GetIndex(group);
  61. return _dic.GetDirectValueByRef(groupIndex).GetIndex(entityID);
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public bool Exists(ExclusiveGroupStruct group, uint entityID)
  65. {
  66. return _dic.TryFindIndex(group, out var groupIndex) &&
  67. _dic.GetDirectValueByRef(groupIndex).ContainsKey(entityID);
  68. }
  69. public Type entityType => TypeCache<T>.type;
  70. SveltoDictionary<ExclusiveGroupStruct,
  71. SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  72. NativeStrategy<int>>, ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  73. ManagedStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  74. NativeStrategy<int>>>, NativeStrategy<int>> _dic;
  75. }
  76. public interface IEGIDMultiMapper
  77. {
  78. bool FindIndex(ExclusiveGroupStruct group, uint entityID, out uint index);
  79. uint GetIndex(ExclusiveGroupStruct group, uint entityID);
  80. bool Exists(ExclusiveGroupStruct group, uint entityID);
  81. Type entityType { get; }
  82. }
  83. }
  84. public struct EGIDMultiMapper<T> where T : struct, IEntityViewComponent
  85. {
  86. public EGIDMultiMapper
  87. (SveltoDictionary<ExclusiveGroupStruct,
  88. SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
  89. ManagedStrategy<int>>, ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  90. ManagedStrategy<SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
  91. ManagedStrategy<int>>>, ManagedStrategy<int>> dictionary)
  92. {
  93. _dic = dictionary;
  94. }
  95. public int count
  96. {
  97. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  98. get => _dic.count;
  99. }
  100. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  101. public ref T Entity(EGID entity)
  102. {
  103. #if DEBUG && !PROFILE_SVELTO
  104. if (Exists(entity) == false)
  105. throw new Exception("NativeEGIDMultiMapper: Entity not found");
  106. #endif
  107. ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
  108. return ref sveltoDictionary.GetValueByRef(entity.entityID);
  109. }
  110. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  111. public bool Exists(EGID entity)
  112. {
  113. return _dic.TryFindIndex(entity.groupID, out var index)
  114. && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
  115. }
  116. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  117. public bool TryGetEntity(EGID entity, out T component)
  118. {
  119. component = default;
  120. return _dic.TryFindIndex(entity.groupID, out var index)
  121. && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
  122. }
  123. SveltoDictionary<ExclusiveGroupStruct,
  124. SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
  125. ManagedStrategy<int>>, ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  126. ManagedStrategy<SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
  127. ManagedStrategy<int>>>, ManagedStrategy<int>> _dic;
  128. }
  129. }