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

187 lines
6.8 KiB
GDScript

class_name PhaseHud
extends Control
## Top-right day and time, hand-drawn (Sketch): a sun by day (a moon at night) on a light wash,
## "Day N of M", the time left in the phase ("1:42 until Dusk"), and a thin teal line filling
## over the phase; under it, a hint when there is one (what tonight brings at dusk, who's on the
## farm at night, the early-night key when allowed). Each phase also announces itself with a
## banner that fades; from dusk into the night, tonight's dusk hint (NightPlan.dusk_hint) shows
## under it.
const BANNER_TIME := 3.0
const WIDGET := Vector2(430.0, 112.0)
const MARGIN := Vector2(24.0, 18.0)
const PROGRESS := Color(0.3, 0.82, 0.8)
const HINT_SIZE := 22
const HINT_LINE := 28.0
var campaign: CampaignController
var waves: WaveDirector
var enemies: EnemySystem
## What the widget shows: "Day 2 of 30", "1:42 until Dusk", and the hint ("" for none).
var title := ""
var sub := ""
var hint := ""
var _banner: Label
var _banner_left: float = 0.0
var _dusk_hint: Label
func setup(p_campaign: CampaignController, p_waves: WaveDirector, p_enemies: EnemySystem) -> void:
campaign = p_campaign
waves = p_waves
enemies = p_enemies
campaign.phase_changed.connect(_on_phase_changed)
_on_phase_changed(campaign.state.phase, campaign.state.phase)
func _ready() -> void:
set_anchors_preset(Control.PRESET_FULL_RECT)
mouse_filter = Control.MOUSE_FILTER_IGNORE
_banner = Label.new()
_banner.set_anchors_preset(Control.PRESET_TOP_WIDE)
_banner.offset_top = 250.0
_banner.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
Sketch.style(_banner, 60, Sketch.TEXT, 14)
_banner.mouse_filter = Control.MOUSE_FILTER_IGNORE
_banner.modulate.a = 0.0
add_child(_banner)
_dusk_hint = Label.new()
_dusk_hint.set_anchors_preset(Control.PRESET_CENTER_TOP)
_dusk_hint.offset_top = 336.0
_dusk_hint.offset_left = -520.0
_dusk_hint.offset_right = 520.0
_dusk_hint.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_dusk_hint.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
Sketch.style(_dusk_hint, 30, Color(1.0, 0.88, 0.7), 10)
_dusk_hint.mouse_filter = Control.MOUSE_FILTER_IGNORE
_dusk_hint.visible = false
add_child(_dusk_hint)
## "Day 1 of 5 · 2:34 until Dusk", as the HUD shows it (also used by tests).
func describe() -> String:
return "%s · %s" % [title, sub]
## The widget's rect (canvas), and the bottom of it and its hint (RadarHud sits under that).
func widget_rect() -> Rect2:
return Rect2(Vector2(size.x - MARGIN.x - WIDGET.x, MARGIN.y), WIDGET)
func bottom() -> float:
var lines := hint.count("\n") + 1 if hint != "" else 0
return widget_rect().end.y + 10.0 + lines * HINT_LINE
func _process(delta: float) -> void:
if campaign == null:
return
var s := campaign.state
var cfg := campaign.config
title = "Day %d of %d" % [s.night, cfg.campaign_nights]
var left := _clock(s.phase_time_left)
hint = ""
match s.phase:
CampaignController.Phase.MORNING:
sub = "Morning"
CampaignController.Phase.DAY:
sub = "%s until Dusk" % left
if campaign.can_start_night_early():
hint = "[N] start the night early"
CampaignController.Phase.DUSK:
sub = "%s until Nightfall" % left
hint = "\n".join(waves.tonight_warning()) if waves != null else ""
CampaignController.Phase.NIGHT:
sub = "%s until Dawn" % left
hint = on_the_farm(enemies)
CampaignController.Phase.DAWN:
sub = "%s until Morning" % left
CampaignController.Phase.FARMERS_RETURN:
sub = "The farmers are back"
CampaignController.Phase.GAME_OVER:
sub = "Game over"
var plan := waves.tonight() if waves != null else null
_dusk_hint.visible = plan != null and plan.dusk_hint != "" and waves.warning_active()
if _dusk_hint.visible:
_dusk_hint.text = plan.dusk_hint
_banner_left = maxf(_banner_left - delta, 0.0)
_banner.modulate.a = clampf(_banner_left / 0.8, 0.0, 1.0)
queue_redraw()
func _draw() -> void:
if campaign == null:
return
var s := campaign.state
var length := campaign.phase_length(s.phase)
var frac := clampf(1.0 - s.phase_time_left / length, 0.0, 1.0) if is_finite(length) and length > 0.0 else 0.0
var r := widget_rect()
Sketch.box(self, r, Sketch.WASH, Color(0, 0, 0, 0), 0.0, 301.0)
Sketch.icon(self, "moon" if s.phase == CampaignController.Phase.NIGHT else "sun", Rect2(r.position + Vector2(4.0, 4.0), Vector2(104.0, 104.0)))
var right := r.end.x - 18.0
Sketch.text(self, Vector2(right, r.position.y + 6.0), title, 40, Sketch.TEXT, HORIZONTAL_ALIGNMENT_RIGHT)
Sketch.text(self, Vector2(right, r.position.y + 54.0), sub, 36, Sketch.TEXT, HORIZONTAL_ALIGNMENT_RIGHT)
var y := r.end.y + 1.0
Sketch.line(self, Vector2(r.position.x + 6.0, y), Vector2(r.end.x - 6.0, y), Color(PROGRESS, 0.25), 4.0, 302.0, 0.8)
if frac > 0.0:
Sketch.line(self, Vector2(r.position.x + 6.0, y), Vector2(r.position.x + 6.0 + (r.size.x - 12.0) * frac, y), PROGRESS, 4.0, 302.0, 0.8)
var lines := hint.split("\n", false)
for i in lines.size():
Sketch.text(self, Vector2(right, r.end.y + 10.0 + i * HINT_LINE), lines[i], HINT_SIZE, Color(1.0, 0.93, 0.75),
HORIZONTAL_ALIGNMENT_RIGHT, 6)
func _on_phase_changed(phase: CampaignController.Phase, _previous: CampaignController.Phase) -> void:
var s := campaign.state
var text := ""
match phase:
CampaignController.Phase.DAY:
text = "Day %d: gather and build" % s.night
CampaignController.Phase.DUSK:
text = "Dusk: %s" % _tonight_title("the aliens come at nightfall")
CampaignController.Phase.NIGHT:
text = "Night %d: %s" % [s.night, _tonight_title("")] if _tonight_title("") != "" else "Night %d" % s.night
CampaignController.Phase.DAWN:
text = "Sunrise"
if text != "":
_banner.text = text
_banner_left = BANNER_TIME
func _tonight_title(fallback: String) -> String:
var plan := waves.tonight() if waves != null else null
return plan.title if plan != null and plan.title != "" else fallback
static func _clock(seconds: float) -> String:
var t := ceili(seconds)
return "%d:%02d" % [floori(t / 60.0), t % 60]
## "3 crows, 2 thieves, and a fence breaker on the farm" (or "" when nobody is).
static func on_the_farm(es: EnemySystem) -> String:
if es == null:
return ""
var counts := PackedInt32Array()
counts.resize(EnemySystem.TYPE_NAMES.size())
for d in es.active_count:
if es.alive[d] == 1:
counts[es.type[d]] += 1
var parts := PackedStringArray()
for t in [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER, EnemySystem.TYPE_UFO]:
if counts[t] == 1:
parts.append(EnemySystem.ARTICLES[t] + " " + EnemySystem.NAMES[t])
elif counts[t] > 1:
parts.append("%d %s" % [counts[t], EnemySystem.PLURALS[t]])
if parts.is_empty():
return "Nobody on the farm"
var text := join_words(parts) + " on the farm"
return text[0].to_upper() + text.substr(1)
static func join_words(words: PackedStringArray) -> String:
if words.size() <= 1:
return "".join(words)
return ", ".join(words.slice(0, words.size() - 1)) + " and " + words[words.size() - 1]