SpaceClient/Scripts/RootNode.gd

97 lines
2.9 KiB
GDScript
Raw Normal View History

2020-02-04 16:38:02 +00:00
extends Node
const PORT : int = 9374
const Arena = preload("res://Scenes/Arena.tscn")
2021-05-14 10:26:04 +00:00
const Lobby = preload("res://Scenes/Lobby.tscn")
2020-02-11 09:59:27 +00:00
var console_output : TextEdit = null
var peer : NetworkedMultiplayerENet = null
2020-09-27 15:08:20 +00:00
var display_name : String = ""
var player_color : Color = Color.white
var name_dict = {}
var color_dict = {}
2020-02-04 16:38:02 +00:00
func _ready() -> void:
get_tree().connect("connected_to_server", self, "_connected_to_server")
get_tree().connect("server_disconnected", self, "_server_disconnected")
get_tree().connect("connection_failed", self, "_connection_failed")
$Lobby/StartButton.connect("pressed", self, "_on_StartButton_pressed")
2020-02-11 09:59:27 +00:00
console_output = $ConsoleOutput
func _process(delta : float) -> void:
if Input.is_action_just_pressed("toggle_console"):
self.toggle_console()
2020-02-04 16:38:02 +00:00
func _connected_to_server() -> void:
2020-02-11 09:59:27 +00:00
self.console_print("Connected to server") # Let the server know all about us (eg name)
2020-09-27 15:08:20 +00:00
rpc("set_display_name", peer.get_unique_id(), display_name)
rpc("set_player_color", peer.get_unique_id(), player_color)
2020-02-04 16:38:02 +00:00
func _server_disconnected() -> void:
2020-02-11 09:59:27 +00:00
var button : Button = $Lobby/StartButton
if button:
button.disabled = false
else:
self.console_print("Tried to re-enable start button, but the start button could not be found")
self.console_print("Disconnected from server") # Kick us out the game and bring back the lobby (if applicable)
2020-02-04 16:38:02 +00:00
func _connection_failed() -> void:
2020-02-11 09:59:27 +00:00
var button : Button = $Lobby/StartButton
button.disabled = false
self.console_print("Connection Error")
2020-02-04 16:38:02 +00:00
2020-02-11 09:59:27 +00:00
func console_print(text : String) -> void:
console_output.text += "\n" + text
console_output.cursor_set_line(console_output.get_line_count())
print(text)
2020-02-04 16:38:02 +00:00
2020-02-11 09:59:27 +00:00
func toggle_console() -> bool:
console_output.visible = !console_output.visible
return console_output.visible
2020-02-04 16:38:02 +00:00
func _on_StartButton_pressed() -> void:
2020-02-11 09:59:27 +00:00
var button : Button = $Lobby/StartButton
2020-02-04 16:38:02 +00:00
var ip_addr : String = $Lobby/IPLineEdit.text
2020-09-27 15:08:20 +00:00
display_name = $Lobby/NameLineEdit.text
player_color = $Lobby/ColorPicker.color
2020-02-11 09:59:27 +00:00
button.disabled = true
peer = NetworkedMultiplayerENet.new()
2020-02-04 16:38:02 +00:00
peer.create_client(ip_addr, PORT)
get_tree().set_network_peer(peer)
func arena_ready() -> void:
rpc_id(1, 'client_ready', peer.get_unique_id())
remote func start_game() -> void:
$Lobby.queue_free()
var arena = Arena.instance()
add_child(arena)
arena.init()
console_print('Starting Game...')
2021-05-14 10:26:04 +00:00
$AudioStreamPlayer.switch_theme("combat")
2020-09-27 15:08:20 +00:00
remote func add_name_to_dict(id : int, display_name : String):
name_dict[id] = display_name
remote func add_color_to_dict(id : int, color : Color):
color_dict[id] = color
2021-05-14 10:26:04 +00:00
func switch_to_lobby():
$Arena.queue_free()
var lobby = Lobby.instance()
add_child(lobby)
$Lobby/StartButton.connect("pressed", self, "_on_StartButton_pressed")
console_print('Switching back to Lobby...')
peer.close_connection()
$AudioStreamPlayer.switch_theme("main")
2020-09-27 15:08:20 +00:00
2021-05-14 10:26:04 +00:00
func exit_button_pressed():
rpc_id(1, "request_exit")
2020-09-27 15:08:20 +00:00
2021-05-14 10:26:04 +00:00
remote func exit_requested():
switch_to_lobby()
2020-09-27 15:08:20 +00:00