@tool class_name ThrashMeter extends ProgressBar ## Medidor visual compartido por vida y especial; no conoce al jugador ni modifica el combate. ## Conserva el contrato de ProgressBar (value/max_value) y dibuja segmentos de amplificador. ## Solo este componente visual se ejecuta en el editor; los controladores del HUD no son @tool. @export_group("Thrash Appearance") ## Color de los segmentos llenos. La escena de vida usa rojo y la de especial, verde acido. @export var fill_color: Color = Color("d73949"): set(new_value): fill_color = new_value queue_redraw() @export_range(4, 32, 1) var segment_count: int = 12: set(new_value): segment_count = new_value queue_redraw() ## Fraccion de la capacidad donde dibujar la marca de activacion. -1 la oculta. @export_range(-1.0, 1.0, 0.01) var activation_ratio: float = -1.0: set(new_value): activation_ratio = new_value queue_redraw() @export_group("Thrash Feedback") ## Desactivar para obtener un HUD estatico: sin pulsos, destellos ni interpolacion. @export var animations_enabled: bool = true: set(new_value): animations_enabled = new_value if not new_value: _snap_to_value() _update_processing() ## Conserva brevemente la vida perdida en color hueso. El especial no necesita esta estela. @export var show_damage_trail: bool = true @export_range(0.0, 1.0, 0.05) var damage_trail_delay: float = 0.35 ## Duracion de cada subida, incluso cuando solo se ganan 10 puntos. @export_range(0.05, 1.0, 0.05) var fill_transition_seconds: float = 0.2 ## El controlador decide cuando destacar la barra: poca vida o carga suficiente. var highlighted: bool = false: set(new_value): highlighted = new_value queue_redraw() _update_processing() var _display_ratio: float = 0.0 var _target_ratio: float = 0.0 var _fill_speed: float = 0.0 var _trail_ratio: float = 0.0 var _trail_wait: float = 0.0 var _damage_flash: float = 0.0 var _pulse_phase: float = 0.0 var _updating_range: bool = false func _ready() -> void: mouse_filter = Control.MOUSE_FILTER_IGNORE value_changed.connect(_on_value_changed) changed.connect(_on_range_changed) resized.connect(queue_redraw) _snap_to_value() _update_processing() ## Actualiza valor y capacidad como una sola operacion; la primera lectura no se anima. ## Una capacidad cero se representa vacia y nunca provoca una division por cero. func set_amount(current: float, capacity: float, immediate: bool = false) -> void: # Cambiar la escala del medidor no es recibir dano: reinicia la representacion visual. var capacity_changed: bool = not is_equal_approx(max_value, maxf(capacity, 1.0)) _updating_range = true max_value = maxf(capacity, 1.0) value = clampf(current, 0.0, maxf(capacity, 0.0)) _updating_range = false if immediate or capacity_changed or Engine.is_editor_hint() or not animations_enabled: _snap_to_value() else: _change_target() _update_processing() func _on_value_changed(_new_value: float) -> void: if not _updating_range: if Engine.is_editor_hint() or not animations_enabled: _snap_to_value() else: _change_target() _update_processing() func _on_range_changed() -> void: # Range.changed notifica tambien los cambios de capacidad desde el Inspector. _on_value_changed(value) func _snap_to_value() -> void: _target_ratio = clampf(value / maxf(max_value, 1.0), 0.0, 1.0) _display_ratio = _target_ratio _fill_speed = 0.0 _trail_ratio = _target_ratio _trail_wait = 0.0 _damage_flash = 0.0 queue_redraw() func _change_target() -> void: var next_ratio: float = clampf(value / maxf(max_value, 1.0), 0.0, 1.0) if next_ratio < _target_ratio: # Perder vida o consumir el solo se muestra al instante; no se finge energia disponible. _display_ratio = next_ratio if show_damage_trail: _trail_ratio = maxf(_trail_ratio, _target_ratio) _trail_wait = damage_trail_delay _damage_flash = 0.18 _target_ratio = next_ratio _fill_speed = absf(_target_ratio - _display_ratio) / maxf(fill_transition_seconds, 0.05) if not show_damage_trail: _trail_ratio = next_ratio queue_redraw() func _update_processing() -> void: # Una barra estable no redibuja cada frame. El editor solo cambia al tocar sus propiedades. if not is_inside_tree(): return var moving: bool = not is_equal_approx(_display_ratio, _target_ratio) var trailing: bool = show_damage_trail and not is_equal_approx(_trail_ratio, _target_ratio) set_process(not Engine.is_editor_hint() and animations_enabled and (highlighted or moving or trailing or _damage_flash > 0.0)) func _process(delta: float) -> void: _display_ratio = move_toward(_display_ratio, _target_ratio, delta * _fill_speed) _trail_wait = maxf(0.0, _trail_wait - delta) if _trail_wait <= 0.0: _trail_ratio = move_toward(_trail_ratio, _target_ratio, delta / 0.45) _damage_flash = maxf(0.0, _damage_flash - delta) # Pulso lento y localizado; no parpadea toda la pantalla ni mueve los controles. _pulse_phase = fmod(_pulse_phase + delta * TAU * 0.75, TAU) if is_visible_in_tree(): queue_redraw() _update_processing() func _draw() -> void: if size.x < 8.0 or size.y < 6.0: return var shown: float = clampf(value / maxf(max_value, 1.0), 0.0, 1.0) if Engine.is_editor_hint() else _display_ratio var inner := Rect2(Vector2(2, 2), size - Vector2(4, 4)) var count: int = maxi(segment_count, 1) var step_width: float = inner.size.x / float(count) var cell_width: float = maxf(step_width - 2.0, 1.0) draw_rect(Rect2(Vector2.ZERO, size), Color("05090b")) draw_rect(Rect2(Vector2.ZERO, size), Color("3b464b"), false, 1.0) for index: int in range(count): var cell := Rect2(inner.position + Vector2(float(index) * step_width, 0), Vector2(cell_width, inner.size.y)) _draw_segment(cell, 1.0, Color("202b30")) if show_damage_trail and not Engine.is_editor_hint(): _draw_segment(cell, clampf(_trail_ratio * count - index, 0.0, 1.0), Color("a99774")) var fraction: float = clampf(shown * count - index, 0.0, 1.0) _draw_segment(cell, fraction, fill_color) if fraction > 0.0: # Bisel iluminado y una linea de sombra dentro de cada segmento. var bevel: float = minf(3.0, cell.size.x * 0.25) var length: float = (cell.size.x - bevel) * fraction draw_line(cell.position + Vector2(bevel, 1), cell.position + Vector2(bevel + length, 1), fill_color.lightened(0.45), 1.0) draw_line(cell.position + Vector2(0, cell.size.y - 1), cell.position + Vector2(length, cell.size.y - 1), fill_color.darkened(0.4), 1.0) if activation_ratio >= 0.0: var marker_x: float = inner.position.x + inner.size.x * clampf(activation_ratio, 0.0, 1.0) draw_line(Vector2(marker_x, 1), Vector2(marker_x, size.y - 1), Color(0.94, 0.94, 0.79, 0.65), 1.0) draw_colored_polygon(PackedVector2Array([ Vector2(marker_x, size.y), Vector2(marker_x - 3, size.y + 4), Vector2(marker_x + 3, size.y + 4) ]), Color("e7e7ca")) if highlighted: var strength: float = 0.7 if animations_enabled and not Engine.is_editor_hint(): strength = 0.65 + sin(_pulse_phase) * 0.2 draw_rect(Rect2(Vector2(-1, -1), size + Vector2(2, 2)), Color(fill_color, strength), false, 1.0) if _damage_flash > 0.0 and animations_enabled and not Engine.is_editor_hint(): draw_rect(Rect2(Vector2.ZERO, size), Color(1.0, 0.87, 0.76, _damage_flash * 1.5)) ## Recorta cada segmento en diagonal sin texturas adicionales ni shaders. func _draw_segment(rect: Rect2, fraction: float, color: Color) -> void: if fraction <= 0.0: return var bevel: float = minf(3.0, rect.size.x * 0.25) var length: float = (rect.size.x - bevel) * fraction var origin: Vector2 = rect.position draw_colored_polygon(PackedVector2Array([ origin + Vector2(bevel, 0), origin + Vector2(bevel + length, 0), origin + Vector2(length, rect.size.y), origin + Vector2(0, rect.size.y) ]), color)