harvest/game/scripts/review_panel.gd
2026-10-07 14:50:55 -04:00

327 lines
13 KiB
GDScript

class_name ReviewPanel
extends CenterContainer
## The morning screen (the untimed MORNING phase), between one night and the next day: a wooden
## sign, hand-drawn (Sketch) over the dimmed farm, the day on its top beam and a sheet of paper
## nailed under it with last night's results (from the night's log entry):
## * per category (crops, animals, supplies): what was taken, recovered, and lost;
## * the enemies defeated, by type;
## * the materials gathered over the day and night.
## Each row has its icon; a type or material with none isn't listed. "Start the day" (focused,
## so Enter / A work) ends it. The first morning explains the vacation instead of results.
const CATEGORIES := ["Crops", "Animals", "Supplies"]
const ENEMY_TYPES := [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER, EnemySystem.TYPE_UFO]
## The sign's layout around its paper: the posts and beam ends either side of it, the top beam
## (the title is written on it), and the bottom beam.
const SIDE := 64.0
const BEAM := 92.0
const BOTTOM := 70.0
const WIDTH := 880.0
var campaign: CampaignController
## Set by FarmSim: the night plans (last night's title).
var waves: WaveDirector
var _board: PanelContainer
var _title: Label
var _last_head: Label
var _rule: HSeparator
var _grid: GridContainer
var _cells := {} # "Crops/taken" -> Label
var _lists: HBoxContainer
var _enemies := {} # type -> [icon, name, count]
var _no_enemies: Label
var _materials := {} # material -> [icon, name, count]
var _no_materials: Label
var _intro: Label
var _start: Button
func setup(p_campaign: CampaignController) -> void:
campaign = p_campaign
campaign.phase_changed.connect(func(_p: CampaignController.Phase, _prev: CampaignController.Phase) -> void: refresh())
refresh()
func _ready() -> void:
set_anchors_preset(Control.PRESET_FULL_RECT)
theme = UiTheme.sketch()
_board = PanelContainer.new()
var margins := StyleBoxEmpty.new()
margins.content_margin_left = SIDE
margins.content_margin_right = SIDE
margins.content_margin_bottom = BOTTOM
_board.add_theme_stylebox_override("panel", margins)
_board.draw.connect(_draw_board)
add_child(_board)
var column := VBoxContainer.new()
column.add_theme_constant_override("separation", 0)
_board.add_child(column)
_title = Label.new()
Sketch.style(_title, 48)
_title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_title.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
_title.custom_minimum_size = Vector2(0.0, BEAM)
column.add_child(_title)
var sheet := MarginContainer.new()
for side: String in ["left", "right"]:
sheet.add_theme_constant_override("margin_" + side, 48)
sheet.add_theme_constant_override("margin_top", 26)
sheet.add_theme_constant_override("margin_bottom", 32)
column.add_child(sheet)
var box := VBoxContainer.new()
box.custom_minimum_size = Vector2(WIDTH, 0.0)
box.add_theme_constant_override("separation", 10)
sheet.add_child(box)
_last_head = _ink(box, 38)
_last_head.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_rule = HSeparator.new()
_rule.add_theme_stylebox_override("separator", SketchBox.new(Color(0, 0, 0, 0), Color(Sketch.PAPER_INK, 0.5), 2.0, true))
box.add_child(_rule)
_grid = GridContainer.new()
_grid.columns = 5
_grid.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
_grid.add_theme_constant_override("h_separation", 18)
_grid.add_theme_constant_override("v_separation", 4)
box.add_child(_grid)
_grid.add_child(Control.new())
_grid.add_child(Control.new())
for head: String in ["Taken", "Recovered", "Lost"]:
var l := _ink(_grid, 28, Sketch.PAPER_DIM)
l.text = head
l.custom_minimum_size = Vector2(150.0, 0.0)
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
for i in CATEGORIES.size():
var cat: String = CATEGORIES[i]
_grid.add_child(SketchIcon.new(LootSystem.CATEGORY_KEYS[i], 52.0))
var label := _ink(_grid, 32)
label.text = cat
label.custom_minimum_size = Vector2(150.0, 0.0)
for col: String in ["taken", "recovered", "lost"]:
var l := _ink(_grid, 34)
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_cells["%s/%s" % [cat, col]] = l
box.add_child(_spacer(8.0))
_lists = HBoxContainer.new()
_lists.alignment = BoxContainer.ALIGNMENT_CENTER
_lists.add_theme_constant_override("separation", 90)
box.add_child(_lists)
var enemies := _list(_lists, "Enemies defeated")
for t: int in ENEMY_TYPES:
var plural := EnemySystem.PLURALS[t]
_enemies[t] = _list_row(enemies, EnemySystem.TYPE_NAMES[t], plural[0].to_upper() + plural.substr(1))
_no_enemies = _ink(enemies, 30, Sketch.PAPER_DIM)
_no_enemies.text = "None"
var materials := _list(_lists, "Materials gathered")
for m in ResourceStore.MATERIALS:
_materials[m] = _list_row(materials, m, m.capitalize())
_no_materials = _ink(materials, 30, Sketch.PAPER_DIM)
_no_materials.text = "Nothing"
_intro = _ink(box, 28)
_intro.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
_intro.add_theme_constant_override("paragraph_spacing", 14)
_start = Button.new()
_start.text = "Start the day"
_start.custom_minimum_size = Vector2(300.0, 62.0)
_start.add_theme_font_size_override("font_size", 32)
_start.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
var wood := {
"normal": SketchBox.new(Sketch.WOOD, Sketch.INK, 2.5),
"hover": SketchBox.new(Sketch.WOOD.lightened(0.15), Sketch.INK, 2.5),
"pressed": SketchBox.new(Sketch.WOOD.darkened(0.2), Sketch.GOLD, 2.5),
}
for key: String in wood:
_start.add_theme_stylebox_override(key, wood[key])
_start.pressed.connect(func() -> void: campaign.begin_day())
box.add_child(_spacer(6.0))
box.add_child(_start)
visible = false
## The dark, nearly opaque panel behind the menus and the end screens.
static func backing() -> StyleBoxFlat:
var bg := StyleBoxFlat.new()
bg.bg_color = Color(0.1, 0.12, 0.1)
bg.border_color = Color(0.55, 0.5, 0.35)
bg.set_border_width_all(2)
bg.set_corner_radius_all(6)
return bg
func refresh() -> void:
if campaign == null:
return
visible = campaign.state.phase == CampaignController.Phase.MORNING
mouse_filter = Control.MOUSE_FILTER_STOP if visible else Control.MOUSE_FILTER_IGNORE
if not visible:
return
var s := campaign.state
var nights := campaign.config.campaign_nights
var first := s.night_log.is_empty()
for n: Control in [_last_head, _rule, _grid, _lists]:
n.visible = not first
_intro.visible = first
if first:
_title.text = "The farmers are away"
_intro.text = "\n".join([
"Keep the farm safe for %d nights until they come home: the crops, the animals in the pen, and the supplies in the shed." % nights,
"By day, gather wood, scrap, and straw (hold E), store them at the barn, and build defenses (B). There's more to gather off the farm, to the north, east, and south.",
"At night, stab aliens with your pitchfork (left mouse), scare them off (Q), and pick up anything they drop. Animals roam loose until you lead them into the pen: walk up to one and it follows you. The supply shed sells a corn shooter (switch weapons with Tab or the number keys), and more weapons arrive there over the days.",
])
else:
var e: Dictionary = s.night_log[s.night_log.size() - 1]
_title.text = "Morning of day %d of %d" % [s.night, nights]
var plan := waves.plan_for(int(e["night"])) if waves != null else null
_last_head.text = "Night %d: %s" % [e["night"], plan.title] if plan != null and plan.title != "" else "Night %d" % e["night"]
_fill_grid(e)
_fill_list(_enemies, _no_enemies, func(t: int) -> int: return int(e.get("killed_" + EnemySystem.TYPE_NAMES[t], 0)))
_fill_list(_materials, _no_materials, func(m: String) -> int: return int(e.get("gathered_" + m, 0)))
_board.queue_redraw()
_start.grab_focus.call_deferred()
## The results table as text, one row per category ("Crops 4 3 1"), for tests.
func grid_text() -> String:
var rows: Array[String] = []
for cat: String in CATEGORIES:
rows.append("%s %s %s %s" % [cat, _cells[cat + "/taken"].text, _cells[cat + "/recovered"].text, _cells[cat + "/lost"].text])
return "\n".join(rows)
## The enemies listed, "Crows 12, Aliens 3" (or "None"), for tests.
func enemies_text() -> String:
return _list_text(_enemies, _no_enemies)
## The materials listed, "Wood 8, Straw 4" (or "Nothing"), for tests.
func materials_text() -> String:
return _list_text(_materials, _no_materials)
## Per category: (taken, recovered, lost) in a night's log entry. Animals taken include those
## that got out of an open pen (loose, not lost).
static func category_counts(e: Dictionary) -> Dictionary:
return {
"Crops": [int(e["crops_stolen"]), int(e["crops_recovered"]), int(e["crops_lost"])],
"Animals": [int(e["animals_stolen"]) + int(e["animals_strayed"]), int(e["animals_recovered"]), int(e["animals_lost"])],
"Supplies": [int(e["supplies_stolen"]), int(e["supplies_recovered"]), int(e["supplies_lost"])],
}
func _fill_grid(e: Dictionary) -> void:
var counts := category_counts(e)
for cat: String in CATEGORIES:
var c: Array = counts[cat]
for k in 3:
var l: Label = _cells["%s/%s" % [cat, ["taken", "recovered", "lost"][k]]]
l.text = str(c[k])
var color := Sketch.PAPER_INK if int(c[k]) > 0 else Sketch.PAPER_DIM
l.add_theme_color_override("font_color", Sketch.PAPER_RED if k == 2 and int(c[k]) > 0 else color)
## Shows the rows of rows (key -> [icon, name, count]) with a count above 0 (count_of(key)), or
## none when there aren't any.
func _fill_list(rows: Dictionary, none: Label, count_of: Callable) -> void:
var any := false
for key: Variant in rows:
var n: int = count_of.call(key)
for c: Control in rows[key]:
c.visible = n > 0
(rows[key][2] as Label).text = str(n)
any = any or n > 0
none.visible = not any
func _list_text(rows: Dictionary, none: Label) -> String:
var parts := PackedStringArray()
for key: Variant in rows:
var cells: Array = rows[key]
if (cells[2] as Control).visible:
parts.append("%s %s" % [(cells[1] as Label).text, (cells[2] as Label).text])
return ", ".join(parts) if not parts.is_empty() else none.text
## A headed table of icon, name, and count; returns the grid the rows go in.
func _list(parent: Control, heading: String) -> GridContainer:
var column := VBoxContainer.new()
column.add_theme_constant_override("separation", 4)
parent.add_child(column)
var head := _ink(column, 30, Sketch.PAPER_DIM)
head.text = heading
var grid := GridContainer.new()
grid.columns = 3
grid.add_theme_constant_override("h_separation", 14)
grid.add_theme_constant_override("v_separation", 0)
column.add_child(grid)
return grid
func _list_row(grid: GridContainer, icon: String, text: String) -> Array:
var i := SketchIcon.new(icon, 46.0)
grid.add_child(i)
var l := _ink(grid, 30)
l.text = text
l.custom_minimum_size = Vector2(150.0, 0.0)
var n := _ink(grid, 32)
n.custom_minimum_size = Vector2(44.0, 0.0)
n.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
return [i, l, n]
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color(0.0, 0.0, 0.0, 0.35)) # dims the farm behind
## The sign behind the board's contents: two posts, the paper between them, a beam across its top
## (under the title) and one across its bottom, nails, and leaves.
func _draw_board() -> void:
var b := _board
var w := b.size.x
var h := b.size.y
var paper := Rect2(SIDE - 14.0, BEAM - 26.0, w - 2.0 * SIDE + 28.0, h - BEAM - BOTTOM + 52.0)
var posts: Array[float] = [SIDE + 34.0, w - SIDE - 34.0]
for k in 2:
b.draw_set_transform(Vector2(posts[k], h * 0.5 + 8.0), PI * 0.5)
Sketch.plank(b, Rect2(-h * 0.5 - 44.0, -27.0, h + 88.0, 54.0), Sketch.WOOD.darkened(0.08), 601.0 + k)
b.draw_set_transform_matrix(Transform2D.IDENTITY)
Sketch.paper(b, paper, 611.0)
Sketch.plank(b, Rect2(0.0, h - BOTTOM - 4.0, w, 62.0), Sketch.WOOD.darkened(0.04), 621.0)
Sketch.plank(b, Rect2(-24.0, 0.0, w + 48.0, BEAM), Sketch.WOOD, 631.0)
for x: float in [paper.position.x + 26.0, paper.end.x - 26.0]:
Sketch.nail(b, Vector2(x, paper.position.y + 46.0))
for k in 2:
Sketch.nail(b, Vector2(posts[k], BEAM * 0.5), 6.0)
Sketch.nail(b, Vector2(posts[k], h - BOTTOM + 27.0), 6.0)
Sketch.leaf(b, Vector2(28.0, 18.0), 46.0, -2.5, 641.0)
Sketch.leaf(b, Vector2(34.0, 22.0), 40.0, -1.4, 642.0)
Sketch.leaf(b, Vector2(w - 26.0, 16.0), 50.0, -0.5, 643.0)
Sketch.leaf(b, Vector2(w - 30.0, 24.0), 38.0, 0.4, 644.0, Sketch.LEAF.lightened(0.12))
Sketch.leaf(b, Vector2(posts[0] - 20.0, h - BOTTOM + 40.0), 42.0, 2.6, 645.0, Sketch.LEAF.darkened(0.1))
static func _spacer(height: float) -> Control:
var c := Control.new()
c.custom_minimum_size = Vector2(0.0, height)
return c
## A Label in ink on the paper (no outline).
func _ink(parent: Node, font_size: int, color: Color = Sketch.PAPER_INK) -> Label:
var l := Label.new()
Sketch.style(l, font_size, color, 0)
parent.add_child(l)
return l
## An icon (Sketch.ICONS) drawn in a square, side long, centered in the control.
class SketchIcon extends Control:
var id := ""
func _init(p_id: String, side: float) -> void:
id = p_id
custom_minimum_size = Vector2(side, side)
mouse_filter = Control.MOUSE_FILTER_IGNORE
func _draw() -> void:
var side := minf(size.x, size.y)
Sketch.icon(self, id, Rect2((size - Vector2(side, side)) * 0.5, Vector2(side, side)))