extends "res://scenes/levels/interlude.gd"
var running = true

const text = [
	["m2", "Hey! Look there!", 0],
	["m3", "What's up?", 2],
	["m2", "Dogs!", 1],
	["m1", "Dogs?", 1],
	["m3", "Dogs!", 1],
	["m2", "What shall we do?", 2],
	["m3", "...", 2],
	["m3", "...", 2],
	["m1", "Let's kidnap!", 1],
	["m3", "Hey! I am your leader! I decide what to do!", 1],
	["m1", "Sorry my great leader. What would you suggest to do?", 2],
	["m3", "...", 2],
	["m3", "...", 2],
	["m3", "I suggest to...", 2],
	["m3", "...", 2],
	["m3", "...", 2],
	["m3", "I have a great idea!", 2],
	["m3", "...", 2],
	["m3", "Let's kidnap!", 2],
	["m1", "What a great idea!", 1],
	["m2", "Our leader is the smartest!", 2]
]

func _ready():
	$CanvasLayer/Monster1.modulate.a = 0
	$CanvasLayer/Monster2.modulate.a = 0
	$CanvasLayer/Monster3.modulate.a = 0
	
	yield(get_tree().create_timer(5), "timeout")
	$CanvasLayer/MonsterAnimation.play("monster2_fade")
	_next_text()
	yield(get_tree().create_timer(3), "timeout")
	$CanvasLayer/MonsterAnimation.play("monster3_fade")
	yield(get_tree().create_timer(4), "timeout")
	$CanvasLayer/MonsterAnimation.play("monster1_fade")
	
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] == "m1":
			$CanvasLayer/speech_monster2.fade_out()
			$CanvasLayer/speech_monster3.fade_out()
			yield(get_tree().create_timer(0.5), "timeout")
			$CanvasLayer/speech_monster1.say(current_text[1])
		elif current_text[0] == "m2":
			$CanvasLayer/speech_monster1.fade_out()
			$CanvasLayer/speech_monster3.fade_out()
			yield(get_tree().create_timer(0.5), "timeout")
			$CanvasLayer/speech_monster2.say(current_text[1])
		elif current_text[0] == "m3":
			$CanvasLayer/speech_monster1.fade_out()
			$CanvasLayer/speech_monster2.fade_out()
			yield(get_tree().create_timer(0.5), "timeout")
			$CanvasLayer/speech_monster3.say(current_text[1])
	else:
		running = false
		yield(get_tree().create_timer(2), "timeout")
		$CanvasLayer/speech_monster1.fade_out()
		$CanvasLayer/speech_monster2.fade_out()
		$CanvasLayer/speech_monster3.fade_out()
		yield(get_tree().create_timer(2), "timeout")
		$CanvasLayer/ZZZAnimation.play("fade")
		$CanvasLayer/MonsterAnimation.play_backwards("monster1_fade")
		yield(get_tree().create_timer(1), "timeout")
		$CanvasLayer/MonsterAnimation.play_backwards("monster2_fade")
		yield(get_tree().create_timer(1), "timeout")
		$CanvasLayer/MonsterAnimation.play_backwards("monster3_fade")
		yield(get_tree().create_timer(4), "timeout")
		$CanvasLayer/speech_monster1.say("Hey, we forgot one!")
		yield(get_tree().create_timer(2), "timeout")
		$CanvasLayer/speech_monster1.fade_out()
		yield(get_tree().create_timer(0.5), "timeout")
		$CanvasLayer/speech_monster3.say("Shut up, stupid!")
		yield(get_tree().create_timer(2), "timeout")
		main.load_level(next_level, true)

func _on_speech_ready():
	if running:
		_next_text()
