Mirror of Svelto.ECS because we're a fan of it
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

231 行
7.0KB

  1. using System.Runtime.CompilerServices;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Internal;
  4. namespace Svelto.ECS
  5. {
  6. public readonly ref struct EntityCollection<T> where T : struct, IEntityComponent
  7. {
  8. static readonly bool IsUnmanaged = TypeSafeDictionary<T>.isUnmanaged;
  9. public EntityCollection(IBuffer<T> buffer, uint count, EntityIDs entityIDs) : this()
  10. {
  11. DBC.ECS.Check.Require(count == 0 || buffer.isValid, "Buffer is found in impossible state");
  12. if (IsUnmanaged)
  13. {
  14. _nativedBuffer = (NB<T>)buffer;
  15. _nativedIndices = entityIDs.nativeIDs;
  16. }
  17. else
  18. {
  19. _managedBuffer = (MB<T>)buffer;
  20. _managedIndices = entityIDs.managedIDs;
  21. }
  22. this.count = count;
  23. }
  24. public EntityCollection(IBuffer<T> buffer, uint count) : this()
  25. {
  26. DBC.ECS.Check.Require(count == 0 || buffer.isValid, "Buffer is found in impossible state");
  27. if (IsUnmanaged)
  28. _nativedBuffer = (NB<T>)buffer;
  29. else
  30. _managedBuffer = (MB<T>)buffer;
  31. this.count = count;
  32. }
  33. public uint count { get; }
  34. internal readonly MB<T> _managedBuffer;
  35. internal readonly NB<T> _nativedBuffer;
  36. internal readonly NativeEntityIDs _nativedIndices;
  37. internal readonly ManagedEntityIDs _managedIndices;
  38. }
  39. public readonly ref struct EntityCollection<T1, T2> where T1 : struct, IEntityComponent
  40. where T2 : struct, IEntityComponent
  41. {
  42. internal EntityCollection(in EntityCollection<T1> array1, in EntityCollection<T2> array2)
  43. {
  44. buffer1 = array1;
  45. buffer2 = array2;
  46. }
  47. public int count => (int)buffer1.count;
  48. internal EntityCollection<T2> buffer2
  49. {
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. get;
  52. }
  53. internal EntityCollection<T1> buffer1
  54. {
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. get;
  57. }
  58. }
  59. public readonly ref struct EntityCollection<T1, T2, T3> where T3 : struct, IEntityComponent
  60. where T2 : struct, IEntityComponent
  61. where T1 : struct, IEntityComponent
  62. {
  63. internal EntityCollection(in EntityCollection<T1> array1, in EntityCollection<T2> array2,
  64. in EntityCollection<T3> array3)
  65. {
  66. buffer1 = array1;
  67. buffer2 = array2;
  68. buffer3 = array3;
  69. }
  70. internal EntityCollection<T1> buffer1
  71. {
  72. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  73. get;
  74. }
  75. internal EntityCollection<T2> buffer2
  76. {
  77. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  78. get;
  79. }
  80. internal EntityCollection<T3> buffer3
  81. {
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. get;
  84. }
  85. internal int count => (int)buffer1.count;
  86. }
  87. public readonly ref struct EntityCollection<T1, T2, T3, T4> where T1 : struct, IEntityComponent
  88. where T2 : struct, IEntityComponent
  89. where T3 : struct, IEntityComponent
  90. where T4 : struct, IEntityComponent
  91. {
  92. internal EntityCollection(in EntityCollection<T1> array1, in EntityCollection<T2> array2,
  93. in EntityCollection<T3> array3, in EntityCollection<T4> array4)
  94. {
  95. buffer1 = array1;
  96. buffer2 = array2;
  97. buffer3 = array3;
  98. buffer4 = array4;
  99. }
  100. internal EntityCollection<T1> buffer1
  101. {
  102. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  103. get;
  104. }
  105. internal EntityCollection<T2> buffer2
  106. {
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. get;
  109. }
  110. internal EntityCollection<T3> buffer3
  111. {
  112. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  113. get;
  114. }
  115. internal EntityCollection<T4> buffer4
  116. {
  117. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  118. get;
  119. }
  120. internal int count => (int)buffer1.count;
  121. }
  122. public readonly struct BT<BufferT1, BufferT2, BufferT3, BufferT4>
  123. {
  124. public readonly BufferT1 buffer1;
  125. public readonly BufferT2 buffer2;
  126. public readonly BufferT3 buffer3;
  127. public readonly BufferT4 buffer4;
  128. public readonly int count;
  129. BT(in (BufferT1 bufferT1, BufferT2 bufferT2, BufferT3 bufferT3, BufferT4 bufferT4, int count) buffer) :
  130. this()
  131. {
  132. buffer1 = buffer.bufferT1;
  133. buffer2 = buffer.bufferT2;
  134. buffer3 = buffer.bufferT3;
  135. buffer4 = buffer.bufferT4;
  136. count = buffer.count;
  137. }
  138. public static implicit operator BT<BufferT1, BufferT2, BufferT3, BufferT4>(
  139. in (BufferT1 bufferT1, BufferT2 bufferT2, BufferT3 bufferT3, BufferT4 bufferT4, int count) buffer)
  140. {
  141. return new BT<BufferT1, BufferT2, BufferT3, BufferT4>(buffer);
  142. }
  143. }
  144. public readonly struct BT<BufferT1, BufferT2, BufferT3>
  145. {
  146. public readonly BufferT1 buffer1;
  147. public readonly BufferT2 buffer2;
  148. public readonly BufferT3 buffer3;
  149. public readonly int count;
  150. BT(in (BufferT1 bufferT1, BufferT2 bufferT2, BufferT3 bufferT3, int count) buffer) : this()
  151. {
  152. buffer1 = buffer.bufferT1;
  153. buffer2 = buffer.bufferT2;
  154. buffer3 = buffer.bufferT3;
  155. count = buffer.count;
  156. }
  157. public static implicit operator BT<BufferT1, BufferT2, BufferT3>(
  158. in (BufferT1 bufferT1, BufferT2 bufferT2, BufferT3 bufferT3, int count) buffer)
  159. {
  160. return new BT<BufferT1, BufferT2, BufferT3>(buffer);
  161. }
  162. }
  163. public readonly struct BT<BufferT1>
  164. {
  165. public readonly BufferT1 buffer;
  166. public readonly int count;
  167. BT(in (BufferT1 bufferT1, int count) buffer) : this()
  168. {
  169. this.buffer = buffer.bufferT1;
  170. count = buffer.count;
  171. }
  172. public static implicit operator BT<BufferT1>(in (BufferT1 bufferT1, int count) buffer)
  173. {
  174. return new BT<BufferT1>(buffer);
  175. }
  176. public static implicit operator BufferT1(BT<BufferT1> t) => t.buffer;
  177. }
  178. public readonly struct BT<BufferT1, BufferT2>
  179. {
  180. public readonly BufferT1 buffer1;
  181. public readonly BufferT2 buffer2;
  182. public readonly int count;
  183. BT(in (BufferT1 bufferT1, BufferT2 bufferT2, int count) buffer) : this()
  184. {
  185. buffer1 = buffer.bufferT1;
  186. buffer2 = buffer.bufferT2;
  187. count = buffer.count;
  188. }
  189. public static implicit operator BT<BufferT1, BufferT2>(
  190. in (BufferT1 bufferT1, BufferT2 bufferT2, int count) buffer)
  191. {
  192. return new BT<BufferT1, BufferT2>(buffer);
  193. }
  194. }
  195. }