extends Node3D

const SENSITIVITY := 0.003
const PITCH_MIN := -80.0
const PITCH_MAX := 80.0

var pitch := -0.3
var camera_yaw := 0.0
var player_yaw := -2.356

func _ready() -> void:
  get_parent().rotation.y = player_yaw
  rotation = Vector3(pitch, camera_yaw, 0)

func _unhandled_input(event: InputEvent) -> void:
  if event is InputEventMouseMotion:
    var lmb := Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)
    var rmb := Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT)

    if lmb or rmb:
      pitch -= event.relative.y * SENSITIVITY
      pitch = clamp(pitch, deg_to_rad(PITCH_MIN), deg_to_rad(PITCH_MAX))

      if rmb:
        player_yaw -= event.relative.x * SENSITIVITY
        get_parent().rotation.y = player_yaw
      else:
        camera_yaw -= event.relative.x * SENSITIVITY

      rotation = Vector3(pitch, camera_yaw, 0)
