SpaceClient/Scripts/RootNode.gd

63 lines
1.9 KiB
GDScript

extends Node
const PORT : int = 9374
const Arena = preload("res://Scenes/Arena.tscn")
var console_output : TextEdit = null
var peer : NetworkedMultiplayerENet = null
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")
console_output = $ConsoleOutput
func _process(delta : float) -> void:
if Input.is_action_just_pressed("toggle_console"):
self.toggle_console()
func _connected_to_server() -> void:
self.console_print("Connected to server") # Let the server know all about us (eg name)
func _server_disconnected() -> void:
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)
func _connection_failed() -> void:
var button : Button = $Lobby/StartButton
button.disabled = false
self.console_print("Connection Error")
func console_print(text : String) -> void:
console_output.text += "\n" + text
console_output.cursor_set_line(console_output.get_line_count())
print(text)
func toggle_console() -> bool:
console_output.visible = !console_output.visible
return console_output.visible
func _on_StartButton_pressed() -> void:
var button : Button = $Lobby/StartButton
var ip_addr : String = $Lobby/IPLineEdit.text
button.disabled = true
peer = NetworkedMultiplayerENet.new()
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...')