extends Camera3D

@export var SPEED = 10.0

func _enter_tree() -> void:
	set_multiplayer_authority(int(name))

func _ready() -> void:
	if not is_multiplayer_authority():
		return
	# Move to level controller when possible
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	current = true

func _process(delta: float) -> void:
	if not is_multiplayer_authority():
		return
	var xz_plane = Input.get_vector("plr_strafe_l","plr_strafe_r","plr_forward","plr_back")
	var y = Input.get_axis("spc_down","spc_up")
	
	var direction = Vector3(xz_plane.x,y,xz_plane.y)
	global_position += global_basis * direction * SPEED * delta

func rotate_camera(x,y) -> void:
	rotate_y(x)
	rotation.x = clamp(rotation.x + y,-PI/2,PI/2)
	orthonormalize()

func _input(event: InputEvent) -> void:
	if not is_multiplayer_authority():
		return
	if event is InputEventMouseMotion:
		rotate_camera(-event.relative.x  * ClientSettings.SENSITIVITY,-event.relative.y  * ClientSettings.SENSITIVITY)

@rpc("any_peer","call_local","reliable")
func set_after_spawn(start_position: Vector3, _team: int):
	global_position = start_position
