spaceship/ui/ResourceReadout.gd

136 lines
5.3 KiB
GDScript3
Raw Normal View History

Generalize the resource HUD readouts (#49) Replaces ResourceCounter/GasCounter/CargoCounter/GasCargoCounter - one hardcoded node plus one script per resource, times two scenes - with a single ResourceReadout.gd that builds one banked + one fleet-hold row per GameState.resource_types() entry at _ready(). A new resource added to ResourceDefinitions gets a HUD row with no scene edit and no new script, matching how ResourceBar and ResourceTransferList already generalize over resources. Preserves the existing visual layout (BANKED left, FLEET HOLD right), the ore-on-bright-surface contrast (via a bright_background flag replacing PlanetSurface's per-node color overrides), and the amber fleet-in-transit color convention. Reuses the already-generic ResourceGlyph for icons instead of NeonNumber's hardcoded ore hexagon. Uncovered and fixed a second bug along the way: test_cargo_readout.gd looked up its NeonNumber nodes immediately after add_child() in _initialize(), but ResourceReadout builds its rows in _ready(), which for a -s entry-point script does not fire synchronously during that add_child() - it's deferred to the first frame. Moved the lookup to after the existing elapsed wait in _process(), which is also what tests/test_resource_readout.gd's setup does from the start. Repo notes for the next agent, confirmed empirically this session: - A -s entry-point script can only reach an autoload's CONSTANTS via the bare identifier (GameState.RESOURCE_ORE works everywhere). Calling any of its METHODS that way (GameState.resource_types()) fails to compile with "Identifier not found: GameState", even from _initialize(). Route method calls through the actual instance (root.get_node("GameState")) instead, as the rest of the suite already does. - `git status --short | awk '{print $2}' | xargs touch` recreates deleted files as empty stubs, since touch on a missing path creates it. Filter out D-status paths before touching, or touch only the paths you know still exist. 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-16 08:30:10 +02:00
class_name ResourceReadout
extends Control
# Generalizes ResourceCounter/GasCounter/CargoCounter/GasCargoCounter - one
# hardcoded node plus one script per resource, times two scenes - into one
# row per GameState.resource_types() entry, built at _ready(). A new
# resource added to ResourceDefinitions.ORDER/DEFINITIONS gets a HUD row
# with no scene edit and no new script, the same way ResourceBar and
# ResourceTransferList already pick up a new resource automatically.
#
# Layout mirrors what it replaces: a BANKED column (left) and a FLEET HOLD
# column (right), one row per resource, each row a resource glyph plus a
# digit readout. Banked rows are always visible, even at zero (matching
# ResourceCounter/GasCounter today); fleet rows hide at zero, since that
# hold isn't the player's yet (see CargoCounter's own note on why).
const FLEET_GLOW: Color = Color(1.0, 0.78, 0.25)
const FLEET_CORE: Color = Color(0.22, 0.12, 0.0)
const ROW_HEIGHT: float = 58.0
const COLUMN_GAP: float = 328.0
const GLYPH_GAP: float = 8.0
@export var digit_height: float = 38.0
# The bright rocky/gas surfaces need a darker banked glow than deep space
# for the same contrast reason PlanetSurface.tscn's old ResourceCounter
# node overrode its colour - set true on the surface-scene instance only.
@export var bright_background: bool = false
var _banked_numbers: Dictionary = {}
var _fleet_numbers: Dictionary = {}
# #56: the fleet hold now has a finite total capacity. Rather than a new
# node, this reuses the existing "FLEET HOLD" caption Label that both Main
# and PlanetSurface already place as a sibling - get_node_or_null so a
# scene that happens to embed ResourceReadout without that sibling (none
# does today) degrades to just not showing a limit, instead of erroring.
@onready var _fleet_caption: Label = get_node_or_null("../FleetCaption")
var _fleet_caption_base_text: String = ""
Generalize the resource HUD readouts (#49) Replaces ResourceCounter/GasCounter/CargoCounter/GasCargoCounter - one hardcoded node plus one script per resource, times two scenes - with a single ResourceReadout.gd that builds one banked + one fleet-hold row per GameState.resource_types() entry at _ready(). A new resource added to ResourceDefinitions gets a HUD row with no scene edit and no new script, matching how ResourceBar and ResourceTransferList already generalize over resources. Preserves the existing visual layout (BANKED left, FLEET HOLD right), the ore-on-bright-surface contrast (via a bright_background flag replacing PlanetSurface's per-node color overrides), and the amber fleet-in-transit color convention. Reuses the already-generic ResourceGlyph for icons instead of NeonNumber's hardcoded ore hexagon. Uncovered and fixed a second bug along the way: test_cargo_readout.gd looked up its NeonNumber nodes immediately after add_child() in _initialize(), but ResourceReadout builds its rows in _ready(), which for a -s entry-point script does not fire synchronously during that add_child() - it's deferred to the first frame. Moved the lookup to after the existing elapsed wait in _process(), which is also what tests/test_resource_readout.gd's setup does from the start. Repo notes for the next agent, confirmed empirically this session: - A -s entry-point script can only reach an autoload's CONSTANTS via the bare identifier (GameState.RESOURCE_ORE works everywhere). Calling any of its METHODS that way (GameState.resource_types()) fails to compile with "Identifier not found: GameState", even from _initialize(). Route method calls through the actual instance (root.get_node("GameState")) instead, as the rest of the suite already does. - `git status --short | awk '{print $2}' | xargs touch` recreates deleted files as empty stubs, since touch on a missing path creates it. Filter out D-status paths before touching, or touch only the paths you know still exist. 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-16 08:30:10 +02:00
func _ready() -> void:
mouse_filter = Control.MOUSE_FILTER_IGNORE
if _fleet_caption != null:
_fleet_caption_base_text = _fleet_caption.text
Generalize the resource HUD readouts (#49) Replaces ResourceCounter/GasCounter/CargoCounter/GasCargoCounter - one hardcoded node plus one script per resource, times two scenes - with a single ResourceReadout.gd that builds one banked + one fleet-hold row per GameState.resource_types() entry at _ready(). A new resource added to ResourceDefinitions gets a HUD row with no scene edit and no new script, matching how ResourceBar and ResourceTransferList already generalize over resources. Preserves the existing visual layout (BANKED left, FLEET HOLD right), the ore-on-bright-surface contrast (via a bright_background flag replacing PlanetSurface's per-node color overrides), and the amber fleet-in-transit color convention. Reuses the already-generic ResourceGlyph for icons instead of NeonNumber's hardcoded ore hexagon. Uncovered and fixed a second bug along the way: test_cargo_readout.gd looked up its NeonNumber nodes immediately after add_child() in _initialize(), but ResourceReadout builds its rows in _ready(), which for a -s entry-point script does not fire synchronously during that add_child() - it's deferred to the first frame. Moved the lookup to after the existing elapsed wait in _process(), which is also what tests/test_resource_readout.gd's setup does from the start. Repo notes for the next agent, confirmed empirically this session: - A -s entry-point script can only reach an autoload's CONSTANTS via the bare identifier (GameState.RESOURCE_ORE works everywhere). Calling any of its METHODS that way (GameState.resource_types()) fails to compile with "Identifier not found: GameState", even from _initialize(). Route method calls through the actual instance (root.get_node("GameState")) instead, as the rest of the suite already does. - `git status --short | awk '{print $2}' | xargs touch` recreates deleted files as empty stubs, since touch on a missing path creates it. Filter out D-status paths before touching, or touch only the paths you know still exist. 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-16 08:30:10 +02:00
var y: float = 0.0
for resource_type in GameState.resource_types():
var banked_color: Color = GameState.resource_color(resource_type)
if bright_background:
banked_color = banked_color.darkened(0.4)
_add_glyph(resource_type, Vector2(0.0, y))
var banked: NeonNumber = _add_number(Vector2(digit_height + GLYPH_GAP, y),
banked_color, banked_color.darkened(0.8))
_banked_numbers[resource_type] = banked
_add_glyph(resource_type, Vector2(COLUMN_GAP, y))
var fleet: NeonNumber = _add_number(Vector2(COLUMN_GAP + digit_height + GLYPH_GAP, y),
FLEET_GLOW, FLEET_CORE)
_fleet_numbers[resource_type] = fleet
y += ROW_HEIGHT
Seat the HUD button columns below the resource readout, and move the planet name The build button overlapped the resource list. ResourceReadout builds one row per resource type AT RUNTIME (58px each), so how far down it reaches is not knowable when the scene is authored: PlanetSurface/Main declared offset_bottom 154, four resources actually reach 270, and BuildButton sat at a hardcoded 160. A 110px collision that grew with every resource type added. Because the readout spans the full width - the fleet hold column is at x 356 - it hit both button columns, TransferButton and EnterAtmosphereButton as well as the left one. Rather than nudge the offsets, the layout now derives from the readout: ResourceReadout.rows_height()/content_bottom() report the real extent and _ready() stops the node understating its own size, and BuildController._seat_hud_columns() seats both columns below that at load. Adding a fifth resource type pushes the buttons down on its own, so the overlap cannot come back. Two details worth keeping: the height is derived from the live resource list rather than cached in _ready(), because BuildController readies before UI's children in scene-tree order and a cached value would answer zero. And it translates the block rather than re-flowing it, since BuildingActionButton and HangarButton deliberately share a row as mutually exclusive options. The planet name moved from the centre - where it sat across resource rows 3 and 4 - to the top-right corner, right-aligned. Right alignment is what keeps it clear: short names sit against the right edge, well clear of the fleet hold column, and long ones wrap downward into empty space rather than growing left into it. That scene edit rode along in the #68 commit, both being changes to PlanetSurface.tscn. Also adds three .uid files orphaned by earlier commits this session; the repo tracks them for every other script. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-17 23:20:51 +02:00
# Stop understating our own extent. The rows are drawn by child nodes,
# which happily paint outside this Control's rect, so the scene-authored
# height stayed at its original guess no matter how many rows appeared.
size.y = rows_height()
# How tall the rows are, derived from the live resource list rather than from
# whatever _ready() happened to have built. That matters: _ready() runs in
# scene-tree order, so a sibling laying itself out relative to this readout
# (BuildController._seat_hud_columns) can legitimately ask BEFORE this node
# has built anything, and a cached height would answer zero.
func rows_height() -> float:
return float(GameState.resource_types().size()) * ROW_HEIGHT
# Bottom edge of the rows in this Control's parent space - the line anything
# stacked below the readout has to clear.
func content_bottom() -> float:
#228: shrink the HUD on a screen the canvas is short on The layout is authored in CANVAS units. With stretch "canvas_items" and aspect "expand" over a 1920x1080 base, a 720x1280 phone scales by 0.375 and gets 1920x3413 canvas units - while ANY 16:9-or-wider screen lands on 1920x1080 units however many pixels it really has. The canvas is three times shorter, so a 148-unit button went from 4.3% of the screen's height to 13.7%. Measured at 3.16x, and confirmed twice: once by hand off the scene file, once by the test failing with the rule disabled. Nothing collided. That was checked pair by pair on both canvases and the same six pairs overlap on each, all of them same-slot alternatives. The fault was only that everything was enormous. HudScale captures each HUD control's authored offsets once, then moves it toward the edge it hangs off and scales the NODE. Scaling the rect alone was tried first and was wrong: half the HUD draws its own contents at fixed sizes - ResourceReadout lays its rows out in code, Labels carry their own font size - so shrunken boxes kept full-size content and the build button slid up underneath the resource list. Scaling the node takes content and children with it. The cost is that position (parent space) and size (local space) now differ by that scale, and a lot of code here assumed they did not. Five places: ResourceReadout.content_bottom() added a scaled position to an unscaled height, overstating itself by more than a button - so _seat_hud_columns pushed the whole column down to clear space nothing occupied. A live bug, not just a bad measurement. HUD_COLUMN_MARGIN would have been the one gap still phone-sized. _layout_context_buttons wrote its stride and pitch unscaled, landing the action row on the building-action row - which is the second of the two regressions test_hud_layout was written for, in a new disguise. test_hud_layout and test_hangar_management compared authored rects. And HudScale reapplies on resize, which wiped the two layouts BuildController writes at runtime - so dragging a desktop window left the context row where it was. Both re-run on size_changed now, deferred, because which of two siblings connected first is not a thing to depend on. test_hud_layout ran at one window shape, which happened to be the desktop canvas. It now runs every check at both, so this cannot hide at whichever shape was not being exercised. Rejected: scaling the UI CanvasLayer does not work at all - Control anchors resolve against the viewport rect, so bottom-anchored elements land mid-screen. A single scaled wrapper Control with the HUD reparented under it is the genuinely clean shape and would need no scale-aware arithmetic anywhere, but it changes every node path and would break every get_node in the controllers and tests. MIN_SCALE floors the shrink at 0.45 rather than the proportional 0.316: a landscape phone gets the same short canvas as a desktop, and there the sticks are still thumb targets. That leaves the HUD at 1.42x its phone share on a PC rather than 1.0x. 177/177 at JOBS=3. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019AJMdB7iuHuLFZM1aBu3GD
2026-09-12 18:04:00 +02:00
# Rendered, not laid out. #228 scales the HUD down on a short canvas by
# scaling the NODE while leaving its rect at the authored size, so
# rows_height() is in unscaled local units while position is already in
# scaled parent ones. Mixing the two overstated this by more than a
# button's height and pushed the columns down to clear space that was
# not occupied.
return position.y + rows_height() * scale.y
Seat the HUD button columns below the resource readout, and move the planet name The build button overlapped the resource list. ResourceReadout builds one row per resource type AT RUNTIME (58px each), so how far down it reaches is not knowable when the scene is authored: PlanetSurface/Main declared offset_bottom 154, four resources actually reach 270, and BuildButton sat at a hardcoded 160. A 110px collision that grew with every resource type added. Because the readout spans the full width - the fleet hold column is at x 356 - it hit both button columns, TransferButton and EnterAtmosphereButton as well as the left one. Rather than nudge the offsets, the layout now derives from the readout: ResourceReadout.rows_height()/content_bottom() report the real extent and _ready() stops the node understating its own size, and BuildController._seat_hud_columns() seats both columns below that at load. Adding a fifth resource type pushes the buttons down on its own, so the overlap cannot come back. Two details worth keeping: the height is derived from the live resource list rather than cached in _ready(), because BuildController readies before UI's children in scene-tree order and a cached value would answer zero. And it translates the block rather than re-flowing it, since BuildingActionButton and HangarButton deliberately share a row as mutually exclusive options. The planet name moved from the centre - where it sat across resource rows 3 and 4 - to the top-right corner, right-aligned. Right alignment is what keeps it clear: short names sit against the right edge, well clear of the fleet hold column, and long ones wrap downward into empty space rather than growing left into it. That scene edit rode along in the #68 commit, both being changes to PlanetSurface.tscn. Also adds three .uid files orphaned by earlier commits this session; the repo tracks them for every other script. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-17 23:20:51 +02:00
Generalize the resource HUD readouts (#49) Replaces ResourceCounter/GasCounter/CargoCounter/GasCargoCounter - one hardcoded node plus one script per resource, times two scenes - with a single ResourceReadout.gd that builds one banked + one fleet-hold row per GameState.resource_types() entry at _ready(). A new resource added to ResourceDefinitions gets a HUD row with no scene edit and no new script, matching how ResourceBar and ResourceTransferList already generalize over resources. Preserves the existing visual layout (BANKED left, FLEET HOLD right), the ore-on-bright-surface contrast (via a bright_background flag replacing PlanetSurface's per-node color overrides), and the amber fleet-in-transit color convention. Reuses the already-generic ResourceGlyph for icons instead of NeonNumber's hardcoded ore hexagon. Uncovered and fixed a second bug along the way: test_cargo_readout.gd looked up its NeonNumber nodes immediately after add_child() in _initialize(), but ResourceReadout builds its rows in _ready(), which for a -s entry-point script does not fire synchronously during that add_child() - it's deferred to the first frame. Moved the lookup to after the existing elapsed wait in _process(), which is also what tests/test_resource_readout.gd's setup does from the start. Repo notes for the next agent, confirmed empirically this session: - A -s entry-point script can only reach an autoload's CONSTANTS via the bare identifier (GameState.RESOURCE_ORE works everywhere). Calling any of its METHODS that way (GameState.resource_types()) fails to compile with "Identifier not found: GameState", even from _initialize(). Route method calls through the actual instance (root.get_node("GameState")) instead, as the rest of the suite already does. - `git status --short | awk '{print $2}' | xargs touch` recreates deleted files as empty stubs, since touch on a missing path creates it. Filter out D-status paths before touching, or touch only the paths you know still exist. 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-16 08:30:10 +02:00
func _add_glyph(resource_type: String, at: Vector2) -> void:
var glyph: ResourceGlyph = ResourceGlyph.new()
glyph.resource_type = resource_type
glyph.position = at
glyph.size = Vector2(digit_height, digit_height)
add_child(glyph)
func _add_number(at: Vector2, glow: Color, core: Color) -> NeonNumber:
var number: NeonNumber = NeonNumber.new()
number.show_icon = false
number.position = at
number.size = Vector2(0.0, digit_height)
number.glow_color = glow
number.core_color = core
number.digit_height = digit_height
add_child(number)
return number
func banked_number(resource_type: String) -> NeonNumber:
return _banked_numbers.get(resource_type)
func fleet_number(resource_type: String) -> NeonNumber:
return _fleet_numbers.get(resource_type)
func _process(_delta: float) -> void:
if _fleet_caption != null:
var capacity: int = GameState.fleet_cargo_total_capacity()
_fleet_caption.text = "%s %d/%d" % [_fleet_caption_base_text, GameState.fleet_cargo_used(), capacity] \
if capacity >= 0 else _fleet_caption_base_text
Generalize the resource HUD readouts (#49) Replaces ResourceCounter/GasCounter/CargoCounter/GasCargoCounter - one hardcoded node plus one script per resource, times two scenes - with a single ResourceReadout.gd that builds one banked + one fleet-hold row per GameState.resource_types() entry at _ready(). A new resource added to ResourceDefinitions gets a HUD row with no scene edit and no new script, matching how ResourceBar and ResourceTransferList already generalize over resources. Preserves the existing visual layout (BANKED left, FLEET HOLD right), the ore-on-bright-surface contrast (via a bright_background flag replacing PlanetSurface's per-node color overrides), and the amber fleet-in-transit color convention. Reuses the already-generic ResourceGlyph for icons instead of NeonNumber's hardcoded ore hexagon. Uncovered and fixed a second bug along the way: test_cargo_readout.gd looked up its NeonNumber nodes immediately after add_child() in _initialize(), but ResourceReadout builds its rows in _ready(), which for a -s entry-point script does not fire synchronously during that add_child() - it's deferred to the first frame. Moved the lookup to after the existing elapsed wait in _process(), which is also what tests/test_resource_readout.gd's setup does from the start. Repo notes for the next agent, confirmed empirically this session: - A -s entry-point script can only reach an autoload's CONSTANTS via the bare identifier (GameState.RESOURCE_ORE works everywhere). Calling any of its METHODS that way (GameState.resource_types()) fails to compile with "Identifier not found: GameState", even from _initialize(). Route method calls through the actual instance (root.get_node("GameState")) instead, as the rest of the suite already does. - `git status --short | awk '{print $2}' | xargs touch` recreates deleted files as empty stubs, since touch on a missing path creates it. Filter out D-status paths before touching, or touch only the paths you know still exist. 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-16 08:30:10 +02:00
for resource_type in GameState.resource_types():
var banked: NeonNumber = _banked_numbers.get(resource_type)
if banked != null:
banked.set_amount(GameState.banked_resource(resource_type))
var fleet: NeonNumber = _fleet_numbers.get(resource_type)
if fleet == null:
continue
var held: int = GameState.fleet_resource(resource_type)
fleet.visible = held > 0
if fleet.visible:
fleet.set_amount(held)