make level select extendable

This commit is contained in:
Evan Pratten 2021-10-03 16:14:36 -04:00
parent 109b04fcb9
commit 30e900dcfd
6 changed files with 84 additions and 108 deletions

View File

@ -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<Utc>),
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<Utc>,
pub discord_rpc_send: Sender<Option<ActivityBuilder>>,
pub flag_send: Sender<Option<ControlFlag>>,

View File

@ -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<dyn std:
config: game_config.clone(),
audio: audio_system,
sounds,
total_levels: 0,
current_level: 0,
player_progress: save_file,
level_start_time: Utc::now(),
@ -210,10 +211,14 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
// Start the song
context.audio.play_music_stream(&mut main_song);
// Load all levels
let levels = load_all_levels(&mut context.renderer.borrow_mut(), &raylib_thread).unwrap();
context.total_levels = levels.len();
// Get the main state machine
info!("Setting up the scene management state machine");
let mut game_state_machine =
build_screen_state_machine(&mut context.renderer.borrow_mut(), &raylib_thread).unwrap();
build_screen_state_machine(&mut context.renderer.borrow_mut(), &raylib_thread, levels).unwrap();
game_state_machine
.force_change_state(Scenes::MainMenuScreen)
.unwrap();
@ -374,9 +379,7 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
.maybe_write_new_time(level, &time);
}
context::ControlFlag::SoundTrigger(name) => {
context.audio.play_sound(
context.sounds.get(&name).unwrap(),
);
context.audio.play_sound(context.sounds.get(&name).unwrap());
}
}
}

View File

@ -6,29 +6,35 @@ use discord_sdk::activity::{ActivityBuilder, Assets};
use pkg_version::pkg_version_major;
use raylib::prelude::*;
use crate::{GameConfig, context::{ControlFlag, GameContext}, utilities::{
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_level_one_pressed: bool, //Is back to menu button pressed
is_btm_pressed: bool,
selected_level: Option<usize>,
visible_levels: usize,
}
impl LevelSelectScreen {
/// Construct a new `LevelSelectScreen`
pub fn new() -> Self {
Self {
is_level_one_pressed: false,
is_btm_pressed: false,
selected_level: None,
visible_levels: 0,
}
}
}
@ -52,6 +58,10 @@ impl Action<Scenes, ScreenError, GameContext> for LevelSelectScreen {
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(())
}
@ -63,14 +73,22 @@ impl Action<Scenes, ScreenError, GameContext> for LevelSelectScreen {
trace!("execute() called on LevelSelectScreen");
self.render_screen_space(&mut context.renderer.borrow_mut(), &context.config);
if self.is_level_one_pressed {
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 {
} else if self.is_btm_pressed {
context
.flag_send
.send(Some(ControlFlag::SoundTrigger("button-press".to_string())))
@ -83,7 +101,7 @@ impl Action<Scenes, ScreenError, GameContext> for LevelSelectScreen {
fn on_finish(&mut self, _interrupted: bool) -> Result<(), ScreenError> {
debug!("Finished LevelSelectScreen");
self.is_level_one_pressed = false;
self.selected_level = None;
self.is_btm_pressed = false;
Ok(())
}
@ -124,101 +142,30 @@ impl ScreenSpaceRender for LevelSelectScreen {
);
// Render the levels
let hovering_level_one_button = Rectangle::new(100.0, 300.0, 180.0, 20.0)
.check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
Vector2::new(100.0, 300.0),
"LEVEL ONE",
25,
hovering_level_one_button,
Color::WHITE,
);
if hovering_level_one_button {
for level in 0..self.visible_levels {
let hovering_button =
Rectangle::new(100.0, 300.0, 180.0, 20.0).check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
Vector2::new(70.0, 300.0),
">>",
Vector2::new(100.0, 300.0),
&format!("LEVEL {}", level),
25,
hovering_level_one_button,
hovering_button,
Color::WHITE,
);
};
self.is_level_one_pressed = mouse_pressed && hovering_level_one_button;
let hovering_level_two_button = Rectangle::new(100.0, 350.0, 180.0, 20.0)
.check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
Vector2::new(100.0, 350.0),
"LEVEL TWO",
25,
hovering_level_two_button,
Color::WHITE,
);
if hovering_level_two_button {
raylib.draw_rgb_split_text(
Vector2::new(70.0, 350.0),
">>",
25,
hovering_level_two_button,
Color::WHITE,
);
};
let hovering_level_three_button = Rectangle::new(100.0, 400.0, 210.0, 20.0)
.check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
Vector2::new(100.0, 400.0),
"LEVEL THREE",
25,
hovering_level_three_button,
Color::WHITE,
);
if hovering_level_three_button {
raylib.draw_rgb_split_text(
Vector2::new(70.0, 400.0),
">>",
25,
hovering_level_three_button,
Color::WHITE,
);
};
let hovering_level_four_button = Rectangle::new(100.0, 450.0, 200.0, 20.0)
.check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
Vector2::new(100.0, 450.0),
"LEVEL FOUR",
25,
hovering_level_four_button,
Color::WHITE,
);
if hovering_level_four_button {
raylib.draw_rgb_split_text(
Vector2::new(70.0, 450.0),
">>",
25,
hovering_level_four_button,
Color::WHITE,
);
};
let hovering_level_five_button = Rectangle::new(100.0, 500.0, 200.0, 20.0)
.check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
Vector2::new(100.0, 500.0),
"LEVEL FIVE",
25,
hovering_level_five_button,
Color::WHITE,
);
if hovering_level_five_button {
raylib.draw_rgb_split_text(
Vector2::new(70.0, 500.0),
">>",
25,
hovering_level_five_button,
Color::WHITE,
);
};
if hovering_button {
raylib.draw_rgb_split_text(
Vector2::new(70.0, 300.0),
">>",
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)

View File

@ -1,4 +1,4 @@
use self::{death_screen::DeathScreen, fsm_error_screen::FsmErrorScreen, how_to_play_screen::HowToPlayScreen, ingame_scene::{level::loader::load_all_levels, InGameScreen}, level_select_screen::LevelSelectScreen, main_menu_screen::MainMenuScreen, next_level_screen::NextLevelScreen, options_screen::OptionsScreen, pause_screen::PauseScreen, win_screen::WinScreen};
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};
@ -41,6 +41,7 @@ pub enum ScreenError {
pub fn build_screen_state_machine(
raylib_handle: &mut HackedRaylibHandle,
thread: &RaylibThread,
levels: Vec<Level>
) -> Result<
// StateMachine<Scenes, ScreenError, RefCell<(NonRefDrawHandle, Rc<RefCell<GameContext>>)>>,
StateMachine<Scenes, ScreenError, GameContext>,
@ -51,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();

View File

@ -9,3 +9,4 @@ pub mod render_layer;
pub mod shaders;
pub mod world_paint_texture;
pub mod audio_player;
pub mod reusable_button;

View File

@ -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 {
// }
// }
// }