| | | 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 | | /// </summary> |
| | | 11 | | public sealed class LifetimeSystem : ISystem |
| | | 12 | | { |
| | 128 | 13 | | public SystemAuthoringContract Contract => new( |
| | 128 | 14 | | SystemFrameAccess.ReadWrite, |
| | 128 | 15 | | SystemGlobalAccess.None, |
| | 128 | 16 | | SystemStructuralChangeAccess.Deferred); |
| | | 17 | | |
| | | 18 | | public void OnInit(Frame frame) |
| | | 19 | | { |
| | 9 | 20 | | } |
| | | 21 | | |
| | | 22 | | public void OnUpdate(Frame frame, FP deltaTime) |
| | | 23 | | { |
| | 119 | 24 | | var query = frame.Query<Lifetime>(); |
| | 119 | 25 | | int capacity = query.Count; |
| | 119 | 26 | | if (capacity <= 0) |
| | | 27 | | { |
| | 9 | 28 | | return; |
| | | 29 | | } |
| | | 30 | | |
| | 110 | 31 | | Span<EntityRef> expiredEntities = capacity <= 64 |
| | 110 | 32 | | ? stackalloc EntityRef[capacity] |
| | 110 | 33 | | : new EntityRef[capacity]; |
| | | 34 | | |
| | 110 | 35 | | int expiredCount = 0; |
| | 110 | 36 | | var enumerator = query.GetEnumerator(); |
| | 658 | 37 | | while (enumerator.MoveNext()) |
| | | 38 | | { |
| | 548 | 39 | | enumerator.Component.Remaining -= deltaTime; |
| | 548 | 40 | | if (enumerator.Component.Remaining <= FP.Zero) |
| | | 41 | | { |
| | 5 | 42 | | expiredEntities[expiredCount++] = enumerator.CurrentEntity; |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | 230 | 46 | | for (int i = 0; i < expiredCount; i++) |
| | | 47 | | { |
| | 5 | 48 | | frame.DestroyEntity(expiredEntities[i]); |
| | | 49 | | } |
| | 110 | 50 | | } |
| | | 51 | | |
| | | 52 | | public void OnDestroy(Frame frame) |
| | | 53 | | { |
| | 9 | 54 | | } |
| | | 55 | | } |
| | | 56 | | } |