| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using Lattice.Core; |
| | | 6 | | using Lattice.ECS.Core; |
| | | 7 | | |
| | | 8 | | namespace Lattice.ECS.Framework |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// 旧版带裁剪能力的单组件 Filter 兼容层。 |
| | | 12 | | /// 新代码建议直接使用 <see cref="Frame.Query{T}()" /> 并在循环中叠加裁剪判断。 |
| | | 13 | | /// </summary> |
| | | 14 | | [Obsolete("请改用 Frame.Query<T>() 并在循环中处理裁剪。FilterOptimized 仅保留为兼容层。", false)] |
| | | 15 | | [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] |
| | | 16 | | public unsafe class FilterOptimized<T1> where T1 : unmanaged, IComponent |
| | | 17 | | { |
| | | 18 | | private readonly Query<T1> _query; |
| | | 19 | | private readonly CullingSystem* _culling; |
| | | 20 | | |
| | 0 | 21 | | public FilterOptimized(Frame frame, CullingSystem* culling = null) |
| | | 22 | | { |
| | 0 | 23 | | if (frame == null) |
| | | 24 | | { |
| | 0 | 25 | | throw new ArgumentNullException(nameof(frame)); |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | _query = frame.Query<T1>(); |
| | 0 | 29 | | _culling = culling; |
| | 0 | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | public Enumerator GetEnumerator() => new Enumerator(_query.GetEnumerator(), _culling); |
| | | 33 | | |
| | | 34 | | public ref struct Enumerator |
| | | 35 | | { |
| | | 36 | | private Query<T1>.Enumerator _enumerator; |
| | | 37 | | private readonly CullingSystem* _culling; |
| | | 38 | | |
| | | 39 | | public Enumerator(Query<T1>.Enumerator enumerator, CullingSystem* culling) |
| | | 40 | | { |
| | 0 | 41 | | _enumerator = enumerator; |
| | 0 | 42 | | _culling = culling; |
| | 0 | 43 | | CurrentEntity = EntityRef.None; |
| | 0 | 44 | | CurrentPtr = null; |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public EntityRef CurrentEntity; |
| | | 48 | | public T1* CurrentPtr; |
| | 0 | 49 | | public ref T1 Component => ref *CurrentPtr; |
| | | 50 | | |
| | | 51 | | public bool MoveNext() |
| | | 52 | | { |
| | 0 | 53 | | while (_enumerator.MoveNext()) |
| | | 54 | | { |
| | 0 | 55 | | EntityRef entity = _enumerator.CurrentEntity; |
| | 0 | 56 | | if (_culling != null && _culling->IsCulled(entity.Index)) |
| | | 57 | | { |
| | | 58 | | continue; |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | CurrentEntity = entity; |
| | 0 | 62 | | CurrentPtr = _enumerator.CurrentPtr; |
| | 0 | 63 | | return true; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return false; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// 旧版带裁剪能力的双组件 Filter 兼容层。 |
| | | 73 | | /// 新代码建议直接使用 <see cref="Frame.Query{T1,T2}()" /> 并在循环中叠加裁剪判断。 |
| | | 74 | | /// </summary> |
| | | 75 | | [Obsolete("请改用 Frame.Query<T1, T2>() 并在循环中处理裁剪。FilterOptimized 仅保留为兼容层。", false)] |
| | | 76 | | [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] |
| | | 77 | | public unsafe class FilterOptimized<T1, T2> |
| | | 78 | | where T1 : unmanaged, IComponent |
| | | 79 | | where T2 : unmanaged, IComponent |
| | | 80 | | { |
| | | 81 | | private readonly Query<T1, T2> _query; |
| | | 82 | | private readonly CullingSystem* _culling; |
| | | 83 | | |
| | | 84 | | public FilterOptimized(Frame frame, CullingSystem* culling = null) |
| | | 85 | | { |
| | | 86 | | if (frame == null) |
| | | 87 | | { |
| | | 88 | | throw new ArgumentNullException(nameof(frame)); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | _query = frame.Query<T1, T2>(); |
| | | 92 | | _culling = culling; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public Enumerator GetEnumerator() => new Enumerator(_query.GetEnumerator(), _culling); |
| | | 96 | | |
| | | 97 | | public ref struct Enumerator |
| | | 98 | | { |
| | | 99 | | private Query<T1, T2>.Enumerator _enumerator; |
| | | 100 | | private readonly CullingSystem* _culling; |
| | | 101 | | |
| | | 102 | | public Enumerator(Query<T1, T2>.Enumerator enumerator, CullingSystem* culling) |
| | | 103 | | { |
| | | 104 | | _enumerator = enumerator; |
| | | 105 | | _culling = culling; |
| | | 106 | | CurrentEntity = EntityRef.None; |
| | | 107 | | CurrentPtr1 = null; |
| | | 108 | | CurrentPtr2 = null; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | public EntityRef CurrentEntity; |
| | | 112 | | public T1* CurrentPtr1; |
| | | 113 | | public T2* CurrentPtr2; |
| | | 114 | | public ref T1 Component1 => ref *CurrentPtr1; |
| | | 115 | | public ref T2 Component2 => ref *CurrentPtr2; |
| | | 116 | | |
| | | 117 | | public bool MoveNext() |
| | | 118 | | { |
| | | 119 | | while (_enumerator.MoveNext()) |
| | | 120 | | { |
| | | 121 | | EntityRef entity = _enumerator.Entity; |
| | | 122 | | if (_culling != null && _culling->IsCulled(entity.Index)) |
| | | 123 | | { |
| | | 124 | | continue; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | CurrentEntity = entity; |
| | | 128 | | CurrentPtr1 = _enumerator.Component1Ptr; |
| | | 129 | | CurrentPtr2 = _enumerator.Component2Ptr; |
| | | 130 | | return true; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | return false; |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | } |