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

126 lines
3.8 KiB
GDScript3
Raw Normal View History

2021-12-06 21:56:37 -06:00
extends MarginContainer
2021-11-15 16:25:13 -06:00
2021-12-06 21:56:37 -06:00
onready var peers = $v/body/peers/peers
2021-12-07 16:09:50 -06:00
onready var lobby_name = $v/head/lobby_info
2021-12-07 09:32:20 -06:00
onready var peers_list_label = $v/body/peers/label
2021-12-02 15:13:12 -06:00
onready var cursors = {}
2021-12-07 09:32:20 -06:00
onready var chat = $v/body/v/messages
onready var chat_edit = $v/body/v/h/chat
2021-12-07 13:51:34 -06:00
onready var auto_scroll_chk = $v/body/v/chat_head/auto_scroll
2021-11-17 13:57:45 -06:00
2021-12-07 16:09:50 -06:00
onready var max_players = $v/head/max_players
onready var ready_up = $v/head/ready_up
onready var lock = $v/head/lock
onready var start = $v/head/start
onready var ready_tex: Texture = load("res://assets/img/check.png")
onready var not_ready_tex: Texture = load("res://assets/img/cross.png")
onready var can_start = true
onready var peers_map = {}
2021-11-15 16:25:13 -06:00
func _ready():
2021-11-17 13:57:45 -06:00
Global.client.signaller.connect("peer_joined", self, "_peer_joined")
Global.client.signaller.connect("peer_left", self, "_peer_left")
2021-12-07 12:33:09 -06:00
# Global.client.connect("peer_disconnected", self, "_peer_left")
2021-11-17 13:57:45 -06:00
Global.client.signaller.connect("lobby_left", self, "_lobby_left")
2021-12-07 16:09:50 -06:00
lobby_name.text = "%s" % Global.client.signaller.lobby_name
2021-11-17 16:24:00 -06:00
Global.client.signaller.connect("websocket_disconnected", self, "_signaller_disconnected")
2021-12-06 16:17:36 -06:00
Global.client.signaller.request_peer_list()
2021-12-07 09:32:20 -06:00
call_deferred("add_chat", "# Connected to %s" % Global.client.signaller.lobby_name)
2021-12-07 16:09:50 -06:00
var is_host = Global.client.signaller.peer_id == 1
if is_host:
ready_up.queue_free()
else:
max_players.editable = false
start.queue_free()
lock.queue_free()
2021-12-07 09:32:20 -06:00
func _host_suffix(id):
if id == 1:
return " (Host)"
else:
return ""
2021-12-06 16:17:36 -06:00
2021-11-17 16:24:00 -06:00
func _signaller_disconnected():
Global.main_menu()
2021-11-17 13:57:45 -06:00
func _peer_joined(joined_peers):
2021-12-07 09:32:20 -06:00
call_deferred("update_player_count")
2021-12-07 16:09:50 -06:00
for peer_index in range(len(joined_peers)):
var peer = joined_peers[peer_index]
var id = peer["id"]
var peer_text = "%s%s" % [peer["name"], _host_suffix(peer["peerId"])]
if peers_map.has(id):
var i = peers_map[id]
var old_peer = peers.get_item_metadata(i)
peers.set_item_metadata(i, peer)
# announce changes?
else:
call_deferred("add_chat", "> %s joined the lobby" % peer["name"])
var ready = false
var tex = not_ready_tex
if peerId == 1:
ready = true
tex = ready_tex
peers.add_item(peer_text, tex)
var i = peers.get_item_count() - 1
peers.set_item_metadata(i, peer)
peers_map[id] = i
if !ready: can_start = false
2021-12-07 09:32:20 -06:00
2021-12-06 16:27:16 -06:00
func _peer_left(ids):
2021-12-07 09:32:20 -06:00
call_deferred("update_player_count")
print("Peer(s) Leaving: %s" % ids)
2021-12-06 16:27:16 -06:00
for data in ids:
2021-12-07 09:32:20 -06:00
print(data)
2021-12-06 16:27:16 -06:00
var id = data["id"]
for i in range(peers.get_item_count()):
2021-12-07 09:32:20 -06:00
var md = peers.get_item_metadata(i)
if md and md.has("id") and md["id"] == id:
2021-12-06 16:27:16 -06:00
peers.remove_item(i)
2021-12-07 09:32:20 -06:00
call_deferred("add_chat", "< %s left the lobby" % md["name"])
2021-12-06 16:27:16 -06:00
return
2021-12-07 09:32:20 -06:00
func update_player_count():
peers_list_label.text = "Players: %d" % peers.get_item_count()
2021-11-15 16:25:13 -06:00
func _on_Button_pressed():
2021-12-07 16:09:50 -06:00
send_chkkkkkkat_message()
func _on_ready_up_toggled(button_pressed: bool):
func _on_lobby_info_text_changed(new_text: String):
Global.client.signaller.set_lobby_name(new_text)
2021-11-17 13:57:45 -06:00
2021-12-07 09:32:20 -06:00
remotesync func add_chat(message):
2021-12-07 13:51:34 -06:00
if auto_scroll_chk.pressed: call_deferred("scroll_chat_to_bottom")
else:
call_deferred("preserve_chat_scroll", chat.cursor_get_line(), chat.get_selection_to_line(), chat.get_selection_from_line(), chat.get_selection_to_column(), chat.get_selection_from_column())
chat.text += "\n" + message
func scroll_chat_to_bottom():
chat.cursor_set_line(chat.get_line_count() + 1)
chat.scroll_vertical = 1000000000
func preserve_chat_scroll(n, tl, fl, tc, fc):
chat.cursor_set_line(n)
chat.select(fl, fc, tl, tc)
chat.center_viewport_to_cursor()
2021-12-06 16:17:36 -06:00
2021-11-17 13:57:45 -06:00
func _lobby_left(_id):
Global.lobby_browser()
2021-12-02 15:13:12 -06:00
func _on_leave_button_pressed():
Global.lobby_browser()
2021-12-07 09:32:20 -06:00
func _on_TextEdit_text_entered(_new_text):
send_chat_message()
func send_chat_message():
2021-12-07 13:51:34 -06:00
var message = chat_edit.text
if message != "":
rpc("add_chat", "%s: %s" % [Global.client.signaller.display_name, message])
chat_edit.text = ""