From 8d26ea38c872f9c7f881f4a9c1ed7d4e09b16ed7 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sun, 3 Apr 2022 16:37:12 -0400 Subject: [PATCH] Implement a pause menu --- game/game_logic/src/rendering/event_loop.rs | 15 +---- game/game_logic/src/scenes/main_menu.rs | 10 +-- game/game_logic/src/scenes/mod.rs | 32 ++++++++-- game/game_logic/src/scenes/pause_menu.rs | 62 +++++++++++++++++++ .../src/scenes/player_interaction.rs | 11 +++- 5 files changed, 105 insertions(+), 25 deletions(-) create mode 100644 game/game_logic/src/scenes/pause_menu.rs diff --git a/game/game_logic/src/rendering/event_loop.rs b/game/game_logic/src/rendering/event_loop.rs index 83944fbf..4d94253c 100644 --- a/game/game_logic/src/rendering/event_loop.rs +++ b/game/game_logic/src/rendering/event_loop.rs @@ -28,7 +28,7 @@ pub async fn handle_graphics_blocking( constants: &ProjectConstants, discord_signaling: DiscordChannel, game_settings: &mut PersistentGameSettings, - save_state: &mut GameSaveState + save_state: &mut GameSaveState, ) where ConfigBuilder: FnOnce(&mut RaylibBuilder), { @@ -100,9 +100,6 @@ pub async fn handle_graphics_blocking( .resources .expect("Failed to get global resources"); - // Tracker for if we are showing the FPS counter - let mut show_fps_counter = false; - // Run the event loop while !raylib_handle.window_should_close() { // Handle state machine updates @@ -138,16 +135,6 @@ pub async fn handle_graphics_blocking( _ => backend_sm = RenderBackendStates::sm_failed(), }; - // Check for F3 being pressed - if raylib_handle.is_key_pressed(KeyboardKey::KEY_F3) { - show_fps_counter = !show_fps_counter; - } - - // Show the FPS counter - if show_fps_counter { - raylib_handle.begin_drawing(&raylib_thread).draw_fps(10, 10); - } - // Tell the profiler that we ended the frame profiling::finish_frame!(); } diff --git a/game/game_logic/src/scenes/main_menu.rs b/game/game_logic/src/scenes/main_menu.rs index 1ff85a28..3c166571 100644 --- a/game/game_logic/src/scenes/main_menu.rs +++ b/game/game_logic/src/scenes/main_menu.rs @@ -10,7 +10,8 @@ use raylib::{ use crate::{ discord::{DiscordChannel, DiscordRpcSignal}, global_resource_package::GlobalResources, - project_constants::ProjectConstants, persistent::settings::PersistentGameSettings, + persistent::settings::PersistentGameSettings, + project_constants::ProjectConstants, }; #[derive(Debug, Clone)] @@ -21,6 +22,7 @@ pub enum MenuStateSignal { DoOptions, DoCredits, DoLeaderboard, + DoPauseMenu, } #[derive(Debug)] @@ -35,7 +37,7 @@ impl MainMenu { raylib_handle: &mut RaylibHandle, thread: &RaylibThread, constants: &ProjectConstants, - game_settings: &mut PersistentGameSettings + game_settings: &mut PersistentGameSettings, ) -> Self { Self { has_updated_discord_rpc: false, @@ -51,7 +53,7 @@ impl MainMenu { global_resources: &GlobalResources, constants: &ProjectConstants, audio_subsystem: &mut RaylibAudio, - game_settings: &mut PersistentGameSettings + game_settings: &mut PersistentGameSettings, ) -> MenuStateSignal { // Handle updating discord RPC if !self.has_updated_discord_rpc { @@ -90,7 +92,7 @@ impl MainMenu { let label_shadow_colors = Color::GRAY; //Initial Option placeholder words in the main menu - draw.draw_text("Game Title", 100, 90, 60, label_colors); + draw.draw_text(&constants.game_name, 100, 90, 60, label_colors); draw.draw_text("Start Game", 100, 190, 34, label_colors); draw.draw_text("Credits", 100, 410, 34, label_colors); draw.draw_text("Leaderboard", 100, 470, 34, label_colors); diff --git a/game/game_logic/src/scenes/mod.rs b/game/game_logic/src/scenes/mod.rs index 69f7cddd..00eb5d87 100644 --- a/game/game_logic/src/scenes/mod.rs +++ b/game/game_logic/src/scenes/mod.rs @@ -6,16 +6,20 @@ use raylib::prelude::*; use crate::{ - discord::DiscordChannel, global_resource_package::GlobalResources, - project_constants::ProjectConstants, persistent::{save_state::GameSaveState, settings::PersistentGameSettings}, + discord::DiscordChannel, + global_resource_package::GlobalResources, + persistent::{save_state::GameSaveState, settings::PersistentGameSettings}, + project_constants::ProjectConstants, }; use self::{ main_menu::{MainMenu, MenuStateSignal}, + pause_menu::PauseMenu, player_interaction::PlayableScene, test_fox::TestFoxScene, }; mod main_menu; +mod pause_menu; mod player_interaction; mod test_fox; @@ -29,6 +33,7 @@ pub struct SceneRenderDelegate { scene_test_fox: TestFoxScene, scene_playable: PlayableScene, scene_main_menu: MainMenu, + scene_pause_menu: PauseMenu, } impl SceneRenderDelegate { @@ -39,12 +44,13 @@ impl SceneRenderDelegate { constants: &ProjectConstants, audio_subsystem: RaylibAudio, game_settings: &mut PersistentGameSettings, - save_state: &mut GameSaveState + save_state: &mut GameSaveState, ) -> Self { // Init some scenes let scene_test_fox = TestFoxScene::new(raylib, rl_thread); let scene_playable = PlayableScene::new(raylib, rl_thread, constants); let scene_main_menu = MainMenu::new(raylib, rl_thread, constants, game_settings); + let scene_pause_menu = PauseMenu::new(raylib, rl_thread, constants, game_settings); Self { menu_control_signal: MenuStateSignal::DoMainMenu, @@ -53,6 +59,7 @@ impl SceneRenderDelegate { scene_test_fox, scene_playable, scene_main_menu, + scene_pause_menu, } } @@ -67,12 +74,12 @@ impl SceneRenderDelegate { global_resources: &GlobalResources, constants: &ProjectConstants, game_settings: &mut PersistentGameSettings, - save_state: &mut GameSaveState + save_state: &mut GameSaveState, ) { // Render the main menu if in it, otherwise, render the game match self.menu_control_signal { MenuStateSignal::StartGame => { - self.scene_playable + self.menu_control_signal = self.scene_playable .render_frame( raylib, rl_thread, @@ -100,7 +107,7 @@ impl SceneRenderDelegate { global_resources, constants, &mut self.audio_subsystem, - game_settings + game_settings, ) .await; @@ -139,6 +146,19 @@ impl SceneRenderDelegate { ) .await } + MenuStateSignal::DoPauseMenu => { + self.menu_control_signal = self + .scene_pause_menu + .render_pause_menu_frame( + raylib, + rl_thread, + discord, + global_resources, + constants, + &mut self.audio_subsystem, + ) + .await; + } } } } diff --git a/game/game_logic/src/scenes/pause_menu.rs b/game/game_logic/src/scenes/pause_menu.rs new file mode 100644 index 00000000..28e386ed --- /dev/null +++ b/game/game_logic/src/scenes/pause_menu.rs @@ -0,0 +1,62 @@ +//! This scene encompasses the main menu system + +use na::Vector1; +use nalgebra as na; +use raylib::{ + ffi::{GetMouseX, GetMouseY, IsMouseButtonDown, Texture}, + prelude::*, +}; + +use crate::{ + discord::{DiscordChannel, DiscordRpcSignal}, + global_resource_package::GlobalResources, + persistent::settings::PersistentGameSettings, + project_constants::ProjectConstants, +}; + +use super::main_menu::MenuStateSignal; + +#[derive(Debug)] +pub struct PauseMenu {} + +impl PauseMenu { + /// Construct a new `PauseMenu` + pub fn new( + raylib_handle: &mut RaylibHandle, + thread: &RaylibThread, + constants: &ProjectConstants, + game_settings: &mut PersistentGameSettings, + ) -> Self { + Self {} + } + + pub async fn render_pause_menu_frame( + &mut self, + raylib: &mut RaylibHandle, + rl_thread: &RaylibThread, + discord: &DiscordChannel, + global_resources: &GlobalResources, + constants: &ProjectConstants, + audio_subsystem: &mut RaylibAudio, + ) -> MenuStateSignal { + // Get a drawing handle + let mut draw = raylib.begin_drawing(rl_thread); + + // Clear the screen + draw.clear_background(Color::WHITE); + + // Title + draw.draw_text("Paused", 100, 90, 60, Color::BLACK); + + // Let the user leave this menu by pressing escape + if draw.is_key_pressed(KeyboardKey::KEY_ESCAPE) { + return MenuStateSignal::StartGame; + } + + // Return MenuStateSignal::DoMainMenu if you want to return to the main menu + // Return MenuStateSignal::StartGame if you want the game to start. + // Return MenuStateSignal::QuitGame if you want the game to quit. + // Otherwise, keep returning MenuStateSignal::DoPauseMenu until the player clicks the start button + return MenuStateSignal::DoPauseMenu; + } +} diff --git a/game/game_logic/src/scenes/player_interaction.rs b/game/game_logic/src/scenes/player_interaction.rs index b5275dcf..2527b167 100644 --- a/game/game_logic/src/scenes/player_interaction.rs +++ b/game/game_logic/src/scenes/player_interaction.rs @@ -13,6 +13,8 @@ use crate::{ rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer}, }; +use super::main_menu::MenuStateSignal; + #[derive(Debug)] pub struct PlayableScene { pub has_updated_discord_rpc: bool, @@ -72,7 +74,7 @@ impl PlayableScene { global_resources: &GlobalResources, constants: &ProjectConstants, audio_subsystem: &mut RaylibAudio, - ) { + ) -> MenuStateSignal { // Handle updating discord RPC if !self.has_updated_discord_rpc { discord @@ -106,6 +108,13 @@ impl PlayableScene { self.draw_world(&mut draw, constants); self.draw_ui(&mut draw, constants); + + // A little hack to make pausing work + if draw.is_key_pressed(KeyboardKey::KEY_ESCAPE) { + return MenuStateSignal::DoPauseMenu; + } else { + return MenuStateSignal::StartGame; + } } pub fn draw_world(&mut self, draw: &mut RaylibDrawHandle, constants: &ProjectConstants) {