extends CharacterBody3D

var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity")
var speed : float = 5.0
var mouse_sensitivity : float = 0.002
@onready var camera_3d : Camera3D = $Camera3D

func _physics_process(delta : float) -> void:
	if not is_on_floor():
		velocity.y += -gravity * delta
	var input : Vector2 = Input.get_vector("left", "right", "forward", "back")
	var movement_dir : Vector3 = transform.basis * Vector3(input.x, 0, input.y)
	velocity.x = movement_dir.x * speed
	velocity.z = movement_dir.z * speed

	move_and_slide()

func _input(event : InputEvent) -> void:
	if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
		rotate_y(-event.relative.x * mouse_sensitivity)
		camera_3d.rotate_x(-event.relative.y * mouse_sensitivity)
		camera_3d.rotation.x = clampf(camera_3d.rotation.x, -deg_to_rad(70), deg_to_rad(70))
