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
|
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 is_host = false
|
2021-12-07 16:09:50 -06:00
|
|
|
|
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")
|
|
|
|
Global.client.signaller.connect("lobby_left", self, "_lobby_left")
|
2021-12-08 16:35:37 -06:00
|
|
|
Global.client.signaller.connect("lobby_new", self, "_lobby_update")
|
|
|
|
|
2021-11-17 16:24:00 -06:00
|
|
|
Global.client.signaller.connect("websocket_disconnected", self, "_signaller_disconnected")
|
2021-12-08 16:35:37 -06:00
|
|
|
|
|
|
|
lobby_name.text = "%s" % Global.client.signaller.lobby_name
|
|
|
|
|
|
|
|
Global.client.signaller.call_deferred("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-08 16:35:37 -06:00
|
|
|
# hide/show controls depending on whether or not we're the host
|
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:
|
2021-12-08 16:35:37 -06:00
|
|
|
lobby_name.editable = false
|
2021-12-07 16:09:50 -06:00
|
|
|
max_players.editable = false
|
|
|
|
start.queue_free()
|
|
|
|
lock.queue_free()
|
2021-12-08 16:35:37 -06:00
|
|
|
|
|
|
|
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
|
2021-11-17 16:24:00 -06:00
|
|
|
|
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-08 16:35:37 -06:00
|
|
|
if Global.client.signaller.is_host(): call_deferred("update_can_start")
|
2021-12-07 16:09:50 -06:00
|
|
|
for peer_index in range(len(joined_peers)):
|
2021-12-08 16:35:37 -06:00
|
|
|
var peer_data = joined_peers[peer_index]
|
|
|
|
var id = peer_data["id"]
|
|
|
|
if peers.has(id):
|
|
|
|
_update_peer(id, peer_data)
|
2021-12-07 16:09:50 -06:00
|
|
|
else:
|
2021-12-08 16:35:37 -06:00
|
|
|
_add_peer(id, peer_data)
|
2021-12-07 09:32:20 -06:00
|
|
|
|
2021-12-08 16:35:37 -06:00
|
|
|
func _add_peer(id, peer_data):
|
2021-12-07 09:32:20 -06:00
|
|
|
call_deferred("update_player_count")
|
2021-12-07 16:09:50 -06:00
|
|
|
|
2021-12-08 16:35:37 -06:00
|
|
|
var new_peer = peer.instance()
|
|
|
|
new_peer.set_with_dict(peer_data)
|
|
|
|
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):
|
|
|
|
if peer_data.ready != peers[id].ready: add_chat("! %s is %s" % [peers[id].display_name, "now ready" if peer_data.ready else "no longer ready"])
|
|
|
|
peers[id].set_with_dict(peer_data)
|
|
|
|
# TODO: announce changes in chat?
|
2021-11-17 13:57:45 -06:00
|
|
|
|
2021-12-08 16:35:37 -06:00
|
|
|
func _peer_left(ids: Array):
|
|
|
|
call_deferred("update_player_count")
|
|
|
|
for data in ids:
|
|
|
|
var id = data["id"]
|
|
|
|
if peers.has(id):
|
|
|
|
call_deferred("add_chat", "< %s left the lobby" % peers[id].display_name)
|
|
|
|
peers[id].queue_free()
|
|
|
|
peers.erase(id)
|
|
|
|
|
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.signaller.display_name, message])
|
|
|
|
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
|
|
|
|
print(can_start)
|
|
|
|
if can_start: add_chat("! All players ready - game may now be started")
|
|
|
|
start.disabled = !can_start
|
|
|
|
|
|
|
|
func _lobby_left(_id): Global.lobby_browser()
|
|
|
|
func _on_leave_button_pressed(): Global.lobby_browser()
|
|
|
|
func _on_TextEdit_text_entered(_new_text): send_chat_message()
|
|
|
|
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): Global.client.signaller.set_ready(button_pressed)
|
|
|
|
func _on_lobby_info_text_changed(new_text: String): Global.client.signaller.set_lobby_name(new_text)
|
|
|
|
func _signaller_disconnected(): Global.main_menu()
|