97 lines
4 KiB
GDScript3
97 lines
4 KiB
GDScript3
|
|
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
|
||
|
|
|
||
|
|
var t_bob: float = 0.0
|
||
|
|
var last_bob_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
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
_last_camera_global_position = camera.global_position
|
||
|
|
|
||
|
|
func do(delta):
|
||
|
|
ui.offset_transform_scale *= UI_SCALE
|
||
|
|
ui_movement_handling(delta)
|
||
|
|
ui.set_stamina_bar(player_properties.stamina)
|
||
|
|
var horizontal_speed = Vector3(player.velocity.x, player.velocity.y, player.velocity.z).length()
|
||
|
|
|
||
|
|
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)
|
||
|
|
|
||
|
|
var current_sin_pos = sin(t_bob * CAMERA_BOB_FREQUENCY)
|
||
|
|
|
||
|
|
#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)
|
||
|
|
|
||
|
|
#idle breathing
|
||
|
|
elif player.is_on_floor() and horizontal_speed < 0.05:
|
||
|
|
t_bob += delta * 1
|
||
|
|
camera_breathing(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):
|
||
|
|
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
|