]> Untitled Git - frog-ninja.git/blob - player/input/CameraHandler.gd
4900293ee59d6654ed064ca5ca0c069e37fee17f
[frog-ninja.git] / player / input / CameraHandler.gd
1 extends Node3D
2 class_name CameraHandler
3
4
5 enum CameraInput {MOUSE, JOYSTICK}
6
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
13
14
15 var camera_input_method := CameraInput.MOUSE 
16 var camera_input_direction := Vector2.ZERO
17 var player_input_direction := Vector2.ZERO
18
19
20 @onready var input: InputHandler
21 @onready var spring: SpringArm3D = $spring
22
23
24 # Called when the node enters the scene tree for the first time.
25 func _ready() -> void:
26         spring.spring_length = camera_distance
27
28
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:
32                 return
33
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)
43
44
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)
50
51         # horizontal camera rotation
52         rotation.y -= camera_input_direction.x * mouse_sensitivity_x * delta
53
54         # reset mouse movement vector if mouse input
55         if camera_input_method == CameraInput.MOUSE:
56                 camera_input_direction = Vector2.ZERO
57                 
58         # change spring length
59         spring.spring_length = lerp(
60                 spring.spring_length, camera_distance, delta 
61         )