]> Untitled Git - frog-ninja.git/blob - player/input/PlayerCameraHandler.gd
Refactoring
[frog-ninja.git] / player / input / PlayerCameraHandler.gd
1 extends Node3D
2 class_name PlayerCameraHandler
3
4
5 @export_range(1.0, 10.0) var camera_distance := 3.0
6
7 @export_range(0.0, 1.0) var mouse_sensitivity := 0.2
8 @export_range(0.0, 1.0) var mouse_sensitivity_x := 1.0
9 @export_range(0.0, 1.0) var mouse_sensitivity_y := mouse_sensitivity_x / 2
10
11 @export_range(0.0, 1.0) var joystick_sensitivity := 0.15
12 @export_range(0.0, 10.0) var joystick_sensitivity_x := 4.0
13 @export_range(0.0, 10.0) var joystick_sensitivity_y := joystick_sensitivity_x / 2
14
15
16 @onready var spring: SpringArm3D = $SpringArm3D
17
18
19 # Called when the node enters the scene tree for the first time.
20 func _ready() -> void:
21         spring.spring_length = camera_distance
22
23
24 # Called every frame. 'delta' is the elapsed time since the previous frame.
25 func _process(delta: float) -> void:
26         spring.spring_length = lerp(
27                 spring.spring_length, camera_distance, delta 
28         )
29
30
31 func update(input: InputPacket, delta: float):
32         var camera_input_direction := input.camera_input_direction
33         
34         if input.camera_input_method == CameraInput.MOUSE:
35                 camera_input_direction *= Vector2(
36                         mouse_sensitivity_x, -mouse_sensitivity_y
37                         ) * mouse_sensitivity
38         elif input.camera_input_method == CameraInput.JOYSTICK:
39                 camera_input_direction *= Vector2(
40                         joystick_sensitivity_x, -joystick_sensitivity_y
41                         ) * joystick_sensitivity
42         
43         # vertical camera rotation
44         rotation.x += camera_input_direction.y * delta
45         rotation.x = clamp(rotation.x, -PI / 6, PI / 3)
46
47         # horizontal camera rotation
48         rotation.y -= camera_input_direction.x * delta