Merge pull request #16 from Ewpratten/ewpratten/main_menu_signaling

Menu routing system
This commit is contained in:
Evan Pratten 2022-04-02 11:33:02 -04:00 committed by GitHub
commit 424d64c75d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 121 additions and 53 deletions

View File

@ -1,6 +1,7 @@
{ {
"git.detectSubmodules": false, "git.detectSubmodules": false,
"cSpell.words": [ "cSpell.words": [
"leaderboard",
"msaa", "msaa",
"raylib", "raylib",
"repr", "repr",

View File

@ -1,7 +1,10 @@
//! This scene encompasses the main menu system //! This scene encompasses the main menu system
use nalgebra as na; use nalgebra as na;
use raylib::{prelude::*, ffi::{Texture, GetMouseX, GetMouseY, IsMouseButtonDown}}; use raylib::{
ffi::{GetMouseX, GetMouseY, IsMouseButtonDown, Texture},
prelude::*,
};
use crate::{ use crate::{
discord::{DiscordChannel, DiscordRpcSignal}, discord::{DiscordChannel, DiscordRpcSignal},
@ -9,6 +12,16 @@ use crate::{
project_constants::ProjectConstants, project_constants::ProjectConstants,
}; };
#[derive(Debug, Clone)]
pub enum MenuStateSignal {
StartGame,
QuitGame,
DoMainMenu,
DoOptions,
DoCredits,
DoLeaderboard,
}
#[derive(Debug)] #[derive(Debug)]
pub struct MainMenu { pub struct MainMenu {
has_updated_discord_rpc: bool, has_updated_discord_rpc: bool,
@ -26,21 +39,17 @@ impl MainMenu {
} }
} }
/// Handler for each frame pub async fn render_main_menu_frame(
pub async fn render_frame(
&mut self, &mut self,
raylib: &mut RaylibHandle, raylib: &mut RaylibHandle,
rl_thread: &RaylibThread, rl_thread: &RaylibThread,
discord: &DiscordChannel, discord: &DiscordChannel,
global_resources: &GlobalResources, global_resources: &GlobalResources,
constants: &ProjectConstants, constants: &ProjectConstants,
) -> bool { ) -> MenuStateSignal {
// Handle updating discord RPC // Handle updating discord RPC
if !self.has_updated_discord_rpc { if !self.has_updated_discord_rpc {
discord discord.send(DiscordRpcSignal::EndGameTimer).await.unwrap();
.send(DiscordRpcSignal::EndGameTimer)
.await
.unwrap();
discord discord
.send(DiscordRpcSignal::ChangeDetails { .send(DiscordRpcSignal::ChangeDetails {
details: "Looking at a menu".to_string(), details: "Looking at a menu".to_string(),
@ -58,11 +67,11 @@ impl MainMenu {
draw.clear_background(Color::WHITE); draw.clear_background(Color::WHITE);
//I wanna see where mouseeee //I wanna see where mouseeee
let mouse_x = draw.get_mouse_x(); let mouse_x = draw.get_mouse_x();
let mouse_y = draw.get_mouse_y(); let mouse_y = draw.get_mouse_y();
draw.draw_text(&mouse_x.to_string(), 20, 5, 20, Color::BLACK); draw.draw_text(&mouse_x.to_string(), 20, 5, 20, Color::BLACK);
draw.draw_text(&mouse_y.to_string(), 70, 5, 20, Color::BLACK); draw.draw_text(&mouse_y.to_string(), 70, 5, 20, Color::BLACK);
// TODO: Render stuff // TODO: Render stuff
//Initial Option placeholder words in the main menu //Initial Option placeholder words in the main menu
@ -73,40 +82,71 @@ impl MainMenu {
draw.draw_text("Leaderboard", 100, 470, 34, Color::BLACK); draw.draw_text("Leaderboard", 100, 470, 34, Color::BLACK);
draw.draw_text("Exit", 100, 550, 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 if mouse_x >= 100 && mouse_y >= 193 && mouse_x <= 290 && mouse_y <= 216 {
draw.draw_text("Start Game", 103, 191, 34, Color::GRAY); //Insides while make a lil shade for it to look cool
draw.draw_text("Start Game", 100, 190, 34, Color::BLACK); draw.draw_text("Start Game", 103, 191, 34, Color::GRAY);
if draw.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON) { draw.draw_text("Start Game", 100, 190, 34, Color::BLACK);
return true; 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{ 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", 103, 251, 34, Color::GRAY);
draw.draw_text("Options", 100, 250, 34, Color::BLACK); 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{ if mouse_x >= 100 && mouse_y >= 550 && mouse_x <= 162 && mouse_y <= 575 {
draw.draw_text("Credits", 103, 411, 34, Color::GRAY); draw.draw_text("Exit", 103, 551, 34, Color::GRAY);
draw.draw_text("Credits", 100, 410, 34, Color::BLACK); draw.draw_text("Exit", 100, 550, 34, Color::BLACK);
}
} // Return MenuStateSignal::StartGame if you want the game to start.
if mouse_x >= 100 && mouse_y >= 470 && mouse_x <= 316 && mouse_y <= 496{ // Otherwise, keep returning MenuStateSignal::DoMainMenu until the player clicks the start button
draw.draw_text("Leaderboard", 103, 471, 34, Color::GRAY); return MenuStateSignal::DoMainMenu;
draw.draw_text("Leaderboard", 100, 470, 34, Color::BLACK); }
} pub async fn render_options_frame(
if mouse_x >= 100 && mouse_y >= 550 && mouse_x <= 162 && mouse_y <= 575{ &mut self,
draw.draw_text("Exit", 103, 551, 34, Color::GRAY); raylib: &mut RaylibHandle,
draw.draw_text("Exit", 100, 550, 34, Color::BLACK); 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;
}
// Return true if you want the game to start. pub async fn render_leaderboard_frame(
// Otherwise, keep returning false until the player clicks the start button &mut self,
return false; raylib: &mut RaylibHandle,
rl_thread: &RaylibThread,
discord: &DiscordChannel,
global_resources: &GlobalResources,
constants: &ProjectConstants,
) -> MenuStateSignal {
return MenuStateSignal::DoLeaderboard;
} }
} }

View File

@ -10,19 +10,23 @@ use crate::{
project_constants::ProjectConstants, 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 player_interaction;
mod test_fox; mod test_fox;
mod main_menu;
/// Delegate for handling rendering. /// Delegate for handling rendering.
/// This is a struct to allow for stateful data (like sub-screens) to be set up /// This is a struct to allow for stateful data (like sub-screens) to be set up
pub struct SceneRenderDelegate { pub struct SceneRenderDelegate {
is_in_main_menu: bool, menu_control_signal: MenuStateSignal,
/* Scenes */ /* Scenes */
scene_test_fox: TestFoxScene, scene_test_fox: TestFoxScene,
scene_playable: PlayableScene, scene_playable: PlayableScene,
scene_main_menu: MainMenu scene_main_menu: MainMenu,
} }
impl SceneRenderDelegate { impl SceneRenderDelegate {
@ -40,7 +44,7 @@ impl SceneRenderDelegate {
let scene_main_menu = MainMenu::new(raylib, rl_thread, constants); let scene_main_menu = MainMenu::new(raylib, rl_thread, constants);
Self { Self {
is_in_main_menu: true, menu_control_signal: MenuStateSignal::DoMainMenu,
scene_test_fox, scene_test_fox,
scene_playable, scene_playable,
scene_main_menu, scene_main_menu,
@ -59,14 +63,37 @@ impl SceneRenderDelegate {
constants: &ProjectConstants, constants: &ProjectConstants,
) { ) {
// Render the main menu if in it, otherwise, render the game // Render the main menu if in it, otherwise, render the game
if self.is_in_main_menu { match self.menu_control_signal {
self.is_in_main_menu = !self.scene_main_menu MenuStateSignal::StartGame => {
.render_frame(raylib, rl_thread, discord, global_resources, constants) self.scene_playable
.await; .render_frame(raylib, rl_thread, &discord, global_resources, constants)
}else { .await;
self.scene_playable }
.render_frame(raylib, rl_thread, &discord, global_resources, constants) MenuStateSignal::QuitGame => unimplemented!(),
.await; 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
},
} }
} }
} }