#99 (#97a): buildings have health, and can be destroyed

Nothing can attack a structure that cannot be hurt, so this is the floor the
rest of #97 stands on.

max_hp is per definition - an extractor bolted to a rock is not the
mothership - and the current value lives on the RECORD in GameState, not on
the Building node. Main.tscn is torn down and rebuilt on every trip to a
planet surface, so damage kept on a node would heal itself the moment the
player landed.

A record written before this carries no hp field at all. That reads as
undamaged rather than as zero, or an existing save would open on a field of
wrecks.

The mothership cannot be destroyed - it floors at 1hp. The bank, the hangar
and research all live there and a wiped fleet rebuilds there, so losing it
would be a soft-lock with no route back. Keyed on the "bank" capability
rather than on the type: whatever building holds the bank is the one that
cannot be lost.

Destroying a hub retires its transporters. Everything else keyed on a
building id already copes with the building being gone - a transporter
mid-delivery puts its cargo back rather than destroying it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Loïc Wisniewski 2026-08-23 13:37:40 +02:00
commit c221584848
12 changed files with 293 additions and 0 deletions

View file

@ -14,6 +14,18 @@ var building_type: String:
set(value): definition_id = value
# #99: drawn only once the structure has actually been hit. A full bar over
# every building would be permanent clutter saying nothing, and the fleet
# would be flying over a field of meters in peacetime.
const HEALTH_BAR_WIDTH: float = 74.0
const HEALTH_BAR_HEIGHT: float = 7.0
const HEALTH_BAR_OFFSET: float = -78.0
const HEALTH_FULL_COLOR: Color = Color(0.45, 0.9, 0.55)
const HEALTH_HURT_COLOR: Color = Color(1.0, 0.35, 0.25)
var _drawn_health: float = 1.0
func _ready() -> void:
add_to_group("buildings")
z_index = -1
@ -24,6 +36,41 @@ func _ready() -> void:
get_node("VisualHost").add_child(visual)
func health_fraction() -> float:
return GameState.building_health_fraction(building_id) if building_id != 0 else 1.0
func is_damaged() -> bool:
return health_fraction() < 0.999
func _process(_delta: float) -> void:
# Redraw only when the number moves. A building that is not being shot at
# costs nothing, which is the same bargain Ship.gd's constant glow makes.
var health: float = health_fraction()
if is_equal_approx(health, _drawn_health):
return
_drawn_health = health
queue_redraw()
func _draw() -> void:
if not is_damaged():
return
# Above the structure rather than across it: the visual scenes all draw
# around their own origin, and a bar laid over one would be read as part
# of the building.
var health: float = health_fraction()
var left: float = -HEALTH_BAR_WIDTH * 0.5
var top: float = HEALTH_BAR_OFFSET
draw_rect(Rect2(left, top, HEALTH_BAR_WIDTH, HEALTH_BAR_HEIGHT),
Color(0.02, 0.02, 0.06, 0.85))
draw_rect(Rect2(left, top, HEALTH_BAR_WIDTH * health, HEALTH_BAR_HEIGHT),
HEALTH_HURT_COLOR.lerp(HEALTH_FULL_COLOR, health))
draw_rect(Rect2(left, top, HEALTH_BAR_WIDTH, HEALTH_BAR_HEIGHT),
Color(0.55, 0.78, 0.86, 0.55), false, 1.0)
func available_actions() -> PackedStringArray:
if visual == null:
return PackedStringArray()