< Summary

Information
Class: Lattice.ECS.Framework.Systems.SpawnerSystem
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Framework/Systems/SpawnerSystem.cs
Line coverage
97%
Covered lines: 42
Uncovered lines: 1
Coverable lines: 43
Total lines: 93
Line coverage: 97.6%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Contract()100%11100%
OnInit(...)100%11100%
OnUpdate(...)83.33%121297.29%
OnDestroy(...)100%11100%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Framework/Systems/SpawnerSystem.cs

#LineLine coverage
 1using System;
 2using Lattice.Core;
 3using Lattice.ECS.Core;
 4using Lattice.Math;
 5
 6namespace Lattice.ECS.Framework.Systems
 7{
 8    /// <summary>
 9    /// 最小样例生成系统。
 10    /// 用于验证“实体在系统更新中创建,再参与后续系统处理”的链路。
 11    /// </summary>
 12    public sealed class SpawnerSystem : ISystem
 13    {
 1014        public SystemAuthoringContract Contract => new(
 1015            SystemFrameAccess.ReadWrite,
 1016            SystemGlobalAccess.None,
 1017            SystemStructuralChangeAccess.Deferred);
 18
 19        public void OnInit(Frame frame)
 20        {
 221        }
 22
 23        public void OnUpdate(Frame frame, FP deltaTime)
 24        {
 825            var query = frame.Query<Position2D, Spawner>();
 826            int capacity = query.Count;
 827            if (capacity <= 0)
 28            {
 029                return;
 30            }
 31
 832            Span<SpawnCommand> commands = capacity <= 32
 833                ? stackalloc SpawnCommand[capacity]
 834                : new SpawnCommand[capacity];
 35
 836            int commandCount = 0;
 837            var enumerator = query.GetEnumerator();
 1638            while (enumerator.MoveNext())
 39            {
 840                ref Position2D position = ref enumerator.Component1;
 841                ref Spawner spawner = ref enumerator.Component2;
 42
 843                if (spawner.RemainingCount <= 0)
 44                {
 45                    continue;
 46                }
 47
 448                spawner.CooldownRemaining -= deltaTime;
 449                if (spawner.CooldownRemaining > FP.Zero)
 50                {
 51                    continue;
 52                }
 53
 454                commands[commandCount++] = new SpawnCommand
 455                {
 456                    Position = position,
 457                    Velocity = new Velocity2D
 458                    {
 459                        X = spawner.SpawnVelocityX,
 460                        Y = spawner.SpawnVelocityY
 461                    },
 462                    Lifetime = new Lifetime
 463                    {
 464                        Remaining = spawner.SpawnLifetime
 465                    }
 466                };
 67
 468                spawner.RemainingCount--;
 469                spawner.CooldownRemaining += spawner.Interval;
 70            }
 71
 2472            for (int i = 0; i < commandCount; i++)
 73            {
 474                EntityRef entity = frame.CreateEntity();
 475                frame.Add(entity, commands[i].Position);
 476                frame.Add(entity, commands[i].Velocity);
 477                frame.Add(entity, commands[i].Lifetime);
 478                frame.Add(entity, new ProjectileTag());
 79            }
 880        }
 81
 82        public void OnDestroy(Frame frame)
 83        {
 284        }
 85
 86        private struct SpawnCommand
 87        {
 88            public Position2D Position;
 89            public Velocity2D Velocity;
 90            public Lifetime Lifetime;
 91        }
 92    }
 93}