extends AudioStreamPlayer
class_name AudioArrayPlayer

signal all_finished()

@export var queue: Array = []

@warning_ignore("native_method_override")
func stop():
	queue.clear()
	super.stop()

func _ready():
	self.finished.connect(_finished)

func start():
	if len(queue) > 0:
		var new = queue[0]
		if new is float:
			get_tree().create_timer(new).timeout.connect(_finish_delay)
			return
		stream = new
		if !playing:
			play()
	else:
		all_finished.emit()

func _finish_delay():
	next()

func next():
	queue.pop_front()
	start()

func _finished():
	next()


func play_all(extra: Array = []):
	add_streams(extra)
	start()

func add_stream(new: AudioStream):
	queue.append(new)

func add_streams(new: Array):
	queue.append_array(new)
