## Balloon — tap to pop, auto-respawns after a delay.
class_name Balloon extends Node2D

enum State { IDLE, POPPING, POPPED, RESPAWNING }

const POP_DURATION: float = 0.15
const RESPAWN_DURATION: float = 0.30
const RESPAWN_DELAY: float = 5.0
const BUTTON_HALF_SIZE: float = 20.0

var _state: State = State.IDLE


func _input(event: InputEvent) -> void:
	if _state != State.IDLE:
		return
	if not event is InputEventScreenTouch:
		return
	var touch: InputEventScreenTouch = event as InputEventScreenTouch
	if not touch.pressed:
		return
	var local: Vector2 = to_local(touch.position)
	if abs(local.x) > BUTTON_HALF_SIZE or abs(local.y) > BUTTON_HALF_SIZE:
		return
	_start_pop()


func _start_pop() -> void:
	AudioManager.play_sfx("object_tap")
	_state = State.POPPING
	var tween: Tween = create_tween()
	tween.set_ease(Tween.EASE_IN)
	tween.set_trans(Tween.TRANS_BACK)
	tween.tween_property(self, "scale", Vector2.ZERO, POP_DURATION)
	tween.tween_callback(_on_pop_complete)


func _on_pop_complete() -> void:
	_state = State.POPPED
	var tween: Tween = create_tween()
	tween.tween_interval(RESPAWN_DELAY)
	tween.tween_callback(_start_respawn)


func _start_respawn() -> void:
	_state = State.RESPAWNING
	var tween: Tween = create_tween()
	tween.set_ease(Tween.EASE_OUT)
	tween.set_trans(Tween.TRANS_BACK)
	tween.tween_property(self, "scale", Vector2.ONE, RESPAWN_DURATION)
	tween.tween_callback(_on_respawn_complete)


func _on_respawn_complete() -> void:
	_state = State.IDLE
