Skip to content

Commit 284889c

Browse files
Redo State architecture (#1424)
An alternative to StateStages that uses SystemSets. Also includes pop and push operations since this was originally developed for my personal project which needed them.
1 parent c3a72e9 commit 284889c

File tree

8 files changed

+554
-279
lines changed

8 files changed

+554
-279
lines changed

crates/bevy_app/src/app_builder.rs

+8-27
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
use bevy_ecs::{
88
component::Component,
99
schedule::{
10-
RunOnce, Schedule, Stage, StageLabel, StateStage, SystemDescriptor, SystemSet, SystemStage,
10+
RunOnce, Schedule, Stage, StageLabel, State, SystemDescriptor, SystemSet, SystemStage,
1111
},
1212
system::{IntoExclusiveSystem, IntoSystem},
1313
world::{FromWorld, World},
@@ -177,37 +177,18 @@ impl AppBuilder {
177177
self
178178
}
179179

180-
pub fn on_state_enter<T: Clone + Component>(
181-
&mut self,
182-
stage: impl StageLabel,
183-
state: T,
184-
system: impl Into<SystemDescriptor>,
185-
) -> &mut Self {
186-
self.stage(stage, |stage: &mut StateStage<T>| {
187-
stage.on_state_enter(state, system)
188-
})
180+
pub fn add_state<T: Component + Clone + Eq>(&mut self, initial: T) -> &mut Self {
181+
self.insert_resource(State::new(initial))
182+
.add_system_set(State::<T>::make_driver())
189183
}
190184

191-
pub fn on_state_update<T: Clone + Component>(
185+
pub fn add_state_to_stage<T: Component + Clone + Eq>(
192186
&mut self,
193187
stage: impl StageLabel,
194-
state: T,
195-
system: impl Into<SystemDescriptor>,
196-
) -> &mut Self {
197-
self.stage(stage, |stage: &mut StateStage<T>| {
198-
stage.on_state_update(state, system)
199-
})
200-
}
201-
202-
pub fn on_state_exit<T: Clone + Component>(
203-
&mut self,
204-
stage: impl StageLabel,
205-
state: T,
206-
system: impl Into<SystemDescriptor>,
188+
initial: T,
207189
) -> &mut Self {
208-
self.stage(stage, |stage: &mut StateStage<T>| {
209-
stage.on_state_exit(state, system)
210-
})
190+
self.insert_resource(State::new(initial))
191+
.add_system_set_to_stage(stage, State::<T>::make_driver())
211192
}
212193

213194
pub fn add_default_stages(&mut self) -> &mut Self {

crates/bevy_ecs/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod prelude {
1919
query::{Added, Changed, Flags, Mutated, Or, QueryState, With, WithBundle, Without},
2020
schedule::{
2121
AmbiguitySetLabel, ExclusiveSystemDescriptorCoercion, ParallelSystemDescriptorCoercion,
22-
Schedule, Stage, StageLabel, State, StateStage, SystemLabel, SystemStage,
22+
Schedule, Stage, StageLabel, State, SystemLabel, SystemSet, SystemStage,
2323
},
2424
system::{
2525
Commands, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem, Local, NonSend,

0 commit comments

Comments
 (0)