From 5d9191b6aa49e3457f9d997e18d1139fc0055f39 Mon Sep 17 00:00:00 2001 From: Linwei Zhang Date: Tue, 14 Feb 2023 15:51:42 +0800 Subject: [PATCH 1/2] Add OnPreUpdate and OnPostUpdate --- crates/bevy_app/src/app.rs | 12 +++++++++--- crates/bevy_ecs/src/lib.rs | 2 +- crates/bevy_ecs/src/schedule/state.rs | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 37f27e6a385bd..62b0a4fd7f521 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -339,12 +339,18 @@ impl App { let main_schedule = self.get_schedule_mut(CoreSchedule::Main).unwrap(); for variant in S::variants() { - main_schedule.configure_set( + main_schedule.configure_sets(( + OnPreUpdate(variant.clone()) + .in_base_set(CoreSet::PreUpdate) + .run_if(state_equals(variant.clone())), OnUpdate(variant.clone()) .in_base_set(CoreSet::Update) - .run_if(state_equals(variant)) + .run_if(state_equals(variant.clone())) .after(apply_state_transition::), - ); + OnPostUpdate(variant.clone()) + .in_base_set(CoreSet::PostUpdate) + .run_if(state_equals(variant)), + )); } // These are different for loops to avoid conflicting access to self diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 77d6befc5d68d..783f86e7b678d 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -38,7 +38,7 @@ pub mod prelude { schedule::{ apply_state_transition, apply_system_buffers, common_conditions::*, IntoSystemConfig, IntoSystemConfigs, IntoSystemSet, IntoSystemSetConfig, IntoSystemSetConfigs, NextState, - OnEnter, OnExit, OnUpdate, Schedule, Schedules, State, States, SystemSet, + OnEnter, OnExit, OnPreUpdate, OnUpdate, OnPostUpdate, Schedule, Schedules, State, States, SystemSet, }, system::{ adapter as system_adapter, diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index 2ae15eb888e85..9922f13bd532a 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -53,6 +53,14 @@ pub struct OnEnter(pub S); #[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)] pub struct OnExit(pub S); +/// A [`SystemSet`] that will run within `CoreSet::PreUpdate` when this state is active. +/// +/// This set, when created via `App::add_state`, is configured with both a base set and a run condition. +/// If all you want is the run condition, use the [`state_equals`](crate::schedule::common_conditions::state_equals) +/// [condition](super::Condition) directly. +#[derive(SystemSet, Clone, Debug, PartialEq, Eq, Hash)] +pub struct OnPreUpdate(pub S); + /// A [`SystemSet`] that will run within `CoreSet::Update` when this state is active. /// /// This set, when created via `App::add_state`, is configured with both a base set and a run condition. @@ -61,6 +69,14 @@ pub struct OnExit(pub S); #[derive(SystemSet, Clone, Debug, PartialEq, Eq, Hash)] pub struct OnUpdate(pub S); +/// A [`SystemSet`] that will run within `CoreSet::PostUpdate` when this state is active. +/// +/// This set, when created via `App::add_state`, is configured with both a base set and a run condition. +/// If all you want is the run condition, use the [`state_equals`](crate::schedule::common_conditions::state_equals) +/// [condition](super::Condition) directly. +#[derive(SystemSet, Clone, Debug, PartialEq, Eq, Hash)] +pub struct OnPostUpdate(pub S); + /// A finite-state machine whose transitions have associated schedules /// ([`OnEnter(state)`] and [`OnExit(state)`]). /// From d3baac5eaf72d70f2784e4899faa6ab20f53caba Mon Sep 17 00:00:00 2001 From: Linwei Zhang Date: Tue, 14 Feb 2023 16:03:22 +0800 Subject: [PATCH 2/2] fmt --- crates/bevy_ecs/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 783f86e7b678d..1c3e6dc70751b 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -38,7 +38,8 @@ pub mod prelude { schedule::{ apply_state_transition, apply_system_buffers, common_conditions::*, IntoSystemConfig, IntoSystemConfigs, IntoSystemSet, IntoSystemSetConfig, IntoSystemSetConfigs, NextState, - OnEnter, OnExit, OnPreUpdate, OnUpdate, OnPostUpdate, Schedule, Schedules, State, States, SystemSet, + OnEnter, OnExit, OnPostUpdate, OnPreUpdate, OnUpdate, Schedule, Schedules, State, + States, SystemSet, }, system::{ adapter as system_adapter,