Homebound / Scripts / TitleScreen.cs
TitleScreen.cs
Raw
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class TitleScreen : MonoBehaviour
{
    [Header("Canvas Attributes")]
    [SerializeField] Button startAndContinueButton; // Reference to the start and continue button that will switch between starting and continuing depending on the state of the player's progress

    void Start()
    {
        bool state = DataPersistenceManager.Instance.CheckDataExists(); // Checks to see if a save file exists

        // If the state returns false then a New game state is created otherwise the scene loads and continues
        if (!state)
            NewGame();
        else
            LoadGame();
    }
    /// <summary>
    /// Initializes the game data and then parameterizes the start/continue button with a listener
    /// </summary>
    void NewGame()
    {
        DataPersistenceManager.Instance.NewGame(); // Initializes a new file

        // Initializes the start/continue button to start the game
        TextMeshProUGUI textComp = startAndContinueButton.GetComponentInChildren<TextMeshProUGUI>();
        textComp.text = "Start";

        // Delegates the start game method to the button for the first level
        startAndContinueButton.onClick.AddListener(() => { StartGame(); });
    }
    /// <summary>
    /// Loads the game data from previous session on player's computer based off of the DPM than paramterizes the start/continue button
    /// </summary>
    void LoadGame() 
    {
        // Checks to see what level was left not completed
        int index = LevelChecker();

        // Initializes the start/continue button to continue the game from where the player left off
        TextMeshProUGUI textComp = startAndContinueButton.GetComponentInChildren<TextMeshProUGUI>();
        textComp.text = "Continue";

        // Delegates the continue game method to the button for the level they left off at
        startAndContinueButton.onClick.AddListener(() => { ContinueGame(index); });
    }
    /// <summary>
    /// The levels in the data manager are sifted through til a level that has not been completed is found, or the first level is started if all have been completed
    /// </summary>
    /// <returns>Build index for the level to load</returns>
    int LevelChecker()
    {
        int counter = 1; 

        // Levels are sifted through until the first one that returns false is found
        foreach (KeyValuePair<string, bool> level in DataPersistenceManager.Instance.gameData.levelsCompleted)
        {
            if (!level.Value)
                return counter;
            
            counter++;
        }

        // If all return true than the first level's index is retrieved 
        return 1;
    }
    /// <summary>
    /// Starts the player on the first level
    /// </summary>
    public void StartGame() => SceneManager.LoadScene(1);
    
    /// <summary>
    /// Plays the level that is marked by the level index based off of level SO parameters
    /// </summary>
    /// <param name="levelIndex"></param>
    public void ContinueGame(int levelIndex) => SceneManager.LoadScene(levelIndex);
    
    /// <summary>
    /// Quits the application
    /// </summary>
    public void ExitGame() => Application.Quit();
}