ludumdare50/Assets/Scripts/MainMenuManager.cs
2022-04-03 22:08:06 -04:00

114 lines
2.9 KiB
C#

using System;
using NaughtyAttributes;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Random = UnityEngine.Random;
public class MainMenuManager : MonoBehaviour {
public enum MenuMode {
MainMenu,
PauseMenu,
Options,
}
[SerializeField] [Required]
GameFlowManager gameFlowManager;
[SerializeField] [Required]
TMP_Text titleLabel;
[SerializeField] string[] titleOptions;
[SerializeField] [Required]
Transform startMenuParent;
[SerializeField] [Required]
Transform pauseParent;
[SerializeField] [Required]
Transform optionsParent;
[SerializeField] [Required]
GameTimer gameTimer;
[SerializeField] [Required]
EventSystem eventSystem;
[SerializeField] [Required]
TMP_Dropdown displayModeDropdown;
MenuMode mode;
MenuMode? preOptionsMode;
void Awake() => SetMenuMode(MenuMode.MainMenu);
void Start() {
if (titleOptions.Length == 0) {
Debug.LogWarning("No titles listed.");
return;
}
if (PlayerPrefs.HasKey(GameTimer.HIGH_SCORE_KEY) && PlayerPrefs.GetFloat(GameTimer.HIGH_SCORE_KEY) > 0f)
titleLabel.text = titleOptions[Random.Range(0, titleOptions.Length)];
else
titleLabel.text = titleOptions[0];
}
public void SetMenuMode(MenuMode mode) {
//meh
titleLabel.gameObject.SetActive(mode == MenuMode.MainMenu);
startMenuParent.gameObject.SetActive(mode == MenuMode.MainMenu);
pauseParent.gameObject.SetActive(mode == MenuMode.PauseMenu);
optionsParent.gameObject.SetActive(mode == MenuMode.Options);
if (mode == MenuMode.Options) {
if (this.mode != MenuMode.Options)
preOptionsMode = this.mode;
} else {
preOptionsMode = null;
}
this.mode = mode;
ResetMenuState();
}
public void SetOptions() => SetMenuMode(MenuMode.Options);
public void ExitOptions() {
if (preOptionsMode != null)
SetMenuMode(preOptionsMode.Value);
}
public void ResetMenuState() {
GameObject selected = mode switch {
MenuMode.MainMenu => startMenuParent.GetChild(0).gameObject,
MenuMode.PauseMenu => pauseParent.GetChild(0).gameObject,
MenuMode.Options => optionsParent.GetChild(0).gameObject,
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null)
};
eventSystem.firstSelectedGameObject = selected;
eventSystem.SetSelectedGameObject(selected);
}
public void GoToScene(int sceneNb) => SceneManager.LoadScene(sceneNb);
public void QuitGame() => Application.Quit(0);
public void ResetHighScore() => gameTimer.ResetHighScore();
public void ToStartFlowState() => gameFlowManager.ToStartFlowState();
public void ToGameplayFlowState() => gameFlowManager.ToGameplayFlowState();
public void DisplayModeChange() {
Screen.fullScreenMode = displayModeDropdown.value switch {
0 => FullScreenMode.Windowed,
1 => FullScreenMode.ExclusiveFullScreen,
2 => FullScreenMode.FullScreenWindow,
_ => Screen.fullScreenMode
};
}
}