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.

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