Toggle debug state with f12

main
Elijah Voigt 4 days ago
parent fd2c52b5ac
commit 198233236d

@ -12,11 +12,16 @@ pub struct DebugPlugin;
// - Show current state(s)
impl Plugin for DebugPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (
log_transition::<Debugger>.run_if(state_changed::<Debugger>),
log_transition::<Loading>.run_if(state_changed::<Loading>),
log_transition::<GameState>.run_if(state_changed::<GameState>),
));
app
.init_state::<Debugger>()
.add_systems(Update,
// Logging state transitions
(
log_transition::<Debugger>.run_if(state_changed::<Debugger>),
log_transition::<Loading>.run_if(state_changed::<Loading>),
log_transition::<GameState>.run_if(state_changed::<GameState>),
))
.add_systems(Update, toggle_debug.run_if(input_just_pressed(KeyCode::F12)));
}
}
@ -28,6 +33,7 @@ pub enum Debugger {
On,
}
/// Logs all state transitions for state T
fn log_transition<T: States + PartialEq + Clone>(
curr: Res<State<T>>,
mut prev: Local<Option<T>>,
@ -38,3 +44,16 @@ fn log_transition<T: States + PartialEq + Clone>(
*prev = Some(curr.get().clone());
}
/// Toggle the debug state when a key is pressed
fn toggle_debug(
curr: Res<State<Debugger>>,
mut next: ResMut<NextState<Debugger>>,
) {
next.set(
match curr.get() {
Debugger::On => Debugger::Off,
Debugger::Off => Debugger::On,
}
);
}

Loading…
Cancel
Save