diff --git a/game/src/character/collisions.rs b/game/src/character/collisions.rs index 8b6e951..0a52deb 100644 --- a/game/src/character/collisions.rs +++ b/game/src/character/collisions.rs @@ -1,6 +1,6 @@ use raylib::math::{Rectangle, Vector2}; -use super::MainCharacter; +use super::{CharacterState, MainCharacter}; const GRAVITY_PPS: f32 = 2.0; @@ -22,6 +22,11 @@ pub fn modify_player_based_on_forces(player: &mut MainCharacter) -> Result<(), ( && player.velocity.y > 0.0 { player.velocity.y = 0.0; + + // Handle ending a jump + if player.current_state == CharacterState::Jumping { + player.set_state(CharacterState::Running); + } } // TODO: Error out if colliding in the X direction diff --git a/game/src/character/mod.rs b/game/src/character/mod.rs index 1386117..d79b4eb 100644 --- a/game/src/character/mod.rs +++ b/game/src/character/mod.rs @@ -8,7 +8,7 @@ use crate::utilities::anim_render::AnimatedSpriteSheet; use self::collisions::modify_player_based_on_forces; -#[derive(Debug, Default)] +#[derive(Debug, Default, PartialEq, Eq)] pub enum CharacterState { #[default] Running, @@ -47,7 +47,6 @@ impl MainCharacter { pub fn apply_force(&mut self, force: Vector2) -> Option<()> { self.velocity = force; modify_player_based_on_forces(self).unwrap(); - // self.position = calculate_player_collisions(&self).unwrap(); Some(()) } @@ -56,7 +55,9 @@ impl MainCharacter { } pub fn set_state(&mut self, state: CharacterState) { - self.current_state = state; - self.state_set_timestamp = Utc::now(); + if state != self.current_state { + self.current_state = state; + self.state_set_timestamp = Utc::now(); + } } } diff --git a/game/src/character/render.rs b/game/src/character/render.rs index c200af8..6844f11 100644 --- a/game/src/character/render.rs +++ b/game/src/character/render.rs @@ -17,17 +17,24 @@ pub fn render_character_in_camera_space( let time_since_state_change = Utc::now() - player.state_set_timestamp; // Calculate the number of frames since state change - let frames_since_state_change = ((time_since_state_change.num_milliseconds() as f64 / 1000.0) * config.animation_fps as f64) as f32; + let frames_since_state_change = ((time_since_state_change.num_milliseconds() as f64 / 1000.0) + * config.animation_fps as f64) as f32; + + // Calculate the frame ID to render + let frame_id = match player.current_state { + crate::character::CharacterState::Jumping => 4, + _ => (frames_since_state_change % player.sprite_sheet.sprite_count as f32).floor() as usize, + }; trace!( "Rendering player frame: {} ({})", - frames_since_state_change % player.sprite_sheet.sprite_count as f32, + frame_id, frames_since_state_change ); player.sprite_sheet.render( raylib, player.position.sub(player.size.div(2.0)), Some(Vector2::new(player.size.y, player.size.y)), - Some((frames_since_state_change % player.sprite_sheet.sprite_count as f32).floor() as usize), + Some(frame_id), ); } diff --git a/game/src/scenes/ingame_scene/update.rs b/game/src/scenes/ingame_scene/update.rs index ea7cf42..13fd65c 100644 --- a/game/src/scenes/ingame_scene/update.rs +++ b/game/src/scenes/ingame_scene/update.rs @@ -2,6 +2,7 @@ use std::ops::Div; use super::InGameScreen; use crate::{ + character::CharacterState, utilities::{non_ref_raylib::HackedRaylibHandle, render_layer::FrameUpdate}, GameConfig, }; @@ -26,6 +27,14 @@ impl FrameUpdate for InGameScreen { if is_jump { self.player.apply_force(Vector2::new(0.0, -30.0)); + self.player.set_state(CharacterState::Jumping); + } else if is_dash { + self.player.apply_force(Vector2::new(20.0, -10.0)); + self.player.set_state(CharacterState::Dashing); + } else { + if self.player.current_state != CharacterState::Jumping { + self.player.set_state(CharacterState::Running); + } } self.player.update_gravity(); } diff --git a/game/src/utilities/anim_render.rs b/game/src/utilities/anim_render.rs index 19d4a53..0ae79de 100644 --- a/game/src/utilities/anim_render.rs +++ b/game/src/utilities/anim_render.rs @@ -11,7 +11,7 @@ pub struct AnimatedSpriteSheet { sprite_size: Vector2, sheet_width: usize, pub sprite_count: usize, - default_sprite_id: usize, + pub default_sprite_id: usize, } impl AnimatedSpriteSheet {