From c31a84ea61c60fc9b138944e36c1e3311f4f88e6 Mon Sep 17 00:00:00 2001 From: Franklin Date: Tue, 12 Sep 2023 13:08:00 -0400 Subject: [PATCH] Added depth to jumping, inertia stays mid air but right before landing damping is restored --- src/comps/core/controller.rs | 1 + src/constants/player_values.rs | 21 ++++++++++------ src/logic/core/player/player_movement.rs | 24 +++++++++++++++---- src/logic/core/player/player_vertical_sync.rs | 13 ++++++---- src/logic/core/player/spawn_player.rs | 8 +++---- 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/comps/core/controller.rs b/src/comps/core/controller.rs index 383c95a..1258e35 100644 --- a/src/comps/core/controller.rs +++ b/src/comps/core/controller.rs @@ -17,6 +17,7 @@ pub fn capture_input( &mut PlayerLinearYState, &mut PlayerLinearXZState, &Transform, + &mut Damping, ), With, >, diff --git a/src/constants/player_values.rs b/src/constants/player_values.rs index c1005f8..f0e5b19 100644 --- a/src/constants/player_values.rs +++ b/src/constants/player_values.rs @@ -1,14 +1,21 @@ -pub const MAX_LINEAR_PLAYER_VELOCITY: f32 = 10.0; -pub const PLAYER_ACCELERATION: f32 = 12.0; -pub const PLAYER_JUMP_FORCE: f32 = 5000.0; +pub const MAX_LINEAR_PLAYER_VELOCITY: f32 = 20.0; +pub const PLAYER_ACCELERATION: f32 = 20.0; +pub const PLAYER_JUMP_FORCE: f32 = 900.0; /// Time in ms that player must be grounded in order to jump again pub const PLAYER_JUMP_COOLDOWN_MS: u128 = 75; - pub const PLAYER_SPRINT_SPEED_MULTIPLIER: f32 = 2.0; pub const PLAYER_CROUCH_SPEED_MULTIPLIER: f32 = 0.25; - -pub const PLAYER_INITIAL_WEIGHT: f32 = 350.0; +pub const PLAYER_INITIAL_WEIGHT: f32 = 75.0; +pub const PLAYER_GRAVITY_SCALE: f32 = 4.0; pub const PLAYER_HEIGHT: f32 = 2.0; -pub const PLAYER_CAMERA_HEIGHT: f32 = 0.5; +pub const PLAYER_CAMERA_HEIGHT: f32 = 1.0; pub const PLAYER_CROUCH_HEIGHT: f32 = 0.0; + +pub const PLAYER_LINEAR_DAMPING: f32 = 3.5; +pub const PLAYER_LINEAR_DAMPING_WHILE_JUMPING: f32 = -0.5; + +pub const PLAYER_LATERAL_ACCELERATION_WHILE_SPRINTING_MULTIPLIER: f32 = 0.2; +pub const PLAYER_LATERAL_ACCELERATION_MULTIPLIER: f32 = 1.0; + +pub const PLAYER_LINEAR_DAMPING_TIME_OFFSET_AFTER_JUMP_IN_MS: u128 = 20; \ No newline at end of file diff --git a/src/logic/core/player/player_movement.rs b/src/logic/core/player/player_movement.rs index f063bb6..f7322d2 100644 --- a/src/logic/core/player/player_movement.rs +++ b/src/logic/core/player/player_movement.rs @@ -5,7 +5,7 @@ use crate::{ comps::core::markers::player::Player, constants::player_values::{ MAX_LINEAR_PLAYER_VELOCITY, PLAYER_ACCELERATION, PLAYER_CROUCH_SPEED_MULTIPLIER, - PLAYER_JUMP_COOLDOWN_MS, PLAYER_JUMP_FORCE, PLAYER_SPRINT_SPEED_MULTIPLIER, + PLAYER_JUMP_COOLDOWN_MS, PLAYER_JUMP_FORCE, PLAYER_SPRINT_SPEED_MULTIPLIER, PLAYER_LINEAR_DAMPING_WHILE_JUMPING, PLAYER_LATERAL_ACCELERATION_MULTIPLIER, PLAYER_LATERAL_ACCELERATION_WHILE_SPRINTING_MULTIPLIER, }, }; @@ -54,6 +54,12 @@ impl PlayerLinearXZState { _ => false, } } + pub fn is_sprinting(&self) -> bool { + match self { + Self::Sprinting => true, + _ => false, + } + } } /// Holds all the possible ways a player can be attempting to move at any time. @@ -87,6 +93,7 @@ pub fn move_player( &mut PlayerLinearYState, &mut PlayerLinearXZState, &Transform, + &mut Damping, ), With, >, @@ -98,9 +105,9 @@ pub fn move_player( mut player_linear_y_state, mut player_linear_xz_state, player_transform, + mut player_damping ) in &mut query { - println!("{:?}", player_linear_xz_state); let crouch_multiplier = if player_movement_input.down { *player_linear_xz_state = PlayerLinearXZState::Crouched; PLAYER_CROUCH_SPEED_MULTIPLIER @@ -155,7 +162,11 @@ pub fn move_player( right, player_velocity.linvel, time.delta_seconds(), - 1.0, + if player_linear_xz_state.is_sprinting() { + PLAYER_LATERAL_ACCELERATION_WHILE_SPRINTING_MULTIPLIER + } else { + PLAYER_LATERAL_ACCELERATION_MULTIPLIER + }, ); } if player_movement_input.left { @@ -168,7 +179,11 @@ pub fn move_player( -right, player_velocity.linvel, time.delta_seconds(), - 1.0, + if player_linear_xz_state.is_sprinting() { + PLAYER_LATERAL_ACCELERATION_WHILE_SPRINTING_MULTIPLIER + } else { + PLAYER_LATERAL_ACCELERATION_MULTIPLIER + }, ); } } @@ -176,6 +191,7 @@ pub fn move_player( if player_movement_input.up && player_linear_y_state.is_grounded(&PLAYER_JUMP_COOLDOWN_MS) { player_external_force.impulse = Vec3::new(0.0, PLAYER_JUMP_FORCE, 0.0); *player_linear_y_state = PlayerLinearYState::Jumping; + *player_damping = Damping { linear_damping: PLAYER_LINEAR_DAMPING_WHILE_JUMPING, ..Default::default()}; } // When player velocity exceeds max linear velocity then set to max_linear_vel value if player_velocity.linvel.x.abs() diff --git a/src/logic/core/player/player_vertical_sync.rs b/src/logic/core/player/player_vertical_sync.rs index 13c575a..7cc1618 100644 --- a/src/logic/core/player/player_vertical_sync.rs +++ b/src/logic/core/player/player_vertical_sync.rs @@ -1,16 +1,16 @@ use bevy::prelude::*; use bevy_rapier3d::prelude::*; -use crate::comps::core::markers::player::Player; +use crate::{comps::core::markers::player::Player, constants::player_values::{PLAYER_LINEAR_DAMPING, PLAYER_JUMP_COOLDOWN_MS, PLAYER_LINEAR_DAMPING_TIME_OFFSET_AFTER_JUMP_IN_MS}}; use super::player_movement::PlayerLinearYState; /// System that captures linear y velocity and determines whether player is falling or grounded, and how long the player has been grounded for. pub fn sync_player_y_state( - mut query: Query<(&Velocity, &mut PlayerLinearYState), With>, + mut query: Query<(&Velocity, &mut PlayerLinearYState, &mut Damping), With>, time: Res