121 lines
4.3 KiB
GDScript
121 lines
4.3 KiB
GDScript
class_name ProceduralAnimator extends Node
|
|
|
|
@export var player: CharacterBody3D # Changed to CharacterBody3D for safety
|
|
@export var camera: Camera3D
|
|
@export var ui: Control
|
|
@export var movement: Node
|
|
|
|
@onready var ui_base_position = ui.position
|
|
|
|
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 target_rotation: Vector3 = Vector3.ZERO
|
|
|
|
var _smoothed_velocity: Vector3 = Vector3.ZERO
|
|
var _smoothed_mouse_offset: Vector2 = Vector2.ZERO
|
|
var _breathing: bool = true
|
|
var _last_camera_position: Vector3 = Vector3.ZERO
|
|
|
|
signal footstep
|
|
signal breath(breathe_in: bool)
|
|
|
|
func _process(delta: float) -> void:
|
|
delta = min(delta, 0.05)
|
|
|
|
_breathing = player.is_on_floor()
|
|
do(delta)
|
|
footstep_sound()
|
|
breathing_sound()
|
|
|
|
func do(delta: float):
|
|
_smoothed_velocity = _smoothed_velocity.lerp(player.velocity, 1.0 - exp(-15.0 * delta))
|
|
|
|
var cam_local_vel = camera.global_transform.basis.inverse() * _smoothed_velocity
|
|
|
|
ui_movement_handling(delta, cam_local_vel)
|
|
|
|
var horizontal_speed = player.velocity.length()
|
|
|
|
if _breathing:
|
|
t_breath += delta * (100.0 / (40.0 + player.stamina_component.stamina))
|
|
|
|
var target_pos = get_breathing_bob_offset_pos()
|
|
var target_rot = get_breathing_bob_offset_rot()
|
|
|
|
if player.is_on_floor() and horizontal_speed > 0.05:
|
|
t_bob += delta * (horizontal_speed + (int(Input.is_action_pressed("crouch")) * 3.0))
|
|
|
|
target_pos += get_moving_bob_offset_pos()
|
|
target_rot += get_moving_bob_offset_rot()
|
|
|
|
camera.position = camera.position.lerp(target_pos, delta * 10.0)
|
|
|
|
var final_pitch = target_rot.x + (cam_local_vel.z * 0.005 - cam_local_vel.y * 0.01)
|
|
var final_roll = target_rot.z - (cam_local_vel.x * 0.005)
|
|
|
|
camera.rotation = Vector3(final_pitch, 0, final_roll)
|
|
|
|
func ui_movement_handling(delta: float, cam_local_vel: Vector3):
|
|
if Console.toggled:
|
|
return
|
|
|
|
var mouse_offset = -movement.mouse_motion_delta * PlayerConsts.UI_MOVEMENT_SENSITIVITY
|
|
|
|
_smoothed_mouse_offset = _smoothed_mouse_offset.lerp(mouse_offset, 1.0 - exp(-15.0 * delta))
|
|
|
|
movement.mouse_motion_delta = Vector2.ZERO
|
|
|
|
# We use the safe velocity calculated in do() instead of manual positions
|
|
var inertia_offset = Vector2(
|
|
-cam_local_vel.x,
|
|
cam_local_vel.y
|
|
) * PlayerConsts.CAMERA_VELOCITY_MULTIPLIER
|
|
|
|
var camera_velocity = (camera.position - _last_camera_position) / delta
|
|
|
|
var bob_offset: Vector2 = Vector2(-camera_velocity.x, camera_velocity.y)
|
|
|
|
var target_offset = mouse_offset + inertia_offset + bob_offset * 5
|
|
var weighted_lerp = 1.0 - exp(-PlayerConsts.UI_MOVEMENT_SPEED * delta)
|
|
|
|
ui.position = ui.position.lerp(ui_base_position + target_offset, weighted_lerp)
|
|
|
|
var target_scale = -cam_local_vel.z * PlayerConsts.UI_MOVEMENT_SENSITIVITY * .01 + 1
|
|
|
|
var ui_scaling_target = Vector2(target_scale, target_scale)
|
|
ui.offset_transform_scale = ui.offset_transform_scale.lerp(Vector2.ONE * ui_scaling_target, weighted_lerp)
|
|
|
|
var lean_dir = -Input.get_axis("lean_left", "lean_right") * .1
|
|
var ui_rotation_target = -inertia_offset.x - mouse_offset.x * 0.25
|
|
ui.offset_transform_rotation = lerpf(ui.offset_transform_rotation, ui_rotation_target * PlayerConsts.UI_MOVEMENT_SENSITIVITY * 0.003 + lean_dir, weighted_lerp)
|
|
|
|
_last_camera_position = camera.position
|
|
|
|
func footstep_sound():
|
|
var current_sin_pos = sin(t_bob * PlayerConsts.CAMERA_BOB_FREQUENCY)
|
|
if last_bob_y >= -0.95 and current_sin_pos < -0.95:
|
|
footstep.emit()
|
|
last_bob_y = current_sin_pos
|
|
|
|
func breathing_sound():
|
|
if _breathing:
|
|
var current_sin_pos = sin(t_breath * PlayerConsts.CAMERA_BOB_FREQUENCY)
|
|
if last_breath_y <= -0.60 and current_sin_pos > -0.60:
|
|
breath.emit(true)
|
|
if last_breath_y >= 0.60 and current_sin_pos < 0.60:
|
|
breath.emit(false)
|
|
last_breath_y = current_sin_pos
|
|
|
|
func get_breathing_bob_offset_pos() -> Vector3:
|
|
return Vector3(0, sin(t_breath * PlayerConsts.CAMERA_BOB_FREQUENCY) * PlayerConsts.CAMERA_BOB_AMPLITUDE + 0.75, 0)
|
|
|
|
func get_breathing_bob_offset_rot() -> Vector3:
|
|
return Vector3(sin(t_breath * PlayerConsts.CAMERA_BOB_FREQUENCY) * PlayerConsts.CAMERA_SIDEWAYS_BOB_AMPLITUDE, 0, 0)
|
|
|
|
func get_moving_bob_offset_pos() -> Vector3:
|
|
return Vector3(0, sin(t_bob * PlayerConsts.CAMERA_BOB_FREQUENCY) * PlayerConsts.CAMERA_BOB_AMPLITUDE, 0)
|
|
|
|
func get_moving_bob_offset_rot() -> Vector3:
|
|
return Vector3(0, 0, sin(t_bob * PlayerConsts.CAMERA_BOB_FREQUENCY * 0.5) * PlayerConsts.CAMERA_SIDEWAYS_BOB_AMPLITUDE)
|