diff --git a/game/assets/levels/levels.json b/game/assets/levels/levels.json index 768078c..626517d 100644 --- a/game/assets/levels/levels.json +++ b/game/assets/levels/levels.json @@ -1,3 +1,4 @@ [ + "level_0", "level_1" ] diff --git a/game/src/context.rs b/game/src/context.rs index 6612f52..422e72c 100644 --- a/game/src/context.rs +++ b/game/src/context.rs @@ -4,7 +4,11 @@ use chrono::{DateTime, Duration, Utc}; use discord_sdk::activity::ActivityBuilder; use raylib::audio::Sound; -use crate::{GameConfig, progress::ProgressData, utilities::{audio_player::AudioPlayer, non_ref_raylib::HackedRaylibHandle}}; +use crate::{ + progress::ProgressData, + utilities::{audio_player::AudioPlayer, non_ref_raylib::HackedRaylibHandle}, + GameConfig, +}; #[derive(Debug)] pub enum ControlFlag { @@ -13,7 +17,7 @@ pub enum ControlFlag { UpdateLevelStart(DateTime), SaveProgress, MaybeUpdateHighScore(usize, Duration), - SoundTrigger(String) + SoundTrigger(String), } #[derive(Debug)] @@ -24,6 +28,7 @@ pub struct GameContext { pub config: GameConfig, pub player_progress: ProgressData, pub current_level: usize, + pub total_levels: usize, pub level_start_time: DateTime, pub discord_rpc_send: Sender>, pub flag_send: Sender>, diff --git a/game/src/lib.rs b/game/src/lib.rs index b768fee..36c5d47 100644 --- a/game/src/lib.rs +++ b/game/src/lib.rs @@ -82,7 +82,7 @@ use crate::{ context::GameContext, discord_rpc::{maybe_set_discord_presence, try_connect_to_local_discord}, progress::ProgressData, - scenes::{build_screen_state_machine, Scenes}, + scenes::{build_screen_state_machine, ingame_scene::level::loader::load_all_levels, Scenes}, utilities::{ audio_player::AudioPlayer, datastore::{load_music_from_internal_data, load_sound_from_internal_data}, @@ -191,6 +191,7 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box Result<(), Box Result<(), Box { - context.audio.play_sound( - context.sounds.get(&name).unwrap(), - ); + context.audio.play_sound(context.sounds.get(&name).unwrap()); } } } diff --git a/game/src/scenes/level_select_screen.rs b/game/src/scenes/level_select_screen.rs new file mode 100644 index 0000000..029d8a3 --- /dev/null +++ b/game/src/scenes/level_select_screen.rs @@ -0,0 +1,182 @@ +use std::ops::{Div, Sub}; + +use chrono::{DateTime, Utc}; +use dirty_fsm::{Action, ActionFlag}; +use discord_sdk::activity::{ActivityBuilder, Assets}; +use pkg_version::pkg_version_major; +use raylib::prelude::*; + +use crate::{ + context::{ControlFlag, GameContext}, + utilities::{ + datastore::{load_texture_from_internal_data, ResourceLoadError}, + game_version::get_version_string, + math::interpolate_exp, + non_ref_raylib::HackedRaylibHandle, + render_layer::ScreenSpaceRender, + }, + GameConfig, +}; + +use super::{Scenes, ScreenError}; +use tracing::{debug, error, info, trace}; + +#[derive(Debug)] +pub struct LevelSelectScreen { + is_btm_pressed: bool, + selected_level: Option, + visible_levels: usize, +} + +impl LevelSelectScreen { + /// Construct a new `LevelSelectScreen` + pub fn new() -> Self { + Self { + is_btm_pressed: false, + selected_level: None, + visible_levels: 0, + } + } +} + +impl Action for LevelSelectScreen { + fn on_register(&mut self) -> Result<(), ScreenError> { + debug!("Registered"); + Ok(()) + } + + fn on_first_run(&mut self, context: &GameContext) -> Result<(), ScreenError> { + debug!("Running LevelSelectScreen for the first time"); + + if let Err(e) = context.discord_rpc_send.send(Some( + ActivityBuilder::default() + .details("learning how to play") + .assets( + Assets::default().large("game-logo-small", Some(context.config.name.clone())), + ), + )) { + error!("Failed to update discord: {}", e); + } + + // Calculate the number of levels to render + self.visible_levels = + (context.player_progress.level_best_times.len() + 1).min(context.total_levels); + + Ok(()) + } + + fn execute( + &mut self, + _delta: &chrono::Duration, + context: &GameContext, + ) -> Result, ScreenError> { + trace!("execute() called on LevelSelectScreen"); + self.render_screen_space(&mut context.renderer.borrow_mut(), &context.config); + + if let Some(level) = self.selected_level { + // Play the sound + context + .flag_send + .send(Some(ControlFlag::SoundTrigger("button-press".to_string()))) + .unwrap(); + + // Switch the level + context + .flag_send + .send(Some(ControlFlag::SwitchLevel(level))) + .unwrap(); + + // Enter the game + Ok(ActionFlag::SwitchState(Scenes::InGameScene)) + } else if self.is_btm_pressed { + context + .flag_send + .send(Some(ControlFlag::SoundTrigger("button-press".to_string()))) + .unwrap(); + Ok(ActionFlag::SwitchState(Scenes::MainMenuScreen)) + } else { + Ok(ActionFlag::Continue) + } + } + + fn on_finish(&mut self, _interrupted: bool) -> Result<(), ScreenError> { + debug!("Finished LevelSelectScreen"); + self.selected_level = None; + self.is_btm_pressed = false; + Ok(()) + } +} + +impl ScreenSpaceRender for LevelSelectScreen { + fn render_screen_space( + &mut self, + raylib: &mut crate::utilities::non_ref_raylib::HackedRaylibHandle, + config: &GameConfig, + ) { + let screen_size = raylib.get_screen_size(); + + // Render the background + raylib.clear_background(Color::BLACK); + raylib.draw_rectangle_lines( + 0, + 0, + screen_size.x as i32, + screen_size.y as i32, + config.colors.white, + ); + + let screen_size = raylib.get_screen_size(); + + //Mouse Position + let mouse_position: Vector2 = raylib.get_mouse_position(); + + let mouse_pressed: bool = raylib.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON); + + //Render the title + raylib.draw_rgb_split_text( + Vector2::new(40.0, 80.0), + "Level Select", + 70, + true, + Color::WHITE, + ); + + // Render the levels + for level in 0..self.visible_levels { + let hovering_button = + Rectangle::new(100.0, 300.0 + (25.0 * level as f32), 180.0, 25.0 ).check_collision_point_rec(mouse_position); + raylib.draw_rgb_split_text( + Vector2::new(100.0, 300.0+ (25.0 * level as f32)), + &format!("LEVEL {}", level), + 25, + hovering_button, + Color::WHITE, + ); + if hovering_button { + raylib.draw_rgb_split_text( + Vector2::new(70.0, 300.0+ (25.0 * level as f32)), + ">>", + 25, + hovering_button, + Color::WHITE, + ); + }; + if mouse_pressed && hovering_button { + self.selected_level = Some(level); + break; + } + } + + //Back to Menu + let hovering_back_button = Rectangle::new(35.0, screen_size.y as f32 - 80.0, 200.0, 40.0) + .check_collision_point_rec(mouse_position); + raylib.draw_rgb_split_text( + Vector2::new(25.0, screen_size.y - 50.0), + "BACK TO MENU", + 25, + hovering_back_button, + Color::WHITE, + ); + self.is_btm_pressed = hovering_back_button && mouse_pressed; + } +} diff --git a/game/src/scenes/main_menu_screen.rs b/game/src/scenes/main_menu_screen.rs index 389dfb9..4092405 100644 --- a/game/src/scenes/main_menu_screen.rs +++ b/game/src/scenes/main_menu_screen.rs @@ -83,7 +83,7 @@ impl Action for MainMenuScreen { .flag_send .send(Some(ControlFlag::SoundTrigger("button-press".to_string()))) .unwrap(); - Ok(ActionFlag::SwitchState(Scenes::InGameScene)) + Ok(ActionFlag::SwitchState(Scenes::LevelSelectScreen)) } else if self.is_htp_pressed { context .flag_send @@ -271,6 +271,8 @@ impl ScreenSpaceRender for MainMenuScreen { }; self.is_options_pressed = mouse_pressed && hovering_options; + + // CREDITS let hovering_credits = Rectangle::new(80.0, 445.0, 135.0, 20.0).check_collision_point_rec(mouse_position); diff --git a/game/src/scenes/mod.rs b/game/src/scenes/mod.rs index 1143503..442fd2d 100644 --- a/game/src/scenes/mod.rs +++ b/game/src/scenes/mod.rs @@ -1,11 +1,4 @@ -use self::{ - pause_screen::PauseScreen, - fsm_error_screen::FsmErrorScreen, - ingame_scene::{level::loader::load_all_levels, InGameScreen}, - main_menu_screen::MainMenuScreen, options_screen::OptionsScreen, how_to_play_screen::HowToPlayScreen, - death_screen::DeathScreen, win_screen::WinScreen, - next_level_screen::NextLevelScreen -}; +use self::{death_screen::DeathScreen, fsm_error_screen::FsmErrorScreen, how_to_play_screen::HowToPlayScreen, ingame_scene::{InGameScreen, level::{Level, loader::load_all_levels}}, level_select_screen::LevelSelectScreen, main_menu_screen::MainMenuScreen, next_level_screen::NextLevelScreen, options_screen::OptionsScreen, pause_screen::PauseScreen, win_screen::WinScreen}; use crate::{context::GameContext, utilities::{datastore::{ResourceLoadError, load_music_from_internal_data, load_sound_from_internal_data, load_texture_from_internal_data}, non_ref_raylib::HackedRaylibHandle}}; use dirty_fsm::StateMachine; use raylib::{texture::Texture2D, RaylibThread}; @@ -19,6 +12,7 @@ pub mod pause_screen; pub mod death_screen; pub mod win_screen; pub mod next_level_screen; +pub mod level_select_screen; /// Defines all scenes #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)] @@ -33,6 +27,7 @@ pub enum Scenes { DeathScreen, WinScreen, NextLevelScreen, + LevelSelectScreen, } /// Contains any possible errors thrown while rendering @@ -46,6 +41,7 @@ pub enum ScreenError { pub fn build_screen_state_machine( raylib_handle: &mut HackedRaylibHandle, thread: &RaylibThread, + levels: Vec ) -> Result< // StateMachine>)>>, StateMachine, @@ -56,7 +52,6 @@ pub fn build_screen_state_machine( load_texture_from_internal_data(raylib_handle, thread, "character/player_run.png").unwrap(); let world_background = load_texture_from_internal_data(raylib_handle, thread, "default-texture.png").unwrap(); - let levels = load_all_levels(raylib_handle, thread).unwrap(); // Set up the state machine let mut machine = StateMachine::new(); @@ -72,6 +67,7 @@ pub fn build_screen_state_machine( machine.add_action(Scenes::DeathScreen, DeathScreen::new())?; machine.add_action(Scenes::WinScreen, WinScreen::new())?; machine.add_action(Scenes::NextLevelScreen, NextLevelScreen::new())?; + machine.add_action(Scenes::LevelSelectScreen, LevelSelectScreen::new())?; Ok(machine) } diff --git a/game/src/utilities/mod.rs b/game/src/utilities/mod.rs index 666e389..a1965ea 100644 --- a/game/src/utilities/mod.rs +++ b/game/src/utilities/mod.rs @@ -9,3 +9,4 @@ pub mod render_layer; pub mod shaders; pub mod world_paint_texture; pub mod audio_player; +pub mod reusable_button; diff --git a/game/src/utilities/reusable_button.rs b/game/src/utilities/reusable_button.rs new file mode 100644 index 0000000..e370d37 --- /dev/null +++ b/game/src/utilities/reusable_button.rs @@ -0,0 +1,20 @@ +// use raylib::prelude::*; + + +// #[derive(Debug)] +// pub struct ReusableButton { +// text: String, +// position: Vector2, +// font_size: f32, +// font_color: Color, +// arrow_on_hover: bool +// } + +// impl ReusableButton { +// /// Construct a new reusable button. +// pub fn new() -> Self { +// Self { + +// } +// } +// }