extends Control

var PlayerList : VBoxContainer
var PlayerPanel : PackedScene
var GameScene

# Called when the node enters the scene tree for the first time.
func _ready():
	PlayerList = get_node("ScrollContainer/VBoxContainer")
	PlayerPanel = load("res://PlayerPanel.tscn")
	GameScene = preload("res://GameScene.tscn").instantiate()
	Player_Manger.clear_players()
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass


func _on_button_pressed():
	var player = PlayerPanel.instantiate()
	var player_name : TextEdit = player.get_child(0)
	if player_name != null:
		player_name.text = "Player " + str(PlayerList.get_child_count() + 1)
		player_name.connect("text_changed", Callable(self, "_on_text_changed"))
		PlayerList.add_child(player)
	else:
		print("Error: player_name is null")
	pass # Replace with function body.

func _on_text_changed():
	var players = PlayerList.get_children()
	for player in players:
		var player_name : TextEdit = player.get_child(0)
		if player_name != null and player_name.text == "":
			return
	pass # Replace with function body.

func _on_button_play_pressed():
	var players = PlayerList.get_children()
	for player in players:
		var player_name : TextEdit = player.get_child(0)
		if player_name != null:
			var player_instance = Player.new(player_name.text)
			Player_Manger.add_player(player_instance)
		else:
			print("Error: player_name is null")
	get_tree().change_scene_to_file("res://GameScene.tscn")
	pass # Replace with function body.
