extends TextureProgressBar

@export var threshold: float = 0.5 
@export var shake_intensity: float = 5.0

var original_x: float

func _ready() -> void:
	EventBus.mask_time_changed.connect(_on_time_changed)
	original_x = position.x

func _process(_delta: float) -> void:
	if value / max_value <= threshold:
		apply_effects()
	else:
		position.x = original_x
		modulate = Color.WHITE

func _on_time_changed(new: float) -> void:
	value = new

func apply_effects() -> void:
	var danger_factor = 1.0 - (value / (max_value * threshold))
	danger_factor = clamp(danger_factor, 0.0, 1.0)
	
	var flash = (sin(Time.get_ticks_msec() * 0.02) + 1.0) / 2.0
	modulate = Color.WHITE * (1 + flash * danger_factor)
	
	var shake = sin(Time.get_ticks_msec() * 0.05) * shake_intensity * danger_factor
	position.x = original_x + shake
