Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve usability of StateStage and cut down on "magic" #1059

Merged
merged 3 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 53 additions & 65 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::any::Any;

use crate::{
app::{App, AppExit},
event::Events,
plugin::Plugin,
stage, startup_stage, PluginGroup, PluginGroupBuilder,
};
use bevy_ecs::{
clear_trackers_system, FromResources, IntoStage, IntoSystem, Resource, Resources, RunOnce,
Schedule, Stage, State, StateStage, System, SystemStage, World,
clear_trackers_system, FromResources, IntoSystem, Resource, Resources, RunOnce, Schedule,
Stage, StateStage, System, SystemStage, World,
};
use bevy_utils::tracing::debug;

Expand Down Expand Up @@ -56,16 +54,12 @@ impl AppBuilder {
self
}

pub fn add_stage<Params, S: IntoStage<Params>>(
&mut self,
name: &'static str,
stage: S,
) -> &mut Self {
pub fn add_stage<S: Stage>(&mut self, name: &'static str, stage: S) -> &mut Self {
self.app.schedule.add_stage(name, stage);
self
}

pub fn add_stage_after<Params, S: IntoStage<Params>>(
pub fn add_stage_after<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
Expand All @@ -75,7 +69,7 @@ impl AppBuilder {
self
}

pub fn add_stage_before<Params, S: IntoStage<Params>>(
pub fn add_stage_before<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
Expand All @@ -85,11 +79,7 @@ impl AppBuilder {
self
}

pub fn add_startup_stage<Params, S: IntoStage<Params>>(
&mut self,
name: &'static str,
stage: S,
) -> &mut Self {
pub fn add_startup_stage<S: Stage>(&mut self, name: &'static str, stage: S) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
Expand All @@ -98,7 +88,7 @@ impl AppBuilder {
self
}

pub fn add_startup_stage_after<Params, S: IntoStage<Params>>(
pub fn add_startup_stage_after<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
Expand All @@ -112,7 +102,7 @@ impl AppBuilder {
self
}

pub fn add_startup_stage_before<Params, S: IntoStage<Params>>(
pub fn add_startup_stage_before<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
Expand Down Expand Up @@ -143,6 +133,51 @@ impl AppBuilder {
self.add_system_to_stage(stage::UPDATE, system)
}

pub fn on_state_enter<T: Clone + Resource, S, Params, IntoS>(
&mut self,
stage: &str,
state: T,
system: IntoS,
) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
self.stage(stage, |stage: &mut StateStage<T>| {
stage.on_state_enter(state, system)
})
}

pub fn on_state_update<T: Clone + Resource, S, Params, IntoS>(
&mut self,
stage: &str,
state: T,
system: IntoS,
) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
self.stage(stage, |stage: &mut StateStage<T>| {
stage.on_state_update(state, system)
})
}

pub fn on_state_exit<T: Clone + Resource, S, Params, IntoS>(
&mut self,
stage: &str,
state: T,
system: IntoS,
) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
self.stage(stage, |stage: &mut StateStage<T>| {
stage.on_state_exit(state, system)
})
}

pub fn add_startup_system_to_stage<S, Params, IntoS>(
&mut self,
stage_name: &'static str,
Expand Down Expand Up @@ -207,53 +242,6 @@ impl AppBuilder {
.add_system_to_stage(stage::EVENT, Events::<T>::update_system)
}

pub fn state_stage_name<T: Any>() -> String {
format!("state({})", std::any::type_name::<T>())
}

pub fn add_state<T: Clone + Resource>(&mut self, initial: T) -> &mut Self {
self.add_resource(State::new(initial));
self.app.schedule.add_stage_after(
stage::UPDATE,
&Self::state_stage_name::<T>(),
StateStage::<T>::default(),
);
self
}

pub fn on_state_enter<T: Clone + Resource, Params, S: IntoStage<Params>>(
&mut self,
value: T,
stage: S,
) -> &mut Self {
self.stage(
&Self::state_stage_name::<T>(),
|state_stage: &mut StateStage<T>| state_stage.on_state_enter(value, stage),
)
}

pub fn on_state_update<T: Clone + Resource, Params, S: IntoStage<Params>>(
&mut self,
value: T,
stage: S,
) -> &mut Self {
self.stage(
&Self::state_stage_name::<T>(),
|state_stage: &mut StateStage<T>| state_stage.on_state_update(value, stage),
)
}

pub fn on_state_exit<T: Clone + Resource, Params, S: IntoStage<Params>>(
&mut self,
value: T,
stage: S,
) -> &mut Self {
self.stage(
&Self::state_stage_name::<T>(),
|state_stage: &mut StateStage<T>| state_stage.on_state_exit(value, stage),
)
}

/// Adds a resource to the current [App] and overwrites any resource previously added of the same type.
pub fn add_resource<T>(&mut self, resource: T) -> &mut Self
where
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod prelude {
pub use crate::{
core::WorldBuilderSource,
resource::{ChangedRes, FromResources, Local, Res, ResMut, Resource, Resources},
schedule::{Schedule, State, SystemStage},
schedule::{Schedule, State, StateStage, SystemStage},
system::{Commands, IntoSystem, Query, System},
Added, Bundle, Changed, Component, Entity, In, IntoChainSystem, Mut, Mutated, Or, QuerySet,
Ref, RefMut, With, Without, World,
Expand Down
43 changes: 10 additions & 33 deletions crates/bevy_ecs/src/schedule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,17 @@ pub struct Schedule {
}

impl Schedule {
pub fn with_stage<Params, S: IntoStage<Params>>(mut self, name: &str, stage: S) -> Self {
self.add_stage(name, stage.into_stage());
pub fn with_stage<S: Stage>(mut self, name: &str, stage: S) -> Self {
self.add_stage(name, stage);
self
}

pub fn with_stage_after<Params, S: IntoStage<Params>>(
mut self,
target: &str,
name: &str,
stage: S,
) -> Self {
pub fn with_stage_after<S: Stage>(mut self, target: &str, name: &str, stage: S) -> Self {
self.add_stage_after(target, name, stage);
self
}

pub fn with_stage_before<Params, S: IntoStage<Params>>(
mut self,
target: &str,
name: &str,
stage: S,
) -> Self {
pub fn with_stage_before<S: Stage>(mut self, target: &str, name: &str, stage: S) -> Self {
self.add_stage_before(target, name, stage);
self
}
Expand Down Expand Up @@ -75,19 +65,13 @@ impl Schedule {
self
}

pub fn add_stage<Params, S: IntoStage<Params>>(&mut self, name: &str, stage: S) -> &mut Self {
pub fn add_stage<S: Stage>(&mut self, name: &str, stage: S) -> &mut Self {
self.stage_order.push(name.to_string());
self.stages
.insert(name.to_string(), Box::new(stage.into_stage()));
self.stages.insert(name.to_string(), Box::new(stage));
self
}

pub fn add_stage_after<Params, S: IntoStage<Params>>(
&mut self,
target: &str,
name: &str,
stage: S,
) -> &mut Self {
pub fn add_stage_after<S: Stage>(&mut self, target: &str, name: &str, stage: S) -> &mut Self {
if self.stages.get(name).is_some() {
panic!("Stage already exists: {}.", name);
}
Expand All @@ -100,18 +84,12 @@ impl Schedule {
.map(|(i, _)| i)
.unwrap_or_else(|| panic!("Target stage does not exist: {}.", target));

self.stages
.insert(name.to_string(), Box::new(stage.into_stage()));
self.stages.insert(name.to_string(), Box::new(stage));
self.stage_order.insert(target_index + 1, name.to_string());
self
}

pub fn add_stage_before<Params, S: IntoStage<Params>>(
&mut self,
target: &str,
name: &str,
stage: S,
) -> &mut Self {
pub fn add_stage_before<S: Stage>(&mut self, target: &str, name: &str, stage: S) -> &mut Self {
if self.stages.get(name).is_some() {
panic!("Stage already exists: {}.", name);
}
Expand All @@ -124,8 +102,7 @@ impl Schedule {
.map(|(i, _)| i)
.unwrap_or_else(|| panic!("Target stage does not exist: {}.", target));

self.stages
.insert(name.to_string(), Box::new(stage.into_stage()));
self.stages.insert(name.to_string(), Box::new(stage));
self.stage_order.insert(target_index, name.to_string());
self
}
Expand Down
23 changes: 0 additions & 23 deletions crates/bevy_ecs/src/schedule/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,6 @@ impl<S: System<In = (), Out = ()>> From<S> for SystemStage {
}
}

pub trait IntoStage<Params> {
type Stage: Stage;
fn into_stage(self) -> Self::Stage;
}

impl<Params, S: System<In = (), Out = ()>, IntoS: IntoSystem<Params, S>> IntoStage<(Params, S)>
for IntoS
{
type Stage = SystemStage;

fn into_stage(self) -> Self::Stage {
SystemStage::single(self)
}
}

impl<S: Stage> IntoStage<()> for S {
type Stage = S;

fn into_stage(self) -> Self::Stage {
self
}
}

pub struct RunOnce {
ran: bool,
system_id: SystemId,
Expand Down
Loading