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

159 lines
5.1 KiB
GDScript

extends MarginContainer
onready var peers_grid = $v/body/peers/p/peers
onready var lobby_name = $v/head/lobby_info
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
onready var auto_scroll_chk = $v/body/v/chat_head/auto_scroll
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 peer = preload("res://objects/peer.tscn")
onready var peers = {}
onready var peers_id_mappings = {}
onready var is_host = false
func _ready():
Global.signaller_client.connect("peer_data", self, "_peer_data")
Global.signaller_client.connect("peer_left", self, "_peer_left")
Global.signaller_client.connect("lobby_left", self, "_lobby_left")
Global.signaller_client.connect("lobby_data", self, "_lobby_data")
Global.signaller_client.connect("disconnected", self, "_signaller_disconnected")
Global.negotiator.connect("connection_succeeded", self, "_connection_succeeded")
# lobby_name.text = "%s" % Global.signaller_client.lobby_name
Global.signaller_client.call_deferred("request_peer_list")
call_deferred("add_chat", "# Connected to %s" % Global.signaller_client.get_lobby_name())
# hide/show controls depending on whether or not we're the host
is_host = Global.signaller_client.is_host()
if is_host:
ready_up.queue_free()
else:
lobby_name.editable = false
max_players.editable = false
start.queue_free()
lock.queue_free()
# until we fully connect
ready_up.disabled = true
chat_edit.disabled = true
send_button.disabled = true
print("Setting up lobby... %s %s" % [Global.signaller_client.peer_id, is_host])
func _connection_succeeded():
ready_up.disabled = false
send_button.disabled = false
func _lobby_update(l):
for u in l:
if !is_host:
max_players.text = str(u.maxPlayers)
lobby_name.text = u.name
print(l)
break
func _peer_data(peers):
call_deferred("update_player_count")
if is_host: call_deferred("update_can_start")
for p in peers:
if peers.has(p.id): _update_peer(p.id, p)
else: _add_peer(p.id, p)
func _add_peer(id, peer_data):
call_deferred("update_player_count")
var new_peer = peer.instance()
if peer_data.peerId == 1: peer_data.ready = true
new_peer.set_with_dict(peer_data)
peers_grid.add_child(new_peer)
peers[id] = new_peer
peers_id_mappings[int(peer_data.peerId)] = id
add_chat("> %s joined the lobby" % new_peer.display_name)
func _update_peer(id, peer_data):
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])
peers[id].set_with_dict(peer_data)
func _peer_left(leavers):
call_deferred("update_player_count")
for data in leavers:
var id = data.id
if peers.has(id):
var peer_id = peers[id].peerId
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)
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
remotesync func add_chat(message):
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()
func send_chat_message():
var message = chat_edit.text
if message != "":
rpc("add_chat", "%s: %s" % [Global.signaller_client.display_name, message])
chat_edit.text = ""
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
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
Global.goto_game()
remotesync func set_ready(ready):
var from = get_tree().get_rpc_sender_id()
print(from)
peer_by_peer_id(from).ready = ready
if is_host: update_can_start()
func _on_TextEdit_text_entered(_new_text):
if !send_button.disabled:
send_chat_message()
func _lobby_left(_id): Global.lobby_browser()
func _on_leave_button_pressed(): Global.lobby_browser()
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.signaller_client.set_lobby_name(new_text)
func _signaller_disconnected(): Global.main_menu()