Skip to content

Commit db27cf7

Browse files
v2.0.0 (#1)
1 parent 7da381a commit db27cf7

10 files changed

+344
-104
lines changed

README.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class HealthBarSystem : UpdateSystem {
2626
.ToSystemStateProcessor(CreateHealthBarForHero, RemoveHealthBarForHero);
2727
}
2828

29+
public override void Dispose() {
30+
heroProcessor.Dispose();
31+
}
32+
2933
public override void OnUpdate(float deltaTime) {
3034
heroProcessor.Process();
3135
}
@@ -49,20 +53,6 @@ public class HealthBarSystem : UpdateSystem {
4953
public GameObject healthBar;
5054
}
5155
}
52-
53-
public class DestroySystem : UpdateSystem {
54-
private Filter toDestroyFilter;
55-
56-
public override void OnAwake() { }
57-
58-
public override void OnUpdate(float deltaTime) {
59-
foreach (var entity in toDestroyFilter) {
60-
// MigrateSystemStateComponents must be called before each RemoveEntity
61-
World.MigrateSystemStateComponents(entity);
62-
World.RemoveEntity(entity);
63-
}
64-
}
65-
}
6656
```
6757

6858
## License

Scellecs.Morpeh/SystemStateFilterExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ public static class SystemStateFilterExtensions
66
{
77
[PublicAPI]
88
public static SystemStateProcessor<TSystemStateComponent> ToSystemStateProcessor<TSystemStateComponent>(
9-
this Filter filter,
10-
SystemStateProcessor<TSystemStateComponent>.CreateDelegate onAdd,
11-
SystemStateProcessor<TSystemStateComponent>.RemoveDelegate onRemove = null)
9+
this FilterBuilder filter,
10+
SystemStateProcessor<TSystemStateComponent>.SetupDelegate setup,
11+
SystemStateProcessor<TSystemStateComponent>.DisposeDelegate dispose = null)
1212
where TSystemStateComponent : struct, ISystemStateComponent
1313
{
14-
return new(filter, onAdd, onRemove);
14+
return new SystemStateProcessor<TSystemStateComponent>(filter, setup, dispose);
1515
}
1616
}
1717
}

Scellecs.Morpeh/SystemStateProcessor.cs

Lines changed: 87 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,133 @@
1+
#if UNITY_EDITOR
2+
#define MORPEH_DEBUG
3+
#endif
4+
5+
using System;
6+
using JetBrains.Annotations;
7+
18
namespace Scellecs.Morpeh
29
{
3-
using System;
4-
using JetBrains.Annotations;
5-
using UnityEngine;
6-
7-
public readonly struct SystemStateProcessor<TSystemStateComponent>
10+
public struct SystemStateProcessor<TSystemStateComponent>
811
where TSystemStateComponent : struct, ISystemStateComponent
912
{
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;
1219

13-
private readonly Filter _entitiesWithoutStateFilter;
14-
private readonly Filter _stateOnlyFilterFilter;
20+
internal readonly Stash<TSystemStateComponent> stateStash;
21+
internal readonly Stash<Info> infoStash;
1522

16-
private readonly Stash<TSystemStateComponent> _stateStash;
17-
private readonly Stash<Info<TSystemStateComponent>> _infoStash;
23+
internal int frame;
1824

19-
public Filter Entities { get; }
25+
public readonly Filter Entities;
2026

21-
internal SystemStateProcessor(
22-
Filter filter,
23-
CreateDelegate onAdd,
24-
RemoveDelegate onRemove = null)
27+
internal SystemStateProcessor(FilterBuilder filter, SetupDelegate setup, DisposeDelegate dispose)
2528
{
26-
Entities = filter;
29+
Entities = filter.Build();
2730

28-
_onAdd = onAdd;
29-
_onRemove = onRemove ?? delegate { };
31+
setupDelegate = setup;
32+
disposeDelegate = dispose;
33+
world = filter.world;
3034

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();
3337

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+
}
3672
}
3773

3874
[PublicAPI]
3975
public void Process()
4076
{
41-
var currentFrame = Time.frameCount;
77+
var currentFrame = ++frame;
4278

4379
foreach (var entity in Entities)
4480
{
45-
_infoStash.Set(entity, new Info<TSystemStateComponent>
81+
infoStash.Set(entity, new Info
4682
{
47-
frame = currentFrame,
83+
frame = currentFrame
4884
});
4985
}
5086

51-
foreach (var entity in _entitiesWithoutStateFilter)
87+
foreach (var entity in entitiesWithoutStateFilter)
5288
{
53-
_stateStash.Set(entity, _onAdd.Invoke(entity));
89+
stateStash.Set(entity, setupDelegate.Invoke(entity));
5490
}
5591

56-
foreach (var entity in _stateOnlyFilterFilter)
92+
foreach (var entity in stateOnlyFilterFilter)
5793
{
58-
var lastFrame = _infoStash.Get(entity, out var exists).frame;
94+
var lastFrame = infoStash.Get(entity, out var exists).frame;
5995

6096
if (exists && lastFrame == currentFrame)
6197
{
6298
continue;
6399
}
64100

65-
ref var state = ref _stateStash.Get(entity);
101+
infoStash.Remove(entity);
102+
stateStash.Remove(entity);
103+
}
104+
105+
world.Commit();
106+
}
66107

67-
_onRemove.Invoke(ref state);
108+
[PublicAPI]
109+
public void DestroyAllStates()
110+
{
111+
if (stateOnlyFilterFilter.IsEmpty())
112+
{
113+
return;
114+
}
68115

69-
_stateStash.Remove(entity);
70-
_infoStash.Remove(entity);
116+
foreach (var entity in stateOnlyFilterFilter)
117+
{
118+
infoStash.Remove(entity);
119+
stateStash.Remove(entity);
71120
}
72121

73-
Entities.world.Commit();
122+
world.Commit();
74123
}
75124

76-
public delegate TSystemStateComponent CreateDelegate(Entity entity);
77-
78-
public delegate void RemoveDelegate(ref TSystemStateComponent data);
125+
public delegate TSystemStateComponent SetupDelegate(Entity entity);
79126

127+
public delegate void DisposeDelegate(ref TSystemStateComponent data);
80128

81129
[Serializable]
82-
internal struct Info<TSystemState> : IComponent
83-
where TSystemState : struct, ISystemStateComponent
130+
internal struct Info : IComponent
84131
{
85132
public int frame;
86133
}

Scellecs.Morpeh/SystemStateWorldExtensions.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

Scellecs.Morpeh/SystemStateWorldExtensions.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

Tests.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "Morpeh.SystemStateProcessor.Tests",
3+
"rootNamespace": "",
4+
"references": [
5+
"Scellecs.Morpeh",
6+
"UnityEngine.TestRunner",
7+
"UnityEditor.TestRunner"
8+
],
9+
"includePlatforms": [
10+
"Editor"
11+
],
12+
"excludePlatforms": [],
13+
"allowUnsafeCode": false,
14+
"overrideReferences": true,
15+
"precompiledReferences": [
16+
"nunit.framework.dll"
17+
],
18+
"autoReferenced": false,
19+
"defineConstraints": [
20+
"UNITY_INCLUDE_TESTS"
21+
],
22+
"versionDefines": [],
23+
"noEngineReferences": false
24+
}

Tests/Morpeh.SystemStateProcessor.Tests.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)