extends CharacterBody3D

const SPEED = 5.0
@export var mouse_sensitivity: float = 0.1
@export var capture_mouse: bool = true

var yaw := 0.0
var pitch := 0.0

func _ready() -> void:
	if capture_mouse:
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	# initialize rotation from scene
	yaw = rotation.y
	pitch = rotation.x
	
func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		# Left/right: yaw (rotate player)
		yaw -= event.relative.x * mouse_sensitivity * 0.01  # scale down for comfort
		rotation.y = yaw
	# Handle jump
	#if Input.is_action_just_pressed("ui_accept") and is_on_floor():
func _physics_process(delta: float) -> void:
	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_backwards")
	var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()
