| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace Lattice.ECS.Core |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// 组件过滤器 - 描述查询条件 |
| | | 10 | | /// |
| | | 11 | | /// FrameSync 风格: |
| | | 12 | | /// - 必须包含的组件(Required) |
| | | 13 | | /// - 必须排除的组件(Excluded) |
| | | 14 | | /// </summary> |
| | | 15 | | public struct ComponentFilter |
| | | 16 | | { |
| | | 17 | | /// <summary>必须包含的组件位图</summary> |
| | | 18 | | public ComponentMask Required; |
| | | 19 | | |
| | | 20 | | /// <summary>必须排除的组件位图</summary> |
| | | 21 | | public ComponentMask Excluded; |
| | | 22 | | |
| | | 23 | | /// <summary>创建空过滤器</summary> |
| | | 24 | | public static ComponentFilter Empty => new ComponentFilter |
| | | 25 | | { |
| | | 26 | | Required = ComponentMask.Empty, |
| | | 27 | | Excluded = ComponentMask.Empty |
| | | 28 | | }; |
| | | 29 | | |
| | | 30 | | /// <summary>添加必须包含的组件</summary> |
| | | 31 | | public ComponentFilter With<T>() where T : unmanaged, IComponent |
| | | 32 | | { |
| | | 33 | | Required.Add(ComponentTypeId<T>.Id); |
| | | 34 | | return this; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary>添加必须排除的组件</summary> |
| | | 38 | | public ComponentFilter Without<T>() where T : unmanaged, IComponent |
| | | 39 | | { |
| | | 40 | | Excluded.Add(ComponentTypeId<T>.Id); |
| | | 41 | | return this; |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// 组件位图 - 512位组件集合(8 x ulong) |
| | | 47 | | /// </summary> |
| | | 48 | | public unsafe struct ComponentMask |
| | | 49 | | { |
| | | 50 | | private fixed ulong _bits[8]; |
| | | 51 | | |
| | | 52 | | /// <summary>最大组件类型数</summary> |
| | | 53 | | public const int MaxComponents = 512; |
| | | 54 | | |
| | | 55 | | /// <summary>位图块数</summary> |
| | | 56 | | public const int BlockCount = 8; |
| | | 57 | | |
| | | 58 | | /// <summary>空位图</summary> |
| | 0 | 59 | | public static ComponentMask Empty => new ComponentMask(); |
| | | 60 | | |
| | | 61 | | /// <summary>是否为空</summary> |
| | | 62 | | public bool IsEmpty |
| | | 63 | | { |
| | | 64 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 65 | | get |
| | | 66 | | { |
| | 0 | 67 | | for (int i = 0; i < BlockCount; i++) |
| | 0 | 68 | | if (_bits[i] != 0) return false; |
| | 0 | 69 | | return true; |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary>获取指定块的位</summary> |
| | | 74 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 75 | | public ulong GetBlock(int index) |
| | | 76 | | { |
| | 0 | 77 | | return _bits[index]; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary>添加组件类型</summary> |
| | | 81 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 82 | | public void Add(int typeId) |
| | | 83 | | { |
| | 0 | 84 | | int block = typeId >> 6; |
| | 0 | 85 | | int bit = typeId & 0x3F; |
| | 0 | 86 | | _bits[block] |= (1UL << bit); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary>移除组件类型</summary> |
| | | 90 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 91 | | public void Remove(int typeId) |
| | | 92 | | { |
| | 0 | 93 | | int block = typeId >> 6; |
| | 0 | 94 | | int bit = typeId & 0x3F; |
| | 0 | 95 | | _bits[block] &= ~(1UL << bit); |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary>检查是否包含组件类型</summary> |
| | | 99 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 100 | | public bool Contains(int typeId) |
| | | 101 | | { |
| | 0 | 102 | | int block = typeId >> 6; |
| | 0 | 103 | | int bit = typeId & 0x3F; |
| | 0 | 104 | | return (_bits[block] & (1UL << bit)) != 0; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary>检查当前集合是否包含另一个集合的所有组件</summary> |
| | | 108 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 109 | | public bool IsSupersetOf(in ComponentMask other) |
| | | 110 | | { |
| | 0 | 111 | | for (int i = 0; i < BlockCount; i++) |
| | 0 | 112 | | if ((_bits[i] & other._bits[i]) != other._bits[i]) |
| | 0 | 113 | | return false; |
| | 0 | 114 | | return true; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary>并集</summary> |
| | | 118 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 119 | | public void UnionWith(in ComponentMask other) |
| | | 120 | | { |
| | 0 | 121 | | for (int i = 0; i < BlockCount; i++) |
| | 0 | 122 | | _bits[i] |= other._bits[i]; |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary>交集</summary> |
| | | 126 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 127 | | public void IntersectWith(in ComponentMask other) |
| | | 128 | | { |
| | 0 | 129 | | for (int i = 0; i < BlockCount; i++) |
| | 0 | 130 | | _bits[i] &= other._bits[i]; |
| | 0 | 131 | | } |
| | | 132 | | } |
| | | 133 | | } |