mirror of
https://codeberg.org/schrottkatze/mgd2-tram-championships.git
synced 2025-07-04 19:17:38 +00:00
make debug console top level
This commit is contained in:
parent
d79475ae2a
commit
4c24f67cd5
9 changed files with 111 additions and 68 deletions
81
src/debug.rs
Normal file
81
src/debug.rs
Normal file
|
@ -0,0 +1,81 @@
|
|||
//! 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue