breadwinner/scenes/player/procedural_animator.gd

122 lines
4.1 KiB
GDScript3
Raw Normal View History

class_name ProceduralAnimator extends Node
@export var player: Player
@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 _last_camera_global_position: Vector3 = Vector3.ZERO
var _breathing: bool = true
#signals for emitting sounds
signal footstep
signal breath(breathe_in:bool)
func _ready() -> void:
_last_camera_global_position = camera.global_position
func _physics_process(_delta: float) -> void:
camera.rotation += target_rotation
func _process(delta: float) -> void:
if not UiManager.in_game_menu.visible:
do(delta)
if player.is_on_floor():
_breathing = true
else:
_breathing = false
footstep_sound()
breathing_sound()
func do(delta):
ui.offset_transform_scale *= PlayerConsts.UI_SCALE
ui_movement_handling(delta)
var horizontal_speed = Vector3(player.velocity.x, player.velocity.y, player.velocity.z).length()
if _breathing:
t_breath += delta * (100 / (40 + 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 * (player.velocity.length() * float(player.is_on_floor()) + (int(Input.is_action_pressed("crouch"))) * 3)
target_pos += get_moving_bob_offset_pos()
target_rot += get_moving_bob_offset_rot()
camera.transform.origin = camera.transform.origin.lerp(target_pos, delta * 10)
var final_pitch = target_rot.x
var final_roll = target_rot.z
target_rotation = Vector3(final_pitch, 0, final_roll)
func ui_movement_handling(delta: float):
var mouse_offset = -movement.mouse_motion_delta * PlayerConsts.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)
) * PlayerConsts.CAMERA_VELOCITY_MULTIPLIER
var target_offset = mouse_offset + inertia_offset
var weighted_lerp = 1.0 - exp(-PlayerConsts.UI_MOVEMENT_SPEED * delta)
ui.position = ui.position.lerp(ui_base_position + target_offset, weighted_lerp)
_last_camera_global_position = current_camera_global_position
var ui_scaling_target = Vector2(-cam_local_vel.z * PlayerConsts.UI_MOVEMENT_SENSITIVITY * .002 + 1, -cam_local_vel.z * PlayerConsts.UI_MOVEMENT_SENSITIVITY * .002 + 1)
ui.offset_transform_scale = ui.offset_transform_scale.lerp(ui_scaling_target, weighted_lerp)
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, weighted_lerp)
func footstep_sound():
var current_sin_pos = sin(t_bob * PlayerConsts.CAMERA_BOB_FREQUENCY)
if last_bob_y >= -.95 and current_sin_pos < -.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 <= -.60 and current_sin_pos > -.60:
breath.emit(true)
if last_breath_y >= .60 and current_sin_pos < .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 + .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 * .5) * PlayerConsts.CAMERA_SIDEWAYS_BOB_AMPLITUDE)