diff --git a/tetris/src/debug.rs b/tetris/src/debug.rs index a787799..11a961c 100644 --- a/tetris/src/debug.rs +++ b/tetris/src/debug.rs @@ -3,11 +3,38 @@ use super::*; /// Debug UI for Tetris /// Some overlap with more general purpose debug tools, /// 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 { fn build(&self, app: &mut App) { - app; + app.add_systems(Update, ( + log_transition::.run_if(state_changed::), + log_transition::.run_if(state_changed::), + log_transition::.run_if(state_changed::), + )); } } + +/// 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( + curr: Res>, + mut prev: Local>, +) { + debug_assert!(Some(curr.get().clone()) != *prev); + + info!("State Change:: {:?} -> {:?}", *prev, *curr); + + *prev = Some(curr.get().clone()); +} diff --git a/tetris/src/main.rs b/tetris/src/main.rs index 8923ac9..b0d35b2 100644 --- a/tetris/src/main.rs +++ b/tetris/src/main.rs @@ -12,7 +12,7 @@ use fighter::*; fn main() { App::new() - .add_plugins((DefaultPlugins, BlocksPlugin, FighterPlugin)) + .add_plugins((DefaultPlugins, BlocksPlugin, FighterPlugin, DebugPlugin)) .init_state::() .init_state::() .init_state::() @@ -42,14 +42,6 @@ enum Loading { 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 #[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash)] enum GameState { @@ -71,6 +63,12 @@ struct SetupChecklist { 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 fn loading_check(mut next: ResMut>, all_assets: Res) { debug_assert!(all_assets.is_changed()); @@ -102,6 +100,7 @@ fn setup_game(curr: Res>, mut next: ResMut next.set(GameState::Setup); } +/// Wait until all checklist items are complete fn setup_wait( curr: Res>, mut next: ResMut>, @@ -110,7 +109,7 @@ fn setup_wait( debug_assert!(*curr.get() == GameState::Setup); // If all checks pass, move on to the run state - if checklist.spawn_shape { + if checklist.done() { next.set(GameState::Run); } }