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