11use crate :: { App , Plugin } ;
22use bevy_ecs:: {
33 schedule:: { ExecutorKind , Schedule , ScheduleLabel } ,
4- system:: { Local , Resource } ,
4+ system:: Resource ,
55 world:: { Mut , World } ,
66} ;
77
8- /// The schedule that contains the app logic that is evaluated each tick of event loop.
9- ///
10- /// By default, it will run the following schedules in the given order:
11- ///
128/// On the first run of the schedule (and only on the first run), it will run:
139/// * [`PreStartup`]
1410/// * [`Startup`]
1511/// * [`PostStartup`]
16- ///
17- /// Then it will run:
18- /// * [`First`]
19- /// * [`PreUpdate`]
20- /// * [`StateTransition`]
21- /// * [`RunFixedUpdateLoop`]
22- /// * This will run [`FixedUpdate`] zero to many times, based on how much time has elapsed.
23- /// * [`Update`]
24- /// * [`PostUpdate`]
25- /// * [`Last`]
2612#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
27- pub struct UpdateFlow ;
13+ pub struct StartupFlow ;
2814
2915/// The schedule that runs before [`Startup`].
30- /// This is run by the [`Main `] schedule.
16+ /// This is run by the [`StartupFlow `] schedule.
3117#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
3218pub struct PreStartup ;
3319
3420/// The schedule that runs once when the app starts.
35- /// This is run by the [`Main `] schedule.
21+ /// This is run by the [`StartupFlow `] schedule.
3622#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
3723pub struct Startup ;
3824
3925/// The schedule that runs once after [`Startup`].
40- /// This is run by the [`Main `] schedule.
26+ /// This is run by the [`StartupFlow `] schedule.
4127#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
4228pub struct PostStartup ;
4329
30+ /// The schedule that contains the app logic that is evaluated each tick of event loop.
31+ ///
32+ /// By default, it will run the following schedules in the given order:
33+ /// * [`First`]
34+ /// * [`PreUpdate`]
35+ /// * [`StateTransition`]
36+ /// * [`RunFixedUpdateLoop`]
37+ /// * This will run [`FixedUpdate`] zero to many times, based on how much time has elapsed.
38+ /// * [`Update`]
39+ /// * [`PostUpdate`]
40+ /// * [`Last`]
41+ #[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
42+ pub struct UpdateFlow ;
43+
4444/// Runs first in the schedule.
45- /// This is run by the [`Main `] schedule.
45+ /// This is run by the [`UpdateFlow `] schedule.
4646#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
4747pub struct First ;
4848
@@ -53,17 +53,17 @@ pub struct First;
5353/// [`PreUpdate`] exists to do "engine/plugin preparation work" that ensures the APIs consumed in [`Update`] are "ready".
5454/// [`PreUpdate`] abstracts out "pre work implementation details".
5555///
56- /// This is run by the [`Main `] schedule.
56+ /// This is run by the [`UpdateFlow `] schedule.
5757#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
5858pub struct PreUpdate ;
5959
6060/// Runs [state transitions](bevy_ecs::schedule::States).
61- /// This is run by the [`Main `] schedule.
61+ /// This is run by the [`UpdateFlow `] schedule.
6262#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
6363pub struct StateTransition ;
6464
6565/// Runs the [`FixedUpdate`] schedule in a loop according until all relevant elapsed time has been "consumed".
66- /// This is run by the [`Main `] schedule.
66+ /// This is run by the [`UpdateFlow `] schedule.
6767#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
6868pub struct RunFixedUpdateLoop ;
6969
@@ -75,7 +75,7 @@ pub struct RunFixedUpdateLoop;
7575pub struct FixedUpdate ;
7676
7777/// The schedule that contains app logic.
78- /// This is run by the [`Main `] schedule.
78+ /// This is run by the [`UpdateFlow `] schedule.
7979#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
8080pub struct Update ;
8181
@@ -86,16 +86,26 @@ pub struct Update;
8686/// [`PostUpdate`] exists to do "engine/plugin response work" to things that happened in [`Update`].
8787/// [`PostUpdate`] abstracts out "implementation details" from users defining systems in [`Update`].
8888///
89- /// This is run by the [`Main `] schedule.
89+ /// This is run by the [`UpdateFlow `] schedule.
9090#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
9191pub struct PostUpdate ;
9292
9393/// Runs last in the schedule.
94- /// This is run by the [`Main `] schedule.
94+ /// This is run by the [`UpdateFlow `] schedule.
9595#[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
9696pub struct Last ;
9797
98- /// The main render schedule.
98+ /// Each time an event is received from windows and devices, this schedule is run.
99+ /// This is useful for responding to events regardless of whether tick updates take place.
100+ #[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
101+ pub struct Control ;
102+
103+ /// Each time a frame is ready to be updated, this schedule is run.
104+ /// This is the best place to decide whether to redraw.
105+ #[ derive( ScheduleLabel , Clone , Debug , PartialEq , Eq , Hash ) ]
106+ pub struct FrameReady ;
107+
108+ /// The schedule that builds and sends drawing queries to the GPU.
99109#[ derive( ScheduleLabel , Debug , Hash , PartialEq , Eq , Clone ) ]
100110pub struct RenderFlow ;
101111
@@ -135,16 +145,19 @@ impl UpdateFlowOrder {
135145 }
136146}
137147
138- impl UpdateFlow {
139- /// A system that runs the "main schedule"
140- pub fn run_main ( world : & mut World , mut run_at_least_once : Local < bool > ) {
141- if !* run_at_least_once {
142- let _ = world. try_run_schedule ( PreStartup ) ;
143- let _ = world. try_run_schedule ( Startup ) ;
144- let _ = world. try_run_schedule ( PostStartup ) ;
145- * run_at_least_once = true ;
146- }
148+ /// Initializes the [`StartupFlow`] schedule, [`UpdateFlow`] schedule, sub schedules, and resources for a given [`App`].
149+ pub struct MainSchedulePlugin ;
147150
151+ impl MainSchedulePlugin {
152+ /// A system that runs the `StartupFlow` sub schedules
153+ pub fn run_startup ( world : & mut World ) {
154+ let _ = world. try_run_schedule ( PreStartup ) ;
155+ let _ = world. try_run_schedule ( Startup ) ;
156+ let _ = world. try_run_schedule ( PostStartup ) ;
157+ }
158+
159+ /// A system that runs the `UpdateFlow` sub schedules
160+ pub fn run_update ( world : & mut World ) {
148161 world. resource_scope ( |world, order : Mut < UpdateFlowOrder > | {
149162 for label in & order. labels {
150163 let _ = world. try_run_schedule ( & * * label) ;
@@ -153,20 +166,21 @@ impl UpdateFlow {
153166 }
154167}
155168
156- /// Initializes the [`Main`] schedule, sub schedules, and resources for a given [`App`].
157- pub struct MainSchedulePlugin ;
158-
159169impl Plugin for MainSchedulePlugin {
160170 fn build ( & self , app : & mut App ) {
161171 // simple "facilitator" schedules benefit from simpler single threaded scheduling
162- let mut main_schedule = Schedule :: new ( ) ;
163- main_schedule. set_executor_kind ( ExecutorKind :: SingleThreaded ) ;
172+ let mut startup_schedule = Schedule :: new ( ) ;
173+ startup_schedule. set_executor_kind ( ExecutorKind :: SingleThreaded ) ;
174+ let mut update_schedule = Schedule :: new ( ) ;
175+ update_schedule. set_executor_kind ( ExecutorKind :: SingleThreaded ) ;
164176 let mut fixed_update_loop_schedule = Schedule :: new ( ) ;
165177 fixed_update_loop_schedule. set_executor_kind ( ExecutorKind :: SingleThreaded ) ;
166178
167- app. add_schedule ( UpdateFlow , main_schedule)
179+ app. add_schedule ( StartupFlow , startup_schedule)
180+ . add_schedule ( UpdateFlow , update_schedule)
168181 . add_schedule ( RunFixedUpdateLoop , fixed_update_loop_schedule)
169182 . init_resource :: < UpdateFlowOrder > ( )
170- . add_systems ( UpdateFlow , UpdateFlow :: run_main) ;
183+ . add_systems ( StartupFlow , Self :: run_startup)
184+ . add_systems ( UpdateFlow , Self :: run_update) ;
171185 }
172186}
0 commit comments