//! Seperate out debugging uis/plugins in it's own module for cleanliness. use bevy::prelude::*; use bevy_inspector_egui::bevy_egui::EguiPlugin; use clap::Subcommand; use console::ConsoleLog; use util::{console_err, console_log}; use crate::AppState; mod console; mod util { use bevy::prelude::*; use super::DebugEvent; pub fn console_log(dbg_writer: &mut EventWriter, s: impl Into) { dbg_writer.write(DebugEvent::PrintToConsole { error: false, text: s.into(), }); } pub fn console_err(dbg_writer: &mut EventWriter, s: impl Into) { dbg_writer.write(DebugEvent::PrintToConsole { error: false, text: s.into(), }); } } /// Debug system set. #[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)] pub struct DebugSet; pub fn plugin(app: &mut App) { app.add_plugins(EguiPlugin { enable_multipass_for_primary_context: true, }) .add_systems(Update, start_game.in_set(DebugSet)) .add_event::() .add_plugins(console::plugin); } #[derive(Event, Debug, Subcommand, PartialEq, Eq)] enum DebugEvent { /// Close the debug console. #[command(name = "close", aliases = ["close-console"])] CloseDebugConsole, /// Output a string to the console. #[command(name = "echo", aliases = ["print", "print-to-console"])] PrintToConsole { /// Print as error #[arg(short, long)] error: bool, /// The text to be printed in the console. text: String, }, /// Start the game StartGame, } fn start_game( mut dbg_reader: EventReader, mut log: ResMut, cur_state: Res>, mut next_state: ResMut>, ) { for ev in dbg_reader.read() { if *ev == DebugEvent::StartGame { if *cur_state == AppState::Ingame { log.err("Can't start game since it's already started."); } else { log.write("Starting game..."); next_state.set(AppState::Ingame); } } } }