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

158 lines
5 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-08 16:35:37 -06:00
onready var peers_grid = $v/body/peers/p/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
onready var chat = $v/body/v/messages
onready var chat_edit = $v/body/v/h/chat
onready var send_button = $v/body/v/h/send
2021-12-07 13:51:34 -06:00
onready var auto_scroll_chk = $v/body/v/chat_head/auto_scroll
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
2021-12-08 16:35:37 -06:00
onready var peer = preload("res://objects/peer.tscn")
onready var peers = {}
onready var peers_id_mappings = {}
2021-12-08 16:35:37 -06:00
onready var is_host = false
2021-12-07 16:09:50 -06:00
2021-11-15 16:25:13 -06:00
func _ready():
2021-12-11 00:08:28 -06:00
Global.client.connect("peer_created", self, "_peer_created")
Global.client.connect("peer_left", self, "_peer_left")
Global.client.connect("lobby_left", self, "_lobby_left")
Global.client.connect("lobby_data", self, "_lobby_data")
2021-12-11 00:08:28 -06:00
Global.client.connect("lobby_joined", self, "_lobby_joined")
2021-12-08 16:35:37 -06:00
Global.client.connect("signaller_disconnected", self, "_signaller_disconnected")
2021-12-11 00:08:28 -06:00
Global.client.connect("webrtc_connection_succeeded", self, "_webrtc_connection_succeeded")
2021-12-08 16:35:37 -06:00
# lobby_name.text = "%s" % Global.client.lobby_name
2021-12-08 16:35:37 -06:00
2021-12-11 00:08:28 -06:00
# call_deferred("add_chat", "# to %s" % Global.client.get_lobby_name())
2021-12-07 09:32:20 -06:00
2021-12-08 16:35:37 -06:00
# hide/show controls depending on whether or not we're the host
2021-12-11 00:08:28 -06:00
_update_ui()
2021-12-08 16:35:37 -06:00
2021-12-11 00:08:28 -06:00
func _signaller_disconnected():
Global.main_menu()
func _lobby_joined(data):
add_chat("# Connected to %s" % data.name)
_update_ui()
Global.client.request_peer_list()
func _webrtc_connection_succeeded():
if !is_host:
ready_up.disabled = false
send_button.disabled = false
chat_edit.editable = true
Global.client.request_peer_list()
func _peer_created(peer):
2021-12-07 09:32:20 -06:00
call_deferred("update_player_count")
2021-12-08 23:39:56 -06:00
if is_host: call_deferred("update_can_start")
2021-12-11 00:08:28 -06:00
if peers.has(peer.id): _update_peer(peer.id, peer)
else: _add_peer(peer.id, peer)
2021-12-07 09:32:20 -06:00
2021-12-11 00:08:28 -06:00
func _add_peer(id, p):
2021-12-07 09:32:20 -06:00
call_deferred("update_player_count")
2021-12-08 16:35:37 -06:00
var new_peer = peer.instance()
2021-12-11 00:08:28 -06:00
if p.id == 1: p.ready = true
new_peer.set_with_dict(p)
2021-12-08 16:35:37 -06:00
peers_grid.add_child(new_peer)
peers[id] = new_peer
add_chat("> %s joined the lobby" % new_peer.display_name)
2021-12-07 16:09:50 -06:00
2021-12-08 16:35:37 -06:00
func _update_peer(id, peer_data):
2021-12-09 21:10:17 -06:00
if peer_data.ready != peers[id].ready:
var update_text = "now ready" if peer_data.ready else "no longer ready"
add_chat("! %s is %s" % [peers[id].display_name, update_text])
2021-12-08 16:35:37 -06:00
peers[id].set_with_dict(peer_data)
2021-11-17 13:57:45 -06:00
func _peer_left(leavers):
2021-12-08 16:35:37 -06:00
call_deferred("update_player_count")
for data in leavers:
var id = data.id
2021-12-08 16:35:37 -06:00
if peers.has(id):
var peer_id = peers[id].peerId
2021-12-08 16:35:37 -06:00
call_deferred("add_chat", "< %s left the lobby" % peers[id].display_name)
peers[id].queue_free()
peers.erase(id)
peers_id_mappings.erase(peer_id)
2021-12-08 16:35:37 -06:00
func peer_by_peer_id(peer_id):
if peers_id_mappings.has(peer_id):
var p = peers_id_mappings[peer_id]
if peers.has(p):
return peers[p]
return null
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-12-07 09:32:20 -06:00
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.display_name, message])
2021-12-07 13:51:34 -06:00
chat_edit.text = ""
2021-12-08 16:35:37 -06:00
func update_can_start():
var can_start = true
for p in peers:
var peer = peers[p]
if !peer.ready:
can_start = false
break
if can_start: add_chat("! All players ready - game may now be started")
start.disabled = !can_start
2021-12-08 23:39:56 -06:00
func _on_start_pressed():
if is_host: rpc("start_game")
remotesync func start_game():
Global.negotiator.peers = peers
Global.negotiator.peers_id_mappings = peers_id_mappings
2021-12-08 23:39:56 -06:00
Global.goto_game()
remotesync func set_ready(ready):
var from = get_tree().get_rpc_sender_id()
2021-12-11 00:08:28 -06:00
print("Set Ready: %s %s" % [from, ready])
Global.client.peers[from].ready = ready
if is_host: update_can_start()
func _on_TextEdit_text_entered(_new_text):
if !send_button.disabled:
send_chat_message()
2021-12-11 00:08:28 -06:00
func _update_ui():
is_host = Global.client.is_host()
ready_up.visible = !is_host
lobby_name.editable = is_host
max_players.editable = is_host
start.visible = is_host
lock.visible = is_host
var is_in_lobby = Global.client.is_in_lobby()
ready_up.disabled = !is_in_lobby
chat_edit.editable = is_in_lobby
send_button.disabled = !is_in_lobby
2021-12-08 16:35:37 -06:00
func _lobby_left(_id): Global.lobby_browser()
2021-12-11 00:08:28 -06:00
func _on_leave_button_pressed(): Global.client.leave_lobby()
2021-12-08 16:35:37 -06:00
func update_player_count(): peers_list_label.text = "Players: %d" % peers.size()
func _on_Button_pressed(): send_chat_message()
func _on_ready_up_toggled(button_pressed: bool): rpc("set_ready", button_pressed)
func _on_lobby_info_text_changed(new_text: String): Global.client.set_lobby_name(new_text)