harvest/game/scripts/radar_hud.gd
2026-10-07 13:41:38 -04:00

114 lines
4.4 KiB
GDScript

class_name RadarHud
extends Control
## Top-right minimap, under PhaseHud's day and time (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 (MARGIN_TOP when there's no
## PhaseHud; else just under its widget and hint, PhaseHud.bottom()).
const MAP_SIZE := Vector2(190.0, 190.0)
const MARGIN_RIGHT := 24.0
const MARGIN_TOP := 150.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"
Sketch.style(_title, 22, Sketch.TEXT, 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
var at := _rect().position + Vector2(10.0, 0.0)
if at != _title.position:
_title.position = at
queue_redraw() # moved with PhaseHud's hint
_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()
Sketch.box(self, r, Sketch.WASH, Color(0, 0, 0, 0), 0.0, 501.0)
var inner := Rect2(r.position + Vector2(10.0, 30.0), r.size - Vector2(20.0, 40.0))
Sketch.box(self, inner, Color(0.08, 0.14, 0.08, 0.85), Color(0.6, 0.8, 0.6, 0.7), 2.0, 502.0)
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:
var top := sim.phase_hud.bottom() + 8.0 if sim != null and sim.phase_hud != null else MARGIN_TOP
return Rect2(Vector2(size.x - MARGIN_RIGHT - MAP_SIZE.x, top), MAP_SIZE)