#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()

View file

@ -19,3 +19,7 @@ extends Resource
@export var menu_cost_name: String
@export_enum("space", "planet") var placement_mode: String = "space"
@export var unique: bool = false
# #99: what a wave has to chew through to level this. Data rather than one
# constant for every structure - an extractor bolted to a rock is not the
# mothership. See GameState.damage_building().
@export var max_hp: int = 40

View file

@ -22,9 +22,27 @@ var spawned: Array = []
func _ready() -> void:
# #99: the record is the truth, so the node follows it out. Connected
# here rather than in Building.gd because the field owns `spawned` and
# would otherwise be left holding a freed node.
if not GameState.building_destroyed.is_connected(_on_building_destroyed):
GameState.building_destroyed.connect(_on_building_destroyed)
rebuild_all()
func _on_building_destroyed(building_id: int, _type: String) -> void:
var kept: Array = []
for b in spawned:
if not is_instance_valid(b):
continue
if int(b.building_id) == building_id:
BuildingExplosionVFX.spawn(get_tree().root, b.global_position)
b.queue_free()
continue
kept.append(b)
spawned = kept
func rebuild_all() -> void:
for b in spawned:
if is_instance_valid(b):

View file

@ -18,3 +18,4 @@ map_phase = 0.785398
menu_button_name = "CommandButton"
menu_label_name = "CommandLabel"
menu_cost_name = "CommandCost"
max_hp = 70

View file

@ -17,3 +17,4 @@ map_radius = 7.0
menu_button_name = "CrafterButton"
menu_label_name = "CrafterLabel"
menu_cost_name = "CrafterCost"
max_hp = 90

View file

@ -14,3 +14,4 @@ menu_button_name = "ExtractorButton"
menu_label_name = "ExtractorLabel"
menu_cost_name = "ExtractorCost"
placement_mode = "planet"
max_hp = 40

View file

@ -32,3 +32,4 @@ map_phase = 0.0
menu_button_name = "GasBurnerButton"
menu_label_name = "GasBurnerLabel"
menu_cost_name = "GasBurnerCost"
max_hp = 60

View file

@ -19,3 +19,4 @@ menu_button_name = "MothershipButton"
menu_label_name = "MothershipLabel"
menu_cost_name = "MothershipCost"
unique = true
max_hp = 240

View file

@ -18,3 +18,4 @@ map_phase = -1.570796
menu_button_name = "ShipLabButton"
menu_label_name = "ShipLabLabel"
menu_cost_name = "ShipLabCost"
max_hp = 90

View file

@ -18,3 +18,4 @@ map_phase = 0.392699
menu_button_name = "TransportHubButton"
menu_label_name = "TransportHubLabel"
menu_cost_name = "TransportHubCost"
max_hp = 80