use bevy::{ diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}, prelude::*, }; use crate::{ config::{SCREEN_HEIGHT, SCREEN_WIDTH}, gameplay::{camera::MainCamera, player::Player}, states::GameState, world::RunSeed, }; #[derive(Component)] pub struct Debug; #[derive(Component)] pub struct DebugCamera; #[derive(Component)] struct ViewDebugText; fn spawn_debug_camera(mut commands: Commands) { commands.spawn(( Debug, DebugCamera, Camera2d, Camera { is_active: false, ..default() }, )); } fn setup_debug_camera(mut camera: Single<&mut Projection, With>) { if let Projection::Orthographic(ref mut projection) = **camera { projection.scale = 4.0; } } fn toggle_debug_camera( input: Res>, mut main_camera: Single<&mut Camera, (With, Without)>, mut debug_camera: Single<&mut Camera, (With, Without)>, ) { if input.just_pressed(KeyCode::Tab) { main_camera.is_active = !main_camera.is_active; debug_camera.is_active = !debug_camera.is_active; } } fn follow_player_debug( player: Single<&Transform, (With, Without)>, mut debug_camera: Single<&mut Transform, (With, Without)>, ) { debug_camera.translation.y = player.translation.y; } enum FrameSide { Top, Bottom, Left, Right, } #[derive(Component)] struct FramePart(FrameSide); fn spawn_debug_frame(mut commands: Commands) { for side in [ FrameSide::Top, FrameSide::Bottom, FrameSide::Left, FrameSide::Right, ] { let size = match side { FrameSide::Top | FrameSide::Bottom => Vec2::new(SCREEN_WIDTH + 16.0, 8.0), FrameSide::Left | FrameSide::Right => Vec2::new(8.0, SCREEN_HEIGHT + 16.0), }; commands.spawn(( Debug, FramePart(side), Sprite::from_color(Color::srgb(1.0, 0.0, 0.0), size), Visibility::Hidden, )); } } fn draw_view_frame( main_camera: Single<&Transform, (With, Without)>, debug_camera: Single<&Camera, (With, Without)>, mut frame: Query< (&FramePart, &mut Transform, &mut Visibility), (Without, Without), >, ) { let center = main_camera.translation; let half_width = SCREEN_WIDTH / 2.0; let half_height = SCREEN_HEIGHT / 2.0; let visible = if debug_camera.is_active { Visibility::Visible } else { Visibility::Hidden }; for (frame_part, mut frame_transform, mut visibility) in &mut frame { let position = match frame_part.0 { FrameSide::Top => Vec3::new(center.x, center.y + half_height, 10.0), FrameSide::Bottom => Vec3::new(center.x, center.y - half_height, 10.0), FrameSide::Left => Vec3::new(center.x - half_width, center.y, 10.0), FrameSide::Right => Vec3::new(center.x + half_width, center.y, 10.0), }; frame_transform.translation = position; *visibility = visible; } } #[derive(Component)] struct SeedDebugText; #[derive(Component)] struct FpsDebugText; // Debug information on screen fn spawn_debug_texts(mut commands: Commands) { commands.spawn(( Debug, Node { position_type: PositionType::Absolute, right: Val::Px(0.0), top: Val::Px(0.0), width: Val::Percent(17.0), height: Val::Percent(10.0), flex_direction: FlexDirection::Column, row_gap: Val::Px(5.0), ..default() }, // Black background BackgroundColor(Color::srgba(0.0, 1.0, 0.0, 0.1)), // Texts children![ // Seed display ( Text::new(""), TextFont { font_size: FontSize::Px(20.0), ..default() }, TextColor(Color::srgb(1.0, 1.0, 0.0)), SeedDebugText, ), ( Text::new(""), TextFont { font_size: FontSize::Px(20.0), ..default() }, TextColor(Color::srgb(1.0, 1.0, 0.0)), FpsDebugText, ), ( Text::new(""), TextFont { font_size: FontSize::Px(20.0), ..default() }, TextColor(Color::srgb(1.0, 1.0, 0.0)), ViewDebugText, ) ], )); } // Update depth score texts fn update_seed_debug_text( run_seed: Res, mut run_seed_debug_text: Single<&mut Text, With>, ) { run_seed_debug_text.0 = format!("Seed: {}", run_seed.0); } fn update_fps_debug_text( store: Res, mut fps_debug_text: Single<&mut Text, With>, ) { if let Some(fps) = store.get(&FrameTimeDiagnosticsPlugin::FPS) { if let Some(value) = fps.smoothed() { fps_debug_text.0 = format!("FPS: {:.0}", value); } } } fn cleanup_debug(mut commands: Commands, entities: Query>) { for entity in entities { commands.entity(entity).despawn(); } } // Show the world rect the game camera renders fn update_view_debug_text(mut text: Single<&mut Text, With>) { text.0 = format!("View: {SCREEN_WIDTH:.0}x{SCREEN_HEIGHT:.0}"); } pub struct DebugPlugin; impl Plugin for DebugPlugin { fn build(&self, app: &mut App) { app.add_systems( OnEnter(GameState::InGame), ( spawn_debug_camera, setup_debug_camera.after(spawn_debug_camera), spawn_debug_frame, spawn_debug_texts, ), ) .add_systems( Update, ( draw_view_frame, follow_player_debug, toggle_debug_camera, update_seed_debug_text, update_fps_debug_text, update_view_debug_text, ) .run_if(in_state(GameState::InGame)), ) .add_plugins(FrameTimeDiagnosticsPlugin::default()) .add_systems(OnExit(GameState::InGame), cleanup_debug); } }