using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Dialog : MonoBehaviour
{
    public Text text;
    public string[] dialogs;
    private int index;
    private float letterPause = 0.05f;
    public GameObject continueButton;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Type());
    }

    // Update is called once per frame
    void Update()
    {
        if (text.text == dialogs[index])
        {
            continueButton.SetActive(true);
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            SceneManager.LoadScene("Level1");
        }
    }

     IEnumerator Type() {
            foreach (char letter in dialogs[index].ToCharArray()) 
            {
            text.text += letter;
            yield return new WaitForSeconds(letterPause);
            }
     }

     public void NextDialog()
     {
         continueButton.SetActive(false);
         if (index < dialogs.Length -1)
         {
             index++;
             text.text = "";
             StartCoroutine(Type());
         }
         else
         {
            text.text = "";
            continueButton.SetActive(false);
            SceneManager.LoadScene("Level1");
         }
     }
}
