2021-11-24 04:44:53 +00:00
|
|
|
extends Control
|
|
|
|
# Ignore discarded return values
|
2021-11-24 18:17:07 +00:00
|
|
|
# warning-ignore-all:return_value_discarded
|
2021-11-24 08:48:02 +00:00
|
|
|
onready var player_list = find_node("Player List")
|
2021-11-24 18:17:07 +00:00
|
|
|
onready var ip_address = find_node("IP Address")
|
|
|
|
onready var name_popup = find_node("Change Name")
|
|
|
|
onready var game_popup = find_node("Connect to Game")
|
2021-11-24 04:44:53 +00:00
|
|
|
|
|
|
|
# TODO: Write a function to update Player List with the list of attached players
|
|
|
|
|
|
|
|
func _on_peers_updated():
|
|
|
|
var connected_peers = ""
|
|
|
|
for peer in Net.peer_info:
|
|
|
|
connected_peers += ("%s\n" % Net.peer_info[peer]["name"])
|
|
|
|
pass
|
2021-11-24 08:48:02 +00:00
|
|
|
player_list.text = connected_peers.rsplit("\n", true, 1)[0].c_unescape()
|
2021-11-24 04:44:53 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
func set_IP_Address_text(show):
|
2021-11-28 08:26:00 +00:00
|
|
|
# Show the IP address (or nothing)
|
2021-11-24 04:44:53 +00:00
|
|
|
if show:
|
2021-11-28 06:08:40 +00:00
|
|
|
# ip_address.text = "IP: %sPort:%s" % [Net.get_ip(), Net.DEFAULT_PORT]
|
|
|
|
ip_address.text = "IP: %s" % Net.get_ip()
|
2021-11-24 04:44:53 +00:00
|
|
|
else:
|
2021-11-24 18:17:07 +00:00
|
|
|
ip_address.text = ""
|
2021-11-24 04:44:53 +00:00
|
|
|
|
|
|
|
func _ready():
|
|
|
|
Net.connect("peers_updated", self, "_on_peers_updated")
|
2021-11-24 08:48:02 +00:00
|
|
|
Net.connect("disconnected", self, "_on_Net_disconnected")
|
2021-11-24 18:17:07 +00:00
|
|
|
name_popup.get_node("Name Entry").text = Net.get_hostname()
|
2021-11-24 04:44:53 +00:00
|
|
|
_on_peers_updated()
|
|
|
|
pass
|
|
|
|
|
2021-11-28 06:08:40 +00:00
|
|
|
func show_Connected_Options():
|
2021-11-24 18:17:07 +00:00
|
|
|
# [Hide]/Show the host options
|
2021-11-28 06:08:40 +00:00
|
|
|
get_node("Lobby Options/Connected Options/Host Options").visible = Net.hosting
|
2021-11-24 18:17:07 +00:00
|
|
|
# [Hide]/Show the host and connect buttons
|
2021-11-28 06:08:40 +00:00
|
|
|
get_node("Lobby Options/Host or Connect").visible = !Net.connected
|
2021-11-24 18:17:07 +00:00
|
|
|
# [Show]/Hide the host options
|
2021-11-28 06:08:40 +00:00
|
|
|
get_node("Lobby Options/Connected Options").visible = Net.connected
|
2021-11-24 04:44:53 +00:00
|
|
|
|
|
|
|
# Buttons
|
|
|
|
# Host Button: Host a game
|
|
|
|
# Hides the connect button
|
|
|
|
func _on_Host_Button_pressed():
|
2021-11-24 18:17:07 +00:00
|
|
|
# Make noise
|
|
|
|
AudioBus.emit_signal("button_clicked")
|
2021-11-24 04:44:53 +00:00
|
|
|
# Show the host IP address
|
|
|
|
set_IP_Address_text(true)
|
|
|
|
# Begin hosting
|
|
|
|
Net.start_host()
|
2021-11-28 06:08:40 +00:00
|
|
|
# Show "Connected Options"
|
|
|
|
show_Connected_Options()
|
2021-11-24 04:44:53 +00:00
|
|
|
|
2021-11-24 09:07:27 +00:00
|
|
|
# Disconnect
|
2021-11-24 04:44:53 +00:00
|
|
|
# Disconnect from (or stop hosting) a game
|
|
|
|
# Shows the host/connect buttons
|
|
|
|
func _on_Disconnect_Button_pressed():
|
2021-11-24 18:17:07 +00:00
|
|
|
# Make noise
|
|
|
|
AudioBus.emit_signal("button_clicked")
|
2021-11-24 08:48:02 +00:00
|
|
|
# Disconnect
|
|
|
|
Net.disconnect_host()
|
|
|
|
# Hide the host IP address
|
|
|
|
set_IP_Address_text(false)
|
2021-11-28 06:08:40 +00:00
|
|
|
# Hide "Connected Options"
|
|
|
|
show_Connected_Options()
|
2021-11-24 08:48:02 +00:00
|
|
|
|
2021-11-24 18:17:07 +00:00
|
|
|
func _on_Start_Game_pressed():
|
|
|
|
# If there are enough players for a game
|
|
|
|
if Net.peer_info.size() >= 2:
|
|
|
|
# Start the game for all players
|
|
|
|
rpc("start_game")
|
|
|
|
pass # Replace with function body.
|
|
|
|
|
2021-11-24 08:48:02 +00:00
|
|
|
func _on_Net_disconnected():
|
|
|
|
# Hide the host IP address
|
2021-11-24 04:44:53 +00:00
|
|
|
set_IP_Address_text(false)
|
2021-11-28 06:08:40 +00:00
|
|
|
# Hide "Connected Options"
|
|
|
|
show_Connected_Options()
|
2021-11-24 04:44:53 +00:00
|
|
|
|
|
|
|
func _on_Change_Name_Button_pressed():
|
2021-11-24 18:17:07 +00:00
|
|
|
# Make noise
|
|
|
|
AudioBus.emit_signal("button_clicked")
|
2021-11-24 04:44:53 +00:00
|
|
|
# Show the Change Name dialogue
|
|
|
|
get_node("Change Name").popup_centered()
|
2021-11-28 06:08:40 +00:00
|
|
|
get_node("Change Name/Name Entry").grab_focus()
|
2021-11-24 04:44:53 +00:00
|
|
|
|
|
|
|
func _on_Connect_Button_pressed():
|
2021-11-24 18:17:07 +00:00
|
|
|
# Make noise
|
|
|
|
AudioBus.emit_signal("button_clicked")
|
2021-11-24 04:44:53 +00:00
|
|
|
# Show the Connect to Game dialogue
|
2021-11-28 06:08:40 +00:00
|
|
|
game_popup.popup_centered()
|
|
|
|
game_popup.get_node("IP and Port Entry").grab_focus()
|
|
|
|
game_popup.get_node("IP and Port Entry").caret_position = -1
|
2021-11-24 04:44:53 +00:00
|
|
|
|
|
|
|
func _on_Exit_Lobby_pressed():
|
2021-11-24 18:17:07 +00:00
|
|
|
# Make noise
|
|
|
|
AudioBus.emit_signal("button_clicked")
|
2021-11-24 04:44:53 +00:00
|
|
|
# Disconnect
|
|
|
|
if Net.connected:
|
|
|
|
Net.disconnect_host()
|
|
|
|
# Close Lobby menu
|
|
|
|
queue_free()
|
2021-11-24 08:48:02 +00:00
|
|
|
|
|
|
|
func _on_IP_and_Port_Entry_text_entered(text):
|
2021-11-24 18:17:07 +00:00
|
|
|
# Make noise
|
|
|
|
AudioBus.emit_signal("button_clicked")
|
2021-11-24 08:48:02 +00:00
|
|
|
# Split it into IP and Port segments
|
|
|
|
var ip_port = text.split(":")
|
|
|
|
# If text exists and contains valid IP address
|
|
|
|
if ip_port.size() > 0 and ip_port[0].is_valid_ip_address():
|
|
|
|
# Connect to host
|
2021-11-28 06:08:40 +00:00
|
|
|
var connected = Net.connect_host(ip_port[0])
|
2021-11-24 08:48:02 +00:00
|
|
|
if connected == OK:
|
|
|
|
# Hide the popup
|
2021-11-24 18:17:07 +00:00
|
|
|
game_popup.hide()
|
2021-11-28 06:08:40 +00:00
|
|
|
# Show "Connected Options"
|
|
|
|
show_Connected_Options()
|
2021-11-24 08:48:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _on_Name_Entry_text_entered(text):
|
2021-11-24 18:17:07 +00:00
|
|
|
# Make noise
|
|
|
|
AudioBus.emit_signal("button_clicked")
|
|
|
|
# Check the length of the name
|
|
|
|
if text.length() < 18:
|
|
|
|
# Change the name
|
|
|
|
Net.change_name(text)
|
|
|
|
# Hide the popup
|
|
|
|
name_popup.hide()
|
|
|
|
|
|
|
|
sync func start_game():
|
|
|
|
MessageBus.emit_signal("change_scene", "Gameplay")
|
|
|
|
queue_free()
|
|
|
|
|
|
|
|
|