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 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

View File

@ -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) {
if state != self.current_state {
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;
// 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),
);
}

View File

@ -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();
}

View File

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