@@ -9,35 +9,38 @@ namespace TinyEcs;
99public 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 ;
0 commit comments