Skip to content
Snippets Groups Projects
Dialog.cs 3.16 KiB
/*
 *      Script par Tanguy Gimenez      
 *
 */

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

public class Dialog : MonoBehaviour
{

    // singleton
    public static Dialog instance;

    private void Awake() 
    {
        if (instance != null)
        {
            Debug.LogWarning("Il y a + d'une instance de PlayerNature");
            return;
        }

        instance = this;
    }



    public GameObject dialogBox;
    public Text dialogText;
    public CanvasRenderer cr;
    public int dialog;
    public bool dialogActive;
    public string situation;


    // clé : nom de la scène
    // valeur : tableau de string contenant chaque réplique
    private Dictionary<string, string[]> ListDialogs = new Dictionary<string, string[]>();

    // Start is called before the first frame update
    void Start()
    {

        this.dialog = 0;

        string[] DialogsLvl1 = {
            "Coucou",
            "Je fais un test pour le niveau 1",
            "Ca marche bien nan ?",
            "Melvyn t moch",
            "Valentin aussi",
            "Raff est pas bo"
            };

        this.ListDialogs.Add("start", DialogsLvl1);

        string[] Dialogsraff = {
            "Salut",
            "cava ?",
            "Je te mange tes morts"
        };

        this.ListDialogs.Add("pnj", Dialogsraff);

        string[] DialogsTest = {
            "Je fais un test",
            "ca marche ? ou pas ?",
            "bisous"
        };

        this.ListDialogs.Add("test", DialogsTest);

        string[] DialogMarchand = {
            "Ouvrir le menu c'est avec",
            "la touche E chacal"
        };

        this.ListDialogs.Add("marchand", DialogMarchand);


        if (this.dialogActive)
            this.Situation("start");
    }

    // Update is called once per frame
    void Update()
    {
        if ((Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown("joystick button 0")) && dialogActive)
            this.Next();

        // DEBUG
        if ((Input.GetKeyDown(KeyCode.H) || Input.GetKeyDown("joystick button 1")))
            this.dialogActive = !this.dialogActive;

        // DEBUG
        if (Input.GetKeyDown(KeyCode.F))
        {
            Debug.Log("tut");
            
            this.Situation("marchand");
        }
            
        if(this.dialogActive)
        {
            this.dialogBox.GetComponent<CanvasRenderer>().SetAlpha(1f);
            this.dialogText.GetComponent<CanvasRenderer>().SetAlpha(1f);
        }
        else
        {
            this.dialogBox.GetComponent<CanvasRenderer>().SetAlpha(0f);
            this.dialogText.GetComponent<CanvasRenderer>().SetAlpha(0f);
        }
    }

    void Next()
    {
        if (this.dialog >= this.ListDialogs[this.situation].Length)
            this.dialogActive = false;
        else
        {
            this.dialogText.text = this.ListDialogs[this.situation][this.dialog];
            this.dialog++;
        }
    }

    // Ouvre une bulle de dialogue dans une situation donnée
    void Situation(string sit)
    {
        this.situation = sit;
        this.dialog = 0;

        this.dialogActive = true;

        this.Next();
    }
}