126 lines
3.4 KiB
C#
126 lines
3.4 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,
|
|
HowTo,
|
|
Controls
|
|
}
|
|
|
|
[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]
|
|
Transform helpParent;
|
|
[SerializeField] [Required]
|
|
Transform howToParent;
|
|
[SerializeField] [Required]
|
|
Transform controlsParent;
|
|
|
|
[SerializeField] [Required]
|
|
GameTimer gameTimer;
|
|
|
|
[SerializeField] [Required]
|
|
EventSystem eventSystem;
|
|
|
|
[SerializeField] [Required]
|
|
TMP_Dropdown displayModeDropdown;
|
|
|
|
MenuMode mode;
|
|
MenuMode? lastMode;
|
|
|
|
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);
|
|
helpParent.gameObject.SetActive(mode == MenuMode.HowTo || mode == MenuMode.Controls);
|
|
howToParent.gameObject.SetActive(mode == MenuMode.HowTo);
|
|
controlsParent.gameObject.SetActive(mode == MenuMode.Controls);
|
|
if(this.mode == MenuMode.MainMenu || this.mode == MenuMode.PauseMenu)
|
|
lastMode = this.mode;
|
|
|
|
this.mode = mode;
|
|
ResetMenuState();
|
|
}
|
|
|
|
public void SetOptions() => SetMenuMode(MenuMode.Options);
|
|
|
|
public void BackToLastMode() {
|
|
if (lastMode != null)
|
|
SetMenuMode(lastMode.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,
|
|
MenuMode.HowTo => helpParent.GetChild(0).gameObject,
|
|
MenuMode.Controls => helpParent.GetChild(1).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 SetHowTo() => SetMenuMode(MenuMode.HowTo);
|
|
public void SetControls() => SetMenuMode(MenuMode.Controls);
|
|
|
|
public void DisplayModeChange() {
|
|
Screen.fullScreenMode = displayModeDropdown.value switch {
|
|
0 => FullScreenMode.Windowed,
|
|
1 => FullScreenMode.ExclusiveFullScreen,
|
|
2 => FullScreenMode.FullScreenWindow,
|
|
_ => Screen.fullScreenMode
|
|
};
|
|
}
|
|
} |