Skip to content

Commit 3ff5502

Browse files
committed
support for Systems[]
1 parent 4443107 commit 3ff5502

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/Bevy.cs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,38 @@ namespace TinyEcs;
99
public sealed partial class FuncSystem<TArg> where TArg : notnull
1010
{
1111
private readonly TArg _arg;
12-
private readonly Action<TArg, Func<TArg, bool>> _fn;
12+
private readonly Func<TArg, Func<TArg, bool>, bool> _fn;
1313
private readonly List<Func<TArg, bool>> _conditions;
1414
private readonly Func<TArg, bool> _validator;
1515
private readonly Func<bool> _checkInUse;
16+
private readonly Stages _stage;
1617
private readonly ThreadingMode _threadingType;
1718
private readonly LinkedList<FuncSystem<TArg>> _after = new();
1819
private readonly LinkedList<FuncSystem<TArg>> _before = new();
1920
internal LinkedListNode<FuncSystem<TArg>>? Node { get; set; }
2021

2122

22-
internal FuncSystem(TArg arg, Action<TArg, Func<TArg, bool>> fn, Func<bool> checkInUse, ThreadingMode threadingType)
23+
internal FuncSystem(TArg arg, Func<TArg, Func<TArg, bool>, bool> fn, Func<bool> checkInUse, Stages stage, ThreadingMode threadingType)
2324
{
2425
_arg = arg;
2526
_fn = fn;
2627
_conditions = new();
2728
_validator = ValidateConditions;
2829
_checkInUse = checkInUse;
2930
_threadingType = threadingType;
31+
_stage = stage;
3032
}
3133

3234
internal void Run()
3335
{
3436
foreach (var s in _before)
3537
s.Run();
3638

37-
_fn(_arg, _validator);
38-
39-
foreach (var s in _after)
40-
s.Run();
39+
if (_fn(_arg, _validator))
40+
{
41+
foreach (var s in _after)
42+
s.Run();
43+
}
4144
}
4245

4346
public FuncSystem<TArg> RunIf(Func<bool> condition)
@@ -57,6 +60,14 @@ public FuncSystem<TArg> RunAfter(FuncSystem<TArg> parent)
5760
return this;
5861
}
5962

63+
public FuncSystem<TArg> RunAfter(params ReadOnlySpan<FuncSystem<TArg>> systems)
64+
{
65+
foreach (var system in systems)
66+
system.RunAfter(this);
67+
68+
return this;
69+
}
70+
6071
public FuncSystem<TArg> RunBefore(FuncSystem<TArg> parent)
6172
{
6273
if (this == parent || Contains(parent, s => s._before))
@@ -68,6 +79,14 @@ public FuncSystem<TArg> RunBefore(FuncSystem<TArg> parent)
6879
return this;
6980
}
7081

82+
public FuncSystem<TArg> RunBefore(params ReadOnlySpan<FuncSystem<TArg>> systems)
83+
{
84+
foreach (var system in systems)
85+
system.RunBefore(this);
86+
87+
return this;
88+
}
89+
7190
private bool Contains(FuncSystem<TArg> system, Func<FuncSystem<TArg>, LinkedList<FuncSystem<TArg>>> direction)
7291
{
7392
var current = this;
@@ -194,13 +213,31 @@ internal void Add(FuncSystem<World> sys, Stages stage)
194213
sys.Node = _systems[(int)stage].AddLast(sys);
195214
}
196215

216+
public FuncSystem<World> AddSystems(ReadOnlySpan<FuncSystem<World>> systems, Stages stage = Stages.Update, ThreadingMode threadingType = ThreadingMode.Auto)
217+
{
218+
var rootSystem = AddSystem(() => { }, stage, threadingType);
219+
foreach (var system in systems)
220+
{
221+
if (system == rootSystem)
222+
continue;
223+
224+
system.RunAfter(rootSystem);
225+
}
226+
227+
return rootSystem;
228+
}
229+
197230
public FuncSystem<World> AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode threadingType = ThreadingMode.Auto)
198231
{
199232
var sys = new FuncSystem<World>(_world, (args, runIf) =>
200233
{
201234
if (runIf?.Invoke(args) ?? true)
235+
{
202236
system();
203-
}, () => false, threadingType);
237+
return true;
238+
}
239+
return false;
240+
}, () => false, stage, threadingType);
204241
Add(sys, stage);
205242

206243
return sys;

tools/TinyEcs.Generator/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,17 @@ static string GenerateSchedulerSystems()
290290
var fn = (World args, Func<World, bool> runIf) =>
291291
{{
292292
if (runIf != null && !runIf.Invoke(args))
293-
return;
293+
return false;
294294
295295
{objsGen}
296296
{objsLock}
297297
args.BeginDeferred();
298298
system({systemCall});
299299
args.EndDeferred();
300300
{objsUnlock}
301+
return true;
301302
}};
302-
var sys = new FuncSystem<World>(_world, fn, checkInuse, threadingType);
303+
var sys = new FuncSystem<World>(_world, fn, checkInuse, stage, threadingType);
303304
Add(sys, stage);
304305
return sys;
305306
}}

0 commit comments

Comments
 (0)