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

76 lines
2.5 KiB
GDScript

class_name BuildHud
extends Control
## The build hotbar, shown in build mode: one slot per buildable with its key, name, and cost
## (red when it can't be afforded), the selected one highlighted, the controls, and how many
## structures stand on the farm.
const SLOT := Vector2(230.0, 76.0)
const GAP := 12.0
var build: BuildMode
var campaign: CampaignController
var _names: Array[Label] = []
var _costs: Array[Label] = []
var _help: Label
func setup(p_build: BuildMode, p_campaign: CampaignController) -> void:
build = p_build
campaign = p_campaign
func _ready() -> void:
set_anchors_preset(Control.PRESET_FULL_RECT)
mouse_filter = Control.MOUSE_FILTER_IGNORE
for i in StructureSystem.ORDER.size():
_names.append(_label(22))
_costs.append(_label(18))
_help = _label(19)
func _process(_delta: float) -> void:
visible = build != null and build.active
if not visible:
return
var r := campaign.state.resources
var origin := _origin()
for i in StructureSystem.ORDER.size():
var t: String = StructureSystem.ORDER[i]
var cost: Dictionary = campaign.config.costs.get(t, {})
var pos := origin + Vector2(i * (SLOT.x + GAP), 0.0)
_names[i].text = "%d %s" % [i + 1, StructureSystem.NAMES[t]]
_names[i].position = pos + Vector2(12.0, 8.0)
_costs[i].text = ResourceStore.describe(cost)
_costs[i].position = pos + Vector2(12.0, 42.0)
_costs[i].add_theme_color_override("font_color", Color(0.9, 0.95, 0.9) if r.can_afford(cost) else Color(1.0, 0.45, 0.4))
_help.text = "Build mode · LMB / RT build · Z C / d-pad rotate · 1-4 / Tab pick · RMB / B dismantle or leave · %d built" % \
build.structures.count()
_help.position = origin + Vector2(0.0, -34.0)
queue_redraw()
func _draw() -> void:
if build == null or not build.active:
return
var origin := _origin()
for i in StructureSystem.ORDER.size():
var rect := Rect2(origin + Vector2(i * (SLOT.x + GAP), 0.0), SLOT)
draw_rect(rect, Color(0.0, 0.0, 0.0, 0.55))
draw_rect(rect, Color(1.0, 0.9, 0.4) if i == build.selected else Color(1.0, 1.0, 1.0, 0.25), false, 3.0 if i == build.selected else 1.5)
func _origin() -> Vector2:
var n := StructureSystem.ORDER.size()
var width := n * SLOT.x + (n - 1) * GAP
return Vector2((size.x - width) * 0.5, size.y - SLOT.y - 28.0)
func _label(font_size: int) -> Label:
var l := Label.new()
l.add_theme_font_size_override("font_size", font_size)
l.add_theme_color_override("font_outline_color", Color.BLACK)
l.add_theme_constant_override("outline_size", 6)
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(l)
return l