@tool
extends GunkNode
## Fires a trigger signal when a sound is made nearby

signal triggered

@export var quick_connect_to: SignalNode:
	set = _editor_connect

@export var splatter_scene: PackedScene

# NOTE
# trigger oscillation animation was generated using the formula
# f(x) = e^(-0.25x) * cos(x * pi / 2 - pi/2) + 1 for x in {0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 20}
@onready var trigger_animation_player: AnimationPlayer = %TriggerAnimationPlayer
@onready var cooldown_timer: Timer = %CooldownTimer


func trigger() -> void:
	if not cooldown_timer.is_stopped():
		return

	# TODO play sfx

	trigger_animation_player.play("trigger")
	triggered.emit()
	cooldown_timer.start()


func _destroy() -> void:
	add_sibling(splatter_scene.instantiate())


func _editor_connect(node: SignalNode) -> void:
	triggered.connect(node.trigger, CONNECT_PERSIST)
	self.notify_property_list_changed()
