Adding debug enable/disable indicator light

main
Elijah Voigt 4 months ago
parent 3a9fdf1c05
commit c08abc5b67

@ -1 +1 @@
/nix/store/4qpbdnz6vq95fb63cr54z1vcpvbjkfq1-home-manager-generation /nix/store/3pbri1x71wcd2b1ggysspqsv5mlisl4r-home-manager-generation

@ -1,12 +1,50 @@
use super::*; use super::*;
/// A good starting place for creating a game building on top of the base Bevy app /// 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 { impl Plugin for BaseGamePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins(DefaultPlugins) app.add_plugins(DefaultPlugins)
.add_plugins(DebuggingPlugin); .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<S: States + Component>(mut q: Query<(Entity, &mut Visibility, &S)>, curr: Res<State<S>>) {
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() }));
}

@ -2,7 +2,7 @@ use games::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(BaseGamePlugin) .add_plugins(BaseGamePlugin { camera: CameraType::Camera3d })
.add_systems(Update, ( .add_systems(Update, (
f.run_if(any_component_added::<Foo>), f.run_if(any_component_added::<Foo>),
f.run_if(any_component_changed::<Foo>), f.run_if(any_component_changed::<Foo>),

@ -1,3 +1,5 @@
use bevy::color::palettes::css::{BLACK, RED, WHITE};
use super::*; use super::*;
/// Debugging systems, resources, events, etc. /// Debugging systems, resources, events, etc.
@ -6,14 +8,18 @@ pub struct DebuggingPlugin;
impl Plugin for DebuggingPlugin { impl Plugin for DebuggingPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.init_state::<DebuggingState>() app.init_state::<DebuggingState>()
.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::<DebuggingState>.run_if(state_changed::<DebuggingState>),
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 /// 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 /// 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 { pub enum DebuggingState {
#[default] #[default]
Off, Off,
@ -31,4 +37,23 @@ fn toggle_debug_state(
On => Off, On => Off,
Off => On, 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()
}
));
} }

Loading…
Cancel
Save