Add death

This commit is contained in:
Evan Pratten 2021-10-02 19:11:32 -04:00
parent 166428ae88
commit 3bc21a4a3a
4 changed files with 57 additions and 16 deletions

View File

@ -8,6 +8,7 @@ use super::{CharacterState, MainCharacter};
pub const GRAVITY_PPS: f32 = 2.0; pub const GRAVITY_PPS: f32 = 2.0;
#[must_use]
pub fn modify_player_based_on_forces( pub fn modify_player_based_on_forces(
player: &mut MainCharacter, player: &mut MainCharacter,
colliders: &Vec<Rectangle>, colliders: &Vec<Rectangle>,
@ -56,16 +57,18 @@ pub fn modify_player_based_on_forces(
if player.current_state == CharacterState::Jumping if player.current_state == CharacterState::Jumping
|| player.current_state == CharacterState::Dashing || player.current_state == CharacterState::Dashing
{ {
player.update_player( return player.update_player(
Some(CharacterState::Running), Some(CharacterState::Running),
colliders, colliders,
level_height_offset, level_height_offset,
); );
return Ok(());
} }
} }
// Check sideways collisions // Check sideways collisions
if player.velocity.y == 0.0 && check_player_colliding_with_colliders(){
return Err(());
}
// Finally apply the velocity to the player // Finally apply the velocity to the player
player.position += player.velocity; player.position += player.velocity;

View File

@ -38,7 +38,7 @@ impl MainCharacter {
movement_force: Vector2::zero(), movement_force: Vector2::zero(),
velocity: Vector2::zero(), velocity: Vector2::zero(),
base_velocity: Vector2::new(0.0, GRAVITY_PPS), base_velocity: Vector2::new(0.0, GRAVITY_PPS),
size: Vector2::new(100.0, 100.0), size: Vector2::new(80.0, 100.0),
sprite_sheet: AnimatedSpriteSheet::new( sprite_sheet: AnimatedSpriteSheet::new(
sprite_sheet, sprite_sheet,
Vector2::new(300.0, 300.0), Vector2::new(300.0, 300.0),
@ -51,12 +51,13 @@ impl MainCharacter {
} }
} }
#[must_use]
pub fn update_player( pub fn update_player(
&mut self, &mut self,
state: Option<CharacterState>, state: Option<CharacterState>,
colliders: &Vec<Rectangle>, colliders: &Vec<Rectangle>,
level_height_offset: f32, level_height_offset: f32,
) { ) -> Result<(), ()> {
if let Some(state) = state { if let Some(state) = state {
// Update the internal state // Update the internal state
if state != self.current_state { if state != self.current_state {
@ -73,6 +74,14 @@ impl MainCharacter {
} }
// Update the player based on the new velocity // Update the player based on the new velocity
modify_player_based_on_forces(self, colliders, level_height_offset).unwrap(); modify_player_based_on_forces(self, colliders, level_height_offset)
}
pub fn reset(&mut self) {
self.position = Vector2::new(0.0, 0.0);
self.velocity = Vector2::zero();
self.movement_force = Vector2::zero();
self.current_state = CharacterState::default();
self.state_set_timestamp = Utc::now();
} }
} }

View File

@ -28,6 +28,7 @@ pub struct InGameScreen {
world_background: WorldPaintTexture, world_background: WorldPaintTexture,
levels: Vec<Level>, levels: Vec<Level>,
current_level_idx: usize, current_level_idx: usize,
player_dead: bool,
} }
impl InGameScreen { impl InGameScreen {
@ -48,6 +49,7 @@ impl InGameScreen {
world_background: WorldPaintTexture::new(background_texture), world_background: WorldPaintTexture::new(background_texture),
levels, levels,
current_level_idx: 0, current_level_idx: 0,
player_dead: false,
} }
} }
} }
@ -63,7 +65,7 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
// Set the player to running // Set the player to running
let cur_level = self.levels.get(self.current_level_idx).unwrap(); let cur_level = self.levels.get(self.current_level_idx).unwrap();
self.player.update_player( let _ = self.player.update_player(
Some(CharacterState::Running), Some(CharacterState::Running),
&cur_level.colliders, &cur_level.colliders,
-cur_level.platform_tex.height as f32, -cur_level.platform_tex.height as f32,
@ -112,6 +114,10 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
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 {
// TODO: (luna) make this switch to the death screen plz
Ok(ActionFlag::SwitchState(Scenes::FsmErrorScreen))
} else { } else {
Ok(ActionFlag::Continue) Ok(ActionFlag::Continue)
} }
@ -119,6 +125,13 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
fn on_finish(&mut self, _interrupted: bool) -> Result<(), ScreenError> { fn on_finish(&mut self, _interrupted: bool) -> Result<(), ScreenError> {
debug!("Finished InGameScreen"); debug!("Finished InGameScreen");
// Handle resetting if the player dies
if self.player_dead {
self.player_dead = false;
self.player.reset();
}
Ok(()) Ok(())
} }
} }

View File

@ -21,7 +21,6 @@ impl FrameUpdate for InGameScreen {
// Get the current level // Get the current level
let cur_level = self.levels.get(self.current_level_idx).unwrap(); let cur_level = self.levels.get(self.current_level_idx).unwrap();
// Set the camera's offset based on screen size // Set the camera's offset based on screen size
self.camera.offset = raylib.get_screen_size().div(Vector2::new(2.0, 1.05)); self.camera.offset = raylib.get_screen_size().div(Vector2::new(2.0, 1.05));
self.camera.target = Vector2::new(self.player.position.x, self.camera.target.y); self.camera.target = Vector2::new(self.player.position.x, self.camera.target.y);
@ -32,22 +31,39 @@ impl FrameUpdate for InGameScreen {
let is_dash = raylib.is_key_pressed(KeyboardKey::KEY_LEFT_SHIFT) let is_dash = raylib.is_key_pressed(KeyboardKey::KEY_LEFT_SHIFT)
&& !(self.player.current_state == CharacterState::Dashing); && !(self.player.current_state == CharacterState::Dashing);
if is_jump { let collision_result = if is_jump {
self.player.update_player(Some(CharacterState::Jumping), &cur_level.colliders, self.player.update_player(
-cur_level.platform_tex.height as f32,); Some(CharacterState::Jumping),
&cur_level.colliders,
-cur_level.platform_tex.height as f32,
)
} else if is_dash { } else if is_dash {
self.player.update_player(Some(CharacterState::Dashing), &cur_level.colliders, self.player.update_player(
-cur_level.platform_tex.height as f32,); Some(CharacterState::Dashing),
&cur_level.colliders,
-cur_level.platform_tex.height as f32,
)
} else { } else {
if self.player.current_state != CharacterState::Jumping if self.player.current_state != CharacterState::Jumping
&& self.player.current_state != CharacterState::Dashing && self.player.current_state != CharacterState::Dashing
{ {
self.player.update_player(Some(CharacterState::Running), &cur_level.colliders, self.player.update_player(
-cur_level.platform_tex.height as f32,); Some(CharacterState::Running),
&cur_level.colliders,
-cur_level.platform_tex.height as f32,
)
} else { } else {
self.player.update_player(None, &cur_level.colliders, self.player.update_player(
-cur_level.platform_tex.height as f32,); None,
&cur_level.colliders,
-cur_level.platform_tex.height as f32,
)
} }
};
// Handle running into a wall
if let Err(_) = collision_result {
self.player_dead = true;
} }
} }
} }