Merge pull request #42 from Ewpratten/win_game

Let the user win the game
This commit is contained in:
Evan Pratten 2021-10-02 21:13:36 -07:00 committed by GitHub
commit 075c0b4886
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 26 deletions

View File

@ -4,7 +4,7 @@ use raylib::prelude::*;
use crate::{ use crate::{
character::{CharacterState, MainCharacter}, character::{CharacterState, MainCharacter},
context::GameContext, context::{ControlFlag, GameContext},
utilities::{ utilities::{
render_layer::{FrameUpdate, ScreenSpaceRender, WorldSpaceRender}, render_layer::{FrameUpdate, ScreenSpaceRender, WorldSpaceRender},
world_paint_texture::WorldPaintTexture, world_paint_texture::WorldPaintTexture,
@ -117,6 +117,22 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
// Render the HUD // Render the HUD
self.render_screen_space(&mut renderer, &context.config); self.render_screen_space(&mut renderer, &context.config);
// Check if the player won
let cur_level = self.levels.get(context.current_level).unwrap();
if self.player.position.x > cur_level.zones.win.x {
// If this is the last level, win the game
if self.current_level_idx >= self.levels.len() - 1 {
return Ok(ActionFlag::SwitchState(Scenes::WinScreen));
} else {
// Otherwise, increment the level counter and switch to the next level
context
.flag_send
.send(Some(ControlFlag::SwitchLevel(self.current_level_idx + 1)))
.unwrap();
return Ok(ActionFlag::SwitchState(Scenes::NextLevelScreen));
}
}
if renderer.is_key_pressed(KeyboardKey::KEY_ESCAPE) { if renderer.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
Ok(ActionFlag::SwitchState(Scenes::PauseScreen)) Ok(ActionFlag::SwitchState(Scenes::PauseScreen))
} else if self.player_dead { } else if self.player_dead {

View File

@ -5,13 +5,17 @@ use dirty_fsm::{Action, ActionFlag};
use pkg_version::pkg_version_major; use pkg_version::pkg_version_major;
use raylib::prelude::*; use raylib::prelude::*;
use crate::{GameConfig, context::GameContext, utilities::{ use crate::{
context::GameContext,
utilities::{
datastore::{load_texture_from_internal_data, ResourceLoadError}, datastore::{load_texture_from_internal_data, ResourceLoadError},
game_version::get_version_string, game_version::get_version_string,
math::interpolate_exp, math::interpolate_exp,
non_ref_raylib::HackedRaylibHandle, non_ref_raylib::HackedRaylibHandle,
render_layer::ScreenSpaceRender, render_layer::ScreenSpaceRender,
}}; },
GameConfig,
};
use super::{Scenes, ScreenError}; use super::{Scenes, ScreenError};
use tracing::{debug, info, trace}; use tracing::{debug, info, trace};
@ -19,7 +23,7 @@ use tracing::{debug, info, trace};
#[derive(Debug)] #[derive(Debug)]
pub struct WinScreen { pub struct WinScreen {
is_menu_pressed: bool, //Is menu button pressed is_menu_pressed: bool, //Is menu button pressed
counter: i32 counter: i32,
} }
impl WinScreen { impl WinScreen {
@ -27,8 +31,7 @@ impl WinScreen {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
is_menu_pressed: false, is_menu_pressed: false,
counter: 0 counter: 0,
} }
} }
} }
@ -55,8 +58,7 @@ impl Action<Scenes, ScreenError, GameContext> for WinScreen {
if self.is_menu_pressed { if self.is_menu_pressed {
Ok(ActionFlag::SwitchState(Scenes::MainMenuScreen)) Ok(ActionFlag::SwitchState(Scenes::MainMenuScreen))
} } else {
else{
Ok(ActionFlag::Continue) Ok(ActionFlag::Continue)
} }
} }
@ -73,20 +75,25 @@ impl ScreenSpaceRender for WinScreen {
fn render_screen_space( fn render_screen_space(
&mut self, &mut self,
raylib: &mut crate::utilities::non_ref_raylib::HackedRaylibHandle, raylib: &mut crate::utilities::non_ref_raylib::HackedRaylibHandle,
config: &GameConfig config: &GameConfig,
) { ) {
let screen_size = raylib.get_screen_size();
// Render the background // Render the background
raylib.clear_background(Color::BLACK); raylib.clear_background(Color::BLACK);
raylib.draw_rectangle_lines(
let screen_size = raylib.get_screen_size(); 0,
0,
screen_size.x as i32,
screen_size.y as i32,
config.colors.white,
);
//Mouse Position //Mouse Position
let mouse_position: Vector2 = raylib.get_mouse_position(); let mouse_position: Vector2 = raylib.get_mouse_position();
let mouse_pressed: bool = raylib.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON); let mouse_pressed: bool = raylib.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON);
if self.counter > 100{
raylib.draw_rgb_split_text( raylib.draw_rgb_split_text(
Vector2::new(100.0, screen_size.y as f32 / 2.0 - 120.0), Vector2::new(100.0, screen_size.y as f32 / 2.0 - 120.0),
"congrats.", "congrats.",
@ -94,8 +101,8 @@ impl ScreenSpaceRender for WinScreen {
false, false,
Color::WHITE, Color::WHITE,
); );
}
if self.counter > 200{ if self.counter > 100 {
raylib.draw_rgb_split_text( raylib.draw_rgb_split_text(
Vector2::new(100.0, screen_size.y as f32 / 2.0 - 60.0), Vector2::new(100.0, screen_size.y as f32 / 2.0 - 60.0),
"you win.", "you win.",
@ -104,7 +111,7 @@ impl ScreenSpaceRender for WinScreen {
Color::WHITE, Color::WHITE,
); );
} }
if self.counter > 400{ if self.counter > 300 {
raylib.draw_rgb_split_text( raylib.draw_rgb_split_text(
Vector2::new(100.0, screen_size.y as f32 / 2.0), Vector2::new(100.0, screen_size.y as f32 / 2.0),
"yay.", "yay.",
@ -115,8 +122,10 @@ impl ScreenSpaceRender for WinScreen {
}; };
//Return to Main Menu //Return to Main Menu
if self.counter > 550{ if self.counter > 450 {
if Rectangle::new(100.0, screen_size.y as f32 / 2.0 + 90.0, 270.0, 20.0).check_collision_point_rec(mouse_position){ if Rectangle::new(100.0, screen_size.y as f32 / 2.0 + 90.0, 270.0, 20.0)
.check_collision_point_rec(mouse_position)
{
raylib.draw_rgb_split_text( raylib.draw_rgb_split_text(
Vector2::new(100.0, screen_size.y as f32 / 2.0 + 100.0), Vector2::new(100.0, screen_size.y as f32 / 2.0 + 100.0),
">> RETURN TO MAIN MENU", ">> RETURN TO MAIN MENU",
@ -126,8 +135,7 @@ impl ScreenSpaceRender for WinScreen {
); );
self.is_menu_pressed = mouse_pressed self.is_menu_pressed = mouse_pressed
} } else {
else {
raylib.draw_rgb_split_text( raylib.draw_rgb_split_text(
Vector2::new(100.0, screen_size.y as f32 / 2.0 + 100.0), Vector2::new(100.0, screen_size.y as f32 / 2.0 + 100.0),
">> RETURN TO MAIN MENU", ">> RETURN TO MAIN MENU",