extends Node3D

func _ready() -> void:
	if not multiplayer.is_server():
		return

	multiplayer.peer_connected.connect(add_player)
	multiplayer.peer_disconnected.connect(del_player)

	# Spawn already connected players.
	for id in multiplayer.get_peers():
		add_player(id)

	# Spawn the local player unless this is a dedicated server export.
	if not OS.has_feature("dedicated_server"):
		add_player(1)

func _exit_tree() -> void:
	if not multiplayer.is_server():
		return
	multiplayer.peer_connected.disconnect(add_player)
	multiplayer.peer_disconnected.disconnect(del_player)


func add_player(id: int) -> void:
	var character: Player = preload("res://src/scenes/characters/player.tscn").instantiate()
	character.name = "%d" % id
	$Players.add_child(character, true)


func del_player(id: int) -> void:
	if not $Players.has_node(str(id)):
		return
	$Players.get_node(str(id)).queue_free()
