Files
mortal-maze-the-game/ui/hud/thrash_meter.gd

173 lines
6.8 KiB
GDScript

@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 un deposito continuo de sangre o luz espectral.
## 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 carmesi y la de especial, turquesa espectral.
@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(1, 1), size - Vector2(2, 2))
draw_rect(Rect2(Vector2.ZERO, size), Color("100e13"))
draw_rect(inner, Color("21181e"))
if show_damage_trail and not Engine.is_editor_hint() and _trail_ratio > shown:
draw_rect(Rect2(inner.position, Vector2(floorf(inner.size.x * _trail_ratio), inner.size.y)), Color("b99874"))
var filled_width: float = floorf(inner.size.x * shown)
if filled_width > 0.0:
var fill := Rect2(inner.position, Vector2(filled_width, inner.size.y))
draw_rect(fill, fill_color.darkened(0.3))
draw_rect(Rect2(fill.position, Vector2(filled_width, maxf(1.0, floorf(fill.size.y * 0.55)))), fill_color)
draw_line(fill.position, fill.position + Vector2(filled_width, 0), fill_color.lightened(0.45), 1.0)
draw_line(fill.position + Vector2(0, 2), fill.position + Vector2(filled_width, 2), fill_color.lightened(0.15), 1.0)
# Pequenias incisiones de metal reflejan luz; el nivel de carga sigue continuo.
for x: int in range(11, int(filled_width), 37):
draw_rect(Rect2(Vector2(x, maxf(2.0, size.y - 4.0)), Vector2(2, 1)), fill_color.lightened(0.2))
if activation_ratio >= 0.0:
var marker_x: float = floorf(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.85, 0.77, 0.56, 0.7), 1.0)
if highlighted:
var strength: float = 0.4
if animations_enabled and not Engine.is_editor_hint():
strength = 0.35 + sin(_pulse_phase) * 0.13
draw_line(Vector2(1, 0), Vector2(maxf(filled_width, 1.0), 0), Color(fill_color.lightened(0.7), strength), 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))