harvest/game/scripts/end_panel.gd
Forrest e8729b41e8 #42: panels at phone scale
On phones (MobilePort.active()) the morning sign, the end screens, and the
workbench's and shed's menus sit in the safe area; their long part scrolls
(UiTheme.TouchScroll: a finger drag scrolls and never presses, a tap does)
with Start the day / the end buttons / Close always in view below it; rows
and buttons are at least the touch target; long text wraps. Desktop layout
is unchanged (panels_test checks the 1920x1080 rects against the old ones).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 15:28:25 -04:00

311 lines
11 KiB
GDScript

class_name EndPanel
extends CenterContainer
## The two end screens. Works with mouse, keyboard (arrows + Enter), and gamepad (d-pad + A)
## through focus.
## * Failure, a moment after a Game Over (so the fall can play): the cause (the scarecrow
## destroyed, or the farm stripped bare), the night reached and its plan, what's left and
## what the night cost so far, the vacation's totals, and Retry the night (D9, focused: back
## to that morning) and Main menu (New vacation when there's no menu, e.g. the farm scene on
## its own).
## * The final assessment, once the farmers' return (FarmersReturn) has played or been
## skipped: the rating and score (Assessment), the share kept per category with a bar, and
## the vacation's totals, with Main menu (or New vacation).
## On a phone (UiTheme's panels) it sits in the safe area: the text wraps to the room's width
## (up to PHONE_WIDTH), scrolls (drag it) if it's still too tall, and the buttons stay below it,
## in view and at least the touch target tall.
signal menu_requested
const DELAY := 1.4
const GOLD := Color(0.95, 0.82, 0.45)
const GREY := Color(0.75, 0.77, 0.72)
const MARGIN := 34
const BUTTON := Vector2(230.0, 56.0)
## The text's widest on a phone (an iPad's room is wider): about the totals' longest line.
const PHONE_WIDTH := 1180.0
var campaign: CampaignController
## Set by Main: the Main menu button shows instead of New vacation.
var has_menu := false
## Set by FarmSim: the final assessment waits for the farmers' return to finish.
var ending: FarmersReturn
var waves: WaveDirector
## Set when the screen is up: "failure" or "assessment".
var mode := ""
var _title: Label
var _rating: Label
var _detail: Label
var _bars: GridContainer
var _bar_rows := {} # category -> [ProgressBar, Label]
var _stats: Label
var _retry: Button
var _menu: Button
var _new: Button
var _wait: float = -1.0
var _pending_mode := ""
var _safe: MarginContainer
var _panel: PanelContainer
var _scroll: UiTheme.TouchScroll
var _box: VBoxContainer
var _fit_queued := false
func setup(p_campaign: CampaignController) -> void:
campaign = p_campaign
campaign.campaign_ended.connect(_on_ended)
campaign.phase_changed.connect(func(phase: CampaignController.Phase, _previous: CampaignController.Phase) -> void:
if phase == CampaignController.Phase.MORNING:
hide_panel())
func _ready() -> void:
set_anchors_preset(Control.PRESET_FULL_RECT)
_safe = MarginContainer.new() # the safe area's margins on a phone (none on desktop)
add_child(_safe)
_panel = PanelContainer.new()
_panel.add_theme_stylebox_override("panel", ReviewPanel.backing())
_safe.add_child(_panel)
var margin := MarginContainer.new()
for side in ["left", "right", "top", "bottom"]:
margin.add_theme_constant_override("margin_" + side, MARGIN)
_panel.add_child(margin)
var page := VBoxContainer.new() # the text (scrolling on a phone), then the buttons
page.add_theme_constant_override("separation", 16)
margin.add_child(page)
_scroll = UiTheme.TouchScroll.new(UiTheme.TEXT)
page.add_child(_scroll)
var box := VBoxContainer.new()
box.size_flags_horizontal = Control.SIZE_EXPAND_FILL
box.add_theme_constant_override("separation", 16)
_scroll.add_child(box)
_box = box
_title = _label(box, 46)
_rating = _label(box, 30, GOLD)
_detail = _label(box, 22)
_bars = GridContainer.new()
_bars.columns = 3
_bars.add_theme_constant_override("h_separation", 18)
_bars.add_theme_constant_override("v_separation", 8)
_bars.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
box.add_child(_bars)
for cat in ["crops", "animals", "supplies"]:
var name_label := _label(_bars, 22)
name_label.text = cat.capitalize()
name_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
var bar := ProgressBar.new()
bar.custom_minimum_size = Vector2(320.0, 26.0)
bar.show_percentage = false
bar.max_value = 1.0
var fill := StyleBoxFlat.new()
fill.bg_color = Color(0.55, 0.78, 0.4)
fill.set_corner_radius_all(4)
var back := StyleBoxFlat.new()
back.bg_color = Color(0.22, 0.24, 0.2)
back.set_corner_radius_all(4)
bar.add_theme_stylebox_override("fill", fill)
bar.add_theme_stylebox_override("background", back)
_bars.add_child(bar)
var text := _label(_bars, 20, GREY)
text.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
_bar_rows[cat] = [bar, text, name_label]
_stats = _label(box, 20, GREY)
var buttons := HBoxContainer.new()
buttons.alignment = BoxContainer.ALIGNMENT_CENTER
buttons.add_theme_constant_override("separation", 24)
page.add_child(buttons)
_retry = _button(buttons, "Retry the night")
_retry.pressed.connect(func() -> void: campaign.retry_night())
_menu = _button(buttons, "Main menu")
_menu.pressed.connect(func() -> void: menu_requested.emit())
_new = _button(buttons, "New vacation")
_new.pressed.connect(func() -> void: campaign.new_campaign())
hide_panel()
box.minimum_size_changed.connect(_queue_fit) # the text reflowed
get_viewport().size_changed.connect(_queue_fit)
func hide_panel() -> void:
visible = false
mode = ""
_wait = -1.0
_pending_mode = ""
mouse_filter = Control.MOUSE_FILTER_IGNORE
func _on_ended(outcome: CampaignController.Outcome) -> void:
if outcome == CampaignController.Outcome.VACATION_COMPLETE:
if ending != null:
if not ending.finished.is_connected(_on_ending_finished):
ending.finished.connect(_on_ending_finished)
return
_pending_mode = "assessment"
else:
_pending_mode = "failure"
_wait = DELAY
func _on_ending_finished() -> void:
_pending_mode = "assessment"
_wait = 0.01
func _process(delta: float) -> void:
if _wait < 0.0:
return
_wait -= delta
if _wait <= 0.0:
_wait = -1.0
if _pending_mode == "assessment":
show_assessment()
else:
show_failure()
func show_failure() -> void:
var s := campaign.state
var cfg := campaign.config
mode = "failure"
var plan := waves.plan_for(s.night) if waves != null else null
var night := "Night %d of %d%s" % [s.night, cfg.campaign_nights, (": " + plan.title) if plan != null and plan.title != "" else ""]
if campaign.outcome == CampaignController.Outcome.GAME_OVER_SCARECROW:
_title.text = "The scarecrow has fallen"
_rating.text = "%s. Its straw gave out." % night
else:
_title.text = "The farm was stripped bare"
_rating.text = "%s. Every crop, animal, and supply is gone." % night
var e := s.night_events
var lines: Array[String] = []
lines.append("You kept the farm for %d %s." % [s.nights_survived, "night" if s.nights_survived == 1 else "nights"])
lines.append("Left: %d of %d crops, %d of %d animals, %d supplies." % [s.crops, s.crops_total, s.animals, s.animals_total, s.supplies])
lines.append("Tonight: %s defeated; %d crops, %d animals, and %d supplies snatched." % [Assessment.enemies_line(e),
int(e.get("crops_stolen", 0)), int(e.get("animals_stolen", 0)), int(e.get("supplies_stolen", 0))])
_detail.text = "\n".join(lines)
_bars.visible = false
_stats.text = _totals_text(s.totals(), false)
_stats.visible = not s.night_log.is_empty()
_retry.visible = true
_retry.text = "Retry the night (from the morning of day %d)" % s.night
_show_buttons()
_open(_retry)
func show_assessment() -> void:
var s := campaign.state
var cfg := campaign.config
mode = "assessment"
var a := Assessment.of(s, cfg)
_title.text = "The farmers are home!"
_rating.text = "%s · %d points" % [a["tier"], a["score"]]
_detail.text = "%s\nYou kept the farm for all %d nights." % [a["text"], s.nights_survived]
_bars.visible = true
var parts: Dictionary = a["parts"]
for cat: String in _bar_rows:
var row: Array = _bar_rows[cat]
var shown := parts.has(cat)
for c: Control in row:
c.visible = shown
if not shown:
continue
(row[0] as ProgressBar).value = float(parts[cat])
var pct := roundi(100.0 * float(parts[cat]))
match cat:
"crops":
(row[1] as Label).text = "%d%% (%d of %d)" % [pct, s.crops, s.crops_total]
"animals":
(row[1] as Label).text = "%d%% (%d of %d)" % [pct, s.animals, s.animals_total]
_:
(row[1] as Label).text = "%d%% kept from aliens (%d of %d carried off)" % [pct, a["supplies_lost"], a["supplies_handled"]]
_stats.text = _totals_text(a["totals"], true)
_stats.visible = true
_retry.visible = false
_show_buttons()
_open(_menu if has_menu else _new)
## The vacation's totals, a few lines.
func _totals_text(t: Dictionary, finished: bool) -> String:
var s := campaign.state
var lines: Array[String] = []
if not finished:
lines.append("Over the vacation so far:")
lines.append("Enemies defeated: %s. Got away with loot: %d." % [Assessment.enemies_line(t), int(t["escaped"])])
lines.append("Recovered: %s, %s, %d supplies. Carried off: %s, %s, %d supplies." % [
_count(t["crops_recovered"], "crop"), _count(t["animals_recovered"], "animal"), int(t["supplies_recovered"]),
_count(t["crops_lost"], "crop"), _count(t["animals_lost"], "animal"), int(t["supplies_lost"])])
lines.append("Built %d, repaired %d, broken %d. Upgrades: %d of %d. Patched up %s, %d damage taken." % [
int(t["structures_built"]), int(t["repairs"]), int(t["structures_destroyed"]), Upgrades.levels_bought(campaign.config, s),
Upgrades.levels_total(campaign.config), _count(t["patches"], "time"), int(t["scarecrow_damage"])])
lines.append("Gathered %s. Spent %s." % [Assessment.materials_line(t, "gathered_"), Assessment.materials_line(t, "spent_")])
return "\n".join(lines)
## "1 crop", "3 crops".
static func _count(n: Variant, word: String) -> String:
return "%d %s%s" % [int(n), word, "" if int(n) == 1 else "s"]
func _show_buttons() -> void:
_menu.visible = has_menu
_new.visible = not has_menu
func _open(focus: Button) -> void:
visible = true
mouse_filter = Control.MOUSE_FILTER_STOP
_fit()
_scroll.scroll_vertical = 0
focus.grab_focus()
## Lays the screen out: on desktop as it always was; on a phone (UiTheme's panels) inside the
## safe area, the text wrapped to the room's width and scrolling past what its height leaves.
func _fit() -> void:
var phone := MobilePort.active()
_scroll.set_phone(phone)
for l: Label in [_title, _rating, _detail, _stats]:
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART if phone else TextServer.AUTOWRAP_OFF
for b: Button in [_retry, _menu, _new]:
b.custom_minimum_size = Vector2(BUTTON.x, maxf(BUTTON.y, MobilePort.minimum_target(self))) if phone else BUTTON
if not phone:
UiTheme.fit_margins(_safe, Rect2())
_box.custom_minimum_size.x = 0.0
return
var room := UiTheme.panel_room(self)
UiTheme.fit_margins(_safe, room)
var frame := 2.0 * MARGIN + _panel.get_theme_stylebox("panel").get_minimum_size().x + _scroll.frame_width()
_box.custom_minimum_size.x = floorf(minf(PHONE_WIDTH, room.size.x - frame))
var others := _panel.get_combined_minimum_size().y - _scroll.get_combined_minimum_size().y
_scroll.fit(room.size.y - others)
## Fits again once this frame's changes are in (the text reflowed, the screen changed size).
func _queue_fit() -> void:
if _fit_queued or not visible:
return
_fit_queued = true
_refit.call_deferred()
func _refit() -> void:
_fit_queued = false
if visible:
_fit()
func _label(parent: Node, font_size: int, color: Color = Color(0.92, 0.92, 0.88)) -> Label:
var l := Label.new()
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
l.add_theme_font_size_override("font_size", font_size)
l.add_theme_color_override("font_color", color)
parent.add_child(l)
return l
func _button(parent: Control, text: String) -> Button:
var b := Button.new()
b.text = text
b.custom_minimum_size = BUTTON
b.add_theme_font_size_override("font_size", 24)
parent.add_child(b)
return b