Fix join logic in menu panel
This commit is contained in:
+23
-36
@@ -6,8 +6,8 @@ extends Control
|
||||
## directly.
|
||||
|
||||
const JON_SCENE := "res://Scenes/jon_scene.tscn"
|
||||
const MULTIPLAYER_SCENE := "res://Scenes/multiplayer.tscn"
|
||||
const SETTINGS_PATH := "user://vryhungry_settings.cfg"
|
||||
const MULTIPLAYER_SCENE := "res://Scenes/multiplayer.tscn"
|
||||
|
||||
var _ip := "127.0.0.1"
|
||||
|
||||
@@ -22,11 +22,12 @@ var _ip := "127.0.0.1"
|
||||
@onready var _join_btn: Button = %JoinButton
|
||||
@onready var _back_btn: Button = %BackButton
|
||||
|
||||
# store original text so we can say "connecting..." while pinging server
|
||||
var original_join_text: String
|
||||
|
||||
func _ready() -> void:
|
||||
print("Menu Panel _ready")
|
||||
if _bypass_menu_for_cmdline():
|
||||
return
|
||||
|
||||
for child in _keypad.get_children():
|
||||
if child is Button:
|
||||
child.pressed.connect(_on_key.bind(child.text))
|
||||
@@ -35,29 +36,12 @@ func _ready() -> void:
|
||||
_join_menu_btn.pressed.connect(_show_join_view)
|
||||
_join_btn.pressed.connect(_on_join_pressed)
|
||||
_back_btn.pressed.connect(_show_root_view)
|
||||
|
||||
NetworkManager.connection_failed.connect(_handle_connection_failed)
|
||||
_load_ip()
|
||||
_show_root_view()
|
||||
_refresh_ip()
|
||||
|
||||
|
||||
## --server / --join <ip> on the command line skip straight to the multiplayer
|
||||
## scene, same as the previously headless-tested main.tscn flow.
|
||||
func _bypass_menu_for_cmdline() -> bool:
|
||||
var args := OS.get_cmdline_user_args()
|
||||
if args.has("--server"):
|
||||
NetworkManager.request_host()
|
||||
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
|
||||
return true
|
||||
if args.has("--join"):
|
||||
var idx := args.find("--join")
|
||||
var addr := "127.0.0.1"
|
||||
if idx + 1 < args.size():
|
||||
addr = args[idx + 1]
|
||||
NetworkManager.request_join(addr)
|
||||
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
|
||||
return true
|
||||
return false
|
||||
|
||||
original_join_text = _join_btn.text
|
||||
|
||||
func _show_root_view() -> void:
|
||||
print("Menu Panel show root view")
|
||||
@@ -69,7 +53,8 @@ func _show_join_view() -> void:
|
||||
print("Menu Panel show join view")
|
||||
_root_view.visible = false
|
||||
_join_view.visible = true
|
||||
set_status("Enter host IP, then Join")
|
||||
_status.text= "Enter host IP, then Join"
|
||||
_status.add_theme_color_override("font_color", Color.WHITE)
|
||||
|
||||
|
||||
func _on_key(key: String) -> void:
|
||||
@@ -95,17 +80,26 @@ func _on_john_pressed() -> void:
|
||||
func _on_host_pressed() -> void:
|
||||
print("Menu Panel press host")
|
||||
NetworkManager.host()
|
||||
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
|
||||
|
||||
|
||||
func _on_join_pressed() -> void:
|
||||
print("Menu Panel press host")
|
||||
print("Menu Panel press join")
|
||||
if _ip.is_empty():
|
||||
set_status("Enter an IP first")
|
||||
_status.text= "Enter an IP first"
|
||||
_status.add_theme_color_override("font_color", Color.ORANGE)
|
||||
return
|
||||
_save_ip()
|
||||
NetworkManager.request_join(_ip)
|
||||
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
|
||||
_join_btn.disabled = true
|
||||
_join_btn.text = "Connecting..."
|
||||
NetworkManager.join(_ip)
|
||||
|
||||
|
||||
func _handle_connection_failed():
|
||||
print("Menu Panel _handle_connection_failed")
|
||||
_status.text= "Connection failed"
|
||||
_status.add_theme_color_override("font_color", Color.RED)
|
||||
_join_btn.disabled = false
|
||||
_join_btn.text = original_join_text
|
||||
|
||||
|
||||
## Remembers the last IP the player tried to join, so the keypad starts
|
||||
@@ -121,10 +115,3 @@ func _save_ip() -> void:
|
||||
cfg.load(SETTINGS_PATH)
|
||||
cfg.set_value("network", "last_ip", _ip)
|
||||
cfg.save(SETTINGS_PATH)
|
||||
|
||||
|
||||
## Called by network_manager (e.g. on connection_failed after a bounce back to
|
||||
## this menu) to show feedback.
|
||||
func set_status(text: String) -> void:
|
||||
if _status:
|
||||
_status.text = text
|
||||
|
||||
Reference in New Issue
Block a user