2 class_name CameraHandler
5 enum CameraInput {MOUSE, JOYSTICK}
7 @export_range(1.0, 10.0) var camera_distance := 3.0
8 @export_range(0.0, 1.0) var mouse_sensitivity := 0.15
9 @export_range(0.0, 1.0) var mouse_sensitivity_x := 1.0
10 @export_range(0.0, 1.0) var mouse_sensitivity_y := 0.5
11 @export_range(0.0, 10.0) var joystick_sensitivity_x := 4.0
12 @export_range(0.0, 10.0) var joystick_sensitivity_y := 2.0
15 var camera_input_method := CameraInput.MOUSE
16 var camera_input_direction := Vector2.ZERO
17 var player_input_direction := Vector2.ZERO
20 @onready var _input: InputHandler = %Input
21 @onready var _spring: SpringArm3D = $spring
24 # Called when the node enters the scene tree for the first time.
25 func _ready() -> void:
26 _spring.spring_length = camera_distance
29 func _unhandled_input(event: InputEvent) -> void:
30 # If user clicks on the window, capture the mouse and direct the camera with it
31 if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
34 #_camera_input_direction *= mouse_sensitivity
35 if event is InputEventMouseMotion:
36 camera_input_method = CameraInput.MOUSE
37 camera_input_direction = event.screen_relative * mouse_sensitivity
38 elif event is InputEventJoypadMotion:
39 # TODO: add these settings!
40 camera_input_method = CameraInput.JOYSTICK
41 camera_input_direction = _input.get_camera_input_direction()
42 camera_input_direction *= Vector2(joystick_sensitivity_x, -joystick_sensitivity_y)
45 # Called every frame. 'delta' is the elapsed time since the previous frame.
46 func _process(delta: float) -> void:
47 # vertical camera rotation
48 rotation.x += camera_input_direction.y * mouse_sensitivity_y * delta
49 rotation.x = clamp(rotation.x, -PI / 6, PI / 3)
51 # horizontal camera rotation
52 rotation.y -= camera_input_direction.x * mouse_sensitivity_x * delta
54 # reset mouse movement vector if mouse input
55 if camera_input_method == CameraInput.MOUSE:
56 camera_input_direction = Vector2.ZERO
58 # change spring length
59 _spring.spring_length = lerp(
60 _spring.spring_length, camera_distance, delta