diff --git a/src/comps/core/controller.rs b/src/comps/core/controller.rs index 6fe68ac..5b59ca5 100644 --- a/src/comps/core/controller.rs +++ b/src/comps/core/controller.rs @@ -1,4 +1,4 @@ -use bevy::{prelude::*, window::CursorGrabMode}; +use bevy::prelude::*; use bevy_rapier3d::prelude::*; use crate::logic::core::player::player_movement::{ @@ -16,7 +16,7 @@ pub fn capture_input( &mut ExternalImpulse, &mut PlayerLinearYState, &mut PlayerLinearXZState, - &Transform, + &mut Transform, &mut Damping, ), With, @@ -31,6 +31,8 @@ pub fn capture_input( KeyCode::W, KeyCode::C, KeyCode::Space, + KeyCode::Q, + KeyCode::E, ]) || keyboard_input.any_just_released([ KeyCode::A, KeyCode::S, @@ -38,6 +40,8 @@ pub fn capture_input( KeyCode::W, KeyCode::C, KeyCode::Space, + KeyCode::Q, + KeyCode::E, ]) { let player_movement_input = PlayerMovementInput { up: keyboard_input.just_pressed(KeyCode::Space), @@ -52,30 +56,4 @@ pub fn capture_input( }; move_player(player_movement_input, player_query, time); } -} - -pub fn capture_cursor( - mut windows: Query<&mut Window>, - btn: Res>, - key: Res>, -) { - let mut window = windows.single_mut(); - - if btn.just_pressed(MouseButton::Left) { - // if you want to use the cursor, but not let it leave the window, - // use `Confined` mode: - // window.cursor.grab_mode = CursorGrabMode::Confined; - - // for a game that doesn't use the cursor (like a shooter): - // use `Locked` mode to keep the cursor in one place - window.cursor.grab_mode = CursorGrabMode::Locked; - // also hide the cursor - window.cursor.visible = false; - } - - if key.just_pressed(KeyCode::Escape) { - window.cursor.grab_mode = CursorGrabMode::None; - // also hide the cursor - window.cursor.visible = true; - } -} +} \ No newline at end of file diff --git a/src/constants/player_values.rs b/src/constants/player_values.rs index a3fa52a..fa4f4cf 100644 --- a/src/constants/player_values.rs +++ b/src/constants/player_values.rs @@ -13,7 +13,7 @@ pub const PLAYER_GRAVITY_SCALE: f32 = 4.0; pub const PLAYER_HEIGHT: f32 = 2.5; pub const PLAYER_CAMERA_HEIGHT: f32 = 1.0; pub const PLAYER_CROUCH_HEIGHT: f32 = 0.0; -pub const PLAYER_CROUCH_TIME_MS: u128 = 130; +pub const PLAYER_CROUCH_TIME_S: f32 = 0.6; pub const PLAYER_LINEAR_DAMPING: f32 = 3.5; pub const PLAYER_LINEAR_DAMPING_WHILE_JUMPING: f32 = 0.25; diff --git a/src/logic/core/player/camera_player_sync.rs b/src/logic/core/player/camera_player_sync.rs index 0b73789..3355280 100644 --- a/src/logic/core/player/camera_player_sync.rs +++ b/src/logic/core/player/camera_player_sync.rs @@ -1,9 +1,9 @@ -use bevy::{input::mouse::MouseMotion, prelude::*}; +use bevy::{input::mouse::MouseMotion, prelude::*, window::CursorGrabMode}; //use bevy_rapier3d::prelude::*; use crate::{ comps::core::markers::{camera::MainCamera, player::Player}, - constants::player_values::{PLAYER_CAMERA_HEIGHT, PLAYER_CROUCH_HEIGHT, PLAYER_CROUCH_TIME_MS}, + constants::player_values::{PLAYER_CAMERA_HEIGHT, PLAYER_CROUCH_HEIGHT, PLAYER_CROUCH_TIME_S}, utils::rad_deg::radians_from_degrees, }; use super::player_movement::PlayerLinearXZState; @@ -39,47 +39,79 @@ pub fn update_camera_vertical_position( if let PlayerLinearXZState::Crouched(since) = player_linear_xz_state { // Lerp/Smooth out this movement - let delta = time.elapsed().as_millis() - since; - if delta > PLAYER_CROUCH_TIME_MS { - camera_transform.translation.y = PLAYER_CROUCH_HEIGHT; - } else { - // Starts at player_camera_height -> ends at player_crouch_height (Only works if player_crouch_height is 0) - camera_transform.translation.y = - PLAYER_CAMERA_HEIGHT * (1.0 - (delta as f32 / PLAYER_CROUCH_TIME_MS as f32)) - } + let delta = time.elapsed().as_secs_f32() - since; + camera_transform.translation = camera_transform.translation.lerp(Vec3 { x: camera_transform.translation.x, y: PLAYER_CROUCH_HEIGHT, z: camera_transform.translation.z }, (delta / PLAYER_CROUCH_TIME_S).clamp(0.0, 1.0)); } else { - camera_transform.translation.y = PLAYER_CAMERA_HEIGHT; + // TODO: Add elapsed time to standup so that crouch time and standup time is the same. + camera_transform.translation = camera_transform.translation.lerp(Vec3 { x: camera_transform.translation.x, y: PLAYER_CAMERA_HEIGHT, z: camera_transform.translation.z }, time.delta_seconds().clamp(0.0, 1.0)); } + } /// Handles looking around if cursor is locked pub fn follow_cursor_with_camera( settings: Res, - primary_window: Query<&Window>, + mut primary_window: Query<&mut Window>, mut motions: EventReader, mut player_query: Query<&mut Transform, (With, Without)>, mut camera_query: Query<&mut Transform, (With, Without)>, - //time: Res