harvest/game/scripts/resource_hud.gd
2026-10-07 13:41:38 -04:00

111 lines
4 KiB
GDScript

class_name ResourceHud
extends Control
## Bottom-left backpack, hand-drawn (Sketch): this machine's scarecrow's pouch, "used/capacity"
## beside it, and a red dot once something new has gone into it since it was last opened (gone
## again when the pouch is emptied). The pouch action (I / R3) opens the pouch panel over it, and
## closes it again: each material carried, and how much of each the supply shed holds.
## The backpack icon, from the screen's bottom-left corner.
const ICON := Rect2(26.0, -136.0, 108.0, 108.0)
const PANEL_WIDTH := 410.0
const ROW := 46.0
const DOT := Color(0.92, 0.12, 0.1)
const SHED := Color(1.0, 0.9, 0.62)
var campaign: CampaignController
## The avatar slot whose pouch is shown (FarmSim.set_local: the partner's machine shows P2's).
var slot: int = 0
## The pouch panel is open.
var open := false
## Something went into the pouch since it was last opened.
var new_items := false
var _last := {}
func setup(p_campaign: CampaignController) -> void:
campaign = p_campaign
_last = campaign.state.resources.pouch_of(slot).duplicate()
func _ready() -> void:
set_anchors_preset(Control.PRESET_FULL_RECT)
mouse_filter = Control.MOUSE_FILTER_IGNORE
func _unhandled_input(event: InputEvent) -> void:
if campaign != null and is_visible_in_tree() and event.is_action_pressed("pouch", false):
toggle()
get_viewport().set_input_as_handled()
func toggle() -> void:
open = not open
if open:
new_items = false
## "6/15": what the pouch holds, of what it can.
func count_text() -> String:
var r := campaign.state.resources
return "%d/%d" % [r.pouch_total(slot), r.pouch_capacity]
## The pouch panel's rows: {material, pouch, shed}, one per material (ResourceStore.MATERIALS).
func rows() -> Array[Dictionary]:
var out: Array[Dictionary] = []
var r := campaign.state.resources
var p := r.pouch_of(slot)
for m in ResourceStore.MATERIALS:
out.append({"material": m, "pouch": int(p.get(m, 0)), "shed": int(r.shed.get(m, 0))})
return out
func _process(_delta: float) -> void:
if campaign == null:
return
var r := campaign.state.resources
var p := r.pouch_of(slot)
for m in ResourceStore.MATERIALS:
if int(p.get(m, 0)) > int(_last.get(m, 0)) and not open:
new_items = true
_last = p.duplicate()
if r.pouch_total(slot) == 0:
new_items = false
queue_redraw()
func _draw() -> void:
if campaign == null:
return
var icon := Rect2(Vector2(ICON.position.x, size.y + ICON.position.y), ICON.size)
Sketch.icon(self, "backpack", icon)
if new_items:
var at := icon.position + Vector2(icon.size.x - 16.0, 16.0)
draw_colored_polygon(Sketch.blob(at, 11.0, 11.0, 401.0, 0.04, 20), DOT)
Sketch.circle(self, at, 11.5, Sketch.INK, 2.0, 402.0)
Sketch.text(self, Vector2(icon.end.x + 4.0, icon.get_center().y - 18.0), count_text(), 42)
if open:
_draw_panel(icon)
func _draw_panel(icon: Rect2) -> void:
var list := rows()
var height := 52.0 + list.size() * ROW + 12.0
var r := Rect2(Vector2(icon.position.x - 4.0, icon.position.y - 14.0 - height), Vector2(PANEL_WIDTH, height))
Sketch.panel(self, r, 411.0)
var pouch_x := r.position.x + 290.0
var shed_x := r.end.x - 26.0
Sketch.text(self, r.position + Vector2(18.0, 8.0), "Pouch", 32)
Sketch.text(self, Vector2(pouch_x, r.position.y + 14.0), "Carried", 24, Sketch.DIM, HORIZONTAL_ALIGNMENT_RIGHT, 6)
Sketch.text(self, Vector2(shed_x, r.position.y + 14.0), "Shed", 24, Sketch.DIM, HORIZONTAL_ALIGNMENT_RIGHT, 6)
Sketch.rule(self, r.position.x + 14.0, r.end.x - 14.0, r.position.y + 50.0, 412.0)
for i in list.size():
var row: Dictionary = list[i]
var m: String = row["material"]
var y := r.position.y + 56.0 + i * ROW
Sketch.icon(self, m, Rect2(Vector2(r.position.x + 14.0, y), Vector2(40.0, 40.0)))
Sketch.text(self, Vector2(r.position.x + 62.0, y + 2.0), m.capitalize(), 30, GatherNode.COLORS[m])
var carried: int = row["pouch"]
Sketch.text(self, Vector2(pouch_x, y + 2.0), str(carried), 30, Sketch.TEXT if carried > 0 else Sketch.DIM, HORIZONTAL_ALIGNMENT_RIGHT)
Sketch.text(self, Vector2(shed_x, y + 2.0), str(row["shed"]), 30, SHED, HORIZONTAL_ALIGNMENT_RIGHT)