|
| 1 | +#if UNITY_EDITOR |
| 2 | +#define MORPEH_DEBUG |
| 3 | +#endif |
| 4 | + |
| 5 | +using System; |
| 6 | +using JetBrains.Annotations; |
| 7 | + |
1 | 8 | namespace Scellecs.Morpeh
|
2 | 9 | {
|
3 |
| - using System; |
4 |
| - using JetBrains.Annotations; |
5 |
| - using UnityEngine; |
6 |
| - |
7 |
| - public readonly struct SystemStateProcessor<TSystemStateComponent> |
| 10 | + public struct SystemStateProcessor<TSystemStateComponent> |
8 | 11 | where TSystemStateComponent : struct, ISystemStateComponent
|
9 | 12 | {
|
10 |
| - private readonly CreateDelegate _onAdd; |
11 |
| - private readonly RemoveDelegate _onRemove; |
| 13 | + internal readonly SetupDelegate setupDelegate; |
| 14 | + internal readonly DisposeDelegate disposeDelegate; |
| 15 | + internal readonly World world; |
| 16 | + |
| 17 | + internal readonly Filter entitiesWithoutStateFilter; |
| 18 | + internal readonly Filter stateOnlyFilterFilter; |
12 | 19 |
|
13 |
| - private readonly Filter _entitiesWithoutStateFilter; |
14 |
| - private readonly Filter _stateOnlyFilterFilter; |
| 20 | + internal readonly Stash<TSystemStateComponent> stateStash; |
| 21 | + internal readonly Stash<Info> infoStash; |
15 | 22 |
|
16 |
| - private readonly Stash<TSystemStateComponent> _stateStash; |
17 |
| - private readonly Stash<Info<TSystemStateComponent>> _infoStash; |
| 23 | + internal int frame; |
18 | 24 |
|
19 |
| - public Filter Entities { get; } |
| 25 | + public readonly Filter Entities; |
20 | 26 |
|
21 |
| - internal SystemStateProcessor( |
22 |
| - Filter filter, |
23 |
| - CreateDelegate onAdd, |
24 |
| - RemoveDelegate onRemove = null) |
| 27 | + internal SystemStateProcessor(FilterBuilder filter, SetupDelegate setup, DisposeDelegate dispose) |
25 | 28 | {
|
26 |
| - Entities = filter; |
| 29 | + Entities = filter.Build(); |
27 | 30 |
|
28 |
| - _onAdd = onAdd; |
29 |
| - _onRemove = onRemove ?? delegate { }; |
| 31 | + setupDelegate = setup; |
| 32 | + disposeDelegate = dispose; |
| 33 | + world = filter.world; |
30 | 34 |
|
31 |
| - _entitiesWithoutStateFilter = Entities.Without<TSystemStateComponent>(); |
32 |
| - _stateOnlyFilterFilter = Entities.world.Filter.With<TSystemStateComponent>(); |
| 35 | + entitiesWithoutStateFilter = filter.Without<TSystemStateComponent>().Build(); |
| 36 | + stateOnlyFilterFilter = world.Filter.With<TSystemStateComponent>().Build(); |
33 | 37 |
|
34 |
| - _stateStash = Entities.world.GetStash<TSystemStateComponent>(); |
35 |
| - _infoStash = Entities.world.GetStash<Info<TSystemStateComponent>>(); |
| 38 | + stateStash = world.GetStash<TSystemStateComponent>(); |
| 39 | + infoStash = world.GetStash<Info>(); |
| 40 | + |
| 41 | + frame = 0; |
| 42 | + |
| 43 | + if (disposeDelegate != null) |
| 44 | + { |
| 45 | +#if MORPEH_DEBUG |
| 46 | + if (typeof(IDisposable).IsAssignableFrom(typeof(TSystemStateComponent))) |
| 47 | + { |
| 48 | + var tName = typeof(TSystemStateComponent).Name; |
| 49 | + throw new Exception($"{tName} cannot be IDisposable"); |
| 50 | + } |
| 51 | + |
| 52 | + if (stateStash.componentDispose != null) |
| 53 | + { |
| 54 | + var tName = typeof(TSystemStateComponent).Name; |
| 55 | + throw new Exception( |
| 56 | + $"Only one instance of DisposableSystemStateProcessor<{tName}> can be created per world"); |
| 57 | + } |
| 58 | +#endif |
| 59 | + |
| 60 | + stateStash.componentDispose = (ref TSystemStateComponent component) => dispose.Invoke(ref component); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void Dispose() |
| 65 | + { |
| 66 | + DestroyAllStates(); |
| 67 | + |
| 68 | + if (disposeDelegate != null) |
| 69 | + { |
| 70 | + stateStash.componentDispose = null; |
| 71 | + } |
36 | 72 | }
|
37 | 73 |
|
38 | 74 | [PublicAPI]
|
39 | 75 | public void Process()
|
40 | 76 | {
|
41 |
| - var currentFrame = Time.frameCount; |
| 77 | + var currentFrame = ++frame; |
42 | 78 |
|
43 | 79 | foreach (var entity in Entities)
|
44 | 80 | {
|
45 |
| - _infoStash.Set(entity, new Info<TSystemStateComponent> |
| 81 | + infoStash.Set(entity, new Info |
46 | 82 | {
|
47 |
| - frame = currentFrame, |
| 83 | + frame = currentFrame |
48 | 84 | });
|
49 | 85 | }
|
50 | 86 |
|
51 |
| - foreach (var entity in _entitiesWithoutStateFilter) |
| 87 | + foreach (var entity in entitiesWithoutStateFilter) |
52 | 88 | {
|
53 |
| - _stateStash.Set(entity, _onAdd.Invoke(entity)); |
| 89 | + stateStash.Set(entity, setupDelegate.Invoke(entity)); |
54 | 90 | }
|
55 | 91 |
|
56 |
| - foreach (var entity in _stateOnlyFilterFilter) |
| 92 | + foreach (var entity in stateOnlyFilterFilter) |
57 | 93 | {
|
58 |
| - var lastFrame = _infoStash.Get(entity, out var exists).frame; |
| 94 | + var lastFrame = infoStash.Get(entity, out var exists).frame; |
59 | 95 |
|
60 | 96 | if (exists && lastFrame == currentFrame)
|
61 | 97 | {
|
62 | 98 | continue;
|
63 | 99 | }
|
64 | 100 |
|
65 |
| - ref var state = ref _stateStash.Get(entity); |
| 101 | + infoStash.Remove(entity); |
| 102 | + stateStash.Remove(entity); |
| 103 | + } |
| 104 | + |
| 105 | + world.Commit(); |
| 106 | + } |
66 | 107 |
|
67 |
| - _onRemove.Invoke(ref state); |
| 108 | + [PublicAPI] |
| 109 | + public void DestroyAllStates() |
| 110 | + { |
| 111 | + if (stateOnlyFilterFilter.IsEmpty()) |
| 112 | + { |
| 113 | + return; |
| 114 | + } |
68 | 115 |
|
69 |
| - _stateStash.Remove(entity); |
70 |
| - _infoStash.Remove(entity); |
| 116 | + foreach (var entity in stateOnlyFilterFilter) |
| 117 | + { |
| 118 | + infoStash.Remove(entity); |
| 119 | + stateStash.Remove(entity); |
71 | 120 | }
|
72 | 121 |
|
73 |
| - Entities.world.Commit(); |
| 122 | + world.Commit(); |
74 | 123 | }
|
75 | 124 |
|
76 |
| - public delegate TSystemStateComponent CreateDelegate(Entity entity); |
77 |
| - |
78 |
| - public delegate void RemoveDelegate(ref TSystemStateComponent data); |
| 125 | + public delegate TSystemStateComponent SetupDelegate(Entity entity); |
79 | 126 |
|
| 127 | + public delegate void DisposeDelegate(ref TSystemStateComponent data); |
80 | 128 |
|
81 | 129 | [Serializable]
|
82 |
| - internal struct Info<TSystemState> : IComponent |
83 |
| - where TSystemState : struct, ISystemStateComponent |
| 130 | + internal struct Info : IComponent |
84 | 131 | {
|
85 | 132 | public int frame;
|
86 | 133 | }
|
|
0 commit comments