Added fps counter & Settings screen triggered by ESC

This commit is contained in:
Franklin 2023-11-07 12:57:27 -04:00
parent 0dccbbd7f8
commit 45639fd0f3
12 changed files with 188 additions and 34 deletions

43
Cargo.lock generated
View File

@ -274,12 +274,6 @@ version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecc7ab41815b3c653ccd2978ec3255c81349336702dfdf62ee6f7069b12a3aae"
[[package]]
name = "atomic_refcell"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76f2bfe491d41d45507b8431da8274f7feeca64a49e86d980eed2937ec2ff020"
[[package]]
name = "autocfg"
version = "1.1.0"
@ -325,9 +319,9 @@ dependencies = [
[[package]]
name = "bevy-inspector-egui"
version = "0.19.0"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd2523be5ae7d482e5435dc75509b80a320989175768fb4b711603b9d2ab8fff"
checksum = "452f1258251b85c06c5bc9df9ea994d8f3b8c0696f346d43878eec52fce8a09c"
dependencies = [
"bevy-inspector-egui-derive",
"bevy_app",
@ -353,9 +347,9 @@ dependencies = [
[[package]]
name = "bevy-inspector-egui-derive"
version = "0.19.0"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0edba455601861b8e8b76128ae5d46dd968114edde60f0ac3d2c21535a947548"
checksum = "7a60a8d711fa10879a4e7d10127077505ba9890cead8d1f5ecedc64d14b22b6c"
dependencies = [
"proc-macro2",
"quote",
@ -564,9 +558,9 @@ dependencies = [
[[package]]
name = "bevy_egui"
version = "0.21.0"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a73a93a8cf6b8c744281d1b88f5b0fa278d608e909af9bbf4eb491a7cb1ad2c"
checksum = "fb1c1f6ad293c60fd8559c4502cda5e832e92b0e0f3d994929b33f24d4352d70"
dependencies = [
"arboard",
"bevy",
@ -1587,18 +1581,18 @@ checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
[[package]]
name = "ecolor"
version = "0.22.0"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e479a7fa3f23d4e794f8b2f8b3568dd4e47886ad1b12c9c095e141cb591eb63"
checksum = "cfdf4e52dbbb615cfd30cf5a5265335c217b5fd8d669593cea74a517d9c605af"
dependencies = [
"bytemuck",
]
[[package]]
name = "egui"
version = "0.22.0"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3aef8ec3ae1b772f340170c65bf27d5b8c28f543a0116c844d2ac08d01123e7"
checksum = "8bd69fed5fcf4fbb8225b24e80ea6193b61e17a625db105ef0c4d71dde6eb8b7"
dependencies = [
"ahash 0.8.3",
"epaint",
@ -1613,9 +1607,9 @@ checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "emath"
version = "0.22.0"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3857d743a6e0741cdd60b622a74c7a36ea75f5f8f11b793b41d905d2c9721a4b"
checksum = "1ef2b29de53074e575c18b694167ccbe6e5191f7b25fe65175a0d905a32eeec0"
dependencies = [
"bytemuck",
]
@ -1654,13 +1648,12 @@ dependencies = [
[[package]]
name = "epaint"
version = "0.22.0"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09333964d4d57f40a85338ba3ca5ed4716070ab184dcfed966b35491c5c64f3b"
checksum = "58067b840d009143934d91d8dcb8ded054d8301d7c11a517ace0a99bb1e1595e"
dependencies = [
"ab_glyph",
"ahash 0.8.3",
"atomic_refcell",
"bytemuck",
"ecolor",
"emath",
@ -3570,9 +3563,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "toml_datetime"
version = "0.6.3"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
[[package]]
name = "toml_edit"
@ -4268,9 +4261,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "winit"
version = "0.28.6"
version = "0.28.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "866db3f712fffba75d31bf0cdecf357c8aeafd158c5b7ab51dba2a2b2d47f196"
checksum = "9596d90b45384f5281384ab204224876e8e8bf7d58366d9b795ad99aa9894b94"
dependencies = [
"android-activity",
"bitflags 1.3.2",

View File

@ -15,7 +15,7 @@ opt-level = 3
opt-level = 3
[dependencies]
bevy = { version = "0.11.3", features = ["dynamic_linking"]}
bevy-inspector-egui = "0.19.0"
bevy = { version = "0.11", features = ["dynamic_linking"]}
bevy-inspector-egui = "0.20.0"
bevy_rapier3d = { version = "0.22.0", features = ["debug-render-3d"] }
bevy_hanabi = { version = "0.7", default-features = false, features = [ "3d" ] }

View File

@ -4,7 +4,7 @@ use bevy::{input::mouse::MouseMotion, prelude::*, window::CursorGrabMode};
use crate::{
comps::core::markers::{camera::MainCamera, player::Player},
constants::player_values::{PLAYER_CAMERA_HEIGHT, PLAYER_CROUCH_HEIGHT, PLAYER_CROUCH_TIME_S, PLAYER_LEAN_TIME, PLAYER_LEAN_ANGLE},
utils::rad_deg::radians_from_degrees,
utils::rad_deg::radians_from_degrees, ui::game::settings::SettingsScreenUIConfiguration,
};
use super::player_movement::PlayerLinearXZState;
@ -74,6 +74,7 @@ pub fn follow_cursor_with_camera(
keyboard_input: Res<Input<KeyCode>>,
btn: Res<Input<MouseButton>>,
time: Res<Time>,
mut settings_screen_config: ResMut<SettingsScreenUIConfiguration>
) {
if let Ok(mut window) = primary_window.get_single_mut() {
if btn.just_pressed(MouseButton::Left) {
@ -89,9 +90,15 @@ pub fn follow_cursor_with_camera(
}
if keyboard_input.just_pressed(KeyCode::Escape) {
window.cursor.grab_mode = CursorGrabMode::None;
// also hide the cursor
window.cursor.visible = true;
if settings_screen_config.settings_menu_shown { // Hide settings screen & Capture Cursor
settings_screen_config.settings_menu_shown = false;
window.cursor.grab_mode = CursorGrabMode::Locked; // use `Locked` mode to keep the cursor in one place
window.cursor.visible = false; // also hide the cursor
} else { // Show Settings screen & Release Cursor
settings_screen_config.settings_menu_shown = true;
window.cursor.grab_mode = CursorGrabMode::None; // Release cursor
window.cursor.visible = true; // Show cursor
}
}
if window.cursor.grab_mode != CursorGrabMode::None {

View File

@ -1,8 +1,9 @@
use bevy::{prelude::*, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}};
use bevy::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_rapier3d::prelude::*;
use logic::core::guns::player_firing::PlayerFiringInfo;
use scenes::scene1;
use ui::game::plugin::MainGameUIPlugin;
mod comps;
mod constants;
@ -10,6 +11,7 @@ mod logic;
mod scenes;
mod setup;
mod utils;
mod ui;
fn main() {
let mut application = App::new();
@ -26,8 +28,7 @@ fn setup_plugins(application: &mut App) {
.add_plugins(DefaultPlugins)
.add_plugins(RapierPhysicsPlugin::<NoUserData>::default())
.add_plugins(WorldInspectorPlugin::new())
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.add_plugins(MainGameUIPlugin)
.register_type::<PlayerFiringInfo>();
}

2
src/ui/editor/mod.rs Normal file
View File

@ -0,0 +1,2 @@
//! Editor UI means everything that won't be in the actual game
pub mod plugin;

9
src/ui/editor/plugin.rs Normal file
View File

@ -0,0 +1,9 @@
use bevy::prelude::*;
pub struct MainEditorUIPlugin;
impl Plugin for MainEditorUIPlugin {
fn build(&self, app: &mut App) {
todo!()
}
}

View File

@ -0,0 +1,48 @@
use std::time::Duration;
use bevy::prelude::*;
#[derive(Component)]
pub struct FpsCounterMarker {
pub timer: Timer,
pub frames_elapsed: u32,
}
impl FpsCounterMarker {
pub fn new() -> Self {
Self { timer: Timer::new(Duration::from_secs_f32(0.5), TimerMode::Repeating), frames_elapsed: 0, }
}
}
pub fn setup_fps_counter(mut commands: Commands, ) {
commands.spawn(TextBundle {
text: Text::from_section("FPS: 0.0", TextStyle {
font_size: 18.0, ..Default::default()
}).with_alignment(TextAlignment::Center),
style: Style {
position_type: PositionType::Absolute,
right: Val::Px(10.0),
top: Val::Px(5.0),
..Default::default()
},
..Default::default()
})
.insert(FpsCounterMarker::new());
}
pub fn tick_fps_counter(
mut fps_counter_query: Query<(&mut Text, &mut FpsCounterMarker)>,
time: Res<Time>,
) {
for (mut fps_counter_text, mut fps_counter_marker) in fps_counter_query.iter_mut() {
if let Some(fps_counter_section) = fps_counter_text.sections.first_mut() {
fps_counter_marker.timer.tick(time.delta());
fps_counter_marker.frames_elapsed += 1;
if fps_counter_marker.timer.finished() {
fps_counter_marker.timer.reset();
let fps = fps_counter_marker.frames_elapsed * 2;
fps_counter_section.value = format!("FPS: {fps}");
fps_counter_marker.frames_elapsed = 0;
}
}
}
}

5
src/ui/game/mod.rs Normal file
View File

@ -0,0 +1,5 @@
//! Game UI means all the UI that will be part of the actual game
pub mod plugin;
pub mod fps_counter;
pub mod settings;
pub mod settings_screen;

20
src/ui/game/plugin.rs Normal file
View File

@ -0,0 +1,20 @@
use bevy::prelude::*;
use crate::ui::game::settings::GameConfiguration;
use super::{fps_counter, settings_screen, settings::SettingsScreenUIConfiguration};
pub struct MainGameUIPlugin;
impl Plugin for MainGameUIPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(GameConfiguration::default());
app.insert_resource(SettingsScreenUIConfiguration::default());
app.add_systems(Startup, (
settings_screen::setup_settings_screen,
fps_counter::setup_fps_counter));
app.add_systems(Update, (
settings_screen::toggle_settings_screen,
fps_counter::tick_fps_counter));
}
}

26
src/ui/game/settings.rs Normal file
View File

@ -0,0 +1,26 @@
use bevy::prelude::*;
#[derive(Resource)]
pub struct SettingsScreenUIConfiguration {
pub settings_menu_shown: bool,
}
impl Default for SettingsScreenUIConfiguration {
fn default() -> Self {
Self { settings_menu_shown: false }
}
}
#[derive(Resource)]
pub struct GameConfiguration {
pub fps_counter_enabled: bool,
}
impl Default for GameConfiguration {
fn default() -> Self {
Self {
fps_counter_enabled: true,
}
}
}

View File

@ -0,0 +1,41 @@
use bevy::prelude::*;
use super::settings::SettingsScreenUIConfiguration;
#[derive(Component)]
pub struct SettingsScreenMarker;
pub fn setup_settings_screen(mut commands: Commands) {
commands.spawn(
NodeBundle {
style: Style {
display: Display::Flex,
width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
visibility: Visibility::Hidden,
background_color: BackgroundColor(Color::BLACK),
..Default::default()
}
).insert(SettingsScreenMarker);
}
pub fn toggle_settings_screen(
settings: Res<SettingsScreenUIConfiguration>,
mut settings_screen_query: Query<(&mut Visibility, &mut Style), With<SettingsScreenMarker>>,
) {
if settings.is_changed() {
for (mut settings_screen_visibility, mut settings_screen_style) in settings_screen_query.iter_mut() {
if settings.settings_menu_shown {
*settings_screen_visibility = Visibility::Visible;
settings_screen_style.display = Display::Flex;
} else {
*settings_screen_visibility = Visibility::Hidden;
settings_screen_style.display = Display::None;
}
}
}
}

2
src/ui/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod game;
pub mod editor;