Log state transitions

main
Elijah Voigt 5 days ago
parent 44a343ff71
commit fd2c52b5ac

@ -3,11 +3,38 @@ use super::*;
/// Debug UI for Tetris /// Debug UI for Tetris
/// Some overlap with more general purpose debug tools, /// Some overlap with more general purpose debug tools,
/// but built one-off because of the changse to UI in Bevy 0.17 /// but built one-off because of the changse to UI in Bevy 0.17
pub struct DebugPlugin;
struct DebugPlugin; // Debugging wish-list:
// - Toggle debug on/off with f12 key
// - Bounding boxes around entities
// - Cursor at the center of the world
// - Show current state(s)
impl Plugin for DebugPlugin { impl Plugin for DebugPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
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>),
));
} }
} }
/// Tracks if the game is in debug mode
#[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash)]
pub enum Debugger {
#[default]
Off,
On,
}
fn log_transition<T: States + PartialEq + Clone>(
curr: Res<State<T>>,
mut prev: Local<Option<T>>,
) {
debug_assert!(Some(curr.get().clone()) != *prev);
info!("State Change:: {:?} -> {:?}", *prev, *curr);
*prev = Some(curr.get().clone());
}

@ -12,7 +12,7 @@ use fighter::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins((DefaultPlugins, BlocksPlugin, FighterPlugin)) .add_plugins((DefaultPlugins, BlocksPlugin, FighterPlugin, DebugPlugin))
.init_state::<Loading>() .init_state::<Loading>()
.init_state::<Debugger>() .init_state::<Debugger>()
.init_state::<GameState>() .init_state::<GameState>()
@ -42,14 +42,6 @@ enum Loading {
Idle, Idle,
} }
/// Tracks if the game is in debug mode
#[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash)]
enum Debugger {
#[default]
Off,
On,
}
/// Tracks what state the main game loop is in /// Tracks what state the main game loop is in
#[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash)] #[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash)]
enum GameState { enum GameState {
@ -71,6 +63,12 @@ struct SetupChecklist {
spawn_shape: bool, spawn_shape: bool,
} }
impl SetupChecklist {
fn done(&self) -> bool {
self.spawn_shape
}
}
/// Sends the game into Loading::Active if assets are added to the AllAssets list /// Sends the game into Loading::Active if assets are added to the AllAssets list
fn loading_check(mut next: ResMut<NextState<Loading>>, all_assets: Res<AllAssets>) { fn loading_check(mut next: ResMut<NextState<Loading>>, all_assets: Res<AllAssets>) {
debug_assert!(all_assets.is_changed()); debug_assert!(all_assets.is_changed());
@ -102,6 +100,7 @@ fn setup_game(curr: Res<State<GameState>>, mut next: ResMut<NextState<GameState>
next.set(GameState::Setup); next.set(GameState::Setup);
} }
/// Wait until all checklist items are complete
fn setup_wait( fn setup_wait(
curr: Res<State<GameState>>, curr: Res<State<GameState>>,
mut next: ResMut<NextState<GameState>>, mut next: ResMut<NextState<GameState>>,
@ -110,7 +109,7 @@ fn setup_wait(
debug_assert!(*curr.get() == GameState::Setup); debug_assert!(*curr.get() == GameState::Setup);
// If all checks pass, move on to the run state // If all checks pass, move on to the run state
if checklist.spawn_shape { if checklist.done() {
next.set(GameState::Run); next.set(GameState::Run);
} }
} }

Loading…
Cancel
Save