extends StaticBody2D @export var max_health: float = 100.0 var current_health: float var is_destroyed: bool = false var event_bus: EventBus func _ready(): current_health = max_health event_bus = get_node("/root/Main/MainEventBus") event_bus.subscribe_to(get_node("/root/Main/DamageEventScope"), "DamageEvent", Callable(self, "on_damage_event")) add_to_group("asteroids") update_health_label() func on_damage_event(event: DamageEvent) -> EventResult.ResultType: if event.target != self or is_destroyed: return EventResult.ResultType.KEEP_SUBSCRIPTION apply_damage(event.damage_value) if current_health <= 0.0: emit_asteroid_destroyed() return EventResult.ResultType.DISPOSE_SUBSCRIPTION return EventResult.ResultType.KEEP_SUBSCRIPTION func apply_damage(damage: float) -> void: current_health = max(current_health - damage, 0) update_health_label() func emit_asteroid_destroyed() -> void: is_destroyed = true queue_free() publish_destroyed_event() func publish_destroyed_event() -> void: var destroyed_event = DestroyedEvent.new(self, "asteroid", global_position, "laser") event_bus.publish(get_node("/root/Main/DestroyedEventScope"), destroyed_event) func update_health_label() -> void: $AsteroidHealthLabel.text = str(round(current_health)) + " / " + str(max_health)