| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using Lattice.Core; |
| | | 7 | | |
| | | 8 | | namespace Lattice.ECS.Core |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// 旧版单组件 Filter 兼容层。 |
| | | 12 | | /// 新代码应直接使用 <see cref="Frame.Query{T}()" />。 |
| | | 13 | | /// </summary> |
| | | 14 | | [Obsolete("请改用 Frame.Query<T>()。Filter 仅保留为兼容层,后续版本将移除。", false)] |
| | | 15 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 16 | | public unsafe class Filter<T1> where T1 : unmanaged, IComponent |
| | | 17 | | { |
| | | 18 | | private readonly Frame _frame; |
| | | 19 | | private readonly Query<T1> _query; |
| | | 20 | | |
| | 0 | 21 | | public Filter(Frame frame) |
| | | 22 | | { |
| | 0 | 23 | | ArgumentNullException.ThrowIfNull(frame); |
| | 0 | 24 | | _frame = frame; |
| | 0 | 25 | | _query = frame.Query<T1>(); |
| | 0 | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | public Enumerator GetEnumerator() => new Enumerator(_query.GetEnumerator()); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// 获取底层 Block 迭代器,供旧调用方继续使用。 |
| | | 32 | | /// </summary> |
| | | 33 | | public ComponentBlockIterator<T1> GetBlockIterator() |
| | | 34 | | { |
| | 0 | 35 | | return _frame.GetComponentBlockIterator<T1>(); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public ref struct Enumerator |
| | | 39 | | { |
| | | 40 | | private Query<T1>.Enumerator _enumerator; |
| | | 41 | | |
| | | 42 | | public Enumerator(Query<T1>.Enumerator enumerator) |
| | | 43 | | { |
| | 0 | 44 | | _enumerator = enumerator; |
| | 0 | 45 | | CurrentEntity = EntityRef.None; |
| | 0 | 46 | | CurrentPtr = null; |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | public EntityRef CurrentEntity; |
| | | 50 | | |
| | | 51 | | public T1* CurrentPtr; |
| | | 52 | | |
| | 0 | 53 | | public ref T1 Component => ref *CurrentPtr; |
| | | 54 | | |
| | | 55 | | public bool MoveNext() |
| | | 56 | | { |
| | 0 | 57 | | if (!_enumerator.MoveNext()) |
| | | 58 | | { |
| | 0 | 59 | | return false; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | CurrentEntity = _enumerator.CurrentEntity; |
| | 0 | 63 | | CurrentPtr = _enumerator.CurrentPtr; |
| | 0 | 64 | | return true; |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// 旧版双组件 Filter 兼容层。 |
| | | 71 | | /// 新代码应直接使用 <see cref="Frame.Query{T1,T2}()" />。 |
| | | 72 | | /// </summary> |
| | | 73 | | [Obsolete("请改用 Frame.Query<T1, T2>()。Filter 仅保留为兼容层,后续版本将移除。", false)] |
| | | 74 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 75 | | public unsafe class Filter<T1, T2> |
| | | 76 | | where T1 : unmanaged, IComponent |
| | | 77 | | where T2 : unmanaged, IComponent |
| | | 78 | | { |
| | | 79 | | private readonly Query<T1, T2> _query; |
| | | 80 | | |
| | | 81 | | public Filter(Frame frame) |
| | | 82 | | { |
| | | 83 | | ArgumentNullException.ThrowIfNull(frame); |
| | | 84 | | _query = frame.Query<T1, T2>(); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public Enumerator GetEnumerator() => new Enumerator(_query.GetEnumerator()); |
| | | 88 | | |
| | | 89 | | public ref struct Enumerator |
| | | 90 | | { |
| | | 91 | | private Query<T1, T2>.Enumerator _enumerator; |
| | | 92 | | |
| | | 93 | | public Enumerator(Query<T1, T2>.Enumerator enumerator) |
| | | 94 | | { |
| | | 95 | | _enumerator = enumerator; |
| | | 96 | | Entity = EntityRef.None; |
| | | 97 | | Component1Ptr = null; |
| | | 98 | | Component2Ptr = null; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public EntityRef Entity { get; private set; } |
| | | 102 | | |
| | | 103 | | public T1* Component1Ptr { get; private set; } |
| | | 104 | | |
| | | 105 | | public T2* Component2Ptr { get; private set; } |
| | | 106 | | |
| | | 107 | | public ref T1 Component1 => ref *Component1Ptr; |
| | | 108 | | |
| | | 109 | | public ref T2 Component2 => ref *Component2Ptr; |
| | | 110 | | |
| | | 111 | | public bool MoveNext() |
| | | 112 | | { |
| | | 113 | | if (!_enumerator.MoveNext()) |
| | | 114 | | { |
| | | 115 | | return false; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | Entity = _enumerator.Entity; |
| | | 119 | | Component1Ptr = _enumerator.Component1Ptr; |
| | | 120 | | Component2Ptr = _enumerator.Component2Ptr; |
| | | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// 旧版三组件 Filter 兼容层。 |
| | | 128 | | /// 新代码应直接使用 <see cref="Frame.Query{T1,T2,T3}()" />。 |
| | | 129 | | /// </summary> |
| | | 130 | | [Obsolete("请改用 Frame.Query<T1, T2, T3>()。Filter 仅保留为兼容层,后续版本将移除。", false)] |
| | | 131 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 132 | | public unsafe class Filter<T1, T2, T3> |
| | | 133 | | where T1 : unmanaged, IComponent |
| | | 134 | | where T2 : unmanaged, IComponent |
| | | 135 | | where T3 : unmanaged, IComponent |
| | | 136 | | { |
| | | 137 | | private readonly Query<T1, T2, T3> _query; |
| | | 138 | | |
| | | 139 | | public Filter(Frame frame) |
| | | 140 | | { |
| | | 141 | | ArgumentNullException.ThrowIfNull(frame); |
| | | 142 | | _query = frame.Query<T1, T2, T3>(); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | public Enumerator GetEnumerator() => new Enumerator(_query.GetEnumerator()); |
| | | 146 | | |
| | | 147 | | public ref struct Enumerator |
| | | 148 | | { |
| | | 149 | | private Query<T1, T2, T3>.Enumerator _enumerator; |
| | | 150 | | |
| | | 151 | | public Enumerator(Query<T1, T2, T3>.Enumerator enumerator) |
| | | 152 | | { |
| | | 153 | | _enumerator = enumerator; |
| | | 154 | | Entity = EntityRef.None; |
| | | 155 | | Component1Ptr = null; |
| | | 156 | | Component2Ptr = null; |
| | | 157 | | Component3Ptr = null; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | public EntityRef Entity { get; private set; } |
| | | 161 | | |
| | | 162 | | public T1* Component1Ptr { get; private set; } |
| | | 163 | | |
| | | 164 | | public T2* Component2Ptr { get; private set; } |
| | | 165 | | |
| | | 166 | | public T3* Component3Ptr { get; private set; } |
| | | 167 | | |
| | | 168 | | public ref T1 Component1 => ref *Component1Ptr; |
| | | 169 | | |
| | | 170 | | public ref T2 Component2 => ref *Component2Ptr; |
| | | 171 | | |
| | | 172 | | public ref T3 Component3 => ref *Component3Ptr; |
| | | 173 | | |
| | | 174 | | public bool MoveNext() |
| | | 175 | | { |
| | | 176 | | if (!_enumerator.MoveNext()) |
| | | 177 | | { |
| | | 178 | | return false; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | Entity = _enumerator.Entity; |
| | | 182 | | Component1Ptr = _enumerator.Component1Ptr; |
| | | 183 | | Component2Ptr = _enumerator.Component2Ptr; |
| | | 184 | | Component3Ptr = _enumerator.Component3Ptr; |
| | | 185 | | return true; |
| | | 186 | | } |
| | | 187 | | } |
| | | 188 | | } |
| | | 189 | | } |