@@ -118,38 +118,58 @@ impl TypeAccess {
118
118
}
119
119
}
120
120
121
- pub type DynamicSystemWorkload =
122
- fn ( & mut GenericQuery < DynamicComponentQuery , DynamicComponentQuery > ) ;
123
-
124
- pub struct DynamicSystem {
125
- name : String ,
121
+ pub struct DynamicSystem < S > {
122
+ pub name : String ,
123
+ pub state : S ,
126
124
system_id : SystemId ,
127
- workload : DynamicSystemWorkload ,
128
125
archetype_access : ArchetypeAccess ,
129
126
resource_access : TypeAccess ,
130
- component_query : DynamicComponentQuery ,
127
+ settings : DynamicSystemSettings < S > ,
128
+ }
129
+
130
+ #[ derive( Clone ) ]
131
+ pub struct DynamicSystemSettings < S > {
132
+ pub workload :
133
+ fn ( & mut S , & Resources , & mut [ GenericQuery < DynamicComponentQuery , DynamicComponentQuery > ] ) ,
134
+ pub queries : Vec < DynamicComponentQuery > ,
135
+ pub thread_local_execution : ThreadLocalExecution ,
136
+ pub thread_local_system : fn ( & mut S , & mut World , & mut Resources ) ,
137
+ pub init_function : fn ( & mut S , & mut World , & mut Resources ) ,
138
+ pub resource_access : TypeAccess ,
139
+ }
140
+
141
+ impl < S > Default for DynamicSystemSettings < S > {
142
+ fn default ( ) -> Self {
143
+ Self {
144
+ workload : |_, _, _| ( ) ,
145
+ queries : Default :: default ( ) ,
146
+ thread_local_execution : ThreadLocalExecution :: NextFlush ,
147
+ thread_local_system : |_, _, _| ( ) ,
148
+ init_function : |_, _, _| ( ) ,
149
+ resource_access : Default :: default ( ) ,
150
+ }
151
+ }
131
152
}
132
153
133
- impl DynamicSystem {
134
- pub fn new (
135
- name : String ,
136
- component_query : DynamicComponentQuery ,
137
- workload : DynamicSystemWorkload ,
138
- ) -> Self {
154
+ impl < S > DynamicSystem < S > {
155
+ pub fn new ( name : String , state : S ) -> Self {
139
156
DynamicSystem {
140
157
name,
141
- workload ,
142
- component_query ,
158
+ state ,
159
+ system_id : SystemId :: new ( ) ,
143
160
resource_access : Default :: default ( ) ,
144
161
archetype_access : Default :: default ( ) ,
145
- system_id : SystemId :: new ( ) ,
162
+ settings : Default :: default ( ) ,
146
163
}
147
164
}
148
165
149
- // TODO: Impl `Default` for `DynamicSystem`
166
+ pub fn settings ( mut self , settings : DynamicSystemSettings < S > ) -> Self {
167
+ self . settings = settings;
168
+ self
169
+ }
150
170
}
151
171
152
- impl System for DynamicSystem {
172
+ impl < S : Send + Sync > System for DynamicSystem < S > {
153
173
fn name ( & self ) -> std:: borrow:: Cow < ' static , str > {
154
174
self . name . clone ( ) . into ( )
155
175
}
@@ -162,40 +182,43 @@ impl System for DynamicSystem {
162
182
// Clear previous archetype access list
163
183
self . archetype_access . clear ( ) ;
164
184
165
- self . archetype_access
166
- . set_access_for_stateful_query :: < _ , DynamicComponentQuery > (
167
- & world,
168
- & self . component_query ,
169
- ) ;
185
+ for query in & self . settings . queries {
186
+ self . archetype_access
187
+ . set_access_for_stateful_query :: < _ , DynamicComponentQuery > ( & world, & query) ;
188
+ }
170
189
}
171
190
172
191
fn archetype_access ( & self ) -> & ArchetypeAccess {
173
192
& self . archetype_access
174
193
}
175
194
176
- // TODO: Allow specifying resource access
177
195
fn resource_access ( & self ) -> & TypeAccess {
178
196
& self . resource_access
179
197
}
180
198
181
- // TODO: Allow specifying the thread local execution
182
199
fn thread_local_execution ( & self ) -> ThreadLocalExecution {
183
- ThreadLocalExecution :: NextFlush
200
+ self . settings . thread_local_execution
184
201
}
185
202
186
- fn run ( & mut self , world : & World , _resources : & Resources ) {
187
- ( self . workload ) ( & mut GenericQuery :: new_stateful (
188
- world,
189
- & self . archetype_access ,
190
- & self . component_query ,
191
- ) ) ;
203
+ fn run ( & mut self , world : & World , resources : & Resources ) {
204
+ let archetype_access = & self . archetype_access ;
205
+ let mut queries: Vec < _ > = self
206
+ . settings
207
+ . queries
208
+ . iter ( )
209
+ . map ( |query| GenericQuery :: new_stateful ( world, & archetype_access, query) )
210
+ . collect ( ) ;
211
+
212
+ ( self . settings . workload ) ( & mut self . state , resources, queries. as_mut_slice ( ) ) ;
192
213
}
193
214
194
- // TODO: Allow specifying a thread local system
195
- fn run_thread_local ( & mut self , _world : & mut World , _resources : & mut Resources ) { }
215
+ fn run_thread_local ( & mut self , world : & mut World , resources : & mut Resources ) {
216
+ ( self . settings . thread_local_system ) ( & mut self . state , world, resources) ;
217
+ }
196
218
197
- // TODO: Allow specifying an initialization function
198
- fn initialize ( & mut self , _world : & mut World , _resources : & mut Resources ) { }
219
+ fn initialize ( & mut self , world : & mut World , resources : & mut Resources ) {
220
+ ( self . settings . init_function ) ( & mut self . state , world, resources) ;
221
+ }
199
222
}
200
223
201
224
#[ cfg( test) ]
0 commit comments