Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.5 kB
2
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MenuBehaviour : MonoBehaviour
{
    // Start is called before the first frame update
    public AudioClip sonidoAbrir;
    public AudioClip sonidoScroll;
    public AudioClip sonidoGuardar;
    public AudioClip sonidoElegir;
    [SerializeField] private AudioSource audio;

    public GameObject thisGAMEOBJECT;
    public bool isOpened = false;
    public Image currentImage; 
    public Sprite[] allStates = new Sprite[5];

    [SerializeField] public PlayerBehaviour Player;

    int lastStateMenu = 3;
    protected int m_currentStateMenu;

    
    public int currentStateMenu { 
        get { return m_currentStateMenu; }
        set { 
            if (value > 5) { m_currentStateMenu = 5; }
            else if (value < 1) { m_currentStateMenu = 1; }
            else m_currentStateMenu = value;
        }
    }

    //ESTA VARIABLE HACE QUE EL MENU NO SE PUEDA ABRIR (PARA COMBATE O CINEMATICAS)
    public bool menuNoAvailable = false;

    void Start()
    {
        currentImage = GetComponent<Image>();
    }

    // Update is called once per frame
    void Update()
    {
        //Si presiono ESC se cierra
        if (Input.GetKeyDown(KeyCode.Escape) && isOpened == true)
        {
            cerrarMenu();
        }

        //Si cambia currentStateMenu cambio el sprite
        if (currentStateMenu != lastStateMenu)
        {
            lastStateMenu = currentStateMenu;
            playAudio(sonidoScroll);
            currentImage.sprite = allStates[currentStateMenu - 1];
        }

        //Si presiono ENTER...
        if (Input.GetKeyDown(KeyCode.Return))
        {
            abrirBoton();
        }

        Debug.Log(currentStateMenu);
    }

    private void OnEnable()
    {
     
        m_currentStateMenu = 3;
        abrirMenu();
    }

    void abrirMenu()
    {
        //Se ha abierto desde Event Manager
        isOpened = true;
        playAudio(sonidoAbrir);
        Player.characterCanMove = false;

        StartCoroutine(esperarInput());

    }

    IEnumerator esperarInput()
    {

        while (!Input.GetKeyDown(KeyCode.UpArrow) || !Input.GetKeyDown(KeyCode.DownArrow)) { 
        
            if (Input.GetKeyDown(KeyCode.UpArrow)) { procesarInput(KeyCode.UpArrow); }
            if (Input.GetKeyDown(KeyCode.DownArrow)) { procesarInput(KeyCode.DownArrow); }

            yield return null;
        }
    }

    void procesarInput(KeyCode input)
    {

        if (input == KeyCode.UpArrow) { currentStateMenu--; }
        if (input == KeyCode.DownArrow) { currentStateMenu++; }

    }

    void cerrarMenu()
    {
        isOpened = false;
        Player.characterCanMove = true;
        thisGAMEOBJECT.SetActive(false);       
    }

    void playAudio(AudioClip sonido)
    {
        audio.clip = sonido;
        audio.Play();
    }

    void abrirBoton()
    {
        playAudio(sonidoElegir);
        StartCoroutine(waitUntilFeedback());
    }

    IEnumerator waitUntilFeedback()
    {
        if (currentStateMenu == 1) {; }
        else if (currentStateMenu == 2) {; }
        else if (currentStateMenu == 3) {; }
        else if (currentStateMenu == 4) { /* llamarGuardar */ ; }
        else if (currentStateMenu == 5) {; }

       
        yield return null;
    }
}