quality-control/Assets/Scripts/UI/UIManager.cs

307 lines
7.2 KiB
C#
Raw Normal View History

2024-08-20 01:30:10 +00:00
using System;
2024-08-19 23:43:15 +00:00
using System.Collections.Generic;
2024-08-20 01:30:10 +00:00
using TMPro;
2024-08-19 23:43:15 +00:00
using UnityEngine;
public class UIManager: MonoBehaviour
{
2024-08-20 01:30:10 +00:00
public static UIManager Instance;
public void OnEnable()
{
if (Instance == null)
{
Instance = this;
}
}
2024-08-19 23:43:15 +00:00
public enum UIState
{
MainMenu = 0,
Game = 1,
Pause = 2,
HowToPlay = 3,
StageSelect = 4,
ConfirmQuit = 5,
2024-08-20 01:30:10 +00:00
StageComplete = 6,
2024-08-19 23:43:15 +00:00
}
public List<UIState> StateStack = new List<UIState>() {UIState.MainMenu};
public GameObject MainMenu;
public GameObject Reticle;
public GameObject HowToPlay;
public GameObject StageSelect;
2024-08-20 01:30:10 +00:00
public GameObject Pause;
2024-08-19 23:43:15 +00:00
public GameObject ConfirmQuit;
2024-08-20 01:30:10 +00:00
public GameObject StageComplete;
public GameObject RetryButton;
public GameObject NextStageButton;
public TextMeshProUGUI StageCompleteText;
2024-08-19 23:43:15 +00:00
public void Start()
{
MainMenu.SetActive(false);
Reticle.SetActive(false);
HowToPlay.SetActive(false);
StageSelect.SetActive(false);
ConfirmQuit.SetActive(false);
2024-08-20 01:30:10 +00:00
Pause.SetActive(false);
StageComplete.SetActive(false);
2024-08-19 23:43:15 +00:00
PushState(UIState.MainMenu);
}
public void PushState(UIState state)
{
if (StateStack.Count > 0)
{
2024-08-20 01:30:10 +00:00
if (StateStack[0] == state)
{
return;
}
2024-08-19 23:43:15 +00:00
OnExitState(StateStack[0]);
}
2024-08-20 01:30:10 +00:00
PopState();
2024-08-19 23:43:15 +00:00
StateStack.Add(state);
OnEnterState(state);
}
2024-08-20 01:30:10 +00:00
public void PushMainMenu()
{
PushState(UIState.MainMenu);
}
public void PushHowToPlay()
{
PushState(UIState.HowToPlay);
}
public void PushStageSelect()
{
PushState(UIState.StageSelect);
}
public void PushConfirmQuit()
{
PushState(UIState.ConfirmQuit);
}
public void Resume()
{
TryTogglePause();
}
public void TryTogglePause()
{
switch (StateStack[0])
{
case UIState.Game:
PushState(UIState.Pause);
break;
case UIState.Pause:
PushState(UIState.Game);
break;
case UIState.MainMenu:
PushState(UIState.ConfirmQuit);
break;
case UIState.ConfirmQuit:
PushState(UIState.MainMenu);
break;
case UIState.StageSelect:
PushState(UIState.MainMenu);
break;
case UIState.HowToPlay:
TryBackFromHelp();
break;
}
}
private bool gameRunning;
public void TryToggleHelp()
{
if (StateStack[0] == UIState.Game)
{
PushState(UIState.HowToPlay);
}
else if (StateStack[0] == UIState.HowToPlay)
{
if (!gameRunning)
{
return;
}
PushState(UIState.Game);
}
}
public void TryBackFromHelp()
{
if (gameRunning)
{
PushState(UIState.Game);
}
else
{
PushState(UIState.MainMenu);
}
}
2024-08-19 23:43:15 +00:00
public void PopState()
{
2024-08-20 01:30:10 +00:00
bool backToMain = false;
2024-08-19 23:43:15 +00:00
if (StateStack.Count > 0)
2024-08-20 01:30:10 +00:00
{
2024-08-19 23:43:15 +00:00
OnExitState(StateStack[0]);
StateStack.RemoveAt(0);
}
}
private void OnEnterState(UIState state)
{
switch (state)
{
case UIState.MainMenu:
MainMenu.SetActive(true);
break;
case UIState.Game:
2024-08-20 01:30:10 +00:00
AudioListener.pause = false;
2024-08-20 09:39:32 +00:00
#if UNITY_EDITOR
2024-08-20 01:30:10 +00:00
Time.timeScale = 1f;
2024-08-20 09:39:32 +00:00
#else
Time.timeScale = 1f;
#endif
2024-08-20 01:30:10 +00:00
Cursor.lockState = CursorLockMode.Locked;
2024-08-19 23:43:15 +00:00
Reticle.SetActive(true);
break;
2024-08-20 01:30:10 +00:00
case UIState.ConfirmQuit:
ConfirmQuit.SetActive(true);
break;
case UIState.StageSelect:
StageSelect.SetActive(true);
break;
case UIState.HowToPlay:
Cursor.lockState = CursorLockMode.None;
HowToPlay.SetActive(true);
break;
case UIState.Pause:
Cursor.lockState = CursorLockMode.None;
Pause.SetActive(true);
break;
case UIState.StageComplete:
Cursor.lockState = CursorLockMode.None;
StageComplete.SetActive(true);
break;
2024-08-19 23:43:15 +00:00
}
}
private void OnExitState(UIState state)
{
switch (state)
{
case UIState.MainMenu:
MainMenu.SetActive(false);
break;
case UIState.Game:
Reticle.SetActive(false);
2024-08-20 01:30:10 +00:00
AudioListener.pause = true;
Time.timeScale = 0f;
break;
case UIState.ConfirmQuit:
ConfirmQuit.SetActive(false);
break;
case UIState.StageSelect:
StageSelect.SetActive(false);
break;
case UIState.HowToPlay:
HowToPlay.SetActive(false);
break;
case UIState.Pause:
Pause.SetActive(false);
2024-08-19 23:43:15 +00:00
break;
2024-08-20 08:50:07 +00:00
case UIState.StageComplete:
StageComplete.SetActive(false);
break;
2024-08-19 23:43:15 +00:00
}
}
public void StartStage(int stage)
{
2024-08-20 01:30:10 +00:00
gameRunning = true;
Time.timeScale = 1f;
2024-08-19 23:43:15 +00:00
Scoreboard.Instance.StartStage((StagingManager.StageEnum)stage);
PushState(UIState.Game);
}
2024-08-20 01:30:10 +00:00
public void RestartStage()
{
Time.timeScale = 1f;
Scoreboard.RestartCurrentStage();
PushState(UIState.Game);
}
2024-08-19 23:43:15 +00:00
public void Quit()
{
Application.Quit();
}
2024-08-20 01:30:10 +00:00
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
TryTogglePause();
return;
}
if (Input.GetKeyDown(KeyCode.F1))
{
TryToggleHelp();
return;
}
}
public void PushStageComplete(bool success, string message)
{
2024-08-20 09:45:02 +00:00
if (Scoreboard.Instance.CurrentStage == StagingManager.StageEnum.Level3)
2024-08-20 01:30:10 +00:00
{
NextStageButton.SetActive(false);
RetryButton.SetActive(false);
}
2024-08-20 09:45:02 +00:00
else if (success)
2024-08-20 01:30:10 +00:00
{
NextStageButton.SetActive(true);
RetryButton.SetActive(false);
}
else
{
NextStageButton.SetActive(false);
RetryButton.SetActive(true);
}
StageCompleteText.text = message;
PushState(UIState.StageComplete);
}
public void NextStage()
{
switch (Scoreboard.Instance.CurrentStage)
{
case StagingManager.StageEnum.Level1:
2024-08-20 09:39:32 +00:00
StartStage(2);
2024-08-20 01:30:10 +00:00
break;
case StagingManager.StageEnum.Level2:
2024-08-20 09:39:32 +00:00
StartStage(4);
2024-08-20 01:30:10 +00:00
break;
case StagingManager.StageEnum.Level3:
break;
}
}
2024-08-19 23:43:15 +00:00
}