godot-webrtc-mplayer-testing/scripts/multiplayer.gd
2021-12-07 16:09:50 -06:00

85 lines
2.6 KiB
GDScript

extends Control
# TODO: rename to server browser
onready var is_loaded = false
onready var lobbies = $v/body/lobbies
onready var join_button = $v/head/join
onready var lobbies_label = $v/subhead/label
onready var display_name_edit = $v/subhead/display_name
onready var lobbies_map = {}
func _ready():
display_name_edit.text = Global.client.signaller.display_name
join_button.disabled = true
Global.client.signaller.connect("lobby_new", self, "_lobby_new")
Global.client.signaller.connect("lobby_delete", self, "_lobby_delete")
Global.client.signaller.connect("lobby_joined", self, "_lobby_joined")
Global.client.signaller.connect("lobby_left", self, "_lobby_left")
Global.client.signaller.connect("websocket_disconnected", self, "_signaller_disconnected")
Global.client.signaller.request_lobby_list()
if Global.create_lobby:
Global.create_lobby = false
Global.client.signaller.create_lobby()
func _signaller_disconnected():
Global.main_menu()
func _lobby_joined(_id, _peerId):
Global.lobby()
func _lobby_left(_id):
Global.lobby_browser()
func _on_back_pressed():
Global.main_menu()
func _on_create_lobby_pressed():
Global.client.signaller.create_lobby()
func _on_join_pressed():
var items = lobbies.get_selected_items()
if len(items) > 0:
Global.client.signaller.join_lobby(lobbies.get_item_metadata(items[0])["id"])
func _lobby_new(new_lobbies):
# TODO: handle scrolling so that the user is never too jarred (like chat)
for lobby_index in range(len(new_lobbies)):
var lobby = new_lobbies[lobby_index]
var id = lobby["id"]
var lobby_text = "%s (%d/%d players)" % [lobby["name"], lobby["currentPlayers"], lobby["maxPlayers"]]
if lobbies_map.has(id):
print("Updated Lobby ", lobby)
var i = lobbies_map[id]
lobbies.set_item_metadata(i, lobby)
lobbies.set_item_text(i, lobby_text)
else:
print("New Lobby ", lobby)
lobbies.add_item(lobby_text)
lobbies_map[id] = lobbies.get_item_count() - 1
lobbies.set_item_metadata(lobbies_map[id], lobby)
if Global.join_first_available_lobby:
Global.join_first_available_lobby = false
Global.client.signaller.join_lobby(id)
lobbies_label.text = "Active Lobbies: %d" % lobbies.get_item_count()
func _lobby_delete(id):
if lobbies_map.has(id):
var i = lobbies_map[id]
# TODO: this will break our current mappings due to re-order
lobbies.remove_item(i)
lobbies_label.text = "Active Lobbies: %d" % lobbies.get_item_count()
func _on_lobbies_item_activated(_index):
_on_join_pressed()
func _on_lobbies_item_selected(_index):
join_button.disabled = false
func _on_display_name_text_changed(new_text):
Global.client.signaller.set_display_name(new_text)