| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using Lattice.Core; |
| | | 7 | | |
| | | 8 | | namespace Lattice.ECS.Core |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// 单组件查询。 |
| | | 12 | | /// </summary> |
| | | 13 | | public readonly unsafe struct Query<T1> where T1 : unmanaged, IComponent |
| | | 14 | | { |
| | | 15 | | private readonly Storage<T1>* _storage1; |
| | | 16 | | |
| | | 17 | | internal Query(Frame frame) |
| | | 18 | | { |
| | 1148 | 19 | | ArgumentNullException.ThrowIfNull(frame); |
| | 1148 | 20 | | _storage1 = frame.GetStoragePointer<T1>(); |
| | 1148 | 21 | | } |
| | | 22 | | |
| | 119 | 23 | | public int Count => _storage1 != null ? _storage1->UsedCount : 0; |
| | | 24 | | |
| | 1138 | 25 | | public Enumerator GetEnumerator() => new Enumerator(_storage1); |
| | | 26 | | |
| | | 27 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 28 | | public void ForEach(delegate* managed<EntityRef, T1*, void> action) |
| | | 29 | | { |
| | 1 | 30 | | if (_storage1 == null || action == null) |
| | | 31 | | { |
| | 0 | 32 | | return; |
| | | 33 | | } |
| | | 34 | | |
| | 1 | 35 | | var iterator = new ActiveComponentIterator<T1>(_storage1); |
| | 3 | 36 | | while (iterator.Next(out EntityRef entity, out T1* component)) |
| | | 37 | | { |
| | 2 | 38 | | action(entity, component); |
| | 2 | 39 | | } |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | public unsafe ref struct Enumerator |
| | | 43 | | { |
| | | 44 | | private readonly Storage<T1>* _storage1; |
| | | 45 | | private ActiveComponentIterator<T1> _iterator; |
| | | 46 | | |
| | | 47 | | internal Enumerator(Storage<T1>* storage1) |
| | | 48 | | { |
| | 1138 | 49 | | _storage1 = storage1; |
| | 1138 | 50 | | _iterator = storage1 != null ? new ActiveComponentIterator<T1>(storage1) : default; |
| | 1138 | 51 | | CurrentEntity = EntityRef.None; |
| | 1138 | 52 | | CurrentPtr = null; |
| | 1138 | 53 | | } |
| | | 54 | | |
| | 2827 | 55 | | public EntityRef CurrentEntity { get; private set; } |
| | | 56 | | |
| | 4890 | 57 | | public T1* CurrentPtr { get; private set; } |
| | | 58 | | |
| | 2068 | 59 | | public ref T1 Component => ref *CurrentPtr; |
| | | 60 | | |
| | | 61 | | public bool MoveNext() |
| | | 62 | | { |
| | 2708 | 63 | | if (_storage1 == null) |
| | | 64 | | { |
| | 15 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | 2693 | 68 | | while (_iterator.Next(out EntityRef entity, out T1* ptr)) |
| | | 69 | | { |
| | 1684 | 70 | | CurrentEntity = entity; |
| | 1684 | 71 | | CurrentPtr = ptr; |
| | 1684 | 72 | | return true; |
| | | 73 | | } |
| | | 74 | | |
| | 1009 | 75 | | return false; |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// 双组件查询。 |
| | | 82 | | /// </summary> |
| | | 83 | | public readonly unsafe struct Query<T1, T2> |
| | | 84 | | where T1 : unmanaged, IComponent |
| | | 85 | | where T2 : unmanaged, IComponent |
| | | 86 | | { |
| | | 87 | | private readonly Frame _frame; |
| | | 88 | | private readonly Storage<T1>* _storage1; |
| | | 89 | | private readonly Storage<T2>* _storage2; |
| | | 90 | | private readonly bool _useStorage2AsPrimary; |
| | | 91 | | private readonly int _typeId1; |
| | | 92 | | private readonly int _typeId2; |
| | | 93 | | |
| | | 94 | | internal Query(Frame frame) |
| | | 95 | | { |
| | | 96 | | _frame = frame ?? throw new ArgumentNullException(nameof(frame)); |
| | | 97 | | _storage1 = frame.GetStoragePointer<T1>(); |
| | | 98 | | _storage2 = frame.GetStoragePointer<T2>(); |
| | | 99 | | _typeId1 = ComponentTypeId<T1>.Id; |
| | | 100 | | _typeId2 = ComponentTypeId<T2>.Id; |
| | | 101 | | |
| | | 102 | | int count1 = _storage1 != null ? _storage1->UsedCount : int.MaxValue; |
| | | 103 | | int count2 = _storage2 != null ? _storage2->UsedCount : int.MaxValue; |
| | | 104 | | _useStorage2AsPrimary = count2 < count1; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | public int Count |
| | | 108 | | { |
| | | 109 | | get |
| | | 110 | | { |
| | | 111 | | if (_storage1 == null || _storage2 == null) |
| | | 112 | | { |
| | | 113 | | return 0; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | return _useStorage2AsPrimary ? _storage2->UsedCount : _storage1->UsedCount; |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | public Enumerator GetEnumerator() => new Enumerator(_frame, _storage1, _storage2, _useStorage2AsPrimary, _typeId |
| | | 121 | | |
| | | 122 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 123 | | public void ForEach(delegate* managed<EntityRef, T1*, T2*, void> action) |
| | | 124 | | { |
| | | 125 | | if (_storage1 == null || _storage2 == null || action == null) |
| | | 126 | | { |
| | | 127 | | return; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | var enumerator = GetEnumerator(); |
| | | 131 | | while (enumerator.MoveNext()) |
| | | 132 | | { |
| | | 133 | | action(enumerator.Entity, enumerator.Component1Ptr, enumerator.Component2Ptr); |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | public unsafe ref struct Enumerator |
| | | 138 | | { |
| | | 139 | | private readonly Frame _frame; |
| | | 140 | | private readonly Storage<T1>* _storage1; |
| | | 141 | | private readonly Storage<T2>* _storage2; |
| | | 142 | | private readonly bool _useStorage2AsPrimary; |
| | | 143 | | private readonly int _typeId1; |
| | | 144 | | private readonly int _typeId2; |
| | | 145 | | private ActiveComponentIterator<T1> _iterator1; |
| | | 146 | | private ActiveComponentIterator<T2> _iterator2; |
| | | 147 | | |
| | | 148 | | internal Enumerator(Frame frame, Storage<T1>* storage1, Storage<T2>* storage2, bool useStorage2AsPrimary, in |
| | | 149 | | { |
| | | 150 | | _frame = frame; |
| | | 151 | | _storage1 = storage1; |
| | | 152 | | _storage2 = storage2; |
| | | 153 | | _useStorage2AsPrimary = useStorage2AsPrimary; |
| | | 154 | | _typeId1 = typeId1; |
| | | 155 | | _typeId2 = typeId2; |
| | | 156 | | _iterator1 = storage1 != null ? new ActiveComponentIterator<T1>(storage1) : default; |
| | | 157 | | _iterator2 = storage2 != null ? new ActiveComponentIterator<T2>(storage2) : default; |
| | | 158 | | Entity = EntityRef.None; |
| | | 159 | | Component1Ptr = null; |
| | | 160 | | Component2Ptr = null; |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | public EntityRef Entity { get; private set; } |
| | | 164 | | |
| | | 165 | | public T1* Component1Ptr { get; private set; } |
| | | 166 | | |
| | | 167 | | public T2* Component2Ptr { get; private set; } |
| | | 168 | | |
| | | 169 | | public ref T1 Component1 => ref *Component1Ptr; |
| | | 170 | | |
| | | 171 | | public ref T2 Component2 => ref *Component2Ptr; |
| | | 172 | | |
| | | 173 | | public bool MoveNext() |
| | | 174 | | { |
| | | 175 | | if (_storage1 == null || _storage2 == null) |
| | | 176 | | { |
| | | 177 | | return false; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | if (_useStorage2AsPrimary) |
| | | 181 | | { |
| | | 182 | | while (_iterator2.Next(out EntityRef entity, out T2* ptr2)) |
| | | 183 | | { |
| | | 184 | | if (!_frame.HasComponentBit(entity.Index, _typeId1)) |
| | | 185 | | { |
| | | 186 | | continue; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | T1* ptr1 = _storage1->GetPointerAssumePresent(entity); |
| | | 190 | | if (ptr1 == null) |
| | | 191 | | { |
| | | 192 | | continue; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | Entity = entity; |
| | | 196 | | Component1Ptr = ptr1; |
| | | 197 | | Component2Ptr = ptr2; |
| | | 198 | | return true; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | return false; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | while (_iterator1.Next(out EntityRef entity, out T1* ptr1)) |
| | | 205 | | { |
| | | 206 | | if (!_frame.HasComponentBit(entity.Index, _typeId2)) |
| | | 207 | | { |
| | | 208 | | continue; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | T2* ptr2 = _storage2->GetPointerAssumePresent(entity); |
| | | 212 | | if (ptr2 == null) |
| | | 213 | | { |
| | | 214 | | continue; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | Entity = entity; |
| | | 218 | | Component1Ptr = ptr1; |
| | | 219 | | Component2Ptr = ptr2; |
| | | 220 | | return true; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | return false; |
| | | 224 | | } |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// 三组件查询。 |
| | | 230 | | /// </summary> |
| | | 231 | | public readonly unsafe struct Query<T1, T2, T3> |
| | | 232 | | where T1 : unmanaged, IComponent |
| | | 233 | | where T2 : unmanaged, IComponent |
| | | 234 | | where T3 : unmanaged, IComponent |
| | | 235 | | { |
| | | 236 | | private readonly Frame _frame; |
| | | 237 | | private readonly Storage<T1>* _storage1; |
| | | 238 | | private readonly Storage<T2>* _storage2; |
| | | 239 | | private readonly Storage<T3>* _storage3; |
| | | 240 | | private readonly int _primaryIndex; |
| | | 241 | | private readonly int _typeId1; |
| | | 242 | | private readonly int _typeId2; |
| | | 243 | | private readonly int _typeId3; |
| | | 244 | | |
| | | 245 | | internal Query(Frame frame) |
| | | 246 | | { |
| | | 247 | | _frame = frame ?? throw new ArgumentNullException(nameof(frame)); |
| | | 248 | | _storage1 = frame.GetStoragePointer<T1>(); |
| | | 249 | | _storage2 = frame.GetStoragePointer<T2>(); |
| | | 250 | | _storage3 = frame.GetStoragePointer<T3>(); |
| | | 251 | | _typeId1 = ComponentTypeId<T1>.Id; |
| | | 252 | | _typeId2 = ComponentTypeId<T2>.Id; |
| | | 253 | | _typeId3 = ComponentTypeId<T3>.Id; |
| | | 254 | | |
| | | 255 | | int count1 = _storage1 != null ? _storage1->UsedCount : int.MaxValue; |
| | | 256 | | int count2 = _storage2 != null ? _storage2->UsedCount : int.MaxValue; |
| | | 257 | | int count3 = _storage3 != null ? _storage3->UsedCount : int.MaxValue; |
| | | 258 | | |
| | | 259 | | if (count1 <= count2 && count1 <= count3) |
| | | 260 | | { |
| | | 261 | | _primaryIndex = 0; |
| | | 262 | | } |
| | | 263 | | else if (count2 <= count1 && count2 <= count3) |
| | | 264 | | { |
| | | 265 | | _primaryIndex = 1; |
| | | 266 | | } |
| | | 267 | | else |
| | | 268 | | { |
| | | 269 | | _primaryIndex = 2; |
| | | 270 | | } |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | public Enumerator GetEnumerator() => new Enumerator(_frame, _storage1, _storage2, _storage3, _primaryIndex, _typ |
| | | 274 | | |
| | | 275 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 276 | | public void ForEach(delegate* managed<EntityRef, T1*, T2*, T3*, void> action) |
| | | 277 | | { |
| | | 278 | | if (_storage1 == null || _storage2 == null || _storage3 == null || action == null) |
| | | 279 | | { |
| | | 280 | | return; |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | var enumerator = GetEnumerator(); |
| | | 284 | | while (enumerator.MoveNext()) |
| | | 285 | | { |
| | | 286 | | action(enumerator.Entity, enumerator.Component1Ptr, enumerator.Component2Ptr, enumerator.Component3Ptr); |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | public unsafe ref struct Enumerator |
| | | 291 | | { |
| | | 292 | | private readonly Frame _frame; |
| | | 293 | | private readonly Storage<T1>* _storage1; |
| | | 294 | | private readonly Storage<T2>* _storage2; |
| | | 295 | | private readonly Storage<T3>* _storage3; |
| | | 296 | | private readonly int _primaryIndex; |
| | | 297 | | private readonly int _typeId1; |
| | | 298 | | private readonly int _typeId2; |
| | | 299 | | private readonly int _typeId3; |
| | | 300 | | private ActiveComponentIterator<T1> _iterator1; |
| | | 301 | | private ActiveComponentIterator<T2> _iterator2; |
| | | 302 | | private ActiveComponentIterator<T3> _iterator3; |
| | | 303 | | |
| | | 304 | | internal Enumerator( |
| | | 305 | | Frame frame, |
| | | 306 | | Storage<T1>* storage1, |
| | | 307 | | Storage<T2>* storage2, |
| | | 308 | | Storage<T3>* storage3, |
| | | 309 | | int primaryIndex, |
| | | 310 | | int typeId1, |
| | | 311 | | int typeId2, |
| | | 312 | | int typeId3) |
| | | 313 | | { |
| | | 314 | | _frame = frame; |
| | | 315 | | _storage1 = storage1; |
| | | 316 | | _storage2 = storage2; |
| | | 317 | | _storage3 = storage3; |
| | | 318 | | _primaryIndex = primaryIndex; |
| | | 319 | | _typeId1 = typeId1; |
| | | 320 | | _typeId2 = typeId2; |
| | | 321 | | _typeId3 = typeId3; |
| | | 322 | | _iterator1 = storage1 != null ? new ActiveComponentIterator<T1>(storage1) : default; |
| | | 323 | | _iterator2 = storage2 != null ? new ActiveComponentIterator<T2>(storage2) : default; |
| | | 324 | | _iterator3 = storage3 != null ? new ActiveComponentIterator<T3>(storage3) : default; |
| | | 325 | | Entity = EntityRef.None; |
| | | 326 | | Component1Ptr = null; |
| | | 327 | | Component2Ptr = null; |
| | | 328 | | Component3Ptr = null; |
| | | 329 | | } |
| | | 330 | | |
| | | 331 | | public EntityRef Entity { get; private set; } |
| | | 332 | | |
| | | 333 | | public T1* Component1Ptr { get; private set; } |
| | | 334 | | |
| | | 335 | | public T2* Component2Ptr { get; private set; } |
| | | 336 | | |
| | | 337 | | public T3* Component3Ptr { get; private set; } |
| | | 338 | | |
| | | 339 | | public ref T1 Component1 => ref *Component1Ptr; |
| | | 340 | | |
| | | 341 | | public ref T2 Component2 => ref *Component2Ptr; |
| | | 342 | | |
| | | 343 | | public ref T3 Component3 => ref *Component3Ptr; |
| | | 344 | | |
| | | 345 | | public bool MoveNext() |
| | | 346 | | { |
| | | 347 | | if (_storage1 == null || _storage2 == null || _storage3 == null) |
| | | 348 | | { |
| | | 349 | | return false; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | switch (_primaryIndex) |
| | | 353 | | { |
| | | 354 | | case 0: |
| | | 355 | | return MovePrimary1(); |
| | | 356 | | case 1: |
| | | 357 | | return MovePrimary2(); |
| | | 358 | | default: |
| | | 359 | | return MovePrimary3(); |
| | | 360 | | } |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | private bool MovePrimary1() |
| | | 364 | | { |
| | | 365 | | while (_iterator1.Next(out EntityRef entity, out T1* ptr1)) |
| | | 366 | | { |
| | | 367 | | if (!_frame.HasComponentBit(entity.Index, _typeId2) || |
| | | 368 | | !_frame.HasComponentBit(entity.Index, _typeId3)) |
| | | 369 | | { |
| | | 370 | | continue; |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | T2* ptr2 = _storage2->GetPointerAssumePresent(entity); |
| | | 374 | | T3* ptr3 = _storage3->GetPointerAssumePresent(entity); |
| | | 375 | | if (ptr2 == null || ptr3 == null) |
| | | 376 | | { |
| | | 377 | | continue; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | Entity = entity; |
| | | 381 | | Component1Ptr = ptr1; |
| | | 382 | | Component2Ptr = ptr2; |
| | | 383 | | Component3Ptr = ptr3; |
| | | 384 | | return true; |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | return false; |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | private bool MovePrimary2() |
| | | 391 | | { |
| | | 392 | | while (_iterator2.Next(out EntityRef entity, out T2* ptr2)) |
| | | 393 | | { |
| | | 394 | | if (!_frame.HasComponentBit(entity.Index, _typeId1) || |
| | | 395 | | !_frame.HasComponentBit(entity.Index, _typeId3)) |
| | | 396 | | { |
| | | 397 | | continue; |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | T1* ptr1 = _storage1->GetPointerAssumePresent(entity); |
| | | 401 | | T3* ptr3 = _storage3->GetPointerAssumePresent(entity); |
| | | 402 | | if (ptr1 == null || ptr3 == null) |
| | | 403 | | { |
| | | 404 | | continue; |
| | | 405 | | } |
| | | 406 | | |
| | | 407 | | Entity = entity; |
| | | 408 | | Component1Ptr = ptr1; |
| | | 409 | | Component2Ptr = ptr2; |
| | | 410 | | Component3Ptr = ptr3; |
| | | 411 | | return true; |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | return false; |
| | | 415 | | } |
| | | 416 | | |
| | | 417 | | private bool MovePrimary3() |
| | | 418 | | { |
| | | 419 | | while (_iterator3.Next(out EntityRef entity, out T3* ptr3)) |
| | | 420 | | { |
| | | 421 | | if (!_frame.HasComponentBit(entity.Index, _typeId1) || |
| | | 422 | | !_frame.HasComponentBit(entity.Index, _typeId2)) |
| | | 423 | | { |
| | | 424 | | continue; |
| | | 425 | | } |
| | | 426 | | |
| | | 427 | | T1* ptr1 = _storage1->GetPointerAssumePresent(entity); |
| | | 428 | | T2* ptr2 = _storage2->GetPointerAssumePresent(entity); |
| | | 429 | | if (ptr1 == null || ptr2 == null) |
| | | 430 | | { |
| | | 431 | | continue; |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | Entity = entity; |
| | | 435 | | Component1Ptr = ptr1; |
| | | 436 | | Component2Ptr = ptr2; |
| | | 437 | | Component3Ptr = ptr3; |
| | | 438 | | return true; |
| | | 439 | | } |
| | | 440 | | |
| | | 441 | | return false; |
| | | 442 | | } |
| | | 443 | | } |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | /// <summary> |
| | | 447 | | /// 四组件查询。 |
| | | 448 | | /// </summary> |
| | | 449 | | public readonly unsafe struct Query<T1, T2, T3, T4> |
| | | 450 | | where T1 : unmanaged, IComponent |
| | | 451 | | where T2 : unmanaged, IComponent |
| | | 452 | | where T3 : unmanaged, IComponent |
| | | 453 | | where T4 : unmanaged, IComponent |
| | | 454 | | { |
| | | 455 | | private readonly Frame _frame; |
| | | 456 | | private readonly Storage<T1>* _storage1; |
| | | 457 | | private readonly Storage<T2>* _storage2; |
| | | 458 | | private readonly Storage<T3>* _storage3; |
| | | 459 | | private readonly Storage<T4>* _storage4; |
| | | 460 | | private readonly int _primaryIndex; |
| | | 461 | | private readonly int _typeId1; |
| | | 462 | | private readonly int _typeId2; |
| | | 463 | | private readonly int _typeId3; |
| | | 464 | | private readonly int _typeId4; |
| | | 465 | | |
| | | 466 | | internal Query(Frame frame) |
| | | 467 | | { |
| | | 468 | | _frame = frame ?? throw new ArgumentNullException(nameof(frame)); |
| | | 469 | | _storage1 = frame.GetStoragePointer<T1>(); |
| | | 470 | | _storage2 = frame.GetStoragePointer<T2>(); |
| | | 471 | | _storage3 = frame.GetStoragePointer<T3>(); |
| | | 472 | | _storage4 = frame.GetStoragePointer<T4>(); |
| | | 473 | | _typeId1 = ComponentTypeId<T1>.Id; |
| | | 474 | | _typeId2 = ComponentTypeId<T2>.Id; |
| | | 475 | | _typeId3 = ComponentTypeId<T3>.Id; |
| | | 476 | | _typeId4 = ComponentTypeId<T4>.Id; |
| | | 477 | | |
| | | 478 | | int count1 = _storage1 != null ? _storage1->UsedCount : int.MaxValue; |
| | | 479 | | int count2 = _storage2 != null ? _storage2->UsedCount : int.MaxValue; |
| | | 480 | | int count3 = _storage3 != null ? _storage3->UsedCount : int.MaxValue; |
| | | 481 | | int count4 = _storage4 != null ? _storage4->UsedCount : int.MaxValue; |
| | | 482 | | |
| | | 483 | | _primaryIndex = 0; |
| | | 484 | | int bestCount = count1; |
| | | 485 | | if (count2 < bestCount) |
| | | 486 | | { |
| | | 487 | | _primaryIndex = 1; |
| | | 488 | | bestCount = count2; |
| | | 489 | | } |
| | | 490 | | |
| | | 491 | | if (count3 < bestCount) |
| | | 492 | | { |
| | | 493 | | _primaryIndex = 2; |
| | | 494 | | bestCount = count3; |
| | | 495 | | } |
| | | 496 | | |
| | | 497 | | if (count4 < bestCount) |
| | | 498 | | { |
| | | 499 | | _primaryIndex = 3; |
| | | 500 | | } |
| | | 501 | | } |
| | | 502 | | |
| | | 503 | | public Enumerator GetEnumerator() => new Enumerator( |
| | | 504 | | _frame, |
| | | 505 | | _storage1, |
| | | 506 | | _storage2, |
| | | 507 | | _storage3, |
| | | 508 | | _storage4, |
| | | 509 | | _primaryIndex, |
| | | 510 | | _typeId1, |
| | | 511 | | _typeId2, |
| | | 512 | | _typeId3, |
| | | 513 | | _typeId4); |
| | | 514 | | |
| | | 515 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 516 | | public void ForEach(delegate* managed<EntityRef, T1*, T2*, T3*, T4*, void> action) |
| | | 517 | | { |
| | | 518 | | if (_storage1 == null || _storage2 == null || _storage3 == null || _storage4 == null || action == null) |
| | | 519 | | { |
| | | 520 | | return; |
| | | 521 | | } |
| | | 522 | | |
| | | 523 | | var enumerator = GetEnumerator(); |
| | | 524 | | while (enumerator.MoveNext()) |
| | | 525 | | { |
| | | 526 | | action( |
| | | 527 | | enumerator.Entity, |
| | | 528 | | enumerator.Component1Ptr, |
| | | 529 | | enumerator.Component2Ptr, |
| | | 530 | | enumerator.Component3Ptr, |
| | | 531 | | enumerator.Component4Ptr); |
| | | 532 | | } |
| | | 533 | | } |
| | | 534 | | |
| | | 535 | | public unsafe ref struct Enumerator |
| | | 536 | | { |
| | | 537 | | private readonly Frame _frame; |
| | | 538 | | private readonly Storage<T1>* _storage1; |
| | | 539 | | private readonly Storage<T2>* _storage2; |
| | | 540 | | private readonly Storage<T3>* _storage3; |
| | | 541 | | private readonly Storage<T4>* _storage4; |
| | | 542 | | private readonly int _primaryIndex; |
| | | 543 | | private readonly int _typeId1; |
| | | 544 | | private readonly int _typeId2; |
| | | 545 | | private readonly int _typeId3; |
| | | 546 | | private readonly int _typeId4; |
| | | 547 | | private ActiveComponentIterator<T1> _iterator1; |
| | | 548 | | private ActiveComponentIterator<T2> _iterator2; |
| | | 549 | | private ActiveComponentIterator<T3> _iterator3; |
| | | 550 | | private ActiveComponentIterator<T4> _iterator4; |
| | | 551 | | |
| | | 552 | | internal Enumerator( |
| | | 553 | | Frame frame, |
| | | 554 | | Storage<T1>* storage1, |
| | | 555 | | Storage<T2>* storage2, |
| | | 556 | | Storage<T3>* storage3, |
| | | 557 | | Storage<T4>* storage4, |
| | | 558 | | int primaryIndex, |
| | | 559 | | int typeId1, |
| | | 560 | | int typeId2, |
| | | 561 | | int typeId3, |
| | | 562 | | int typeId4) |
| | | 563 | | { |
| | | 564 | | _frame = frame; |
| | | 565 | | _storage1 = storage1; |
| | | 566 | | _storage2 = storage2; |
| | | 567 | | _storage3 = storage3; |
| | | 568 | | _storage4 = storage4; |
| | | 569 | | _primaryIndex = primaryIndex; |
| | | 570 | | _typeId1 = typeId1; |
| | | 571 | | _typeId2 = typeId2; |
| | | 572 | | _typeId3 = typeId3; |
| | | 573 | | _typeId4 = typeId4; |
| | | 574 | | _iterator1 = storage1 != null ? new ActiveComponentIterator<T1>(storage1) : default; |
| | | 575 | | _iterator2 = storage2 != null ? new ActiveComponentIterator<T2>(storage2) : default; |
| | | 576 | | _iterator3 = storage3 != null ? new ActiveComponentIterator<T3>(storage3) : default; |
| | | 577 | | _iterator4 = storage4 != null ? new ActiveComponentIterator<T4>(storage4) : default; |
| | | 578 | | Entity = EntityRef.None; |
| | | 579 | | Component1Ptr = null; |
| | | 580 | | Component2Ptr = null; |
| | | 581 | | Component3Ptr = null; |
| | | 582 | | Component4Ptr = null; |
| | | 583 | | } |
| | | 584 | | |
| | | 585 | | public EntityRef Entity { get; private set; } |
| | | 586 | | |
| | | 587 | | public T1* Component1Ptr { get; private set; } |
| | | 588 | | |
| | | 589 | | public T2* Component2Ptr { get; private set; } |
| | | 590 | | |
| | | 591 | | public T3* Component3Ptr { get; private set; } |
| | | 592 | | |
| | | 593 | | public T4* Component4Ptr { get; private set; } |
| | | 594 | | |
| | | 595 | | public ref T1 Component1 => ref *Component1Ptr; |
| | | 596 | | |
| | | 597 | | public ref T2 Component2 => ref *Component2Ptr; |
| | | 598 | | |
| | | 599 | | public ref T3 Component3 => ref *Component3Ptr; |
| | | 600 | | |
| | | 601 | | public ref T4 Component4 => ref *Component4Ptr; |
| | | 602 | | |
| | | 603 | | public bool MoveNext() |
| | | 604 | | { |
| | | 605 | | if (_storage1 == null || _storage2 == null || _storage3 == null || _storage4 == null) |
| | | 606 | | { |
| | | 607 | | return false; |
| | | 608 | | } |
| | | 609 | | |
| | | 610 | | switch (_primaryIndex) |
| | | 611 | | { |
| | | 612 | | case 0: |
| | | 613 | | return MovePrimary1(); |
| | | 614 | | case 1: |
| | | 615 | | return MovePrimary2(); |
| | | 616 | | case 2: |
| | | 617 | | return MovePrimary3(); |
| | | 618 | | default: |
| | | 619 | | return MovePrimary4(); |
| | | 620 | | } |
| | | 621 | | } |
| | | 622 | | |
| | | 623 | | private bool MovePrimary1() |
| | | 624 | | { |
| | | 625 | | while (_iterator1.Next(out EntityRef entity, out T1* ptr1)) |
| | | 626 | | { |
| | | 627 | | if (!_frame.HasComponentBit(entity.Index, _typeId2) || |
| | | 628 | | !_frame.HasComponentBit(entity.Index, _typeId3) || |
| | | 629 | | !_frame.HasComponentBit(entity.Index, _typeId4)) |
| | | 630 | | { |
| | | 631 | | continue; |
| | | 632 | | } |
| | | 633 | | |
| | | 634 | | T2* ptr2 = _storage2->GetPointerAssumePresent(entity); |
| | | 635 | | T3* ptr3 = _storage3->GetPointerAssumePresent(entity); |
| | | 636 | | T4* ptr4 = _storage4->GetPointerAssumePresent(entity); |
| | | 637 | | if (ptr2 == null || ptr3 == null || ptr4 == null) |
| | | 638 | | { |
| | | 639 | | continue; |
| | | 640 | | } |
| | | 641 | | |
| | | 642 | | Entity = entity; |
| | | 643 | | Component1Ptr = ptr1; |
| | | 644 | | Component2Ptr = ptr2; |
| | | 645 | | Component3Ptr = ptr3; |
| | | 646 | | Component4Ptr = ptr4; |
| | | 647 | | return true; |
| | | 648 | | } |
| | | 649 | | |
| | | 650 | | return false; |
| | | 651 | | } |
| | | 652 | | |
| | | 653 | | private bool MovePrimary2() |
| | | 654 | | { |
| | | 655 | | while (_iterator2.Next(out EntityRef entity, out T2* ptr2)) |
| | | 656 | | { |
| | | 657 | | if (!_frame.HasComponentBit(entity.Index, _typeId1) || |
| | | 658 | | !_frame.HasComponentBit(entity.Index, _typeId3) || |
| | | 659 | | !_frame.HasComponentBit(entity.Index, _typeId4)) |
| | | 660 | | { |
| | | 661 | | continue; |
| | | 662 | | } |
| | | 663 | | |
| | | 664 | | T1* ptr1 = _storage1->GetPointerAssumePresent(entity); |
| | | 665 | | T3* ptr3 = _storage3->GetPointerAssumePresent(entity); |
| | | 666 | | T4* ptr4 = _storage4->GetPointerAssumePresent(entity); |
| | | 667 | | if (ptr1 == null || ptr3 == null || ptr4 == null) |
| | | 668 | | { |
| | | 669 | | continue; |
| | | 670 | | } |
| | | 671 | | |
| | | 672 | | Entity = entity; |
| | | 673 | | Component1Ptr = ptr1; |
| | | 674 | | Component2Ptr = ptr2; |
| | | 675 | | Component3Ptr = ptr3; |
| | | 676 | | Component4Ptr = ptr4; |
| | | 677 | | return true; |
| | | 678 | | } |
| | | 679 | | |
| | | 680 | | return false; |
| | | 681 | | } |
| | | 682 | | |
| | | 683 | | private bool MovePrimary3() |
| | | 684 | | { |
| | | 685 | | while (_iterator3.Next(out EntityRef entity, out T3* ptr3)) |
| | | 686 | | { |
| | | 687 | | if (!_frame.HasComponentBit(entity.Index, _typeId1) || |
| | | 688 | | !_frame.HasComponentBit(entity.Index, _typeId2) || |
| | | 689 | | !_frame.HasComponentBit(entity.Index, _typeId4)) |
| | | 690 | | { |
| | | 691 | | continue; |
| | | 692 | | } |
| | | 693 | | |
| | | 694 | | T1* ptr1 = _storage1->GetPointerAssumePresent(entity); |
| | | 695 | | T2* ptr2 = _storage2->GetPointerAssumePresent(entity); |
| | | 696 | | T4* ptr4 = _storage4->GetPointerAssumePresent(entity); |
| | | 697 | | if (ptr1 == null || ptr2 == null || ptr4 == null) |
| | | 698 | | { |
| | | 699 | | continue; |
| | | 700 | | } |
| | | 701 | | |
| | | 702 | | Entity = entity; |
| | | 703 | | Component1Ptr = ptr1; |
| | | 704 | | Component2Ptr = ptr2; |
| | | 705 | | Component3Ptr = ptr3; |
| | | 706 | | Component4Ptr = ptr4; |
| | | 707 | | return true; |
| | | 708 | | } |
| | | 709 | | |
| | | 710 | | return false; |
| | | 711 | | } |
| | | 712 | | |
| | | 713 | | private bool MovePrimary4() |
| | | 714 | | { |
| | | 715 | | while (_iterator4.Next(out EntityRef entity, out T4* ptr4)) |
| | | 716 | | { |
| | | 717 | | if (!_frame.HasComponentBit(entity.Index, _typeId1) || |
| | | 718 | | !_frame.HasComponentBit(entity.Index, _typeId2) || |
| | | 719 | | !_frame.HasComponentBit(entity.Index, _typeId3)) |
| | | 720 | | { |
| | | 721 | | continue; |
| | | 722 | | } |
| | | 723 | | |
| | | 724 | | T1* ptr1 = _storage1->GetPointerAssumePresent(entity); |
| | | 725 | | T2* ptr2 = _storage2->GetPointerAssumePresent(entity); |
| | | 726 | | T3* ptr3 = _storage3->GetPointerAssumePresent(entity); |
| | | 727 | | if (ptr1 == null || ptr2 == null || ptr3 == null) |
| | | 728 | | { |
| | | 729 | | continue; |
| | | 730 | | } |
| | | 731 | | |
| | | 732 | | Entity = entity; |
| | | 733 | | Component1Ptr = ptr1; |
| | | 734 | | Component2Ptr = ptr2; |
| | | 735 | | Component3Ptr = ptr3; |
| | | 736 | | Component4Ptr = ptr4; |
| | | 737 | | return true; |
| | | 738 | | } |
| | | 739 | | |
| | | 740 | | return false; |
| | | 741 | | } |
| | | 742 | | } |
| | | 743 | | } |
| | | 744 | | } |