class_name PhaseHud extends Control ## Top-left phase indicator: a dial whose ring fills over the current phase with a sun (day), a ## setting sun (dusk), a moon (night), or a rising sun (dawn) in it; "Night N of M"; the phase ## and its time left; and a hint line (what tonight brings at dusk, thieves 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 DIAL_CENTER := Vector2(62.0, 64.0) const DIAL_RADIUS := 34.0 const BANNER_TIME := 3.0 var campaign: CampaignController var waves: WaveDirector var enemies: EnemySystem var _title: Label var _sub: Label var _hint: Label 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 _title = _label(28, Vector2(112.0, 26.0)) _sub = _label(22, Vector2(112.0, 62.0)) _hint = _label(19, Vector2(112.0, 92.0)) _banner = Label.new() _banner.set_anchors_preset(Control.PRESET_TOP_WIDE) _banner.offset_top = 250.0 _banner.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER _banner.add_theme_font_size_override("font_size", 52) _banner.add_theme_color_override("font_outline_color", Color.BLACK) _banner.add_theme_constant_override("outline_size", 12) _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 = 330.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 _dusk_hint.add_theme_font_size_override("font_size", 26) _dusk_hint.add_theme_color_override("font_color", Color(1.0, 0.88, 0.7)) _dusk_hint.add_theme_color_override("font_outline_color", Color.BLACK) _dusk_hint.add_theme_constant_override("outline_size", 8) _dusk_hint.mouse_filter = Control.MOUSE_FILTER_IGNORE _dusk_hint.visible = false add_child(_dusk_hint) ## "Night 1 of 5 · Day · 2:34 left", as the HUD shows it (also used by tests). func describe() -> String: return "%s · %s" % [_title.text, _sub.text] func _process(delta: float) -> void: if campaign == null: return var s := campaign.state var cfg := campaign.config _title.text = "Night %d of %d" % [s.night, cfg.campaign_nights] var left := _clock(s.phase_time_left) var hint := "" match s.phase: CampaignController.Phase.MORNING: _sub.text = "Morning" CampaignController.Phase.DAY: _sub.text = "Day · %s left" % left if campaign.can_start_night_early(): hint = "[N] start the night early" CampaignController.Phase.DUSK: _sub.text = "Dusk · nightfall in %s" % left hint = "\n".join(waves.tonight_warning()) if waves != null else "" CampaignController.Phase.NIGHT: _sub.text = "Night · sunrise in %s" % left hint = on_the_farm(enemies) CampaignController.Phase.DAWN: _sub.text = "Dawn · the aliens are fleeing (%s)" % left CampaignController.Phase.FARMERS_RETURN: _sub.text = "The farmers are back" CampaignController.Phase.GAME_OVER: _sub.text = "Game over" _hint.text = hint 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 := 1.0 - s.phase_time_left / length if is_finite(length) and length > 0.0 else 0.0 var phase := s.phase var night := phase == CampaignController.Phase.NIGHT var hint_size := _hint.get_minimum_size() if _hint.text != "" else Vector2.ZERO var box := Vector2(maxf(430.0, 112.0 + hint_size.x + 18.0), maxf(104.0, 92.0 + hint_size.y - 14.0 + 22.0)) draw_rect(Rect2(Vector2(14.0, 14.0), box), Color(0.0, 0.0, 0.0, 0.42)) draw_circle(DIAL_CENTER, DIAL_RADIUS, Color(0.08, 0.1, 0.18, 0.95) if night else Color(0.25, 0.45, 0.7, 0.95)) var ring := Color(0.6, 0.7, 1.0) if night else Color(1.0, 0.85, 0.4) draw_arc(DIAL_CENTER, DIAL_RADIUS - 4.0, -PI * 0.5, -PI * 0.5 + TAU * frac, 48, ring, 5.0) match phase: CampaignController.Phase.NIGHT: draw_circle(DIAL_CENTER, 15.0, Color(0.93, 0.95, 1.0)) draw_circle(DIAL_CENTER + Vector2(7.0, -5.0), 13.0, Color(0.08, 0.1, 0.18)) CampaignController.Phase.DUSK, CampaignController.Phase.DAWN: var c := DIAL_CENTER + Vector2(0.0, 8.0) draw_circle(c, 15.0, Color(1.0, 0.55, 0.3) if phase == CampaignController.Phase.DUSK else Color(1.0, 0.72, 0.6)) draw_rect(Rect2(c + Vector2(-20.0, 4.0), Vector2(40.0, 16.0)), Color(0.12, 0.2, 0.12)) _: draw_circle(DIAL_CENTER, 15.0, Color(1.0, 0.88, 0.35)) 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] func _label(font_size: int, at: Vector2) -> Label: var l := Label.new() l.position = at 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