mgd2-tram-championships/src/debug.rs

82 lines
2.1 KiB
Rust
Raw Normal View History

2025-06-18 17:13:52 +02:00
//! 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<DebugEvent>, s: impl Into<String>) {
dbg_writer.write(DebugEvent::PrintToConsole {
error: false,
text: s.into(),
});
}
pub fn console_err(dbg_writer: &mut EventWriter<DebugEvent>, s: impl Into<String>) {
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::<DebugEvent>()
.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<DebugEvent>,
mut log: ResMut<ConsoleLog>,
cur_state: Res<State<AppState>>,
mut next_state: ResMut<NextState<AppState>>,
) {
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);
}
}
}
}