extends CanvasLayer
@export var path = ""
@export var mainScene = preload("res://src/scenes/main.tscn")
var text_to_display = []
var current_index = 0
var current_text_index = 0
func _ready() -> void:
	text_to_display = load_text_file(path)
func _on_text_delay_timeout() -> void:
	if current_index < text_to_display.size():
		if current_text_index < text_to_display[current_index].length():
			%TextScenario.text = text_to_display[current_index].substr(0, current_text_index + 1)
			current_text_index += 1
		else:
			%TextDelay.stop()
			%Next.visible = true
	else:
		%TextDelay.stop()
		get_tree().change_scene_to_packed(mainScene)
func _on_color_rect_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("talk"):
		if current_index < text_to_display.size():
			if current_text_index == text_to_display[current_index].length() || current_text_index == 0:
				%TextScenario.text = ""
				%TextDelay.start()
				current_text_index = 0
				current_index += 1
				%Next.visible = false
			else:
				%TextScenario.text = text_to_display[current_index]
				current_text_index = 0
				%TextDelay.stop()
				%Next.visible = true
func load_text_file(file_path: String) -> PackedStringArray:
	var txt = FileAccess.open(file_path, FileAccess.READ)
	var txt_packed = PackedStringArray()
	while txt.get_position() < txt.get_length():
		txt_packed.append(txt.get_line().replace("。", "\n"))
	txt.close()
	return txt_packed