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.

224 lines
6.8KB

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