Merge remote-tracking branch 'origin/master' into rsninja722/collisions

This commit is contained in:
rsninja722 2022-04-03 17:11:44 -04:00
commit 637750a689
5 changed files with 169 additions and 29 deletions

@ -28,7 +28,7 @@ pub async fn handle_graphics_blocking<ConfigBuilder>(
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<ConfigBuilder>(
.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<ConfigBuilder>(
_ => 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!();
}

@ -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,12 +22,14 @@ pub enum MenuStateSignal {
DoOptions,
DoCredits,
DoLeaderboard,
DoPauseMenu,
}
#[derive(Debug)]
pub struct MainMenu {
pub has_updated_discord_rpc: bool,
volume_percentage: f32,
show_debug_info: bool,
}
impl MainMenu {
@ -35,11 +38,12 @@ 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,
volume_percentage: game_settings.volume.unwrap_or(0.5),
show_debug_info: false,
}
}
@ -51,7 +55,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 {
@ -76,9 +80,21 @@ impl MainMenu {
let mouse_x = draw.get_mouse_x();
let mouse_y = draw.get_mouse_y();
//TODO Errase later
draw.draw_text(&mouse_x.to_string(), 20, 5, 20, Color::BLACK);
draw.draw_text(&mouse_y.to_string(), 70, 5, 20, Color::BLACK);
// Optionally display debug info
if draw.is_key_pressed(KeyboardKey::KEY_F3) {
self.show_debug_info = !self.show_debug_info;
}
if self.show_debug_info {
// Draw FPS and mouse location
draw.draw_fps(10, 10);
draw.draw_text(
format!("Mouse position: ({}, {})", mouse_x, mouse_y).as_str(),
10,
30,
20,
Color::GREEN,
);
}
//Screen Size
let window_height = draw.get_screen_height();
@ -90,7 +106,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);

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

@ -0,0 +1,86 @@
//! 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 {
show_debug_info: bool,
}
impl PauseMenu {
/// Construct a new `PauseMenu`
pub fn new(
raylib_handle: &mut RaylibHandle,
thread: &RaylibThread,
constants: &ProjectConstants,
game_settings: &mut PersistentGameSettings,
) -> Self {
Self {
show_debug_info: false,
}
}
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);
//Obtain mouse position
let mouse_x = draw.get_mouse_x();
let mouse_y = draw.get_mouse_y();
// Optionally display debug info
if draw.is_key_pressed(KeyboardKey::KEY_F3) {
self.show_debug_info = !self.show_debug_info;
}
if self.show_debug_info {
// Draw FPS and mouse location
draw.draw_fps(10, 10);
draw.draw_text(
format!("Mouse position: ({}, {})", mouse_x, mouse_y).as_str(),
10,
30,
20,
Color::GREEN,
);
}
// 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;
}
}

@ -16,6 +16,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,
@ -25,6 +27,7 @@ pub struct PlayableScene {
last_update: SystemTime,
game_soundtrack: Music,
world_colliders: Vec<WorldSpaceObjectCollider>,
show_debug_info: bool,
}
impl PlayableScene {
@ -63,6 +66,7 @@ impl PlayableScene {
last_update: SystemTime::UNIX_EPOCH,
game_soundtrack,
world_colliders,
show_debug_info: false,
}
}
@ -75,7 +79,7 @@ impl PlayableScene {
global_resources: &GlobalResources,
constants: &ProjectConstants,
audio_subsystem: &mut RaylibAudio,
) {
) -> MenuStateSignal {
// Handle updating discord RPC
if !self.has_updated_discord_rpc {
discord
@ -109,6 +113,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) {
@ -117,7 +128,7 @@ impl PlayableScene {
// Render the map
self.world_map
.render_map(&mut ctx2d, &self.camera, true, self.player.position);
.render_map(&mut ctx2d, &self.camera, self.show_debug_info, self.player.position);
let player_size =
(constants.tile_size as f32 * constants.player.start_size * self.player.size) as i32;
@ -132,6 +143,26 @@ impl PlayableScene {
}
pub fn draw_ui(&mut self, draw: &mut RaylibDrawHandle, constants: &ProjectConstants) {
// Obtain mouse position
let mouse_x = draw.get_mouse_x();
let mouse_y = draw.get_mouse_y();
// Optionally display debug info
if draw.is_key_pressed(KeyboardKey::KEY_F3) {
self.show_debug_info = !self.show_debug_info;
}
if self.show_debug_info {
// Draw FPS and mouse location
draw.draw_fps(10, 10);
draw.draw_text(
format!("Mouse position: ({}, {})", mouse_x, mouse_y).as_str(),
10,
30,
20,
Color::GREEN,
);
}
draw.draw_rectangle(draw.get_screen_width() / 2 - 225, 0, 450, 40, Color::WHITE);
draw.draw_text(
"Unregistered HyperCam 2",