1
+ use std:: any:: Any ;
2
+
1
3
use crate :: {
2
4
app:: { App , AppExit } ,
3
5
event:: Events ,
4
6
plugin:: Plugin ,
5
7
stage, startup_stage, PluginGroup , PluginGroupBuilder ,
6
8
} ;
7
- use bevy_ecs:: { FromResources , IntoSystem , Resources , System , World } ;
9
+ use bevy_ecs:: {
10
+ clear_trackers_system, FromResources , IntoStage , IntoSystem , Resource , Resources , RunOnce ,
11
+ Schedule , Stage , State , StateStage , System , SystemStage , World ,
12
+ } ;
8
13
use bevy_utils:: tracing:: debug;
9
14
10
15
/// Configure [App]s using the builder pattern
@@ -18,8 +23,10 @@ impl Default for AppBuilder {
18
23
app : App :: default ( ) ,
19
24
} ;
20
25
21
- app_builder. add_default_stages ( ) ;
22
- app_builder. add_event :: < AppExit > ( ) ;
26
+ app_builder
27
+ . add_default_stages ( )
28
+ . add_event :: < AppExit > ( )
29
+ . add_system_to_stage ( stage:: LAST , clear_trackers_system) ;
23
30
app_builder
24
31
}
25
32
}
@@ -49,55 +56,88 @@ impl AppBuilder {
49
56
self
50
57
}
51
58
52
- pub fn add_stage ( & mut self , stage_name : & ' static str ) -> & mut Self {
53
- self . app . schedule . add_stage ( stage_name) ;
59
+ pub fn add_stage < Params , S : IntoStage < Params > > (
60
+ & mut self ,
61
+ name : & ' static str ,
62
+ stage : S ,
63
+ ) -> & mut Self {
64
+ self . app . schedule . add_stage ( name, stage) ;
54
65
self
55
66
}
56
67
57
- pub fn add_stage_after ( & mut self , target : & ' static str , stage_name : & ' static str ) -> & mut Self {
58
- self . app . schedule . add_stage_after ( target, stage_name) ;
68
+ pub fn add_stage_after < Params , S : IntoStage < Params > > (
69
+ & mut self ,
70
+ target : & ' static str ,
71
+ name : & ' static str ,
72
+ stage : S ,
73
+ ) -> & mut Self {
74
+ self . app . schedule . add_stage_after ( target, name, stage) ;
59
75
self
60
76
}
61
77
62
- pub fn add_stage_before (
78
+ pub fn add_stage_before < Params , S : IntoStage < Params > > (
63
79
& mut self ,
64
80
target : & ' static str ,
65
- stage_name : & ' static str ,
81
+ name : & ' static str ,
82
+ stage : S ,
66
83
) -> & mut Self {
67
- self . app . schedule . add_stage_before ( target, stage_name ) ;
84
+ self . app . schedule . add_stage_before ( target, name , stage ) ;
68
85
self
69
86
}
70
87
71
- pub fn add_startup_stage ( & mut self , stage_name : & ' static str ) -> & mut Self {
72
- self . app . startup_schedule . add_stage ( stage_name) ;
88
+ pub fn add_startup_stage < Params , S : IntoStage < Params > > (
89
+ & mut self ,
90
+ name : & ' static str ,
91
+ stage : S ,
92
+ ) -> & mut Self {
93
+ self . app
94
+ . schedule
95
+ . stage ( stage:: STARTUP , |schedule : & mut Schedule | {
96
+ schedule. add_stage ( name, stage)
97
+ } ) ;
73
98
self
74
99
}
75
100
76
- pub fn add_startup_stage_after (
101
+ pub fn add_startup_stage_after < Params , S : IntoStage < Params > > (
77
102
& mut self ,
78
103
target : & ' static str ,
79
- stage_name : & ' static str ,
104
+ name : & ' static str ,
105
+ stage : S ,
80
106
) -> & mut Self {
81
107
self . app
82
- . startup_schedule
83
- . add_stage_after ( target, stage_name) ;
108
+ . schedule
109
+ . stage ( stage:: STARTUP , |schedule : & mut Schedule | {
110
+ schedule. add_stage_after ( target, name, stage)
111
+ } ) ;
84
112
self
85
113
}
86
114
87
- pub fn add_startup_stage_before (
115
+ pub fn add_startup_stage_before < Params , S : IntoStage < Params > > (
88
116
& mut self ,
89
117
target : & ' static str ,
90
- stage_name : & ' static str ,
118
+ name : & ' static str ,
119
+ stage : S ,
91
120
) -> & mut Self {
92
121
self . app
93
- . startup_schedule
94
- . add_stage_before ( target, stage_name) ;
122
+ . schedule
123
+ . stage ( stage:: STARTUP , |schedule : & mut Schedule | {
124
+ schedule. add_stage_before ( target, name, stage)
125
+ } ) ;
126
+ self
127
+ }
128
+
129
+ pub fn stage < T : Stage , F : FnOnce ( & mut T ) -> & mut T > (
130
+ & mut self ,
131
+ name : & str ,
132
+ func : F ,
133
+ ) -> & mut Self {
134
+ self . app . schedule . stage ( name, func) ;
95
135
self
96
136
}
97
137
98
138
pub fn add_system < S , Params , IntoS > ( & mut self , system : IntoS ) -> & mut Self
99
139
where
100
- S : System < Input = ( ) , Output = ( ) > ,
140
+ S : System < In = ( ) , Out = ( ) > ,
101
141
IntoS : IntoSystem < Params , S > ,
102
142
{
103
143
self . add_system_to_stage ( stage:: UPDATE , system)
@@ -109,37 +149,41 @@ impl AppBuilder {
109
149
system : IntoS ,
110
150
) -> & mut Self
111
151
where
112
- S : System < Input = ( ) , Output = ( ) > ,
152
+ S : System < In = ( ) , Out = ( ) > ,
113
153
IntoS : IntoSystem < Params , S > ,
114
154
{
115
155
self . app
116
- . startup_schedule
117
- . add_system_to_stage ( stage_name, system) ;
156
+ . schedule
157
+ . stage ( stage:: STARTUP , |schedule : & mut Schedule | {
158
+ schedule. add_system_to_stage ( stage_name, system)
159
+ } ) ;
118
160
self
119
161
}
120
162
121
163
pub fn add_startup_system < S , Params , IntoS > ( & mut self , system : IntoS ) -> & mut Self
122
164
where
123
- S : System < Input = ( ) , Output = ( ) > ,
165
+ S : System < In = ( ) , Out = ( ) > ,
124
166
IntoS : IntoSystem < Params , S > ,
125
167
{
126
- self . app
127
- . startup_schedule
128
- . add_system_to_stage ( startup_stage:: STARTUP , system) ;
129
- self
168
+ self . add_startup_system_to_stage ( startup_stage:: STARTUP , system)
130
169
}
131
170
132
171
pub fn add_default_stages ( & mut self ) -> & mut Self {
133
- self . add_startup_stage ( startup_stage:: PRE_STARTUP )
134
- . add_startup_stage ( startup_stage:: STARTUP )
135
- . add_startup_stage ( startup_stage:: POST_STARTUP )
136
- . add_stage ( stage:: FIRST )
137
- . add_stage ( stage:: PRE_EVENT )
138
- . add_stage ( stage:: EVENT )
139
- . add_stage ( stage:: PRE_UPDATE )
140
- . add_stage ( stage:: UPDATE )
141
- . add_stage ( stage:: POST_UPDATE )
142
- . add_stage ( stage:: LAST )
172
+ self . add_stage (
173
+ stage:: STARTUP ,
174
+ Schedule :: default ( )
175
+ . with_run_criteria ( RunOnce :: default ( ) )
176
+ . with_stage ( startup_stage:: PRE_STARTUP , SystemStage :: parallel ( ) )
177
+ . with_stage ( startup_stage:: STARTUP , SystemStage :: parallel ( ) )
178
+ . with_stage ( startup_stage:: POST_STARTUP , SystemStage :: parallel ( ) ) ,
179
+ )
180
+ . add_stage ( stage:: FIRST , SystemStage :: parallel ( ) )
181
+ . add_stage ( stage:: PRE_EVENT , SystemStage :: parallel ( ) )
182
+ . add_stage ( stage:: EVENT , SystemStage :: parallel ( ) )
183
+ . add_stage ( stage:: PRE_UPDATE , SystemStage :: parallel ( ) )
184
+ . add_stage ( stage:: UPDATE , SystemStage :: parallel ( ) )
185
+ . add_stage ( stage:: POST_UPDATE , SystemStage :: parallel ( ) )
186
+ . add_stage ( stage:: LAST , SystemStage :: parallel ( ) )
143
187
}
144
188
145
189
pub fn add_system_to_stage < S , Params , IntoS > (
@@ -148,28 +192,13 @@ impl AppBuilder {
148
192
system : IntoS ,
149
193
) -> & mut Self
150
194
where
151
- S : System < Input = ( ) , Output = ( ) > ,
195
+ S : System < In = ( ) , Out = ( ) > ,
152
196
IntoS : IntoSystem < Params , S > ,
153
197
{
154
198
self . app . schedule . add_system_to_stage ( stage_name, system) ;
155
199
self
156
200
}
157
201
158
- pub fn add_system_to_stage_front < S , Params , IntoS > (
159
- & mut self ,
160
- stage_name : & ' static str ,
161
- system : IntoS ,
162
- ) -> & mut Self
163
- where
164
- S : System < Input = ( ) , Output = ( ) > ,
165
- IntoS : IntoSystem < Params , S > ,
166
- {
167
- self . app
168
- . schedule
169
- . add_system_to_stage_front ( stage_name, system. system ( ) ) ;
170
- self
171
- }
172
-
173
202
pub fn add_event < T > ( & mut self ) -> & mut Self
174
203
where
175
204
T : Send + Sync + ' static ,
@@ -178,6 +207,53 @@ impl AppBuilder {
178
207
. add_system_to_stage ( stage:: EVENT , Events :: < T > :: update_system)
179
208
}
180
209
210
+ pub fn state_stage_name < T : Any > ( ) -> String {
211
+ format ! ( "state({})" , std:: any:: type_name:: <T >( ) )
212
+ }
213
+
214
+ pub fn add_state < T : Clone + Resource > ( & mut self , initial : T ) -> & mut Self {
215
+ self . add_resource ( State :: new ( initial) ) ;
216
+ self . app . schedule . add_stage_after (
217
+ stage:: UPDATE ,
218
+ & Self :: state_stage_name :: < T > ( ) ,
219
+ StateStage :: < T > :: default ( ) ,
220
+ ) ;
221
+ self
222
+ }
223
+
224
+ pub fn on_state_enter < T : Clone + Resource , Params , S : IntoStage < Params > > (
225
+ & mut self ,
226
+ value : T ,
227
+ stage : S ,
228
+ ) -> & mut Self {
229
+ self . stage (
230
+ & Self :: state_stage_name :: < T > ( ) ,
231
+ |state_stage : & mut StateStage < T > | state_stage. on_state_enter ( value, stage) ,
232
+ )
233
+ }
234
+
235
+ pub fn on_state_update < T : Clone + Resource , Params , S : IntoStage < Params > > (
236
+ & mut self ,
237
+ value : T ,
238
+ stage : S ,
239
+ ) -> & mut Self {
240
+ self . stage (
241
+ & Self :: state_stage_name :: < T > ( ) ,
242
+ |state_stage : & mut StateStage < T > | state_stage. on_state_update ( value, stage) ,
243
+ )
244
+ }
245
+
246
+ pub fn on_state_exit < T : Clone + Resource , Params , S : IntoStage < Params > > (
247
+ & mut self ,
248
+ value : T ,
249
+ stage : S ,
250
+ ) -> & mut Self {
251
+ self . stage (
252
+ & Self :: state_stage_name :: < T > ( ) ,
253
+ |state_stage : & mut StateStage < T > | state_stage. on_state_exit ( value, stage) ,
254
+ )
255
+ }
256
+
181
257
/// Adds a resource to the current [App] and overwrites any resource previously added of the same type.
182
258
pub fn add_resource < T > ( & mut self , resource : T ) -> & mut Self
183
259
where
0 commit comments