harvest/game/scripts/radar_hud.gd
Forrest 3e31afb2d9 Task #8: Radar item, a Supply Shed minimap reveal
Adds a Radar upgrade (hidden until enough Alien Goop is collected,
same as the Alien Ray; buyable at the supply shed for 8 scrap/6 wood)
and a RadarHud minimap, created in code under the Hud CanvasLayer
(top-right corner, under the materials readout): hidden until bought,
then draws the playable farm's outline with a dot for the scarecrow
and one per alive enemy within GameConfig.radar_range (40 m) of it,
redrawn every radar_refresh (0.25 s). New test_radar suite covers the
hidden/revealed/bought shop states, minimap visibility, in-range vs
out-of-range dot filtering, and upgrade ownership surviving a save
round trip.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 16:08:54 -04:00

114 lines
4.4 KiB
GDScript

class_name RadarHud
extends Control
## Top-right minimap, under ResourceHud's materials readout (Task #8): the playable farm's
## outline scaled into a small panel, a white dot for the local scarecrow, and one dot per alive
## enemy within GameConfig.radar_range meters of it, colored by type. Hidden unless the Radar
## upgrade is bought (Upgrades.owned(state, "radar")); like the Alien Ray, it's a hidden shed item
## revealed once enough Alien Goop has been collected (Upgrades.revealed). Redrawn every
## GameConfig.radar_refresh seconds rather than every frame, matching the "sweep" feel of a radar
## rather than a live camera.
## Panel size, and its offset from the screen's top-right corner (below ResourceHud, which uses
## roughly the top 120 px of that corner: PhaseHud/AssetHud/ScarecrowHud/CoopHud leave the rest of
## the right side clear).
const MAP_SIZE := Vector2(190.0, 190.0)
const MARGIN_RIGHT := 24.0
const MARGIN_TOP := 140.0
const DOT_RADIUS := 4.0
const PLAYER_RADIUS := 5.0
## thief (alien), crow, breaker (alien robot), ufo: EnemySystem.TYPE_*.
const TYPE_COLORS: Array[Color] = [Color(1.0, 0.3, 0.25), Color(0.95, 0.85, 0.3), Color(0.8, 0.4, 1.0), Color(0.4, 1.0, 0.55)]
var sim: FarmSim
var _title: Label
var _refresh_left: float = 0.0
## The last plotted enemy dots: {"pos": Vector2 (world x, z), "type": int}. Exposed for tests.
var dots: Array[Dictionary] = []
## Whether the scarecrow was plotted this refresh (false until the first refresh happens).
var player_plotted := false
var _player_pos := Vector2.ZERO
func setup(p_sim: FarmSim) -> void:
sim = p_sim
_refresh_left = 0.0
func _ready() -> void:
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
mouse_filter = Control.MOUSE_FILTER_IGNORE
_title = Label.new()
_title.text = "Radar"
_title.add_theme_font_size_override("font_size", 18)
_title.add_theme_color_override("font_outline_color", Color.BLACK)
_title.add_theme_constant_override("outline_size", 6)
_title.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(_title)
func _process(delta: float) -> void:
visible = sim != null and sim.campaign != null and Upgrades.owned(sim.campaign.state, "radar")
if not visible:
return
_title.position = _rect().position + Vector2(10.0, 6.0)
_refresh_left -= delta
if _refresh_left <= 0.0:
_refresh()
_refresh_left = sim.campaign.config.radar_refresh
queue_redraw()
## Rebuilds `dots` from the live enemy positions, filtered to radar_range of the local scarecrow
## (EnemySystem's dense arrays: alive, pos_x/pos_z, type). Works the same on a co-op replica,
## whose EnemySystem gets the host's positions synced into it.
func _refresh() -> void:
dots.clear()
player_plotted = false
if sim == null or sim.enemies == null or sim.local_avatar == null or sim.campaign == null:
return
var cfg := sim.campaign.config
_player_pos = sim.local_avatar.position()
player_plotted = true
var es := sim.enemies
for d in es.active_count:
if es.alive[d] == 1:
var p := Vector2(es.pos_x[d], es.pos_z[d])
if _player_pos.distance_to(p) <= cfg.radar_range:
dots.append({"pos": p, "type": int(es.type[d])})
## How many enemy dots are currently plotted (tests).
func dot_count() -> int:
return dots.size()
func _draw() -> void:
if not visible:
return
var r := _rect()
draw_rect(r, Color(0.0, 0.0, 0.0, 0.42))
var inner := Rect2(r.position + Vector2(10.0, 30.0), r.size - Vector2(20.0, 40.0))
draw_rect(inner, Color(0.08, 0.14, 0.08, 0.85))
draw_rect(inner, Color(0.5, 0.7, 0.5, 0.6), false, 1.5)
if sim == null or sim.zones == null:
return
var playable: Rect2 = sim.zones.playable
if player_plotted:
draw_circle(_to_map(inner, playable, _player_pos), PLAYER_RADIUS, Color(1.0, 1.0, 1.0))
for dot: Dictionary in dots:
var t := int(dot["type"])
var color := TYPE_COLORS[t] if t < TYPE_COLORS.size() else Color.WHITE
draw_circle(_to_map(inner, playable, dot["pos"]), DOT_RADIUS, color)
## A world point (x, z) placed proportionally inside the minimap's inner rect (clamped: an enemy
## within radar_range but outside the playable rect still shows, pinned to the nearest edge).
static func _to_map(inner: Rect2, playable: Rect2, world: Vector2) -> Vector2:
var fx := (world.x - playable.position.x) / playable.size.x
var fy := (world.y - playable.position.y) / playable.size.y
return inner.position + Vector2(clampf(fx, 0.0, 1.0), clampf(fy, 0.0, 1.0)) * inner.size
func _rect() -> Rect2:
return Rect2(Vector2(size.x - MARGIN_RIGHT - MAP_SIZE.x, MARGIN_TOP), MAP_SIZE)