Untitled

 avatar
unknown
plain_text
3 years ago
3.2 kB
4
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public static GameManager instance = null;

    [SerializeField] WordData[] wordDatas;
    [SerializeField] InputField wordInputField;
    [SerializeField] Image imageDisplayer = null;

    [Header("Debug parameters")]
    [SerializeField] string noWordTypedMessage = "No word was found";
    [SerializeField] string wrongWordUIMessage = "Wrong word!";
    [SerializeField] string correctWordUIMessage = "Correct word!";

    UIHandler uiHandler = null;
    int currentLevelIndex = 0;

    private void Awake()
    {
        // Simple version of the singletton pattern
        if(instance == null)
        {
            instance = this;
        }
    }

    private void Start()
    {
        uiHandler = FindObjectOfType<UIHandler>();
        currentLevelIndex = PlayerPrefs.GetInt("Level", 1) - 1;

        SetUpUI();
    }

    private void SetUpUI()
    {
        if(GetCurrentWordData().Image != null) // Protects from NULL References
        {
            imageDisplayer.sprite = GetCurrentWordData().Image;
        }
    }

    public void SubmitWord()
    {
        WordData currentWordData = GetCurrentWordData();
        if (currentWordData == null) return; // Protects from NULL References

        string wordTyped = wordInputField.text;

        // If no word is typed at all
        if(wordTyped.ToLower() == "")
        {
            uiHandler.DisplayUIMessage(noWordTypedMessage);
            return; // Exit from the function since no word was typed
        }

        // If the word is correct
        if (wordTyped.ToLower() == currentWordData.Word.ToLower())
        {
            StartCoroutine(CorrectWordSequence());
        }
        else // If the word is incorrect
        {
            uiHandler.DisplayUIMessage(wrongWordUIMessage);
        }

        // Reset the word input field
        wordInputField.text = "";
    }

    IEnumerator CorrectWordSequence()
    {
        uiHandler.DisplayUIMessage(correctWordUIMessage);

        // TODO Replace hard coded value
        yield return new WaitForSeconds(2.0f);

        // Unlock the next mission
        int currentMission = PlayerPrefs.GetInt("Level");
        Debug.Log(currentMission);
        PlayerPrefs.SetInt("Mission" + (currentMission + 1).ToString(), 0);
        WordData currentWordData = GetCurrentWordData();

        // Redirect back to main menu, will be changed to actual UI navigation
        if (currentWordData.isLastLevel)
        PlayFabManager.instance.SendLeaderboard();
        FindObjectOfType<SceneLoader>().LoadScene("MainMenu");
    }

    public void RequestHint()
    {
        WordData currentWordData = GetCurrentWordData();
        if (currentWordData == null) return; // Protects from NULL References

        // Display the hint
        uiHandler.ShowHintADMessage(currentWordData.Hint);
    }

    private WordData GetCurrentWordData()
    {
        return wordDatas[currentLevelIndex]; 
    }
}
Editor is loading...