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>
This commit is contained in:
Forrest 2026-10-05 16:08:54 -04:00
parent ff2ace9d3c
commit 3e31afb2d9
9 changed files with 219 additions and 1 deletions

View file

@ -289,6 +289,11 @@ func _ready() -> void:
review_panel.refresh()
if hud != null:
hud.setup(scarecrow, campaign, combat)
if hud != null and hud.get_parent() != null:
var radar_hud := RadarHud.new()
radar_hud.name = "RadarHud"
hud.get_parent().add_child(radar_hud)
radar_hud.setup(self)
ending = FarmersReturn.new()
ending.name = "FarmersReturn"
add_child(ending)

View file

@ -130,6 +130,7 @@ extends Resource
"reinforced_fences": {"wood": 20, "scrap": 6},
"long_range": {"scrap": 14, "wood": 10},
"sticky_tar": {"straw": 14, "scrap": 6},
"radar": {"scrap": 8, "wood": 6},
"fence": {"wood": 2},
"gate": {"wood": 4, "scrap": 1},
"slowing_trap": {"scrap": 3, "wood": 1},
@ -153,6 +154,7 @@ extends Resource
"shurikens": {"name": "Shurikens", "text": "Fast, cheap throws for a quick hit", "track": "Weapon", "shop": "shed"},
"propane_torch": {"name": "Propane torch", "text": "A short-range jet of flame that burns everything in its cone while held", "track": "Weapon", "shop": "shed"},
"alien_ray": {"name": "Alien ray", "text": "A salvaged alien weapon: a fast, powerful beam", "track": "Weapon", "shop": "shed", "hidden": true},
"radar": {"name": "Radar", "text": "A minimap showing nearby enemy positions", "track": "Item", "shop": "shed", "hidden": true},
"wide_fear": {"name": "Wide fear", "text": "Fear reaches 30% farther", "track": "Fear", "stat": "fear_radius", "mult": 1.3},
"quick_fear": {"name": "Quick fear", "text": "Fear recharges 20% faster", "track": "Fear", "stat": "fear_cooldown", "mult": 0.8},
"straw_stuffing": {"name": "Straw stuffing", "text": "+2 max health", "track": "Scarecrow", "stat": "scarecrow_max_hp", "add": 2},
@ -297,6 +299,16 @@ extends Resource
@export var alien_ray_speed: float = 50.0
@export var alien_ray_lifetime: float = 2.0
@export_group("Radar")
## Radar (the Radar item, bought at the supply shed, hidden until hidden_reveal_goop Alien Goop is
## collected, #8): once owned, a minimap shows every alive enemy within radar_range meters of the
## local scarecrow, redrawn every radar_refresh seconds (RadarHud). 40 m is generous: it covers
## most of the playable farm (60x60) from a central position, so the minimap is useful without
## needing to be perfectly centered on the scarecrow at all times. A quarter-second refresh keeps
## the dots responsive without redrawing every frame.
@export var radar_range: float = 40.0
@export var radar_refresh: float = 0.25
@export_group("Enemies")
@export_range(1, 1024) var enemy_capacity: int = 800
## The enemy roster (EnemySystem.TYPE_NAMES), one row per type:

114
game/scripts/radar_hud.gd Normal file
View file

@ -0,0 +1,114 @@
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)

View file

@ -0,0 +1 @@
uid://by4nfx6ob5nsn

View file

@ -16,7 +16,7 @@ extends RefCounted
## Menu order, grouped by track.
const ORDER: Array[String] = ["corn_shooter", "rapid_fire", "heavy_seeds", "pumpkin_bomb", "potato_cannon", "shurikens",
"propane_torch", "alien_ray", "wide_fear", "quick_fear",
"propane_torch", "alien_ray", "radar", "wide_fear", "quick_fear",
"straw_stuffing", "pouch_upgrade", "sprint", "reinforced_fences", "long_range", "sticky_tar"]