spaceship/buildings/Building.gd
Loïc Wisniewski c221584848 #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>
2026-08-23 13:37:40 +02:00

89 lines
2.7 KiB
GDScript

extends Node2D
const BUILDING_DEFINITIONS = preload("res://buildings/BuildingDefinitions.gd")
@export var definition_id: String = "mothership"
@export var building_id: int = 0
@export var interact_radius: float = 150.0
var definition: Resource
var visual: Node2D
var building_type: String:
get: return definition_id
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
definition = BUILDING_DEFINITIONS.get_definition(definition_id)
if definition == null or definition.visual_scene == null:
return
visual = definition.visual_scene.instantiate()
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()
var provider: Node = visual.get_node_or_null("ActionProvider")
return provider.actions if provider != null else PackedStringArray()
func primary_action() -> String:
for action in available_actions():
if action != "transfer" and action != "hangar":
return action
return ""
func get_display_name() -> String:
return String(definition.display_name) if definition != null else definition_id.capitalize()