Unify transition names to exited and entered (#13594)

# Objective

Unifies the naming convention between `StateTransitionEvent<S>` and
transition schedules.

## Migration Guide

- `StateTransitionEvent<S>` and `OnTransition<S>` schedule had their
fields renamed to `exited` and `entered` to match schedules.
This commit is contained in:
MiniaczQ 2024-05-31 17:20:01 +02:00 committed by GitHub
parent ea283c1dea
commit 912f77b2fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 27 deletions

View file

@ -32,9 +32,9 @@ pub struct OnExit<S: States>(pub S);
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct OnTransition<S: States> {
/// The state being exited.
pub from: S,
pub exited: S,
/// The state being entered.
pub to: S,
pub entered: S,
}
/// Runs [state transitions](States).
@ -46,10 +46,10 @@ pub struct StateTransition;
/// If you know exactly what state you want to respond to ahead of time, consider [`OnEnter`], [`OnTransition`], or [`OnExit`]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Event)]
pub struct StateTransitionEvent<S: States> {
/// the state we were in before
pub before: Option<S>,
/// the state we're in now
pub after: Option<S>,
/// The state being exited.
pub exited: Option<S>,
/// The state being entered.
pub entered: Option<S>,
}
/// Applies manual state transitions using [`NextState<S>`].
@ -96,8 +96,8 @@ pub(crate) fn internal_apply_state_transition<S: States>(
let exited = mem::replace(&mut state_resource.0, entered.clone());
event.send(StateTransitionEvent {
before: Some(exited.clone()),
after: Some(entered.clone()),
exited: Some(exited.clone()),
entered: Some(entered.clone()),
});
}
}
@ -106,8 +106,8 @@ pub(crate) fn internal_apply_state_transition<S: States>(
commands.insert_resource(State(entered.clone()));
event.send(StateTransitionEvent {
before: None,
after: Some(entered.clone()),
exited: None,
entered: Some(entered.clone()),
});
}
};
@ -118,8 +118,8 @@ pub(crate) fn internal_apply_state_transition<S: States>(
commands.remove_resource::<State<S>>();
event.send(StateTransitionEvent {
before: Some(resource.get().clone()),
after: None,
exited: Some(resource.get().clone()),
entered: None,
});
}
}
@ -215,8 +215,8 @@ pub(crate) fn should_run_transition<S: States, T: ScheduleLabel>(
return (
Some(StateTransitionEvent {
before: None,
after: Some(res.get().clone()),
exited: None,
entered: Some(res.get().clone()),
}),
PhantomData,
);
@ -232,12 +232,11 @@ pub(crate) fn run_enter<S: States>(
let Some(transition) = transition else {
return;
};
let Some(after) = transition.after else {
let Some(entered) = transition.entered else {
return;
};
let _ = world.try_run_schedule(OnEnter(after));
let _ = world.try_run_schedule(OnEnter(entered));
}
pub(crate) fn run_exit<S: States>(
@ -247,12 +246,11 @@ pub(crate) fn run_exit<S: States>(
let Some(transition) = transition else {
return;
};
let Some(before) = transition.before else {
let Some(exited) = transition.exited else {
return;
};
let _ = world.try_run_schedule(OnExit(before));
let _ = world.try_run_schedule(OnExit(exited));
}
pub(crate) fn run_transition<S: States>(
@ -265,12 +263,12 @@ pub(crate) fn run_transition<S: States>(
let Some(transition) = transition else {
return;
};
let Some(from) = transition.before else {
let Some(exited) = transition.exited else {
return;
};
let Some(to) = transition.after else {
let Some(entered) = transition.entered else {
return;
};
let _ = world.try_run_schedule(OnTransition { from, to });
let _ = world.try_run_schedule(OnTransition { exited, entered });
}

View file

@ -337,13 +337,13 @@ fn log_transitions(
for transition in transitions.read() {
info!(
"transition: {:?} => {:?}",
transition.before, transition.after
transition.exited, transition.entered
);
}
for transition in tutorial_transitions.read() {
info!(
"tutorial transition: {:?} => {:?}",
transition.before, transition.after
transition.exited, transition.entered
);
}
}

View file

@ -170,7 +170,7 @@ fn log_transitions(mut transitions: EventReader<StateTransitionEvent<AppState>>)
for transition in transitions.read() {
info!(
"transition: {:?} => {:?}",
transition.before, transition.after
transition.exited, transition.entered
);
}
}

View file

@ -162,7 +162,7 @@ fn log_transitions(mut transitions: EventReader<StateTransitionEvent<AppState>>)
for transition in transitions.read() {
info!(
"transition: {:?} => {:?}",
transition.before, transition.after
transition.exited, transition.entered
);
}
}