extends Node


func _ready() -> void:
	multiplayer.peer_connected.connect(on_peer_connected)
	multiplayer.peer_disconnected.connect(on_peer_disconnected)
	Lobby.lobby_created.emit(add_self)
	Lobby.lobby_joined.emit(add_self)
	Lobby.lobby_closed.emit(clear)

func on_peer_connected(id: int) -> void:
	var label = Label.new()
	label.text = str(id)
	label.name = str(id)
	add_child(label,true)

func on_peer_disconnected(id: int) -> void:
	get_node(str(id)).queue_free()

func add_self() -> void:
	var label = Label.new()
	label.text = str(multiplayer.get_unique_id())
	label.name = str(multiplayer.get_unique_id())
	add_child(label,true)

func clear() -> void:
	for child in get_children():
		child.queue_free()
