From c08abc5b672df56b6152bd493916916cc444aa54 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Tue, 24 Jun 2025 14:29:45 -0700 Subject: [PATCH] Adding debug enable/disable indicator light --- result | 2 +- src/base_game.rs | 40 +++++++++++++++++++++++++++++++++++++++- src/bin/hello/main.rs | 2 +- src/debug.rs | 29 +++++++++++++++++++++++++++-- 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/result b/result index e525d5c..2f3ff97 120000 --- a/result +++ b/result @@ -1 +1 @@ -/nix/store/4qpbdnz6vq95fb63cr54z1vcpvbjkfq1-home-manager-generation \ No newline at end of file +/nix/store/3pbri1x71wcd2b1ggysspqsv5mlisl4r-home-manager-generation \ No newline at end of file diff --git a/src/base_game.rs b/src/base_game.rs index 9f86860..19e0746 100644 --- a/src/base_game.rs +++ b/src/base_game.rs @@ -1,12 +1,50 @@ use super::*; /// A good starting place for creating a game building on top of the base Bevy app -pub struct BaseGamePlugin; +pub struct BaseGamePlugin { + pub camera: CameraType +} impl Plugin for BaseGamePlugin { fn build(&self, app: &mut App) { app.add_plugins(DefaultPlugins) .add_plugins(DebuggingPlugin); + + // Add a camera setup system and startup + app.add_systems(Startup, match self.camera { + CameraType::Camera2d => setup_camera_2d, + CameraType::Camera3d => setup_camera_3d, + }); } } +/// For selecting the type of camera this game requires +pub enum CameraType { + Camera2d, + Camera3d +} + + +/// System to toggle the visibility of entities based on their state +pub fn toggle_state_visibility(mut q: Query<(Entity, &mut Visibility, &S)>, curr: Res>) { + q.iter_mut().for_each(|(e, mut v, s)| { + if curr.get() == s { + *v = Visibility::Inherited; + } else { + *v = Visibility::Hidden; + } + debug!("Changing visibility of {:?} to {:?}", e, *v); + }); +} + +fn setup_camera_2d( + mut commands: Commands, +) { + commands.spawn((Camera { ..default() }, Camera2d)); +} + +fn setup_camera_3d( + mut commands: Commands, +) { + commands.spawn((Camera { ..default() }, Camera3d { ..default() })); +} diff --git a/src/bin/hello/main.rs b/src/bin/hello/main.rs index ab84804..3890900 100644 --- a/src/bin/hello/main.rs +++ b/src/bin/hello/main.rs @@ -2,7 +2,7 @@ use games::*; fn main() { App::new() - .add_plugins(BaseGamePlugin) + .add_plugins(BaseGamePlugin { camera: CameraType::Camera3d }) .add_systems(Update, ( f.run_if(any_component_added::), f.run_if(any_component_changed::), diff --git a/src/debug.rs b/src/debug.rs index 2490189..8ba4a84 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,3 +1,5 @@ +use bevy::color::palettes::css::{BLACK, RED, WHITE}; + use super::*; /// Debugging systems, resources, events, etc. @@ -6,14 +8,18 @@ pub struct DebuggingPlugin; impl Plugin for DebuggingPlugin { fn build(&self, app: &mut App) { app.init_state::() - .add_systems(Update, toggle_debug_state.run_if(on_keyboard_press(KeyCode::F12))); + .add_systems(Startup, init_debug_ui) + .add_systems(Update, ( + toggle_state_visibility::.run_if(state_changed::), + toggle_debug_state.run_if(on_keyboard_press(KeyCode::F12)) + )); } } /// Tracks if the debugger is on or off for other games systems to hook into /// /// The Debugging state may add it's own global debugging information, but is mostly a shell -#[derive(States, Debug, Default, Hash, Eq, PartialEq, Clone)] +#[derive(States, Component, Default, Debug, Clone, Hash, Eq, PartialEq)] pub enum DebuggingState { #[default] Off, @@ -31,4 +37,23 @@ fn toggle_debug_state( On => Off, Off => On, }); + debug!("Toggling debug state: {:?} -> {:?}", curr, next); +} + +/// Create the Debugging UI +fn init_debug_ui( + mut commands: Commands +) { + commands.spawn(( + DebuggingState::On, + Text(" Debug: ON ".into()), + TextColor(WHITE.into()), + BackgroundColor(RED.into()), + BorderRadius::MAX, + Node { + align_self: AlignSelf::Center, + justify_self: JustifySelf::End, + ..default() + } + )); }