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.

222 lines
6.8KB

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