UnityGameProjectsCode / Rise2Point0Game / Managers / SettingsManager.cs
SettingsManager.cs
Raw
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class SettingsManager : MonoBehaviour
{
    //recieves changes from the options menu on the main menu.
    //sends change info to the save manager and input manager as needed.

    private InputManager inputMgr;
    private ApplyForceToPlayer applyForce;
    private ApplyForceController applyForceController;
    private LevelGenerator levelGen;
    private DeathWall deathWall;
    private ScoreManager scoreMgr;
    private LevelCameraEffects camEffects;
    private AchievementSystem achSys;
    private UnlockSystem unlockSys;
    public ResolutionGridFiller gridFiller;
    private TutorialControl tutControl;
    private int currentResolutionIndex;

    [Header("Options Panels")]
    public GameObject videoAudioPanel;
    public GameObject controlsPanel;
    public GameObject gameSettingsPanel;

    [Space]

    [Header("Interactive Objects - Video")]
    public TMP_Dropdown graphicsQuality;
    public Toggle postProcessing;
    public Toggle fullScreen;
    public Slider sfxVolume;
    public Slider pauseSFXVolume;
    public Slider musicVolume;
    public Slider pauseMusicVolume;
    public Slider masterVolume;
    public Slider pauseMasterVolume;
    public AudioMixer mainMix;

    [Space]

    [Header("Interactive Objects - Controls")]
    //public Button pauseMenu;
    public TextMeshProUGUI pauseMenuText;
    //public Button zoomOut;
    public TextMeshProUGUI zoomText;
    //public Button playNextTrack;
    public TextMeshProUGUI playText;
    //public Button pauseMusic;
    public TextMeshProUGUI pauseMusicText;
    //public Button playLastTrack;
    public TextMeshProUGUI playLastText;
    //public Button openUnlocks;
    public TextMeshProUGUI openUnlocksText;
    //public Button toggleShuffle;
    public TextMeshProUGUI toggleShuffleText;
    //public Button toggleLoop;
    public TextMeshProUGUI toggleLoopText;
    public Button launchPlayer;
    public TextMeshProUGUI launchPlayerText;

    [Space]

    [Header("Interactive Objects - Game Settings")]
    public Toggle disableJumpBar;
    public Toggle enableClassicMovement;
    public Toggle disableTutorial;
    public Toggle enableChillMode;
    public Toggle disableScoreText;
    public Toggle enableInvertedMovement;
    public Toggle disableLoopShuffleButtons;
    public Toggle disableIcons;

    [Space]

    [Header("Default Values")]
    public float defaultSFXVolume;
    public float defaultMusicVolume;
    public float defaultMasterVolume;

    private void Awake()
    {
        inputMgr = GetComponent<InputManager>();
        applyForce = ObjectManager.GetObject(3).GetComponent<ApplyForceToPlayer>();
        applyForceController = ObjectManager.GetObject(3).GetComponent<ApplyForceController>();
        levelGen = GetComponent<LevelGenerator>();
        deathWall = ObjectManager.GetObject(2).GetComponent<DeathWall>();
        scoreMgr = GetComponent<ScoreManager>();
        camEffects = GUIManager.GetObject(12).GetComponent<LevelCameraEffects>();
        achSys = GetComponent<AchievementSystem>();
        unlockSys = GetComponent<UnlockSystem>();
        tutControl = GetComponent<TutorialControl>();

        graphicsQuality.ClearOptions();
        graphicsQuality.AddOptions(QualitySettings.names.ToList());

        if (PlayerPrefs.HasKey("currentQuality"))
            graphicsQuality.value = PlayerPrefs.GetInt("currentQuality");
        else
        {
            graphicsQuality.value = QualitySettings.GetQualityLevel();
            PlayerPrefs.SetInt("currentQuality", graphicsQuality.value);
        }

        if (PlayerPrefs.HasKey("currentResolution"))
            currentResolutionIndex = PlayerPrefs.GetInt("currentResolution");
        else
        {
            Resolution currentRes = Screen.currentResolution;
            int resIndex = 0;

            for (int i = 0; i < Screen.resolutions.Length; i++)
            {
                if (Screen.resolutions[i].width == currentRes.width && Screen.resolutions[i].height == currentRes.height)
                {
                    resIndex = i;
                    break;
                }
            }

            currentResolutionIndex = resIndex;
            PlayerPrefs.SetInt("currentResolution", currentResolutionIndex);
        }

        if (!PlayerPrefs.HasKey("postProcessing"))
            PlayerPrefs.SetInt("postProcessing", 1);

        int postBool = PlayerPrefs.GetInt("postProcessing");
        bool postState = false;

        if (postBool == 1)
            postState = true;

        postProcessing.isOn = postState;

        if (!PlayerPrefs.HasKey("fullScreen"))
            PlayerPrefs.SetInt("fullScreen", 1);

        int fullScreenBool = PlayerPrefs.GetInt("fullScreen");
        bool fullScreenState = false;

        if (fullScreenBool == 1)
            fullScreenState = true;

        fullScreen.isOn = fullScreenState;

        if (!PlayerPrefs.HasKey("sfxVolume"))
            PlayerPrefs.SetFloat("sfxVolume", defaultSFXVolume);

        sfxVolume.value = PlayerPrefs.GetFloat("sfxVolume");
        pauseSFXVolume.value = PlayerPrefs.GetFloat("sfxVolume");


        if (!PlayerPrefs.HasKey("musicVolume"))
            PlayerPrefs.SetFloat("musicVolume", defaultMusicVolume);

        musicVolume.value = PlayerPrefs.GetFloat("musicVolume");
        pauseMusicVolume.value = PlayerPrefs.GetFloat("musicVolume");


        if (!PlayerPrefs.HasKey("masterVolume"))
            PlayerPrefs.SetFloat("masterVolume", defaultMasterVolume);

        masterVolume.value = PlayerPrefs.GetFloat("masterVolume");
        pauseMasterVolume.value = PlayerPrefs.GetFloat("masterVolume");

        if (!PlayerPrefs.HasKey("jumpBar"))
            PlayerPrefs.SetInt("jumpBar", 0);

        int jumpBarBool = PlayerPrefs.GetInt("jumpBar");
        bool jumpBarState = false;

        if (jumpBarBool == 1)
            jumpBarState = true;

        disableJumpBar.isOn = jumpBarState;

        if (!PlayerPrefs.HasKey("classicMovement"))
            PlayerPrefs.SetInt("classicMovement", 0);

        int classicMovementBool = PlayerPrefs.GetInt("classicMovement");
        bool classicMovementState = false;

        if (classicMovementBool == 1)
            classicMovementState = true;

        enableClassicMovement.isOn = classicMovementState;

        if (!PlayerPrefs.HasKey("disableTut"))
            PlayerPrefs.SetInt("disableTut", 0);

        int disableTutBool = PlayerPrefs.GetInt("disableTut");
        bool disableTutState = false;

        if (disableTutBool == 1)
            disableTutState = true;

        disableTutorial.isOn = disableTutState;

        if (!PlayerPrefs.HasKey("chillMode"))
            PlayerPrefs.SetInt("chillMode", 0);

        int chillModeBool = PlayerPrefs.GetInt("chillMode");
        bool chillModeState = false;

        if (chillModeBool == 1)
            chillModeState = true;

        enableChillMode.isOn = chillModeState;

        if (!PlayerPrefs.HasKey("disableScore"))
            PlayerPrefs.SetInt("disableScore", 0);

        int disableScoreBool = PlayerPrefs.GetInt("disableScore");
        bool disableScoreState = false;

        if (disableScoreBool == 1)
            disableScoreState = true;

        disableScoreText.isOn = disableScoreState;

        if (!PlayerPrefs.HasKey("disableIcons"))
            PlayerPrefs.SetInt("disableIcons", 0);

        int disableIconsBool = PlayerPrefs.GetInt("disableIcons");
        bool disableIconsState = false;

        if (disableIconsBool == 1)
            disableIconsState = true;

        disableIcons.isOn = disableIconsState;

        if (!PlayerPrefs.HasKey("disableLoopShuffle"))
            PlayerPrefs.SetInt("disableLoopShuffle", 0);

        int disableLoopShuffleBool = PlayerPrefs.GetInt("disableLoopShuffle");
        bool disableLoopShuffleState = false;

        if (disableLoopShuffleBool == 1)
            disableLoopShuffleState = true;

        disableLoopShuffleButtons.isOn = disableLoopShuffleState;
    }

    private void Start()
    {
        if (!PlayerPrefs.HasKey("invertedMovement"))
            PlayerPrefs.SetInt("invertedMovement", 0);

        int invertedMovementBool = PlayerPrefs.GetInt("invertedMovement");
        bool invertedMovementState = false;

        if (invertedMovementBool == 1)
            invertedMovementState = true;

        enableInvertedMovement.isOn = invertedMovementState;

        SetSFXVolume();
        SetMusicVolume();
        SetMasterVolume();
        SaveManager.SetupInputs();

        SetAllButtonTexts();
        SetCDCircleState();
        SetIconsState();
        SetChillModeState();
        SetTutorialState();
        SetClassicMovementState();
        SetScoreTextState();
        FillResolutionsList();
    }

    public void CheckVolumeLevels()
    {
        //on returning to the options menu make sure the audio levels are accurate to any changes.
        if (!PlayerPrefs.HasKey("sfxVolume"))
            PlayerPrefs.SetFloat("sfxVolume", defaultSFXVolume);

        if (!PlayerPrefs.HasKey("musicVolume"))
            PlayerPrefs.SetFloat("musicVolume", defaultMusicVolume);

        if (!PlayerPrefs.HasKey("masterVolume"))
            PlayerPrefs.SetFloat("masterVolume", defaultMasterVolume);

        sfxVolume.value = PlayerPrefs.GetFloat("sfxVolume");
        pauseSFXVolume.value = PlayerPrefs.GetFloat("sfxVolume");

        musicVolume.value = PlayerPrefs.GetFloat("musicVolume");
        pauseMusicVolume.value = PlayerPrefs.GetFloat("musicVolume");

        masterVolume.value = PlayerPrefs.GetFloat("masterVolume");
        pauseMasterVolume.value = PlayerPrefs.GetFloat("masterVolume");
    }

    public void FillResolutionsList()
    {
        gridFiller.ClearGrid();
        List<string> newResolutions = new List<string>();

        for (int i = 0; i < Screen.resolutions.Length; i++)
        {
            int width = Screen.resolutions[i].width;
            int height = Screen.resolutions[i].height;
            int hertz = Screen.resolutions[i].refreshRate;

            newResolutions.Add(width + " x " + height + " & " + hertz + "Hz");
        }

        gridFiller.FillGrid(newResolutions.ToArray(), currentResolutionIndex);
    }

    public void SetGfxQualityLevel()
    {
        QualitySettings.SetQualityLevel(graphicsQuality.value);
        PlayerPrefs.SetInt("currentQuality", graphicsQuality.value);
    }

    public void SetScreenResolution(int resValue)
    {
        Screen.SetResolution(Screen.resolutions[resValue].width, Screen.resolutions[resValue].height, fullScreen.isOn);
        PlayerPrefs.SetInt("currentResolution", resValue);
        currentResolutionIndex = resValue;
    }

    public void SetPostProcessingState()
    {
        GUIManager.SetCameraPostProcessingState(postProcessing.isOn);

        int boolValue = 0;

        if (postProcessing.isOn)
            boolValue = 1;

        PlayerPrefs.SetInt("postProcessing", boolValue);
    }

    public void SetFullScreenState()
    {
        Screen.fullScreen = fullScreen.isOn;

        int boolValue = 0;

        if (fullScreen.isOn)
            boolValue = 1;

        PlayerPrefs.SetInt("fullScreen", boolValue);
    }

    public void SetSFXVolume()
    {
        float newVolume = Mathf.Log10(sfxVolume.value) * 20;
        mainMix.SetFloat("sfxVolume", newVolume);
        PlayerPrefs.SetFloat("sfxVolume", sfxVolume.value);
    }

    public void SetPauseSFXVolume()
    {
        float newVolume = Mathf.Log10(pauseSFXVolume.value) * 20;
        mainMix.SetFloat("sfxVolume", newVolume);
        PlayerPrefs.SetFloat("sfxVolume", pauseSFXVolume.value);
    }

    public void SetMusicVolume()
    {
        float newVolume = Mathf.Log10(musicVolume.value) * 20;
        mainMix.SetFloat("musicVolume", newVolume);
        PlayerPrefs.SetFloat("musicVolume", musicVolume.value);
    }

    public void SetPauseMusicVolume()
    {
        float newVolume = Mathf.Log10(pauseMusicVolume.value) * 20;
        mainMix.SetFloat("musicVolume", newVolume);
        PlayerPrefs.SetFloat("musicVolume", pauseMusicVolume.value);
    }

    public void SetMasterVolume()
    {
        float newVolume = Mathf.Log10(masterVolume.value) * 20;
        mainMix.SetFloat("masterVolume", newVolume);
        PlayerPrefs.SetFloat("masterVolume", masterVolume.value);
    }

    public void SetPauseMasterVolume()
    {
        float newVolume = Mathf.Log10(pauseMasterVolume.value) * 20;
        mainMix.SetFloat("masterVolume", newVolume);
        PlayerPrefs.SetFloat("masterVolume", pauseMasterVolume.value);
    }

    public void SetCDCircleState()
    {
        GUIManager.SetCooldownCircleState(!disableJumpBar.isOn);

        int boolValue = 0;

        if (disableJumpBar.isOn)
            boolValue = 1;

        PlayerPrefs.SetInt("jumpBar", boolValue);
    }

    public void SetIconsState()
    {
        GUIManager.SetGameplayIconsState(disableIcons.isOn);

        int boolValue = 0;

        if (disableIcons.isOn)
            boolValue = 1;

        PlayerPrefs.SetInt("disableIcons", boolValue);
    }

    public void SetClassicMovementState()
    {
        applyForce.SetClassicMovementState(enableClassicMovement.isOn);

        int boolValue = 0;

        if (enableClassicMovement.isOn)
            boolValue = 1;

        PlayerPrefs.SetInt("classicMovement", boolValue);
    }

    public void SetTutorialState()
    {
        GUIManager.SetTutorialCanvasState(!disableTutorial.isOn);
        tutControl.DisableTutorial(!disableTutorial.isOn);

        int boolValue = 0;

        if (disableTutorial.isOn)
            boolValue = 1;

        PlayerPrefs.SetInt("disableTut", boolValue);
    }

    public void SetChillModeState()
    {
        bool chillModeOn = enableChillMode.isOn;

        levelGen.SetTileObstaclesStates(!chillModeOn);
        deathWall.SetDisabledState(chillModeOn);
        scoreMgr.SetScoreSavingState(chillModeOn);
        achSys.SetChillModeActiveState(chillModeOn);
        unlockSys.SetChillModeState(chillModeOn);

        int boolValue = 0;

        if (chillModeOn)
            boolValue = 1;

        PlayerPrefs.SetInt("chillMode", boolValue);
    }

    public void SetInvertedControlsState()
    {
        applyForce.SetInvertedMovementState(enableInvertedMovement.isOn);
        applyForceController.SetInvertedMovementState(enableInvertedMovement.isOn);
        camEffects.AdjustCameraPositionForInvertedControl(enableInvertedMovement.isOn);

        int boolValue = 0;

        if (enableInvertedMovement.isOn)
            boolValue = 1;

        PlayerPrefs.SetInt("invertedMovement", boolValue);
    }

    public void SetScoreTextState()
    {
        GUIManager.SetScoreTextState(!disableScoreText.isOn);

        int boolValue = 0;

        if (disableScoreText.isOn)
            boolValue = 1;

        PlayerPrefs.SetInt("disableScore", boolValue);
    }

    public void SetLoopAndShuffleButtonsState()
    {
        GUIManager.SetLoopShuffleButtonsState(!disableLoopShuffleButtons.isOn);

        int boolValue = 0;

        if (disableLoopShuffleButtons.isOn)
            boolValue = 1;

        PlayerPrefs.SetInt("disableLoopShuffle", boolValue);
    }

    public void SetPauseMenuKey()
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();

        if (!gamepadActive)
            inputMgr.SetNewBindingForAction(0, 0, 0);
        else
            inputMgr.SetNewBindingForAction(0, 5, 0);

    }

    public void SetZoomKey()
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();

        if (!gamepadActive)
            inputMgr.SetNewBindingForAction(1, 4, 0);
        else
            inputMgr.SetNewBindingForAction(1, 9, 0);

    }

    public void SetPlayNextKey()
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();

        if (!gamepadActive)
            inputMgr.SetNewBindingForAction(2, 1, 0);
        else
            inputMgr.SetNewBindingForAction(2, 6, 0);

    }

    public void SetMusicPauseKey()
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();

        if (!gamepadActive)
            inputMgr.SetNewBindingForAction(3, 2, 0);
        else
            inputMgr.SetNewBindingForAction(3, 7, 0);

    }

    public void SetPlayLastKey()
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();

        if (!gamepadActive)
            inputMgr.SetNewBindingForAction(4, 3, 0);
        else
            inputMgr.SetNewBindingForAction(4, 8, 0);
    }

    public void SetOpenUnlocksMenuKey()
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();

        if (!gamepadActive)
            inputMgr.SetNewBindingForAction(5, 11, 0);
        else
            inputMgr.SetNewBindingForAction(5, 14, 0);
    }

    public void SetToggleShuffleKey()
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();

        if (!gamepadActive)
            inputMgr.SetNewBindingForAction(6, 12, 0);
        else
            inputMgr.SetNewBindingForAction(6, 15, 0);
    }

    public void SetToggleLoopKey()
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();

        if (!gamepadActive)
            inputMgr.SetNewBindingForAction(7, 13, 0);
        else
            inputMgr.SetNewBindingForAction(7, 16, 0);
    }

    public void SetLaunchPlayerKey()
    {
        inputMgr.SetNewBindingForAction(8, 10, 0);
    }

    public void SetButtonText(int buttonID)
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();

        switch (buttonID)
        {
            default:
                break;

            case 0:
                pauseMenuText.text = inputMgr.GetBindingString(0, gamepadActive);
                break;

            case 1:
                zoomText.text = inputMgr.GetBindingString(1, gamepadActive);
                break;

            case 2:
                playText.text = inputMgr.GetBindingString(2, gamepadActive);
                break;

            case 3:
                pauseMusicText.text = inputMgr.GetBindingString(3, gamepadActive);
                break;

            case 4:
                playLastText.text = inputMgr.GetBindingString(4, gamepadActive);
                break;

            case 5:
                openUnlocksText.text = inputMgr.GetBindingString(5, gamepadActive);
                break;

            case 6:
                toggleShuffleText.text = inputMgr.GetBindingString(6, gamepadActive);
                break;

            case 7:
                toggleLoopText.text = inputMgr.GetBindingString(7, gamepadActive);
                break;

            case 8:
                if (gamepadActive == true)
                    launchPlayerText.text = inputMgr.GetBindingString(8, true);
                break;
        }
    }

    public void SetAllButtonTexts()
    {
        bool gamepadActive = inputMgr.GetGamepadActiveState();


        pauseMenuText.text = inputMgr.GetBindingString(0, gamepadActive);

        zoomText.text = inputMgr.GetBindingString(1, gamepadActive);

        playText.text = inputMgr.GetBindingString(2, gamepadActive);

        pauseMusicText.text = inputMgr.GetBindingString(3, gamepadActive);

        playLastText.text = inputMgr.GetBindingString(4, gamepadActive);

        openUnlocksText.text = inputMgr.GetBindingString(5, gamepadActive);

        toggleShuffleText.text = inputMgr.GetBindingString(6, gamepadActive);

        toggleLoopText.text = inputMgr.GetBindingString(7, gamepadActive);

        if (gamepadActive == true)
        {
            launchPlayer.interactable = true;
            launchPlayerText.text = inputMgr.GetBindingString(8, true);
        }
        else
        {
            launchPlayer.interactable = false;
            launchPlayerText.text = "N/A";
        }

    }

    public void RevertVideoAndAudioToDefault()
    {
        //call save manager to delete keys for video/audio settings
        SaveManager.DeleteVideoAndAudioData();

        //reset interactables to their default values
        graphicsQuality.value = 2;

        Resolution currentRes = Screen.currentResolution;
        int resIndex = 0;

        for (int i = 0; i < Screen.resolutions.Length; i++)
        {
            if (Screen.resolutions[i].width == currentRes.width && Screen.resolutions[i].height == currentRes.height)
            {
                resIndex = i;
                break;
            }
        }

        currentResolutionIndex = resIndex;

        postProcessing.isOn = true;
        fullScreen.isOn = true;
        sfxVolume.value = defaultSFXVolume;
        pauseSFXVolume.value = defaultSFXVolume;
        musicVolume.value = defaultMusicVolume;
        pauseMusicVolume.value = defaultMusicVolume;
        masterVolume.value = defaultMasterVolume;
        pauseMasterVolume.value = defaultMasterVolume;
    }

    public void RevertControlsToDefault()
    {
        SaveManager.DeleteControlsData();
        SaveManager.SetupInputs();
        SetAllButtonTexts();
    }

    public void RevertGameSettingsToDefault()
    {
        SaveManager.DeleteGameSettingsData();
        disableJumpBar.isOn = false;
        enableClassicMovement.isOn = false;
        disableTutorial.isOn = false;
        enableChillMode.isOn = false;
        disableScoreText.isOn = false;
        enableInvertedMovement.isOn = false;
        disableLoopShuffleButtons.isOn = false;
        disableIcons.isOn = false;
    }

    public void RevertSettingsOnContext()
    {
        if (videoAudioPanel.activeSelf)
            RevertVideoAndAudioToDefault();
        else if (controlsPanel.activeSelf)
            RevertControlsToDefault();
        else if (gameSettingsPanel.activeSelf)
            RevertGameSettingsToDefault();
    }
}