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

205 lines
7.3 KiB
GDScript

class_name ScarecrowHud
extends Control
## Bottom-left status for the scarecrow: health as pips, the fear ability as a circle that
## sweeps full while it cools down and flashes when it is ready again, the patch hint with its
## progress, and once the Sprint upgrade is bought a sprint line (ready, seconds left, or
## recharging); plus a red screen tint when the scarecrow is hit. Bottom-right, the weapons:
## the three by number, the one in hand marked, the ones not bought yet greyed out (from the
## workbench), the pumpkin bomb's recharge, and how to switch.
const PIP := Vector2(22.0, 16.0)
const PIP_GAP := 5.0
const FEAR_RADIUS := 26.0
## Extra height of the panel for the sprint line.
const SPRINT_ROW := 30.0
## The weapon panel, from the bottom-right corner, and its rows.
const WEAPONS := Rect2(-340.0, -176.0, 304.0, 150.0)
const WEAPON_ROW := 30.0
var kit: Scarecrow
## This machine's scarecrow's weapons.
var combat: ScarecrowCombat
var campaign: CampaignController
## The health line's name ("Scarecrow"; in co-op "P1 Scarecrow" / "P2 Scarecrow").
var label := "Scarecrow"
var _tint: ColorRect
var _hp_label: Label
var _fear_label: Label
var _patch_label: Label
var _sprint_label: Label
var _weapon_title: Label
var _weapon_rows: Array[Label] = []
var _weapon_hint: Label
var _hit: float = 0.0
var _ready_flash: float = 0.0
func setup(p_kit: Scarecrow, p_campaign: CampaignController, p_combat: ScarecrowCombat = null) -> void:
campaign = p_campaign
retarget(p_kit, p_combat)
## Shows another kit (co-op: the partner's game shows player 2's).
func retarget(p_kit: Scarecrow, p_combat: ScarecrowCombat = null) -> void:
combat = p_combat
if kit != null:
kit.damaged.disconnect(_on_damaged)
kit.fear_ready.disconnect(_on_fear_ready)
kit = p_kit
kit.damaged.connect(_on_damaged)
kit.fear_ready.connect(_on_fear_ready)
func _on_damaged(_hp: int) -> void:
_hit = 1.0
func _on_fear_ready() -> void:
_ready_flash = 1.0
func _ready() -> void:
set_anchors_preset(Control.PRESET_FULL_RECT)
mouse_filter = Control.MOUSE_FILTER_IGNORE
_tint = ColorRect.new()
_tint.set_anchors_preset(Control.PRESET_FULL_RECT)
_tint.color = Color(0.85, 0.05, 0.0, 0.0)
_tint.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(_tint)
_hp_label = _label(22)
_fear_label = _label(20)
_patch_label = _label(20)
_sprint_label = _label(20)
_weapon_title = _label(18)
_weapon_title.text = "Weapon"
for i in ScarecrowCombat.WEAPON_NAMES.size():
_weapon_rows.append(_label(20))
_weapon_hint = _label(16)
_weapon_hint.text = "Switch: Tab / 1-3 / d-pad down"
func _process(delta: float) -> void:
_hit = maxf(_hit - delta * 2.5, 0.0)
_ready_flash = maxf(_ready_flash - delta * 2.0, 0.0)
_tint.color.a = 0.22 * _hit
if kit == null:
return
var base := _base()
var s := campaign.state
_hp_label.text = "%s %d/%d" % [label, kit.hp(), kit.max_hp()]
_hp_label.position = base + Vector2(0.0, -34.0)
if not kit.alive:
_fear_label.text = "Fear"
elif kit.fear_left == 0.0:
_fear_label.text = "Fear Q / LB"
else:
_fear_label.text = "Fear %.0f s" % ceilf(kit.fear_left)
_fear_label.position = base + Vector2(FEAR_RADIUS * 2.0 + 14.0, 34.0)
if not kit.alive:
_patch_label.text = "Patch"
elif kit.patching:
_patch_label.text = "Patching up..."
elif kit.hp() >= kit.max_hp():
_patch_label.text = "Patch (full health)"
else:
_patch_label.text = "Patch hold F / Y (%s)" % ResourceStore.describe(kit.config.costs.get("patch", {}))
_patch_label.position = base + Vector2(FEAR_RADIUS * 2.0 + 14.0, 62.0)
var body := kit.player
_sprint_label.visible = _sprint_shown()
if _sprint_label.visible:
if not kit.alive:
_sprint_label.text = "Sprint"
elif body.sprinting:
_sprint_label.text = "Sprinting %.1f s" % body.sprint_left
elif body.sprint_recharge > 0.0:
_sprint_label.text = "Sprint %.0f s" % ceilf(body.sprint_recharge)
else:
_sprint_label.text = "Sprint hold Shift / L3"
_sprint_label.position = base + Vector2(0.0, 100.0)
_update_weapons()
queue_redraw()
func _update_weapons() -> void:
var shown := combat != null
_weapon_title.visible = shown
_weapon_hint.visible = shown
for l in _weapon_rows:
l.visible = shown
if not shown:
return
var at := _weapons_rect().position
_weapon_title.position = at + Vector2(12.0, 6.0)
for i in _weapon_rows.size():
var l := _weapon_rows[i]
var text := "%d %s" % [i + 1, ScarecrowCombat.WEAPON_NAMES[i]]
var color := Color(0.95, 0.95, 0.95)
if not combat.owns(i):
text += " (workbench)"
color = Color(0.55, 0.55, 0.55)
elif i == ScarecrowCombat.Weapon.PUMPKIN_BOMB and combat.bomb_left > 0.0:
text += " %.0f s" % ceilf(combat.bomb_left)
if i == combat.weapon:
color = Color(1.0, 0.85, 0.3)
l.text = text
l.add_theme_color_override("font_color", color)
l.position = at + Vector2(30.0, 30.0 + i * WEAPON_ROW)
_weapon_hint.position = at + Vector2(12.0, 30.0 + _weapon_rows.size() * WEAPON_ROW + 2.0)
func _weapons_rect() -> Rect2:
return Rect2(size + WEAPONS.position, WEAPONS.size)
func _draw() -> void:
if kit == null:
return
var base := _base()
var s := campaign.state
var height := 146.0 + (SPRINT_ROW if _sprint_shown() else 0.0)
draw_rect(Rect2(base - Vector2(14.0, 46.0), Vector2(340.0, height)), Color(0.0, 0.0, 0.0, 0.42))
var frac := float(kit.hp()) / float(maxi(kit.max_hp(), 1))
var full := Color(0.45, 0.9, 0.35) if frac > 0.5 else (Color(1.0, 0.8, 0.2) if frac > 0.25 else Color(1.0, 0.3, 0.2))
full = full.lerp(Color.WHITE, _hit * 0.6)
for i in kit.max_hp():
var r := Rect2(base + Vector2(i * (PIP.x + PIP_GAP), 0.0), PIP)
draw_rect(r, full if i < kit.hp() else Color(0.15, 0.15, 0.15, 0.8))
draw_rect(r, Color(0.0, 0.0, 0.0, 0.9), false, 2.0)
# Fear: dark disc, a sweep that fills while cooling down, bright when ready.
var c := base + Vector2(FEAR_RADIUS, 32.0 + FEAR_RADIUS)
var ready := kit.fear_left == 0.0 and kit.alive
draw_circle(c, FEAR_RADIUS, Color(0.08, 0.1, 0.14, 0.9))
if ready:
draw_circle(c, FEAR_RADIUS - 5.0, Color(0.55, 0.85, 1.0).lerp(Color.WHITE, _ready_flash))
else:
draw_arc(c, FEAR_RADIUS - 8.0, -PI * 0.5, -PI * 0.5 + TAU * kit.fear_charge(), 48, Color(0.4, 0.6, 0.75), 9.0)
draw_arc(c, FEAR_RADIUS + 3.0 + _ready_flash * 10.0, 0.0, TAU, 48, Color(1.0, 1.0, 1.0, 0.25 + 0.75 * _ready_flash), 2.5)
if combat != null:
var w := _weapons_rect()
draw_rect(w, Color(0.0, 0.0, 0.0, 0.42))
var row := Rect2(w.position + Vector2(6.0, 32.0 + combat.weapon * WEAPON_ROW), Vector2(w.size.x - 12.0, WEAPON_ROW - 2.0))
draw_rect(row, Color(1.0, 0.85, 0.3, 0.18))
draw_rect(Rect2(row.position, Vector2(4.0, row.size.y)), Color(1.0, 0.85, 0.3))
if kit.patching:
var bar := Rect2(base + Vector2(FEAR_RADIUS * 2.0 + 14.0, 92.0), Vector2(210.0, 8.0))
draw_rect(bar, Color(0.1, 0.1, 0.1, 0.9))
draw_rect(Rect2(bar.position, Vector2(bar.size.x * kit.patch_elapsed / kit.config.patch_time, bar.size.y)), Color(0.45, 1.0, 0.4))
func _base() -> Vector2:
return Vector2(36.0, size.y - 126.0 - (SPRINT_ROW if _sprint_shown() else 0.0))
func _sprint_shown() -> bool:
return kit != null and kit.player != null and kit.player.can_sprint()
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