harvest/game/scripts/coop_menu.gd
2026-10-04 12:07:36 -04:00

234 lines
7.9 KiB
GDScript

class_name CoopMenu
extends CenterContainer
## "Play with a friend" from the main menu (Main does the rest):
## * your name, and how to connect: on the same network (LAN, no account) or online (GD-Sync's
## servers; needs the project's API keys, see game/COOP.md)
## * Host: continue your saved vacation or start a new one; the farm opens to your friend
## * Join: the games found (LAN: announced on the network; online: open lobbies), or a lobby's
## name typed in (online)
## The status line says what's happening and why something failed.
signal host_requested(mode: CoopLobby.Mode, player_name: String, resume: bool)
signal search_requested(mode: CoopLobby.Mode, player_name: String)
signal join_requested(lobby: String)
signal join_address_requested(address: String)
signal back_requested
var is_open := false
var mode: CoopLobby.Mode = CoopLobby.Mode.LAN
var name_edit: LineEdit
var lan_button: Button
var online_button: Button
var host_button: Button
var join_button: Button
var continue_button: Button
var new_button: Button
var back_button: Button
var lobby_edit: LineEdit
var lobby_join_button: Button
var status: Label
var list: VBoxContainer
## "home", "host" (which vacation), "join" (the list)
var view := "home"
var _host_box: VBoxContainer
var _join_box: VBoxContainer
var _online_row: HBoxContainer
func _ready() -> void:
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
var panel := PanelContainer.new()
add_child(panel)
var margin := MarginContainer.new()
for side in ["left", "right", "top", "bottom"]:
margin.add_theme_constant_override("margin_" + side, 30)
panel.add_child(margin)
var box := VBoxContainer.new()
box.add_theme_constant_override("separation", 14)
box.custom_minimum_size = Vector2(760.0, 0.0)
margin.add_child(box)
var title := Label.new()
title.text = "Play with a friend"
title.add_theme_font_size_override("font_size", 44)
title.add_theme_color_override("font_color", UiTheme.GOLD)
box.add_child(title)
var name_row := HBoxContainer.new()
name_row.add_theme_constant_override("separation", 16)
box.add_child(name_row)
var nl := Label.new()
nl.text = "Your name"
nl.custom_minimum_size = Vector2(170.0, 0.0)
name_row.add_child(nl)
name_edit = LineEdit.new()
name_edit.max_length = 20
name_edit.custom_minimum_size = Vector2(420.0, 44.0)
name_edit.placeholder_text = "Shown over your scarecrow"
name_row.add_child(name_edit)
var mode_row := HBoxContainer.new()
mode_row.add_theme_constant_override("separation", 16)
box.add_child(mode_row)
var ml := Label.new()
ml.text = "Connect"
ml.custom_minimum_size = Vector2(170.0, 0.0)
mode_row.add_child(ml)
var group := ButtonGroup.new()
lan_button = _toggle(mode_row, "Same network (LAN)", group)
lan_button.button_pressed = true
lan_button.pressed.connect(func() -> void: _set_mode(CoopLobby.Mode.LAN))
online_button = _toggle(mode_row, "Online", group)
online_button.pressed.connect(func() -> void: _set_mode(CoopLobby.Mode.ONLINE))
var actions := HBoxContainer.new()
actions.add_theme_constant_override("separation", 16)
box.add_child(actions)
host_button = _button(actions, "Host a game")
host_button.pressed.connect(func() -> void: _show("host"))
join_button = _button(actions, "Join a game")
join_button.pressed.connect(func() -> void:
_show("join")
search_requested.emit(mode, player_name()))
_host_box = VBoxContainer.new()
_host_box.add_theme_constant_override("separation", 10)
box.add_child(_host_box)
var hl := Label.new()
hl.text = "Which vacation? Your friend joins your farm."
_host_box.add_child(hl)
var host_row := HBoxContainer.new()
host_row.add_theme_constant_override("separation", 16)
_host_box.add_child(host_row)
continue_button = _button(host_row, "Continue")
continue_button.pressed.connect(func() -> void: host_requested.emit(mode, player_name(), true))
new_button = _button(host_row, "New vacation")
new_button.pressed.connect(func() -> void: host_requested.emit(mode, player_name(), false))
_join_box = VBoxContainer.new()
_join_box.add_theme_constant_override("separation", 8)
box.add_child(_join_box)
var jl := Label.new()
jl.text = "Games found:"
_join_box.add_child(jl)
list = VBoxContainer.new()
list.add_theme_constant_override("separation", 6)
_join_box.add_child(list)
_online_row = HBoxContainer.new()
_online_row.add_theme_constant_override("separation", 12)
_join_box.add_child(_online_row)
lobby_edit = LineEdit.new()
lobby_edit.custom_minimum_size = Vector2(440.0, 44.0)
_online_row.add_child(lobby_edit)
lobby_join_button = _button(_online_row, "Join")
lobby_join_button.pressed.connect(func() -> void:
var t := lobby_edit.text.strip_edges()
if t == "":
return
if mode == CoopLobby.Mode.LAN:
join_address_requested.emit(t)
else:
join_requested.emit(t))
status = Label.new()
status.custom_minimum_size = Vector2(740.0, 0.0)
status.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
status.add_theme_font_size_override("font_size", 19)
status.add_theme_color_override("font_color", Color(0.78, 0.8, 0.72))
box.add_child(status)
back_button = _button(box, "Back")
back_button.pressed.connect(func() -> void: back_requested.emit())
visible = false
mouse_filter = Control.MOUSE_FILTER_IGNORE
func open(saved: SaveGame.Loaded, nights: int, p_name: String) -> void:
is_open = true
visible = true
mouse_filter = Control.MOUSE_FILTER_STOP
if name_edit.text == "":
name_edit.text = p_name
var ok := saved != null and saved.status == SaveGame.Status.OK
continue_button.disabled = not ok
continue_button.text = "Continue (morning of day %d of %d)" % [saved.state.night, nights] if ok else "Continue"
status.text = ""
_show("home")
host_button.grab_focus.call_deferred()
func close() -> void:
is_open = false
visible = false
mouse_filter = Control.MOUSE_FILTER_IGNORE
func player_name() -> String:
return name_edit.text.strip_edges()
func set_status(text: String) -> void:
status.text = text
func show_lobbies(lobbies: Array) -> void:
for c in list.get_children():
c.queue_free()
if lobbies.is_empty():
var l := Label.new()
l.text = "None yet. (Ask your friend to host; on a LAN you need to be on the same network.)"
l.add_theme_font_size_override("font_size", 18)
l.add_theme_color_override("font_color", Color(0.7, 0.72, 0.66))
list.add_child(l)
return
for lob: Dictionary in lobbies:
var b := _button(list, "%s (%d/2)" % [lob["name"], int(lob["players"])])
b.alignment = HORIZONTAL_ALIGNMENT_LEFT
var n := String(lob["name"])
b.pressed.connect(func() -> void: join_requested.emit(n))
func _show(v: String) -> void:
view = v
_host_box.visible = v == "host"
_join_box.visible = v == "join"
_online_row.visible = v == "join"
_placeholder()
if v == "host":
(continue_button if not continue_button.disabled else new_button).grab_focus.call_deferred()
if v == "join":
show_lobbies([])
func _set_mode(m: CoopLobby.Mode) -> void:
mode = m
if m == CoopLobby.Mode.ONLINE and not CoopLobby.has_online_keys():
status.text = "Online play needs the GD-Sync API keys (free at gd-sync.com): in the editor, Project > Tools > GD-Sync. Until then, play on the same network (LAN)."
else:
status.text = ""
_placeholder()
if view == "join":
search_requested.emit(mode, player_name())
func _placeholder() -> void:
lobby_edit.placeholder_text = "Or the host's address, e.g. 192.168.1.20" if mode == CoopLobby.Mode.LAN \
else "Or type the game's name, e.g. Ana's farm"
func _button(parent: Control, text: String) -> Button:
var b := Button.new()
b.text = text
b.custom_minimum_size = Vector2(240.0, 50.0)
parent.add_child(b)
return b
func _toggle(parent: Control, text: String, group: ButtonGroup) -> Button:
var b := _button(parent, text)
b.toggle_mode = true
b.button_group = group
return b
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color(0.0, 0.0, 0.0, 0.4))
func _unhandled_input(event: InputEvent) -> void:
if is_open and event.is_action_pressed("ui_cancel"):
back_requested.emit()
get_viewport().set_input_as_handled()