using NaughtyAttributes; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; //Could be a singleton public class GameFlowManager : MonoBehaviour { [SerializeField] [Required] GameTimer gameTimer = null!; [SerializeField] [Required] MainMenuManager mainMenu = null!; [SerializeField] [Required] Canvas UICanvas = null!; public enum PauseLevel { NotPaused, PreventActions, NothingMoves, TimeStop, } [field: SerializeField] public PauseLevel pauseLevel { get; private set; } = PauseLevel.PreventActions; public bool CanDoAction => pauseLevel <= PauseLevel.NotPaused; public BaseState CurrentState { get; private set; } = null!; bool lastStartButton; bool lastAcceptButton; [SerializeField] GameObject startPrompt; #region Unity Messages void Awake() { CurrentState = new StartMenuFlowState(this); } void Start() => CurrentState.EnterState(); #endregion void SetPauseLevel(PauseLevel value) { pauseLevel = value; Time.timeScale = value >= PauseLevel.TimeStop ? 0f : 1f; } public void GameOver() => SwitchState(new DeadFlowState(this)); /// /// For calling from main menu button /// public void ToStartFlowState() => SwitchState(new StartFlowState(this)); #region Inputs public void OnStart(InputAction.CallbackContext ctx) { if (!ctx.WasPressedThisFrame(ref lastStartButton)) return; switch (CurrentState) { case StartFlowState _: SwitchState(new GameplayFlowState(this)); break; case GameplayFlowState _: SwitchState(new PauseMenuFlowState(this)); break; case PauseMenuFlowState _: SwitchState(new GameplayFlowState(this)); break; } } public void OnAccept(InputAction.CallbackContext ctx) { if (!ctx.WasPressedThisFrame(ref lastAcceptButton)) return; if (CurrentState is DeadFlowState deadState) deadState.ReloadGame(); } #endregion #region States void SwitchState(BaseState newState) { CurrentState.LeaveState(); CurrentState = newState; newState.EnterState(); } abstract class GameFlowState : BaseState { readonly protected GameFlowManager gameFlowManager; protected GameFlowState(GameFlowManager gameFlowManager) { this.gameFlowManager = gameFlowManager; } } class StartMenuFlowState : GameFlowState { public StartMenuFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { base.EnterState(); gameFlowManager.mainMenu.gameObject.SetActive(true); gameFlowManager.mainMenu.SetMenuMode(MainMenuManager.MenuMode.MainMenu); gameFlowManager.UICanvas.gameObject.SetActive(false); } public override void LeaveState() { base.LeaveState(); gameFlowManager.mainMenu.ResetMenuState(); gameFlowManager.mainMenu.gameObject.SetActive(false); gameFlowManager.UICanvas.gameObject.SetActive(true); } } class StartFlowState : GameFlowState { public StartFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { base.EnterState(); gameFlowManager.startPrompt.SetActive(true); gameFlowManager.SetPauseLevel(PauseLevel.PreventActions); } public override void LeaveState() => gameFlowManager.startPrompt.SetActive(false); } class GameplayFlowState : GameFlowState { public GameplayFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { base.EnterState(); gameFlowManager.gameTimer.stopped = false; gameFlowManager.SetPauseLevel(PauseLevel.NotPaused); } public override void LeaveState() { base.LeaveState(); gameFlowManager.gameTimer.stopped = true; } } class PauseMenuFlowState : GameFlowState { public PauseMenuFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { base.EnterState(); gameFlowManager.mainMenu.gameObject.SetActive(true); gameFlowManager.mainMenu.SetMenuMode(MainMenuManager.MenuMode.PauseMenu); gameFlowManager.SetPauseLevel(PauseLevel.TimeStop); } public override void LeaveState() { base.EnterState(); gameFlowManager.mainMenu.ResetMenuState(); gameFlowManager.mainMenu.gameObject.SetActive(false); gameFlowManager.SetPauseLevel(PauseLevel.NotPaused); } } class DeadFlowState : GameFlowState { public DeadFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { base.EnterState(); Debug.Log("You died!\nPress Accept to restart!"); gameFlowManager.SetPauseLevel(PauseLevel.PreventActions); } public void ReloadGame() { Debug.Log("Reloading scene..."); SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } } #endregion }