From 0d54f3118b13b1eac7d497378ee5a5e3d0a35b23 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sat, 2 Apr 2022 11:29:43 -0400 Subject: [PATCH 1/2] Menu routing system --- .vscode/settings.json | 1 + game/game_logic/src/scenes/main_menu.rs | 93 ++++++++++++++++++------- game/game_logic/src/scenes/mod.rs | 53 ++++++++++---- 3 files changed, 109 insertions(+), 38 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1754528f..611d78e5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "git.detectSubmodules": false, "cSpell.words": [ + "leaderboard", "msaa", "raylib", "repr", diff --git a/game/game_logic/src/scenes/main_menu.rs b/game/game_logic/src/scenes/main_menu.rs index 71a1a575..9131c79a 100644 --- a/game/game_logic/src/scenes/main_menu.rs +++ b/game/game_logic/src/scenes/main_menu.rs @@ -1,7 +1,10 @@ //! This scene encompasses the main menu system use nalgebra as na; -use raylib::{prelude::*, ffi::{Texture, GetMouseX, GetMouseY, IsMouseButtonDown}}; +use raylib::{ + ffi::{GetMouseX, GetMouseY, IsMouseButtonDown, Texture}, + prelude::*, +}; use crate::{ discord::{DiscordChannel, DiscordRpcSignal}, @@ -9,6 +12,16 @@ use crate::{ project_constants::ProjectConstants, }; +#[derive(Debug, Clone)] +pub enum MenuStateSignal { + StartGame, + QuitGame, + DoMainMenu, + DoOptions, + DoCredits, + DoLeaderboard, +} + #[derive(Debug)] pub struct MainMenu { has_updated_discord_rpc: bool, @@ -26,21 +39,17 @@ impl MainMenu { } } - /// Handler for each frame - pub async fn render_frame( + pub async fn render_main_menu_frame( &mut self, raylib: &mut RaylibHandle, rl_thread: &RaylibThread, discord: &DiscordChannel, global_resources: &GlobalResources, constants: &ProjectConstants, - ) -> bool { + ) -> MenuStateSignal { // Handle updating discord RPC if !self.has_updated_discord_rpc { - discord - .send(DiscordRpcSignal::EndGameTimer) - .await - .unwrap(); + discord.send(DiscordRpcSignal::EndGameTimer).await.unwrap(); discord .send(DiscordRpcSignal::ChangeDetails { details: "Looking at a menu".to_string(), @@ -58,15 +67,13 @@ impl MainMenu { draw.clear_background(Color::WHITE); //I wanna see where mouseeee - unsafe{ + unsafe { let mut mouseX = GetMouseX(); let mut mouseY = GetMouseY(); draw.draw_text((&mouseX.to_string()), 20, 5, 20, Color::BLACK); draw.draw_text((&mouseY.to_string()), 70, 5, 20, Color::BLACK); } - - // TODO: Render stuff //Initial Option placeholder words in the main menu @@ -80,41 +87,77 @@ impl MainMenu { //Unsafe block?? unsafe { //First two are starting X and Y position, last two finishing X and Y. Made to resemble a box - if GetMouseX() >= 100 && GetMouseY() >= 193 && GetMouseX() <= 290 && GetMouseY() <= 216{ + if GetMouseX() >= 100 && GetMouseY() >= 193 && GetMouseX() <= 290 && GetMouseY() <= 216 + { //Insides while make a lil shade for it to look cool draw.draw_text("Start Game", 103, 191, 34, Color::GRAY); draw.draw_text("Start Game", 100, 190, 34, Color::BLACK); if IsMouseButtonDown(0) { - return true; + return MenuStateSignal::StartGame; } } - if GetMouseX() >= 100 && GetMouseY() >= 250 && GetMouseX() <= 222 && GetMouseY() <= 275{ + if GetMouseX() >= 100 && GetMouseY() >= 250 && GetMouseX() <= 222 && GetMouseY() <= 275 + { draw.draw_text("Options", 103, 251, 34, Color::GRAY); draw.draw_text("Options", 100, 250, 34, Color::BLACK); - } - if GetMouseX() >= 100 && GetMouseY() >= 410 && GetMouseX() <= 222 && GetMouseY() <= 437{ + if GetMouseX() >= 100 && GetMouseY() >= 410 && GetMouseX() <= 222 && GetMouseY() <= 437 + { draw.draw_text("Credits", 103, 411, 34, Color::GRAY); draw.draw_text("Credits", 100, 410, 34, Color::BLACK); - } - if GetMouseX() >= 100 && GetMouseY() >= 470 && GetMouseX() <= 316 && GetMouseY() <= 496{ + if GetMouseX() >= 100 && GetMouseY() >= 470 && GetMouseX() <= 316 && GetMouseY() <= 496 + { draw.draw_text("Leaderboard", 103, 471, 34, Color::GRAY); draw.draw_text("Leaderboard", 100, 470, 34, Color::BLACK); - } - if GetMouseX() >= 100 && GetMouseY() >= 550 && GetMouseX() <= 162 && GetMouseY() <= 575{ + if GetMouseX() >= 100 && GetMouseY() >= 550 && GetMouseX() <= 162 && GetMouseY() <= 575 + { draw.draw_text("Exit", 103, 551, 34, Color::GRAY); draw.draw_text("Exit", 100, 550, 34, Color::BLACK); - } - } - // Return true if you want the game to start. - // Otherwise, keep returning false until the player clicks the start button - return false; + // Return MenuStateSignal::StartGame if you want the game to start. + // Otherwise, keep returning MenuStateSignal::DoMainMenu until the player clicks the start button + return MenuStateSignal::DoMainMenu; + } + + pub async fn render_options_frame( + &mut self, + raylib: &mut RaylibHandle, + rl_thread: &RaylibThread, + discord: &DiscordChannel, + global_resources: &GlobalResources, + constants: &ProjectConstants, + ) -> MenuStateSignal { + + return MenuStateSignal::DoOptions; + } + + pub async fn render_credits_frame( + &mut self, + raylib: &mut RaylibHandle, + rl_thread: &RaylibThread, + discord: &DiscordChannel, + global_resources: &GlobalResources, + constants: &ProjectConstants, + ) -> MenuStateSignal { + + return MenuStateSignal::DoCredits; + } + + pub async fn render_leaderboard_frame( + &mut self, + raylib: &mut RaylibHandle, + rl_thread: &RaylibThread, + discord: &DiscordChannel, + global_resources: &GlobalResources, + constants: &ProjectConstants, + ) -> MenuStateSignal { + + return MenuStateSignal::DoLeaderboard; } } diff --git a/game/game_logic/src/scenes/mod.rs b/game/game_logic/src/scenes/mod.rs index 41fe528a..ee1a40c8 100644 --- a/game/game_logic/src/scenes/mod.rs +++ b/game/game_logic/src/scenes/mod.rs @@ -10,19 +10,23 @@ use crate::{ project_constants::ProjectConstants, }; -use self::{player_interaction::PlayableScene, test_fox::TestFoxScene, main_menu::MainMenu}; +use self::{ + main_menu::{MainMenu, MenuStateSignal}, + player_interaction::PlayableScene, + test_fox::TestFoxScene, +}; +mod main_menu; mod player_interaction; mod test_fox; -mod main_menu; /// Delegate for handling rendering. /// This is a struct to allow for stateful data (like sub-screens) to be set up pub struct SceneRenderDelegate { - is_in_main_menu: bool, + menu_control_signal: MenuStateSignal, /* Scenes */ scene_test_fox: TestFoxScene, scene_playable: PlayableScene, - scene_main_menu: MainMenu + scene_main_menu: MainMenu, } impl SceneRenderDelegate { @@ -40,7 +44,7 @@ impl SceneRenderDelegate { let scene_main_menu = MainMenu::new(raylib, rl_thread, constants); Self { - is_in_main_menu: true, + menu_control_signal: MenuStateSignal::DoMainMenu, scene_test_fox, scene_playable, scene_main_menu, @@ -59,14 +63,37 @@ impl SceneRenderDelegate { constants: &ProjectConstants, ) { // Render the main menu if in it, otherwise, render the game - if self.is_in_main_menu { - self.is_in_main_menu = !self.scene_main_menu - .render_frame(raylib, rl_thread, discord, global_resources, constants) - .await; - }else { - self.scene_playable - .render_frame(raylib, rl_thread, &discord, global_resources, constants) - .await; + match self.menu_control_signal { + MenuStateSignal::StartGame => { + self.scene_playable + .render_frame(raylib, rl_thread, &discord, global_resources, constants) + .await; + } + MenuStateSignal::QuitGame => unimplemented!(), + MenuStateSignal::DoMainMenu => { + self.menu_control_signal = self + .scene_main_menu + .render_main_menu_frame(raylib, rl_thread, discord, global_resources, constants) + .await + } + MenuStateSignal::DoOptions => { + self.menu_control_signal = self + .scene_main_menu + .render_options_frame(raylib, rl_thread, discord, global_resources, constants) + .await + }, + MenuStateSignal::DoCredits => { + self.menu_control_signal = self + .scene_main_menu + .render_credits_frame(raylib, rl_thread, discord, global_resources, constants) + .await + }, + MenuStateSignal::DoLeaderboard => { + self.menu_control_signal = self + .scene_main_menu + .render_leaderboard_frame(raylib, rl_thread, discord, global_resources, constants) + .await + }, } } } From bc7fb12564ac99be0383d080474f686e7518be63 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sat, 2 Apr 2022 11:32:50 -0400 Subject: [PATCH 2/2] Fix the rest of the merge issues --- game/game_logic/src/scenes/main_menu.rs | 62 +++++++++++-------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/game/game_logic/src/scenes/main_menu.rs b/game/game_logic/src/scenes/main_menu.rs index 7c7d1825..769819e1 100644 --- a/game/game_logic/src/scenes/main_menu.rs +++ b/game/game_logic/src/scenes/main_menu.rs @@ -67,11 +67,11 @@ impl MainMenu { draw.clear_background(Color::WHITE); //I wanna see where mouseeee - let mouse_x = draw.get_mouse_x(); - let mouse_y = draw.get_mouse_y(); - - draw.draw_text(&mouse_x.to_string(), 20, 5, 20, Color::BLACK); - draw.draw_text(&mouse_y.to_string(), 70, 5, 20, Color::BLACK); + let mouse_x = draw.get_mouse_x(); + let mouse_y = draw.get_mouse_y(); + + draw.draw_text(&mouse_x.to_string(), 20, 5, 20, Color::BLACK); + draw.draw_text(&mouse_y.to_string(), 70, 5, 20, Color::BLACK); // TODO: Render stuff //Initial Option placeholder words in the main menu @@ -82,36 +82,34 @@ impl MainMenu { draw.draw_text("Leaderboard", 100, 470, 34, Color::BLACK); draw.draw_text("Exit", 100, 550, 34, Color::BLACK); - //First two are starting X and Y position, last two finishing X and Y. Made to resemble a box + //First two are starting X and Y position, last two finishing X and Y. Made to resemble a box - if mouse_x >= 100 && mouse_y >= 193 && mouse_x <= 290 && mouse_y <= 216{ - //Insides while make a lil shade for it to look cool - draw.draw_text("Start Game", 103, 191, 34, Color::GRAY); - draw.draw_text("Start Game", 100, 190, 34, Color::BLACK); - if draw.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON) { - return true; - } + if mouse_x >= 100 && mouse_y >= 193 && mouse_x <= 290 && mouse_y <= 216 { + //Insides while make a lil shade for it to look cool + draw.draw_text("Start Game", 103, 191, 34, Color::GRAY); + draw.draw_text("Start Game", 100, 190, 34, Color::BLACK); + if draw.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON) { + return MenuStateSignal::StartGame; } + } - if mouse_x >= 100 && mouse_y >= 250 && mouse_x <= 222 && mouse_y <= 275{ - draw.draw_text("Options", 103, 251, 34, Color::GRAY); - draw.draw_text("Options", 100, 250, 34, Color::BLACK); - } + if mouse_x >= 100 && mouse_y >= 250 && mouse_x <= 222 && mouse_y <= 275 { + draw.draw_text("Options", 103, 251, 34, Color::GRAY); + draw.draw_text("Options", 100, 250, 34, Color::BLACK); + } + if mouse_x >= 100 && mouse_y >= 410 && mouse_x <= 222 && mouse_y <= 437 { + draw.draw_text("Credits", 103, 411, 34, Color::GRAY); + draw.draw_text("Credits", 100, 410, 34, Color::BLACK); + } + if mouse_x >= 100 && mouse_y >= 470 && mouse_x <= 316 && mouse_y <= 496 { + draw.draw_text("Leaderboard", 103, 471, 34, Color::GRAY); + draw.draw_text("Leaderboard", 100, 470, 34, Color::BLACK); + } - if mouse_x >= 100 && mouse_y >= 410 && mouse_x <= 222 && mouse_y <= 437{ - draw.draw_text("Credits", 103, 411, 34, Color::GRAY); - draw.draw_text("Credits", 100, 410, 34, Color::BLACK); - } - if mouse_x >= 100 && mouse_y >= 470 && mouse_x <= 316 && mouse_y <= 496{ - draw.draw_text("Leaderboard", 103, 471, 34, Color::GRAY); - draw.draw_text("Leaderboard", 100, 470, 34, Color::BLACK); - } - - if mouse_x >= 100 && mouse_y >= 550 && mouse_x <= 162 && mouse_y <= 575{ - draw.draw_text("Exit", 103, 551, 34, Color::GRAY); - draw.draw_text("Exit", 100, 550, 34, Color::BLACK); - } + if mouse_x >= 100 && mouse_y >= 550 && mouse_x <= 162 && mouse_y <= 575 { + draw.draw_text("Exit", 103, 551, 34, Color::GRAY); + draw.draw_text("Exit", 100, 550, 34, Color::BLACK); } // Return MenuStateSignal::StartGame if you want the game to start. @@ -127,7 +125,6 @@ impl MainMenu { global_resources: &GlobalResources, constants: &ProjectConstants, ) -> MenuStateSignal { - return MenuStateSignal::DoOptions; } @@ -139,7 +136,6 @@ impl MainMenu { global_resources: &GlobalResources, constants: &ProjectConstants, ) -> MenuStateSignal { - return MenuStateSignal::DoCredits; } @@ -151,8 +147,6 @@ impl MainMenu { global_resources: &GlobalResources, constants: &ProjectConstants, ) -> MenuStateSignal { - return MenuStateSignal::DoLeaderboard; - } }