From 4c7e259537ae5c39c0c2d920d97b84fc64899ca3 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 29 Jul 2024 16:54:43 -0500 Subject: [PATCH] Show FPS --- src/camera.rs | 3 +- src/main.rs | 154 +++++++++++++++++++++++++++++++----------------- src/movement.rs | 5 +- src/player.rs | 2 + src/statue.rs | 4 +- 5 files changed, 112 insertions(+), 56 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 12de5b9..8dbe97d 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -16,7 +16,8 @@ pub fn update( ) { if let Ok(mut camera) = camera.get_single_mut() { if let Ok(watched) = watched.get_single() { - camera.translation.x = watched.translation.x + camera.translation.x = watched.translation.x; + camera.translation.y = watched.translation.y; } else { camera.translation = Vec3::ZERO; } diff --git a/src/main.rs b/src/main.rs index fa3a881..2680c5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use bevy::audio::{AudioPlugin, SpatialScale}; +use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; use bevy::prelude::{default, *}; use bevy::window::{PrimaryWindow, WindowMode}; @@ -39,58 +40,106 @@ fn main() { app.insert_state(View::default()); app.insert_state(Game::default()); - app.add_plugins((DefaultPlugins - .set(WindowPlugin { - primary_window: Some(Window { - title: "Kodo Tag".into(), - mode: bevy::window::WindowMode::BorderlessFullscreen, - // resolution: (640. * 2., 360. * 2.).into(), + app.add_plugins(( + DefaultPlugins + .set(WindowPlugin { + primary_window: Some(Window { + title: "Kodo Tag".into(), + mode: bevy::window::WindowMode::BorderlessFullscreen, + // resolution: (640. * 2., 360. * 2.).into(), + ..default() + }), ..default() - }), - ..default() - }) - .set(AudioPlugin { - default_spatial_scale: SpatialScale::new_2d(1.), - global_volume: GlobalVolume::new(1.), - ..default() - }) - .set(ImagePlugin::default_nearest()),)) - .add_systems(OnEnter(View::MainMenu), main_menu::startup) - .add_systems(OnExit(View::MainMenu), main_menu::exit) - .add_systems(OnEnter(View::InGame), (player::startup, statue::startup)) - .add_systems(OnExit(View::InGame), (player::exit, statue::exit)) - .add_systems(Startup, (assets::startup, camera::startup)) - .add_systems( - Update, + }) + .set(AudioPlugin { + default_spatial_scale: SpatialScale::new_2d(1.), + global_volume: GlobalVolume::new(1.), + ..default() + }) + .set(ImagePlugin::default_nearest()), + FrameTimeDiagnosticsPlugin, + )) + .add_systems(OnEnter(View::MainMenu), main_menu::startup) + .add_systems(OnExit(View::MainMenu), main_menu::exit) + .add_systems(OnEnter(View::InGame), (player::startup, statue::startup)) + .add_systems(OnExit(View::InGame), (player::exit, statue::exit)) + .add_systems(Startup, (startup, assets::startup, camera::startup)) + .add_systems( + Update, + ( + update, ( - global_hotkeys, - ( - player::sprite_select, - player::controls, - movement::update_velocity_by_heading, - movement::resolve_velocity.after(movement::update_velocity_by_heading), - movement::ysort.after(movement::update_velocity_by_heading), - camera::update - .after(player::controls) - .after(movement::resolve_velocity), - ) - .in_set(InGameSet) - .run_if(in_state(View::InGame)), - (main_menu::update) - .in_set(MainMenuSet) - .run_if(in_state(View::MainMenu)), - ), - ) - .insert_resource(ClearColor(Color::rgb(0.3, 0.1, 0.5))) - .insert_resource(AmbientLight { - color: Color::rgb(1., 1., 1.), - brightness: 1., - }); + player::sprite_select, + player::controls, + movement::update_velocity_by_heading, + movement::resolve_velocity.after(movement::update_velocity_by_heading), + movement::ysort.after(movement::update_velocity_by_heading), + camera::update + .after(player::controls) + .after(movement::resolve_velocity), + ) + .in_set(InGameSet) + .run_if(in_state(View::InGame)), + (main_menu::update) + .in_set(MainMenuSet) + .run_if(in_state(View::MainMenu)), + ), + ) + .insert_resource(ClearColor(Color::rgb(0.3, 0.1, 0.5))) + .insert_resource(AmbientLight { + color: Color::rgb(1., 1., 1.), + brightness: 1., + }); app.run() } -fn global_hotkeys( +fn toggle_fullscreen(mut window: Mut) { + if window.mode == WindowMode::Windowed { + window.mode = WindowMode::BorderlessFullscreen + } else { + window.mode = WindowMode::Windowed + } +} + +#[derive(Component)] +struct FpsText; + +fn startup(mut commands: Commands, assets: Res) { + commands.spawn(( + FpsText, + TextBundle::from_sections([ + TextSection::new( + "FPS: ", + TextStyle { + font: assets.fonts.iosevkalytemin.clone(), + font_size: 60., + ..default() + }, + ), + TextSection::from_style(TextStyle { + font: assets.fonts.iosevkalytemin.clone(), + font_size: 60., + color: Color::Hsla { + hue: 0.5, + saturation: 0.5, + lightness: 0.5, + alpha: 0.5, + }, + }), + ]) + .with_style(Style { + position_type: PositionType::Absolute, + bottom: Val::Px(5.0), + right: Val::Px(5.0), + ..default() + }), + )); +} + +fn update( + diagnostics: Res, + mut query: Query<&mut Text, With>, keyboard: Res>, mut window: Query<&mut Window, With>, ) { @@ -101,12 +150,11 @@ fn global_hotkeys( toggle_fullscreen(window) } } -} - -fn toggle_fullscreen(mut window: Mut) { - if window.mode == WindowMode::Windowed { - window.mode = WindowMode::BorderlessFullscreen - } else { - window.mode = WindowMode::Windowed + for mut text in &mut query { + if let Some(fps) = diagnostics.get(&FrameTimeDiagnosticsPlugin::FPS) { + if let Some(value) = fps.smoothed() { + text.sections[1].value = format!("{value:.2}"); + } + } } } diff --git a/src/movement.rs b/src/movement.rs index 4c10ee3..9ef1ac4 100644 --- a/src/movement.rs +++ b/src/movement.rs @@ -12,6 +12,9 @@ pub struct Heading(pub Vec2); #[derive(Component, Deref, DerefMut, Debug, Default)] pub struct Speed(pub f32); +#[derive(Component, Debug)] +pub struct YSortable; + pub fn resolve_velocity(mut query: Query<(&Velocity, &mut Transform)>, time: Res