Merge pull request #35 from Ewpratten/ewpratten/better_sampling

Implement a pause menu
This commit is contained in:
Evan Pratten 2022-04-03 16:37:49 -04:00 committed by GitHub
commit 5ca4bb022f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 25 deletions

View File

@ -28,7 +28,7 @@ pub async fn handle_graphics_blocking<ConfigBuilder>(
constants: &ProjectConstants, constants: &ProjectConstants,
discord_signaling: DiscordChannel, discord_signaling: DiscordChannel,
game_settings: &mut PersistentGameSettings, game_settings: &mut PersistentGameSettings,
save_state: &mut GameSaveState save_state: &mut GameSaveState,
) where ) where
ConfigBuilder: FnOnce(&mut RaylibBuilder), ConfigBuilder: FnOnce(&mut RaylibBuilder),
{ {
@ -100,9 +100,6 @@ pub async fn handle_graphics_blocking<ConfigBuilder>(
.resources .resources
.expect("Failed to get global 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 // Run the event loop
while !raylib_handle.window_should_close() { while !raylib_handle.window_should_close() {
// Handle state machine updates // Handle state machine updates
@ -138,16 +135,6 @@ pub async fn handle_graphics_blocking<ConfigBuilder>(
_ => backend_sm = RenderBackendStates::sm_failed(), _ => 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 // Tell the profiler that we ended the frame
profiling::finish_frame!(); profiling::finish_frame!();
} }

View File

@ -10,7 +10,8 @@ use raylib::{
use crate::{ use crate::{
discord::{DiscordChannel, DiscordRpcSignal}, discord::{DiscordChannel, DiscordRpcSignal},
global_resource_package::GlobalResources, global_resource_package::GlobalResources,
project_constants::ProjectConstants, persistent::settings::PersistentGameSettings, persistent::settings::PersistentGameSettings,
project_constants::ProjectConstants,
}; };
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -21,6 +22,7 @@ pub enum MenuStateSignal {
DoOptions, DoOptions,
DoCredits, DoCredits,
DoLeaderboard, DoLeaderboard,
DoPauseMenu,
} }
#[derive(Debug)] #[derive(Debug)]
@ -35,7 +37,7 @@ impl MainMenu {
raylib_handle: &mut RaylibHandle, raylib_handle: &mut RaylibHandle,
thread: &RaylibThread, thread: &RaylibThread,
constants: &ProjectConstants, constants: &ProjectConstants,
game_settings: &mut PersistentGameSettings game_settings: &mut PersistentGameSettings,
) -> Self { ) -> Self {
Self { Self {
has_updated_discord_rpc: false, has_updated_discord_rpc: false,
@ -51,7 +53,7 @@ impl MainMenu {
global_resources: &GlobalResources, global_resources: &GlobalResources,
constants: &ProjectConstants, constants: &ProjectConstants,
audio_subsystem: &mut RaylibAudio, audio_subsystem: &mut RaylibAudio,
game_settings: &mut PersistentGameSettings game_settings: &mut PersistentGameSettings,
) -> MenuStateSignal { ) -> MenuStateSignal {
// Handle updating discord RPC // Handle updating discord RPC
if !self.has_updated_discord_rpc { if !self.has_updated_discord_rpc {
@ -90,7 +92,7 @@ impl MainMenu {
let label_shadow_colors = Color::GRAY; let label_shadow_colors = Color::GRAY;
//Initial Option placeholder words in the main menu //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("Start Game", 100, 190, 34, label_colors);
draw.draw_text("Credits", 100, 410, 34, label_colors); draw.draw_text("Credits", 100, 410, 34, label_colors);
draw.draw_text("Leaderboard", 100, 470, 34, label_colors); draw.draw_text("Leaderboard", 100, 470, 34, label_colors);

View File

@ -6,16 +6,20 @@
use raylib::prelude::*; use raylib::prelude::*;
use crate::{ use crate::{
discord::DiscordChannel, global_resource_package::GlobalResources, discord::DiscordChannel,
project_constants::ProjectConstants, persistent::{save_state::GameSaveState, settings::PersistentGameSettings}, global_resource_package::GlobalResources,
persistent::{save_state::GameSaveState, settings::PersistentGameSettings},
project_constants::ProjectConstants,
}; };
use self::{ use self::{
main_menu::{MainMenu, MenuStateSignal}, main_menu::{MainMenu, MenuStateSignal},
pause_menu::PauseMenu,
player_interaction::PlayableScene, player_interaction::PlayableScene,
test_fox::TestFoxScene, test_fox::TestFoxScene,
}; };
mod main_menu; mod main_menu;
mod pause_menu;
mod player_interaction; mod player_interaction;
mod test_fox; mod test_fox;
@ -29,6 +33,7 @@ pub struct SceneRenderDelegate {
scene_test_fox: TestFoxScene, scene_test_fox: TestFoxScene,
scene_playable: PlayableScene, scene_playable: PlayableScene,
scene_main_menu: MainMenu, scene_main_menu: MainMenu,
scene_pause_menu: PauseMenu,
} }
impl SceneRenderDelegate { impl SceneRenderDelegate {
@ -39,12 +44,13 @@ impl SceneRenderDelegate {
constants: &ProjectConstants, constants: &ProjectConstants,
audio_subsystem: RaylibAudio, audio_subsystem: RaylibAudio,
game_settings: &mut PersistentGameSettings, game_settings: &mut PersistentGameSettings,
save_state: &mut GameSaveState save_state: &mut GameSaveState,
) -> Self { ) -> Self {
// Init some scenes // Init some scenes
let scene_test_fox = TestFoxScene::new(raylib, rl_thread); let scene_test_fox = TestFoxScene::new(raylib, rl_thread);
let scene_playable = PlayableScene::new(raylib, rl_thread, constants); let scene_playable = PlayableScene::new(raylib, rl_thread, constants);
let scene_main_menu = MainMenu::new(raylib, rl_thread, constants, game_settings); 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 { Self {
menu_control_signal: MenuStateSignal::DoMainMenu, menu_control_signal: MenuStateSignal::DoMainMenu,
@ -53,6 +59,7 @@ impl SceneRenderDelegate {
scene_test_fox, scene_test_fox,
scene_playable, scene_playable,
scene_main_menu, scene_main_menu,
scene_pause_menu,
} }
} }
@ -67,12 +74,12 @@ impl SceneRenderDelegate {
global_resources: &GlobalResources, global_resources: &GlobalResources,
constants: &ProjectConstants, constants: &ProjectConstants,
game_settings: &mut PersistentGameSettings, game_settings: &mut PersistentGameSettings,
save_state: &mut GameSaveState save_state: &mut GameSaveState,
) { ) {
// Render the main menu if in it, otherwise, render the game // Render the main menu if in it, otherwise, render the game
match self.menu_control_signal { match self.menu_control_signal {
MenuStateSignal::StartGame => { MenuStateSignal::StartGame => {
self.scene_playable self.menu_control_signal = self.scene_playable
.render_frame( .render_frame(
raylib, raylib,
rl_thread, rl_thread,
@ -100,7 +107,7 @@ impl SceneRenderDelegate {
global_resources, global_resources,
constants, constants,
&mut self.audio_subsystem, &mut self.audio_subsystem,
game_settings game_settings,
) )
.await; .await;
@ -139,6 +146,19 @@ impl SceneRenderDelegate {
) )
.await .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;
}
} }
} }
} }

View File

@ -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;
}
}

View File

@ -13,6 +13,8 @@ use crate::{
rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer}, rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer},
}; };
use super::main_menu::MenuStateSignal;
#[derive(Debug)] #[derive(Debug)]
pub struct PlayableScene { pub struct PlayableScene {
pub has_updated_discord_rpc: bool, pub has_updated_discord_rpc: bool,
@ -72,7 +74,7 @@ impl PlayableScene {
global_resources: &GlobalResources, global_resources: &GlobalResources,
constants: &ProjectConstants, constants: &ProjectConstants,
audio_subsystem: &mut RaylibAudio, audio_subsystem: &mut RaylibAudio,
) { ) -> MenuStateSignal {
// Handle updating discord RPC // Handle updating discord RPC
if !self.has_updated_discord_rpc { if !self.has_updated_discord_rpc {
discord discord
@ -106,6 +108,13 @@ impl PlayableScene {
self.draw_world(&mut draw, constants); self.draw_world(&mut draw, constants);
self.draw_ui(&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) { pub fn draw_world(&mut self, draw: &mut RaylibDrawHandle, constants: &ProjectConstants) {