extends "res://scenes/levels/interlude.gd"

const text = [
	["flake", "Hey Flopsy stop! Wait for me!", 0],
	["flopsy", "Flake? What are you doing here?", 1],
	["flake", "I asked around a little bit, and I got some important information.", 1],
	["flake", "The monsters, that kidnapped your friends were seen on the way to the lairs of Aprela.", 2],
	["flake", "You have to go through the catacombs of this abadoned castle and to the great river.", 3],
	["flake", "There you will meet Ricky the ferryman, who can bring you to the other side.", 3],
	["flake", "But take care! He is a rascal!", 3],
	["flake", "He changes the fee for a cross over arbitrary. So better have some extra coins.", 1],
	["flopsy", "Okay, catacombs, ferryman, extra coins, got it.", 3],
	["flake", "Be careful!", 2],
	["flopsy", "Sure. Thank you for your help.", 1]
]

func _ready():
	_next_text()

func _next_text():
	var current_text = text.pop_front()
	
	if current_text != null:
		yield(get_tree().create_timer(current_text[2]), "timeout")
		
		if current_text[0] == "flake":
			$CanvasLayer/speech_flopsy.fade_out()
			yield(get_tree().create_timer(0.5), "timeout")
			$CanvasLayer/speech_flake.say(current_text[1])
		elif current_text[0] == "flopsy":
			$CanvasLayer/speech_flake.fade_out()
			yield(get_tree().create_timer(0.5), "timeout")
			$CanvasLayer/speech_flopsy.say(current_text[1])
	else:
		yield(get_tree().create_timer(1), "timeout")
		$CanvasLayer/speech_flopsy.fade_out()
		$CanvasLayer/speech_flake.fade_out()
		yield(get_tree().create_timer(0.5), "timeout")
		$CanvasLayer/FlopsyFader.play("fade")

func _on_speech_flake_ready():
	_next_text()

func _on_speech_flopsy_ready():
	_next_text()

func _on_FlopsyFader_finished(anim_name):
	main.load_level(next_level, true)
