harvest/game/tests/test_radar.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

73 lines
3.9 KiB
GDScript

extends RefCounted
## Task #8: Radar, a hidden Supply Shed item (minimap reveal, Decision 16). Hidden (a "?" row,
## unbuyable) until enough Alien Goop is collected, like the Alien Ray; buyable once revealed; the
## RadarHud minimap stays hidden until it's bought, then shows a dot for every alive enemy within
## GameConfig.radar_range of the local scarecrow (farther ones excluded) plus the scarecrow
## itself; ownership survives a FarmState JSON round trip (FarmState.upgrades is saved).
const DT := 1.0 / 60.0
const SPOT := Vector2(8.0, -10.0) # open ground between the pen, the shed, and the field
func run(r: GameTestRunner) -> void:
var farm := (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D
r.root.add_child(farm)
await r.process_frame
await r.physics_frame
var sim := farm.get_node("FarmSim") as FarmSim
sim.set_physics_process(false)
sim.waves.enabled = false
var player := farm.get_node("Player") as PlayerCharacter
player.global_position = Vector3(SPOT.x, 0.0, SPOT.y)
var radar := farm.get_node("Hud/RadarHud") as RadarHud
var shed_panel := farm.get_node("Hud/ShedPanel") as UpgradePanel
var c := sim.campaign
var s := c.state
var cfg := sim.config
radar._process(0.0)
r.check(radar != null and not radar.visible, "the radar minimap is hidden before the Radar item is bought")
r.check(Upgrades.blocker(c, "radar") == "Not discovered yet", "radar is unbuyable with no Alien Goop collected: '%s'" % Upgrades.blocker(c, "radar"))
shed_panel.refresh()
r.check((shed_panel._rows["radar"] as Button).text == "?", "radar's shed row shows only '?' before it's revealed")
c.begin_day()
s.resources.shed = {"wood": 50, "scrap": 50, "straw": 50, "alien_goop": 0}
r.check(not Upgrades.revealed(s, cfg, "radar"), "still not revealed with zero goop collected")
s.night_events["gathered_alien_goop"] = cfg.hidden_reveal_goop # enough to reveal it, like the Alien Ray
r.check(Upgrades.revealed(s, cfg, "radar"), "revealed once hidden_reveal_goop (%d) is collected" % cfg.hidden_reveal_goop)
shed_panel.refresh()
r.check((shed_panel._rows["radar"] as Button).text.begins_with("Radar —"), "radar's shed row shows its name once revealed: '%s'" % (shed_panel._rows["radar"] as Button).text)
var text := Upgrades.buy(c, "radar", sim.structures)
r.check(Upgrades.owned(s, "radar") and text.contains("done:"), "radar bought: '%s'" % text)
radar._process(0.0)
r.check(radar.visible, "the radar minimap shows once the Radar item is bought")
var es := sim.enemies
var near_thief := es.spawn(EnemySystem.TYPE_THIEF, SPOT.x + 5.0, SPOT.y, Vector2.LEFT)
var near_crow := es.spawn(EnemySystem.TYPE_CROW, SPOT.x, SPOT.y - 6.0, Vector2.UP)
var far_breaker := es.spawn(EnemySystem.TYPE_BREAKER, SPOT.x + cfg.radar_range + 20.0, SPOT.y, Vector2.LEFT)
radar._refresh()
r.check(radar.dot_count() == 2, "two alive enemies within radar_range are plotted (got %d)" % radar.dot_count())
r.check(radar.player_plotted, "the scarecrow itself is plotted on the minimap")
var types: Array = []
for dot: Dictionary in radar.dots:
types.append(int(dot["type"]))
types.sort()
r.check(types == [EnemySystem.TYPE_THIEF, EnemySystem.TYPE_CROW],
"the plotted dots are the near thief and crow, not the far-off breaker (got %s)" % [types])
# Killing the two in-range enemies drops the plotted count back to zero.
es.apply_hit(es.resolve_handle(near_thief), 99)
es.apply_hit(es.resolve_handle(near_crow), 99)
radar._refresh()
r.check(radar.dot_count() == 0, "no dots once the in-range enemies are dead (the far breaker was never counted)")
es.apply_hit(es.resolve_handle(far_breaker), 99)
# Ownership survives a save round trip (FarmState.to_dict/from_dict, same as SaveGame does through JSON).
var round_tripped := FarmState.from_dict(JSON.parse_string(JSON.stringify(s.to_dict())))
r.check(round_tripped != null and Upgrades.owned(round_tripped, "radar"), "radar ownership survives a FarmState JSON round trip")
farm.queue_free()
await r.process_frame