diff --git a/src/logic-scenes/player_controller/player_controller.gd b/src/logic-scenes/player_controller/player_controller.gd index 09f21ec..0238344 100644 --- a/src/logic-scenes/player_controller/player_controller.gd +++ b/src/logic-scenes/player_controller/player_controller.gd @@ -31,97 +31,97 @@ var crouched:bool = false func set_active(activate): - if !is_inside_tree(): return - if activate: - camera.make_current() - Input.mouse_mode = Input.MOUSE_MODE_CAPTURED - sleeping = active + if !is_inside_tree(): return + if activate: + camera.make_current() + Input.mouse_mode = Input.MOUSE_MODE_CAPTURED + sleeping = active func _ready(): - - if active: - set_active(active) - - + + if active: + set_active(active) + + func _physics_process(delta:float): - _handle_movement(delta) - _handle_rotation(delta) - _handle_jitter(delta) + _handle_movement(delta) + _handle_rotation(delta) + _handle_jitter(delta) func _handle_movement(delta:float): - var input:Vector2 = Vector2(Input.get_action_strength("player_right") - Input.get_action_strength("player_left"), - Input.get_action_strength("player_backwards")*0.8 - Input.get_action_strength("player_forwards")) - - if input.length()>1: - input = input.normalized() - - var direction: Vector3 = Vector3(input.x, 0, input.y) - - direction = yaw.global_transform.basis.x * direction.x + transform.basis.y * direction.y + yaw.global_transform.basis.z * direction.z - - if linear_velocity.length() > (linear_velocity + (direction*max_speed - linear_velocity)).length(): - direction = Vector3.ZERO - else: - direction *= (direction*max_speed - linear_velocity).length()*max_acceleration - - linear_damp = damp * max(0.5, 1 - input.length()) - - apply_central_impulse(direction*delta) + var input:Vector2 = Vector2(Input.get_action_strength("player_right") - Input.get_action_strength("player_left"), + Input.get_action_strength("player_backwards")*0.8 - Input.get_action_strength("player_forwards")) + + if input.length()>1: + input = input.normalized() + + var direction: Vector3 = Vector3(input.x, 0, input.y) + + direction = yaw.global_transform.basis.x * direction.x + transform.basis.y * direction.y + yaw.global_transform.basis.z * direction.z + + if linear_velocity.length() > (linear_velocity + (direction*max_speed - linear_velocity)).length(): + direction = Vector3.ZERO + else: + direction *= (direction*max_speed - linear_velocity).length()*max_acceleration + + linear_damp = damp * max(0.5, 1 - input.length()) + + apply_central_impulse(direction*delta) func _handle_rotation(delta:float): - var smoothness = 60/Engine.get_frames_per_second() - - var input_speed = Vector2( Input.get_action_strength("look_right")-Input.get_action_strength("look_left"), Input.get_action_strength("look_down")-Input.get_action_strength("look_up")) * gamepad_response - - if current_mouse_rotation.length()>0: - input_speed = current_mouse_rotation - current_mouse_rotation = Vector2.ZERO - - rotation_speed = rotation_speed * (1-mouse_jerk*smoothness) + input_speed * mouse_jerk * smoothness - - if rotation_speed.y > 0 and pitch.rotation_degrees.x < 0: - rotation_speed.y *= 1-pow(pitch.rotation_degrees.x/-max_angle, 4) - elif rotation_speed.y < 0 and pitch.rotation_degrees.x > 0 : - rotation_speed.y *= 1-pow(pitch.rotation_degrees.x/max_angle, 4) - - yaw.rotate_y(deg_to_rad(-rotation_speed.x * delta * mouse_sensitivity.x)) - pitch.rotate_x(deg_to_rad(-rotation_speed.y * delta * mouse_sensitivity.y)) + var smoothness = min(3, 60.0/Engine.get_frames_per_second()) + + var input_speed = Vector2( Input.get_action_strength("look_right")-Input.get_action_strength("look_left"), Input.get_action_strength("look_down")-Input.get_action_strength("look_up")) * gamepad_response + + if current_mouse_rotation.length()>0: + input_speed = current_mouse_rotation + current_mouse_rotation = Vector2.ZERO + + rotation_speed = rotation_speed * (1-mouse_jerk*smoothness) + input_speed * mouse_jerk * smoothness + + if rotation_speed.y > 0 and pitch.rotation_degrees.x < 0: + rotation_speed.y *= 1-pow(pitch.rotation_degrees.x/-max_angle, 4) + elif rotation_speed.y < 0 and pitch.rotation_degrees.x > 0 : + rotation_speed.y *= 1-pow(pitch.rotation_degrees.x/max_angle, 4) + + yaw.rotate_y(deg_to_rad(-rotation_speed.x * delta * mouse_sensitivity.x)) + pitch.rotate_x(deg_to_rad(-rotation_speed.y * delta * mouse_sensitivity.y)) func _handle_jitter(delta): - loc_noise_spot += Vector3(delta * camera_jitter_speed * location_jitter_speed) - rot_noise_spot += Vector3(delta * camera_jitter_speed * angular_jitter_speed) - pitch.position = Vector3( - noise.get_noise_1d(loc_noise_spot.x), - noise.get_noise_1d(loc_noise_spot.y), - noise.get_noise_1d(loc_noise_spot.z) - ) * location_jitter - - mount.rotation = Vector3( - noise.get_noise_1d(rot_noise_spot.x), - noise.get_noise_1d(rot_noise_spot.y), - noise.get_noise_1d(rot_noise_spot.z) - ) * angular_jitter - + loc_noise_spot += Vector3(delta * camera_jitter_speed * location_jitter_speed) + rot_noise_spot += Vector3(delta * camera_jitter_speed * angular_jitter_speed) + pitch.position = Vector3( + noise.get_noise_1d(loc_noise_spot.x), + noise.get_noise_1d(loc_noise_spot.y), + noise.get_noise_1d(loc_noise_spot.z) + ) * location_jitter + + mount.rotation = Vector3( + noise.get_noise_1d(rot_noise_spot.x), + noise.get_noise_1d(rot_noise_spot.y), + noise.get_noise_1d(rot_noise_spot.z) + ) * angular_jitter + func _handle_mouse_input(event:InputEventMouseMotion): - if event.relative.length() < mouse_jerk_rejection: - current_mouse_rotation = event.relative + if event.relative.length() < mouse_jerk_rejection: + current_mouse_rotation = event.relative func _unhandled_input(event:InputEvent): - if active: - if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: - _handle_mouse_input(event) + if active: + if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: + _handle_mouse_input(event) func _on_bed_enter(body): - if not crouched: - $PlayerAnimationPlayer.queue("crouch") - max_speed *= .5 - crouched = true + if not crouched: + $PlayerAnimationPlayer.queue("crouch") + max_speed *= .5 + crouched = true func _on_bed_exited(body): - if crouched: - crouched = false - $PlayerAnimationPlayer.queue("stand_up") - await $PlayerAnimationPlayer.animation_finished - max_speed *= 2 + if crouched: + crouched = false + $PlayerAnimationPlayer.queue("stand_up") + await $PlayerAnimationPlayer.animation_finished + max_speed *= 2