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.

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