2020-07-10 13:37:13 +00:00
|
|
|
extends Control
|
|
|
|
|
2020-07-14 07:21:52 +00:00
|
|
|
const REFRESH_SERVER_DELAY = 5.0
|
|
|
|
|
2020-07-13 00:20:09 +00:00
|
|
|
export var scale = 4 setget set_scale
|
2020-07-13 09:20:51 +00:00
|
|
|
export var upThreshold = 1.0/59.0
|
2020-07-10 13:20:56 +00:00
|
|
|
export var downThreshold = 1.0/30.0
|
|
|
|
|
|
|
|
export var delay = 1.0
|
2020-07-14 07:21:52 +00:00
|
|
|
export var refresh_server_remaining = REFRESH_SERVER_DELAY
|
2020-07-10 13:20:56 +00:00
|
|
|
|
2020-07-13 09:20:51 +00:00
|
|
|
onready var viewport = $Viewport
|
|
|
|
onready var background = $Background
|
2020-07-13 23:59:45 +00:00
|
|
|
onready var netgame = $"/root/Multiplayer"
|
|
|
|
onready var server_list = $Popup/MarginContainer/VBoxContainer/ItemList
|
2020-07-23 08:50:47 +00:00
|
|
|
onready var name_field = $CenterContainer/PanelContainer/HBoxContainer/HBoxContainer/NameField
|
2020-07-13 09:20:51 +00:00
|
|
|
|
2020-07-13 23:59:45 +00:00
|
|
|
var servers = []
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2020-07-23 08:50:47 +00:00
|
|
|
name_field.text = netgame.player_name
|
2020-07-10 13:20:56 +00:00
|
|
|
set_scale(scale)
|
2020-07-10 13:37:13 +00:00
|
|
|
$"/root/Music/BGM".play()
|
2020-07-13 23:59:45 +00:00
|
|
|
netgame.connect("ms_updated", self, "_ms_update")
|
|
|
|
request_servers()
|
2020-07-10 13:20:56 +00:00
|
|
|
|
2020-07-13 23:59:45 +00:00
|
|
|
func _process(delta: float) -> void:
|
2020-07-14 07:21:52 +00:00
|
|
|
refresh_server_remaining -= delta
|
|
|
|
if refresh_server_remaining <= 0:
|
|
|
|
request_servers()
|
|
|
|
refresh_server_remaining = REFRESH_SERVER_DELAY
|
2020-07-10 13:20:56 +00:00
|
|
|
# Give the system time to adjust
|
|
|
|
if delay > 0:
|
|
|
|
delay -= delta
|
|
|
|
return
|
|
|
|
if delta > downThreshold:
|
2020-07-23 08:50:47 +00:00
|
|
|
print("GPU is taking too much (%ss > %ss), adjusting shader scale" % [ delta, downThreshold ])
|
2020-07-10 13:20:56 +00:00
|
|
|
set_scale(scale+1)
|
2020-07-12 15:26:40 +00:00
|
|
|
elif delta < upThreshold and scale > 2:
|
2020-07-23 08:50:47 +00:00
|
|
|
print("GPU is taking it easy (%ss < %ss), adjusting shader scale" % [ delta, upThreshold ])
|
2020-07-10 13:20:56 +00:00
|
|
|
set_scale(scale-1)
|
|
|
|
|
2020-07-13 23:59:45 +00:00
|
|
|
func request_servers():
|
|
|
|
netgame.ms_get_entries()
|
|
|
|
|
2020-07-23 08:50:47 +00:00
|
|
|
const server_list_format = "{name} ({address}) - {players}/{max_players} players"
|
2020-07-13 23:59:45 +00:00
|
|
|
func _ms_update(action, result):
|
|
|
|
if action == "list_games":
|
|
|
|
# Reset server list
|
|
|
|
server_list.clear()
|
2020-07-22 15:34:04 +00:00
|
|
|
servers = result
|
|
|
|
for server in servers:
|
2020-07-23 08:50:47 +00:00
|
|
|
server.data.address = server.address
|
|
|
|
server_list.add_item(server_list_format.format(server.data))
|
2020-07-13 23:59:45 +00:00
|
|
|
|
|
|
|
func set_scale(val) -> void:
|
2020-07-10 13:20:56 +00:00
|
|
|
scale = val
|
2020-07-13 09:20:51 +00:00
|
|
|
if viewport:
|
|
|
|
viewport.size = rect_size / scale
|
|
|
|
background.update()
|
2020-07-10 13:20:56 +00:00
|
|
|
|
2020-07-13 23:59:45 +00:00
|
|
|
func _on_resized() -> void:
|
2020-07-13 09:20:51 +00:00
|
|
|
if viewport:
|
|
|
|
viewport.size = rect_size / scale
|
|
|
|
background.update()
|
2020-07-12 15:26:40 +00:00
|
|
|
|
2020-07-13 23:59:45 +00:00
|
|
|
func _host_pressed() -> void:
|
2020-07-12 15:26:40 +00:00
|
|
|
$"/root/Multiplayer".host()
|
|
|
|
|
2020-07-13 23:59:45 +00:00
|
|
|
func _join_pressed() -> void:
|
|
|
|
$Popup.popup_centered_ratio()
|
|
|
|
|
2020-07-14 14:20:06 +00:00
|
|
|
func join_server(server) -> void:
|
2020-07-13 09:20:51 +00:00
|
|
|
$"/root/Music/BGM".stop()
|
2020-07-14 14:20:06 +00:00
|
|
|
netgame.join(server)
|
2020-07-13 23:59:45 +00:00
|
|
|
|
|
|
|
func _server_addr_changed(new_text: String) -> void:
|
|
|
|
$Popup/MarginContainer/VBoxContainer/HBoxContainer/Button.disabled = new_text.length() < 1
|
|
|
|
|
|
|
|
func _manual_join_pressed():
|
2020-07-22 15:34:04 +00:00
|
|
|
join_server({ "address": $Popup/MarginContainer/VBoxContainer/HBoxContainer/LineEdit.text })
|
2020-07-12 15:26:40 +00:00
|
|
|
|
2020-07-13 23:59:45 +00:00
|
|
|
func _server_item_clicked(index):
|
2020-07-14 14:20:06 +00:00
|
|
|
$"/root/Music/BGM".stop()
|
|
|
|
join_server(servers[index])
|