Skip to content

Commit

Permalink
Remove the config api (bevyengine#3633)
Browse files Browse the repository at this point in the history
# Objective

- Fix the ugliness of the `config` api. 
- Supercedes bevyengine#2440, bevyengine#2463, bevyengine#2491

## Solution

- Since bevyengine#2398, capturing closure systems have worked.
- Use those instead where we needed config before
- Remove the rest of the config api. 
- Related: bevyengine#2777
  • Loading branch information
DJMcNab authored and Ku95 committed Mar 6, 2022
1 parent 46a8d7f commit 55ac6c3
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 284 deletions.
33 changes: 18 additions & 15 deletions crates/bevy_core/src/time/fixed_timestep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_ecs::{
component::ComponentId,
query::Access,
schedule::ShouldRun,
system::{ConfigurableSystem, IntoSystem, Local, Res, ResMut, System},
system::{IntoSystem, Res, ResMut, System},
world::World,
};
use bevy_utils::HashMap;
Expand Down Expand Up @@ -79,7 +79,9 @@ impl Default for FixedTimestep {
fn default() -> Self {
Self {
state: LocalFixedTimestepState::default(),
internal_system: Box::new(IntoSystem::into_system(Self::prepare_system)),
internal_system: Box::new(IntoSystem::into_system(Self::prepare_system(
Default::default(),
))),
}
}
}
Expand Down Expand Up @@ -116,18 +118,18 @@ impl FixedTimestep {
}

fn prepare_system(
mut state: Local<LocalFixedTimestepState>,
time: Res<Time>,
mut fixed_timesteps: ResMut<FixedTimesteps>,
) -> ShouldRun {
let should_run = state.update(&time);
if let Some(ref label) = state.label {
let res_state = fixed_timesteps.fixed_timesteps.get_mut(label).unwrap();
res_state.step = state.step;
res_state.accumulator = state.accumulator;
mut state: LocalFixedTimestepState,
) -> impl FnMut(Res<Time>, ResMut<FixedTimesteps>) -> ShouldRun {
move |time, mut fixed_timesteps| {
let should_run = state.update(&time);
if let Some(ref label) = state.label {
let res_state = fixed_timesteps.fixed_timesteps.get_mut(label).unwrap();
res_state.step = state.step;
res_state.accumulator = state.accumulator;
}

should_run
}

should_run
}
}

Expand Down Expand Up @@ -202,8 +204,9 @@ impl System for FixedTimestep {
}

fn initialize(&mut self, world: &mut World) {
self.internal_system =
Box::new(Self::prepare_system.config(|c| c.0 = Some(self.state.clone())));
self.internal_system = Box::new(IntoSystem::into_system(Self::prepare_system(
self.state.clone(),
)));
self.internal_system.initialize(world);
if let Some(ref label) = self.state.label {
let mut fixed_timesteps = world.get_resource_mut::<FixedTimesteps>().unwrap();
Expand Down
14 changes: 3 additions & 11 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ pub fn impl_query_set(_input: TokenStream) -> TokenStream {
unsafe impl<#(#query: WorldQuery + 'static,)* #(#filter: WorldQuery + 'static,)*> SystemParamState for QuerySetState<(#(QueryState<#query, #filter>,)*)>
where #(#filter::Fetch: FilterFetch,)*
{
type Config = ();
fn init(world: &mut World, system_meta: &mut SystemMeta, config: Self::Config) -> Self {
fn init(world: &mut World, system_meta: &mut SystemMeta) -> Self {
#(
let mut #query = QueryState::<#query, #filter>::new(world);
assert_component_access_compatibility(
Expand Down Expand Up @@ -255,8 +254,6 @@ pub fn impl_query_set(_input: TokenStream) -> TokenStream {
.extend(&#query.archetype_component_access);
)*
}

fn default_config() {}
}

impl<'w, 's, #(#query: WorldQuery + 'static,)* #(#filter: WorldQuery + 'static,)*> SystemParamFetch<'w, 's> for QuerySetState<(#(QueryState<#query, #filter>,)*)>
Expand Down Expand Up @@ -389,10 +386,9 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
}

unsafe impl<TSystemParamState: #path::system::SystemParamState, #punctuated_generics> #path::system::SystemParamState for #fetch_struct_name<TSystemParamState, #punctuated_generic_idents> {
type Config = TSystemParamState::Config;
fn init(world: &mut #path::world::World, system_meta: &mut #path::system::SystemMeta, config: Self::Config) -> Self {
fn init(world: &mut #path::world::World, system_meta: &mut #path::system::SystemMeta) -> Self {
Self {
state: TSystemParamState::init(world, system_meta, config),
state: TSystemParamState::init(world, system_meta),
marker: std::marker::PhantomData,
}
}
Expand All @@ -401,10 +397,6 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
self.state.new_archetype(archetype, system_meta)
}

fn default_config() -> TSystemParamState::Config {
TSystemParamState::default_config()
}

fn apply(&mut self, world: &mut #path::world::World) {
self.state.apply(world)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub mod prelude {
Schedule, Stage, StageLabel, State, SystemLabel, SystemSet, SystemStage,
},
system::{
Commands, ConfigurableSystem, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem,
Local, NonSend, NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System,
Commands, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem, Local, NonSend,
NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System,
},
world::{FromWorld, Mut, World},
};
Expand Down
84 changes: 39 additions & 45 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
RunCriteriaDescriptor, RunCriteriaDescriptorCoercion, RunCriteriaLabel, ShouldRun,
SystemSet,
},
system::{ConfigurableSystem, In, IntoChainSystem, Local, Res, ResMut},
system::{In, IntoChainSystem, Local, Res, ResMut},
};
use std::{any::TypeId, fmt::Debug, hash::Hash};
use thiserror::Error;
Expand Down Expand Up @@ -98,134 +98,128 @@ impl<T> State<T>
where
T: StateData,
{
pub fn on_update(s: T) -> RunCriteriaDescriptor {
(|state: Res<State<T>>, pred: Local<Option<T>>| {
state.stack.last().unwrap() == pred.as_ref().unwrap() && state.transition.is_none()
pub fn on_update(pred: T) -> RunCriteriaDescriptor {
let pred_clone = pred.clone();
(move |state: Res<State<T>>| {
state.stack.last().unwrap() == &pred && state.transition.is_none()
})
.config(|(_, pred)| *pred = Some(Some(s.clone())))
.chain(should_run_adapter::<T>)
.after(DriverLabel::of::<T>())
.label_discard_if_duplicate(StateCallback::Update.into_label(s))
.label_discard_if_duplicate(StateCallback::Update.into_label(pred_clone))
}

pub fn on_inactive_update(s: T) -> RunCriteriaDescriptor {
(|state: Res<State<T>>, mut is_inactive: Local<bool>, pred: Local<Option<T>>| match &state
.transition
{
pub fn on_inactive_update(pred: T) -> RunCriteriaDescriptor {
let pred_clone = pred.clone();
(move |state: Res<State<T>>, mut is_inactive: Local<bool>| match &state.transition {
Some(StateTransition::Pausing(ref relevant, _))
| Some(StateTransition::Resuming(_, ref relevant)) => {
if relevant == pred.as_ref().unwrap() {
if relevant == &pred {
*is_inactive = !*is_inactive;
}
false
}
Some(_) => false,
None => *is_inactive,
})
.config(|(_, _, pred)| *pred = Some(Some(s.clone())))
.chain(should_run_adapter::<T>)
.after(DriverLabel::of::<T>())
.label_discard_if_duplicate(StateCallback::InactiveUpdate.into_label(s))
.label_discard_if_duplicate(StateCallback::InactiveUpdate.into_label(pred_clone))
}

pub fn on_in_stack_update(s: T) -> RunCriteriaDescriptor {
(|state: Res<State<T>>, mut is_in_stack: Local<bool>, pred: Local<Option<T>>| match &state
.transition
{
pub fn on_in_stack_update(pred: T) -> RunCriteriaDescriptor {
let pred_clone = pred.clone();
(move |state: Res<State<T>>, mut is_in_stack: Local<bool>| match &state.transition {
Some(StateTransition::Entering(ref relevant, _))
| Some(StateTransition::ExitingToResume(_, ref relevant)) => {
if relevant == pred.as_ref().unwrap() {
if relevant == &pred {
*is_in_stack = !*is_in_stack;
}
false
}
Some(StateTransition::ExitingFull(_, ref relevant)) => {
if relevant == pred.as_ref().unwrap() {
if relevant == &pred {
*is_in_stack = !*is_in_stack;
}
false
}
Some(StateTransition::Startup) => {
if state.stack.last().unwrap() == pred.as_ref().unwrap() {
if state.stack.last().unwrap() == &pred {
*is_in_stack = !*is_in_stack;
}
false
}
Some(_) => false,
None => *is_in_stack,
})
.config(|(_, _, pred)| *pred = Some(Some(s.clone())))
.chain(should_run_adapter::<T>)
.after(DriverLabel::of::<T>())
.label_discard_if_duplicate(StateCallback::InStackUpdate.into_label(s))
.label_discard_if_duplicate(StateCallback::InStackUpdate.into_label(pred_clone))
}

pub fn on_enter(s: T) -> RunCriteriaDescriptor {
(|state: Res<State<T>>, pred: Local<Option<T>>| {
pub fn on_enter(pred: T) -> RunCriteriaDescriptor {
let pred_clone = pred.clone();
(move |state: Res<State<T>>| {
state
.transition
.as_ref()
.map_or(false, |transition| match transition {
StateTransition::Entering(_, entering) => entering == pred.as_ref().unwrap(),
StateTransition::Startup => {
state.stack.last().unwrap() == pred.as_ref().unwrap()
}
StateTransition::Entering(_, entering) => entering == &pred,
StateTransition::Startup => state.stack.last().unwrap() == &pred,
_ => false,
})
})
.config(|(_, pred)| *pred = Some(Some(s.clone())))
.chain(should_run_adapter::<T>)
.after(DriverLabel::of::<T>())
.label_discard_if_duplicate(StateCallback::Enter.into_label(s))
.label_discard_if_duplicate(StateCallback::Enter.into_label(pred_clone))
}

pub fn on_exit(s: T) -> RunCriteriaDescriptor {
(|state: Res<State<T>>, pred: Local<Option<T>>| {
pub fn on_exit(pred: T) -> RunCriteriaDescriptor {
let pred_clone = pred.clone();
(move |state: Res<State<T>>| {
state
.transition
.as_ref()
.map_or(false, |transition| match transition {
StateTransition::ExitingToResume(exiting, _)
| StateTransition::ExitingFull(exiting, _) => exiting == pred.as_ref().unwrap(),
| StateTransition::ExitingFull(exiting, _) => exiting == &pred,
_ => false,
})
})
.config(|(_, pred)| *pred = Some(Some(s.clone())))
.chain(should_run_adapter::<T>)
.after(DriverLabel::of::<T>())
.label_discard_if_duplicate(StateCallback::Exit.into_label(s))
.label_discard_if_duplicate(StateCallback::Exit.into_label(pred_clone))
}

pub fn on_pause(s: T) -> RunCriteriaDescriptor {
(|state: Res<State<T>>, pred: Local<Option<T>>| {
pub fn on_pause(pred: T) -> RunCriteriaDescriptor {
let pred_clone = pred.clone();
(move |state: Res<State<T>>| {
state
.transition
.as_ref()
.map_or(false, |transition| match transition {
StateTransition::Pausing(pausing, _) => pausing == pred.as_ref().unwrap(),
StateTransition::Pausing(pausing, _) => pausing == &pred,
_ => false,
})
})
.config(|(_, pred)| *pred = Some(Some(s.clone())))
.chain(should_run_adapter::<T>)
.after(DriverLabel::of::<T>())
.label_discard_if_duplicate(StateCallback::Pause.into_label(s))
.label_discard_if_duplicate(StateCallback::Pause.into_label(pred_clone))
}

pub fn on_resume(s: T) -> RunCriteriaDescriptor {
(|state: Res<State<T>>, pred: Local<Option<T>>| {
pub fn on_resume(pred: T) -> RunCriteriaDescriptor {
let pred_clone = pred.clone();
(move |state: Res<State<T>>| {
state
.transition
.as_ref()
.map_or(false, |transition| match transition {
StateTransition::Resuming(_, resuming) => resuming == pred.as_ref().unwrap(),
StateTransition::Resuming(_, resuming) => resuming == &pred,
_ => false,
})
})
.config(|(_, pred)| *pred = Some(Some(s.clone())))
.chain(should_run_adapter::<T>)
.after(DriverLabel::of::<T>())
.label_discard_if_duplicate(StateCallback::Resume.into_label(s))
.label_discard_if_duplicate(StateCallback::Resume.into_label(pred_clone))
}

pub fn on_update_set(s: T) -> SystemSet {
Expand Down
Loading

0 comments on commit 55ac6c3

Please sign in to comment.