]> Untitled Git - frog-ninja.git/blob - player/input/PlayerCameraHandler.gd
Moved camera vector stuff into CameraHandler
[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 @onready var camera: Camera3D = $SpringArm3D/Camera3D
18
19
20 # Called when the node enters the scene tree for the first time.
21 func _ready() -> void:
22         spring.spring_length = camera_distance
23
24
25 # Called every frame. 'delta' is the elapsed time since the previous frame.
26 func _process(delta: float) -> void:
27         spring.spring_length = lerp(
28                 spring.spring_length, camera_distance, delta 
29         )
30
31
32 func update(input: InputPacket, delta: float):
33         var camera_input_direction := input.camera_input_direction
34         
35         if input.camera_input_method == CameraInput.MOUSE:
36                 camera_input_direction *= Vector2(
37                         mouse_sensitivity_x, -mouse_sensitivity_y
38                         ) * mouse_sensitivity
39         elif input.camera_input_method == CameraInput.JOYSTICK:
40                 camera_input_direction *= Vector2(
41                         joystick_sensitivity_x, -joystick_sensitivity_y
42                         ) * joystick_sensitivity
43         
44         # vertical camera rotation
45         rotation.x += camera_input_direction.y * delta
46         rotation.x = clamp(rotation.x, -PI / 6, PI / 3)
47
48         # horizontal camera rotation
49         rotation.y -= camera_input_direction.x * delta
50
51
52 # Get the XZ input direction based on player's input relative to the camera
53 func get_xz_direction_relative_to_camera(d: Vector2) -> Vector2:
54         if camera:
55                 var forward := camera.global_basis.z
56                 var right := camera.global_basis.x
57                 var dir3 := forward * d.y + right * d.x
58                 return Vector2(dir3.x, dir3.z).normalized()
59         else:
60                 return Vector2.ZERO