grunk/src/player/camera_controller.gd

52 lines
1.6 KiB
GDScript3
Raw Normal View History

2025-02-28 15:20:52 -07:00
class_name CameraController extends Node3D
const PITCH_LIMIT := deg_to_rad(85.0)
2025-03-05 16:11:03 -07:00
const FOCUS_SENSITIVITY := 0.2
2025-03-06 14:57:57 -07:00
const FOCUS_ACCELERATION := 8
2025-03-05 16:11:03 -07:00
@onready var player: Player = owner
2025-02-28 15:20:52 -07:00
@onready var _target := Vector2(rotation.x, rotation.y)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
2025-04-19 10:11:10 -06:00
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and player.look_enabled:
2025-02-28 15:20:52 -07:00
camera_motion((event as InputEventMouseMotion).relative)
elif event is InputEventMouseButton:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func camera_motion(motion: Vector2) -> void:
2025-03-28 16:16:43 -06:00
var x_sensitivity: float = Game.settings.mouse_sensitivity_x
var y_sensitivity: float = Game.settings.mouse_sensitivity_y
var invert_pitch: bool = Game.settings.invert_pitch
2025-03-05 16:11:03 -07:00
if player.firing:
# Focus movement when firing
# Game mechanic, should not be user-configurable.
x_sensitivity = FOCUS_SENSITIVITY
y_sensitivity = FOCUS_SENSITIVITY
2025-02-28 15:20:52 -07:00
_target.y -= deg_to_rad(motion.x * x_sensitivity)
_target.x = clampf(
_target.x - deg_to_rad(motion.y * y_sensitivity) * (-1 if invert_pitch else 1),
-PITCH_LIMIT,
PITCH_LIMIT
)
2025-04-19 12:08:16 -06:00
func reset_pitch(tween_duration: float) -> void:
var tween := create_tween()
tween.tween_property(self, "_target:x", 0, tween_duration)
2025-04-18 15:52:31 -06:00
func _physics_process(delta: float) -> void:
var mouse_accel: float = Game.settings.mouse_acceleration
2025-03-05 16:11:03 -07:00
if player.firing:
2025-04-18 15:52:31 -06:00
mouse_accel = FOCUS_ACCELERATION
var weight := 1 - exp(-mouse_accel * delta)
#var weight := mouse_accel / 60.0
rotation.y = lerp_angle(rotation.y, _target.y, weight)
rotation.x = lerp_angle(rotation.x, _target.x, weight)