extends Spatial

var RemotePlayer = preload("lib/3dRemotePlayer.tscn")
var mpclient = preload("lib/client_ui.tscn")

# Declare member variables here. Examples:
# var a = 2
# var b = "text"

var id
var remoteplayers = {}

# Called when the node enters the scene tree for the first time.
func _ready():
	mpclient = mpclient.instance()
	mpclient.name="mpclient"
	add_child(mpclient)
	#get_tree().set_network_peer(mpclient.client.rtc_mp)
	mpclient.client.rtc_mp.connect("peer_connected", self, "spawn_remotePlayer")
	mpclient.client.rtc_mp.connect("peer_disconnected", self, "remove_remotePlayer")
	mpclient.client.connect("connected", self, "set_id")
	get_node("mpclient/HideButton").connect("pressed", self, "_on_hide_pressed")

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

func set_id(id):
	self.id = id
	 
func spawn_remotePlayer(id):
	var remoteplayer = RemotePlayer.instance()
	remoteplayer.owner = self
	remoteplayer.name = "Player "+str(id)
	remoteplayer.id = id
	get_node("RemotePlayers").add_child(remoteplayer, true)
	remoteplayers[id] = remoteplayer
	
func remove_remotePlayer(id):
	print("removing player", id)
	if remoteplayers.has(id):
		remoteplayers[id].queue_free()

func _on_hide_pressed():
	$MultiplayerButton.show()


func _on_Multiplayer_pressed():
	$MultiplayerButton.hide()
	mpclient.show()
