godot-webrtc-mplayer-testing/scripts/screens/lobby_browser.gd

108 lines
3 KiB
GDScript3
Raw Permalink Normal View History

2021-11-17 08:18:12 -06:00
extends Control
onready var is_loaded = false
2021-12-08 16:35:37 -06:00
onready var lobbies_grid = $v/body/p/lobbies
onready var zero_state = $v/body/p/lobbies/zero_state
2021-12-06 21:56:37 -06:00
onready var lobbies_label = $v/subhead/label
2021-12-07 09:32:20 -06:00
onready var display_name_edit = $v/subhead/display_name
2021-12-06 21:56:37 -06:00
2021-12-08 16:35:37 -06:00
onready var lobbies = {}
onready var lobby = preload("res://objects/lobby.tscn")
2021-12-07 16:09:50 -06:00
2021-11-17 08:18:12 -06:00
func _ready():
2021-12-10 12:31:48 -06:00
display_name_edit.text = Global.client.display_name
Global.client.connect("lobby_data", self, "_lobby_data")
Global.client.connect("lobby_delete", self, "_lobby_delete")
2021-12-11 00:08:28 -06:00
Global.client.connect("lobby_begin_join", self, "_lobby_begin_join")
Global.client.connect("lobby_left", self, "_lobby_left")
Global.client.connect("signaller_connected", self, "_signaller_connected")
Global.client.connect("signaller_disconnected", self, "_signaller_disconnected")
2021-12-11 00:08:28 -06:00
if Global.client.is_signaller_connected():
_signaller_connected()
else:
Global.client.connect_to_signaller()
2021-11-17 16:24:00 -06:00
2021-12-08 16:35:37 -06:00
# TODO: add search
func _input(ev):
2021-12-08 23:39:56 -06:00
pass
# if ev is InputEventKey and ev.pressed:
# if ev.scancode == KEY_T:
# var ls = []
# for n in range(5):
# var j = n + lobbies.size()
# ls.push_back({id = j, name = j, currentPlayers = j, maxPlayers = j, locked = false})
# call_deferred("_lobby_new", ls)
2021-12-08 16:35:37 -06:00
2021-11-17 16:24:00 -06:00
func _signaller_disconnected():
Global.main_menu()
2021-11-17 08:18:12 -06:00
func _signaller_connected():
2021-12-11 00:08:28 -06:00
print("Lobby Browser: Signaller Connected")
if Global.check_onetime_flag("create-lobby"):
call_deferred("_on_create_lobby_pressed")
else:
Global.client.request_lobby_list()
2021-12-11 00:08:28 -06:00
func _lobby_begin_join(data):
print("Joining Lobby: %s" % data)
2021-11-17 13:57:45 -06:00
Global.lobby()
2021-12-11 00:08:28 -06:00
func _lobby_left(_uuid):
2021-11-17 13:57:45 -06:00
Global.lobby_browser()
2021-11-17 08:18:12 -06:00
func _on_back_pressed():
2021-11-17 13:57:45 -06:00
Global.main_menu()
func _on_create_lobby_pressed():
2021-12-11 00:08:28 -06:00
Global.lobby()
Global.client.create_lobby()
2021-11-17 13:57:45 -06:00
2021-12-09 21:10:17 -06:00
func _lobby_data(new_lobbies: Array):
for l in new_lobbies:
2021-12-11 00:08:28 -06:00
if lobbies.has(l.uuid): _update_lobby(l.uuid, l)
else: _add_lobby(l.uuid, l)
2021-12-09 21:10:17 -06:00
if Global.check_onetime_flag("join-first-available-lobby"):
2021-12-11 00:08:28 -06:00
Global.client.join_lobby(l.uuid)
2021-12-06 21:56:37 -06:00
2021-12-11 00:08:28 -06:00
func _lobby_delete(uuid: String):
print("Lobby Deleted: %s" % uuid)
_delete_lobby(uuid)
2021-12-08 16:35:37 -06:00
2021-12-11 00:08:28 -06:00
func _add_lobby(uuid, lobby_data):
2021-12-08 23:39:56 -06:00
call_deferred("_update_lobbies_text")
2021-12-08 16:35:37 -06:00
print("New Lobby ", lobby_data)
# if lobby_data.currentPlayers > 0:
var new_lobby = lobby.instance()
new_lobby.set_with_dict(lobby_data)
lobbies_grid.add_child(new_lobby)
2021-12-11 00:08:28 -06:00
lobbies[uuid] = new_lobby
2021-12-08 16:35:37 -06:00
2021-12-11 00:08:28 -06:00
func _update_lobby(uuid, lobby_data):
2021-12-08 23:39:56 -06:00
call_deferred("_update_lobbies_text")
2021-12-08 16:35:37 -06:00
print("Updated Lobby ", lobby_data)
2021-12-11 00:08:28 -06:00
lobbies[uuid].set_with_dict(lobby_data)
2021-12-08 16:35:37 -06:00
2021-12-11 00:08:28 -06:00
func _delete_lobby(uuid):
2021-12-08 23:39:56 -06:00
call_deferred("_update_lobbies_text")
2021-12-11 00:08:28 -06:00
if lobbies.has(uuid):
2021-12-08 16:35:37 -06:00
print("Removing lobby...")
2021-12-11 00:08:28 -06:00
lobbies_grid.remove_child(lobbies[uuid])
lobbies[uuid].queue_free()
lobbies.erase(uuid)
2021-12-08 16:35:37 -06:00
2021-12-08 23:39:56 -06:00
func _update_lobbies_text():
2021-12-08 16:35:37 -06:00
var n = lobbies.size()
lobbies_label.text = "Active Lobbies: %d" % n
if n < 1:
if !lobbies_grid.is_a_parent_of(zero_state):
lobbies_grid.add_child(zero_state)
if n > 0:
if lobbies_grid.is_a_parent_of(zero_state):
lobbies_grid.remove_child(zero_state)
2021-12-07 09:32:20 -06:00
func _on_display_name_text_changed(new_text):
Global.client.display_name = new_text