< Summary

Information
Class: Lattice.ECS.Core.ComponentMask
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Core/ComponentFilter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 26
Coverable lines: 26
Total lines: 133
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Empty()100%210%
get_IsEmpty()0%2040%
GetBlock(...)100%210%
Add(...)100%210%
Remove(...)100%210%
Contains(...)100%210%
IsSupersetOf(...)0%2040%
UnionWith(...)0%620%
IntersectWith(...)0%620%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Core/ComponentFilter.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4using System.Runtime.CompilerServices;
 5
 6namespace 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>
 059        public static ComponentMask Empty => new ComponentMask();
 60
 61        /// <summary>是否为空</summary>
 62        public bool IsEmpty
 63        {
 64            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 65            get
 66            {
 067                for (int i = 0; i < BlockCount; i++)
 068                    if (_bits[i] != 0) return false;
 069                return true;
 70            }
 71        }
 72
 73        /// <summary>获取指定块的位</summary>
 74        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 75        public ulong GetBlock(int index)
 76        {
 077            return _bits[index];
 78        }
 79
 80        /// <summary>添加组件类型</summary>
 81        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 82        public void Add(int typeId)
 83        {
 084            int block = typeId >> 6;
 085            int bit = typeId & 0x3F;
 086            _bits[block] |= (1UL << bit);
 087        }
 88
 89        /// <summary>移除组件类型</summary>
 90        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 91        public void Remove(int typeId)
 92        {
 093            int block = typeId >> 6;
 094            int bit = typeId & 0x3F;
 095            _bits[block] &= ~(1UL << bit);
 096        }
 97
 98        /// <summary>检查是否包含组件类型</summary>
 99        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 100        public bool Contains(int typeId)
 101        {
 0102            int block = typeId >> 6;
 0103            int bit = typeId & 0x3F;
 0104            return (_bits[block] & (1UL << bit)) != 0;
 105        }
 106
 107        /// <summary>检查当前集合是否包含另一个集合的所有组件</summary>
 108        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 109        public bool IsSupersetOf(in ComponentMask other)
 110        {
 0111            for (int i = 0; i < BlockCount; i++)
 0112                if ((_bits[i] & other._bits[i]) != other._bits[i])
 0113                    return false;
 0114            return true;
 115        }
 116
 117        /// <summary>并集</summary>
 118        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 119        public void UnionWith(in ComponentMask other)
 120        {
 0121            for (int i = 0; i < BlockCount; i++)
 0122                _bits[i] |= other._bits[i];
 0123        }
 124
 125        /// <summary>交集</summary>
 126        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 127        public void IntersectWith(in ComponentMask other)
 128        {
 0129            for (int i = 0; i < BlockCount; i++)
 0130                _bits[i] &= other._bits[i];
 0131        }
 132    }
 133}