extends Node3D
class_name SpringArmCharacter

const MOUSE_SENSIBILITY: float = 0.005

@export_category("Objects")
@export var _spring_arm: SpringArm3D = null

var mouse_captured: bool = true

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)  # Захватываем мышь при старте

func _unhandled_input(event) -> void:
	if event.is_action_pressed("ui_cancel") or event.is_action_pressed("toggle_mouse_capture"):
		toggle_mouse_capture()
	
	if mouse_captured and (event is InputEventMouseMotion):
		rotate_y(-event.relative.x * MOUSE_SENSIBILITY)
		_spring_arm.rotate_x(-event.relative.y * MOUSE_SENSIBILITY)
		_spring_arm.rotation.x = clamp(_spring_arm.rotation.x, -PI/4, PI/24)

func toggle_mouse_capture():
	mouse_captured = !mouse_captured
	if mouse_captured:
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	else:
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
