variable jump height

This commit is contained in:
TudbuT 2024-11-24 02:55:20 +01:00
parent e7e1242ef4
commit 78acd0313c

View file

@ -26,12 +26,14 @@ struct RespawnPointSetEvent;
#[derive(Component)] #[derive(Component)]
struct Player { struct Player {
move_cooldown: Timer, move_cooldown: Timer,
last_grounded: u64,
} }
impl Default for Player { impl Default for Player {
fn default() -> Self { fn default() -> Self {
Self { Self {
move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating), move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating),
last_grounded: u64::MAX / 2,
} }
} }
} }
@ -94,6 +96,9 @@ fn move_player(
let mut move_y = 0; let mut move_y = 0;
let mut jump = false; let mut jump = false;
let grounded = output.is_ok_and(|x| x.grounded); let grounded = output.is_ok_and(|x| x.grounded);
if grounded {
player.last_grounded = 0;
}
if kb_input.pressed(KeyCode::KeyA) { if kb_input.pressed(KeyCode::KeyA) {
moved = true; moved = true;
move_x -= 1; move_x -= 1;
@ -103,8 +108,7 @@ fn move_player(
move_x += 1; move_x += 1;
} }
if (kb_input.pressed(KeyCode::Space) || kb_input.pressed(KeyCode::KeyW)) if (kb_input.pressed(KeyCode::Space) || kb_input.pressed(KeyCode::KeyW))
&& vel.linvel.y < 0.1 && player.last_grounded <= 5
&& grounded
{ {
moved = true; moved = true;
move_y += 1; move_y += 1;
@ -114,7 +118,7 @@ fn move_player(
cam_transform.translation -= ((orig - p_transform.translation.xy().extend(0.0)) / 30.0); cam_transform.translation -= ((orig - p_transform.translation.xy().extend(0.0)) / 30.0);
controller.translation = Some(Vec2::new(move_x as f32 * 6., 0. - 0.01)); controller.translation = Some(Vec2::new(move_x as f32 * 6., 0. - 0.01));
vel.linvel += Vec2::new(0., move_y as f32 * 6. * METER); vel.linvel += Vec2::new(0., move_y as f32 * METER);
if move_x != 0 && grounded { if move_x != 0 && grounded {
*anim_state = PlayerAnimations::Walk; *anim_state = PlayerAnimations::Walk;
@ -142,6 +146,7 @@ fn move_player(
p_transform.translation = p_transform.translation =
Vec2::new(player_coords.x, player_coords.y).extend(PLAYER_DEPTH); Vec2::new(player_coords.x, player_coords.y).extend(PLAYER_DEPTH);
} }
player.last_grounded += 1;
} }
} }