cleanup of animation

This commit is contained in:
Evan Pratten 2021-10-01 22:56:16 -04:00
parent 6ef9655a2b
commit 21272ba5d6
5 changed files with 31 additions and 9 deletions

View File

@ -1,6 +1,6 @@
use raylib::math::{Rectangle, Vector2}; use raylib::math::{Rectangle, Vector2};
use super::MainCharacter; use super::{CharacterState, MainCharacter};
const GRAVITY_PPS: f32 = 2.0; 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
{ {
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 // TODO: Error out if colliding in the X direction

View File

@ -8,7 +8,7 @@ use crate::utilities::anim_render::AnimatedSpriteSheet;
use self::collisions::modify_player_based_on_forces; use self::collisions::modify_player_based_on_forces;
#[derive(Debug, Default)] #[derive(Debug, Default, PartialEq, Eq)]
pub enum CharacterState { pub enum CharacterState {
#[default] #[default]
Running, Running,
@ -47,7 +47,6 @@ impl MainCharacter {
pub fn apply_force(&mut self, force: Vector2) -> Option<()> { pub fn apply_force(&mut self, force: Vector2) -> Option<()> {
self.velocity = force; self.velocity = force;
modify_player_based_on_forces(self).unwrap(); modify_player_based_on_forces(self).unwrap();
// self.position = calculate_player_collisions(&self).unwrap();
Some(()) Some(())
} }
@ -56,7 +55,9 @@ impl MainCharacter {
} }
pub fn set_state(&mut self, state: CharacterState) { pub fn set_state(&mut self, state: CharacterState) {
self.current_state = state; if state != self.current_state {
self.state_set_timestamp = Utc::now(); self.current_state = state;
self.state_set_timestamp = Utc::now();
}
} }
} }

View File

@ -17,17 +17,24 @@ pub fn render_character_in_camera_space(
let time_since_state_change = Utc::now() - player.state_set_timestamp; let time_since_state_change = Utc::now() - player.state_set_timestamp;
// Calculate the number of frames since state change // 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!( trace!(
"Rendering player frame: {} ({})", "Rendering player frame: {} ({})",
frames_since_state_change % player.sprite_sheet.sprite_count as f32, frame_id,
frames_since_state_change frames_since_state_change
); );
player.sprite_sheet.render( player.sprite_sheet.render(
raylib, raylib,
player.position.sub(player.size.div(2.0)), player.position.sub(player.size.div(2.0)),
Some(Vector2::new(player.size.y, player.size.y)), 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),
); );
} }

View File

@ -2,6 +2,7 @@ use std::ops::Div;
use super::InGameScreen; use super::InGameScreen;
use crate::{ use crate::{
character::CharacterState,
utilities::{non_ref_raylib::HackedRaylibHandle, render_layer::FrameUpdate}, utilities::{non_ref_raylib::HackedRaylibHandle, render_layer::FrameUpdate},
GameConfig, GameConfig,
}; };
@ -26,6 +27,14 @@ impl FrameUpdate for InGameScreen {
if is_jump { if is_jump {
self.player.apply_force(Vector2::new(0.0, -30.0)); 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(); self.player.update_gravity();
} }

View File

@ -11,7 +11,7 @@ pub struct AnimatedSpriteSheet {
sprite_size: Vector2, sprite_size: Vector2,
sheet_width: usize, sheet_width: usize,
pub sprite_count: usize, pub sprite_count: usize,
default_sprite_id: usize, pub default_sprite_id: usize,
} }
impl AnimatedSpriteSheet { impl AnimatedSpriteSheet {