| | | 1 | | using System; |
| | | 2 | | using Lattice.Core; |
| | | 3 | | using Lattice.ECS.Core; |
| | | 4 | | using Lattice.Math; |
| | | 5 | | |
| | | 6 | | namespace Lattice.ECS.Framework.Systems |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// 最小样例生成系统。 |
| | | 10 | | /// 用于验证“实体在系统更新中创建,再参与后续系统处理”的链路。 |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class SpawnerSystem : ISystem |
| | | 13 | | { |
| | 10 | 14 | | public SystemAuthoringContract Contract => new( |
| | 10 | 15 | | SystemFrameAccess.ReadWrite, |
| | 10 | 16 | | SystemGlobalAccess.None, |
| | 10 | 17 | | SystemStructuralChangeAccess.Deferred); |
| | | 18 | | |
| | | 19 | | public void OnInit(Frame frame) |
| | | 20 | | { |
| | 2 | 21 | | } |
| | | 22 | | |
| | | 23 | | public void OnUpdate(Frame frame, FP deltaTime) |
| | | 24 | | { |
| | 8 | 25 | | var query = frame.Query<Position2D, Spawner>(); |
| | 8 | 26 | | int capacity = query.Count; |
| | 8 | 27 | | if (capacity <= 0) |
| | | 28 | | { |
| | 0 | 29 | | return; |
| | | 30 | | } |
| | | 31 | | |
| | 8 | 32 | | Span<SpawnCommand> commands = capacity <= 32 |
| | 8 | 33 | | ? stackalloc SpawnCommand[capacity] |
| | 8 | 34 | | : new SpawnCommand[capacity]; |
| | | 35 | | |
| | 8 | 36 | | int commandCount = 0; |
| | 8 | 37 | | var enumerator = query.GetEnumerator(); |
| | 16 | 38 | | while (enumerator.MoveNext()) |
| | | 39 | | { |
| | 8 | 40 | | ref Position2D position = ref enumerator.Component1; |
| | 8 | 41 | | ref Spawner spawner = ref enumerator.Component2; |
| | | 42 | | |
| | 8 | 43 | | if (spawner.RemainingCount <= 0) |
| | | 44 | | { |
| | | 45 | | continue; |
| | | 46 | | } |
| | | 47 | | |
| | 4 | 48 | | spawner.CooldownRemaining -= deltaTime; |
| | 4 | 49 | | if (spawner.CooldownRemaining > FP.Zero) |
| | | 50 | | { |
| | | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 4 | 54 | | commands[commandCount++] = new SpawnCommand |
| | 4 | 55 | | { |
| | 4 | 56 | | Position = position, |
| | 4 | 57 | | Velocity = new Velocity2D |
| | 4 | 58 | | { |
| | 4 | 59 | | X = spawner.SpawnVelocityX, |
| | 4 | 60 | | Y = spawner.SpawnVelocityY |
| | 4 | 61 | | }, |
| | 4 | 62 | | Lifetime = new Lifetime |
| | 4 | 63 | | { |
| | 4 | 64 | | Remaining = spawner.SpawnLifetime |
| | 4 | 65 | | } |
| | 4 | 66 | | }; |
| | | 67 | | |
| | 4 | 68 | | spawner.RemainingCount--; |
| | 4 | 69 | | spawner.CooldownRemaining += spawner.Interval; |
| | | 70 | | } |
| | | 71 | | |
| | 24 | 72 | | for (int i = 0; i < commandCount; i++) |
| | | 73 | | { |
| | 4 | 74 | | EntityRef entity = frame.CreateEntity(); |
| | 4 | 75 | | frame.Add(entity, commands[i].Position); |
| | 4 | 76 | | frame.Add(entity, commands[i].Velocity); |
| | 4 | 77 | | frame.Add(entity, commands[i].Lifetime); |
| | 4 | 78 | | frame.Add(entity, new ProjectileTag()); |
| | | 79 | | } |
| | 8 | 80 | | } |
| | | 81 | | |
| | | 82 | | public void OnDestroy(Frame frame) |
| | | 83 | | { |
| | 2 | 84 | | } |
| | | 85 | | |
| | | 86 | | private struct SpawnCommand |
| | | 87 | | { |
| | | 88 | | public Position2D Position; |
| | | 89 | | public Velocity2D Velocity; |
| | | 90 | | public Lifetime Lifetime; |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |