breadwinner/scenes/player/procedural_camera_mover_component.gd

119 lines
4.6 KiB
GDScript3
Raw Normal View History

extends Node
@export_category("Camera")
@export var LOOK_SENSITIVITY = 1.0 ##camera sensitivity
@export var CAMERA_VELOCITY_MULTIPLIER = 1.5 ##how heavy should the UI movement feel
@export var CAMERA_BOB_FREQUENCY = 2.4 ##how quick the camera bobs whilst moving
@export var CAMERA_BOB_AMPLITUDE = 0.08 ##how hard the camera bobs whilst moving
@export var CAMERA_BOB_MULTIPLIER = 200.0 ##how much the ui reacts to head bobbing
@export var CAMERA_SIDEWAYS_BOB_AMPLITUDE = 0.003 ## how many degrees does the camera bob sideways
@export_category("UI")
@export var UI_SCALE: float = 1 ##rational scale of the ui
@export var UI_MOVEMENT_SENSITIVITY = 0.5 ##how much the UI moves when you move the camera
@export var UI_MOVEMENT_SPEED = 15.0 ##how fast the UI moves when you move the camera
@export var UI_MOVEMENT_VELOCITY_MULTIPLIER = 2.0 ##how much the ui leans when walking or strafing
@onready var player: PlayerCharacter = $"../.."
@onready var sound_controller: Node = %SoundController
@onready var camera: Camera3D = %PlayerCamera
@onready var ui: Control = %PlayerUi
@onready var player_properties: Node = %PlayerProperties
@onready var ui_base_position = ui.position
@onready var ui_handler: Node = %UiHandler
var t_bob: float = 0.0
var last_bob_y: float = 0.0
var t_breath: float = 0.0
var last_breath_y: float = 0.0
var _camera_original_position: Vector3 = Vector3(0,.75,0)
var _mouse_motion: Vector2 = Vector2.ZERO
var _mouse_motion_delta: Vector2 = Vector2.ZERO
var _last_camera_global_position: Vector3 = Vector3.ZERO
var _breathing: bool = true
func _ready() -> void:
_last_camera_global_position = camera.global_position
func _process(delta: float) -> void:
if player.is_on_floor():
_breathing = true
else:
_breathing = false
camera_breathing(delta)
func do(delta):
ui.offset_transform_scale *= UI_SCALE
ui_movement_handling(delta)
ui_handler.set_stamina_bar(player_properties.stamina)
var horizontal_speed = Vector3(player.velocity.x, player.velocity.y, player.velocity.z).length()
var current_sin_pos = sin(t_bob * CAMERA_BOB_FREQUENCY)
if player.is_on_floor() and horizontal_speed > 0.05:
t_bob += delta * (player.velocity.length() * float(player.is_on_floor()) + (int(Input.is_action_pressed("crouch"))) * 3)
#footstep handling
if last_bob_y >= -.95 and current_sin_pos < -.95:
sound_controller.play_footstep_sound()
last_bob_y = current_sin_pos
camera_bobbing(t_bob)
else:
camera.rotation.z = lerpf(camera.rotation.z,0, delta * 0.004)
camera.transform.origin.y = lerpf(camera.transform.origin.y, _camera_original_position.y, delta * 5.0)
if camera.transform.origin.length() < 0.01:
t_bob = 0
last_bob_y = 0.0
func ui_movement_handling(delta: float):
var mouse_offset = -_mouse_motion_delta * UI_MOVEMENT_SENSITIVITY
var current_camera_global_position = camera.global_position
var camera_global_velocity = (current_camera_global_position - _last_camera_global_position) / delta
var cam_local_vel = camera.global_transform.basis.inverse() * camera_global_velocity
var inertia_offset = Vector2(
-cam_local_vel.x,
cam_local_vel.y - (cam_local_vel.z * 0.25)
) * CAMERA_VELOCITY_MULTIPLIER
var target_offset = mouse_offset + inertia_offset
ui.position = ui.position.lerp(ui_base_position + target_offset, delta * UI_MOVEMENT_SPEED)
_last_camera_global_position = current_camera_global_position
var ui_scaling_target = Vector2(-cam_local_vel.z * UI_MOVEMENT_SENSITIVITY * .002 + 1, -cam_local_vel.z * UI_MOVEMENT_SENSITIVITY * .002 + 1)
ui.offset_transform_scale = ui.offset_transform_scale.lerp(ui_scaling_target, delta * UI_MOVEMENT_SPEED)
var ui_rotation_target = -inertia_offset.x - mouse_offset.x * 0.25
ui.offset_transform_rotation = lerpf(ui.offset_transform_rotation, ui_rotation_target * UI_MOVEMENT_SENSITIVITY * 0.003, delta * UI_MOVEMENT_SPEED)
func camera_bobbing(time: float):
camera.transform.origin.y = sin(time * CAMERA_BOB_FREQUENCY) * CAMERA_BOB_AMPLITUDE + .75
camera.rotation.z += sin(time * CAMERA_BOB_FREQUENCY * .5) * CAMERA_SIDEWAYS_BOB_AMPLITUDE
func camera_breathing(time: float):
if _breathing:
t_breath += time * (100 / (40 + player_properties.stamina))
var current_sin_pos = sin(t_breath * CAMERA_BOB_FREQUENCY)
if last_breath_y <= -.60 and current_sin_pos > -.60:
sound_controller.play_breath_sound(true)
if last_breath_y >= .60 and current_sin_pos < .60:
sound_controller.play_breath_sound(false)
last_breath_y = current_sin_pos
camera.transform.origin.y = sin(time * CAMERA_BOB_FREQUENCY) * CAMERA_BOB_AMPLITUDE + .75
camera.rotation.x += sin(time * CAMERA_BOB_FREQUENCY) * CAMERA_SIDEWAYS_BOB_AMPLITUDE