using UnityEngine; using UnityEngine.InputSystem; //Could be a singleton public class GameFlowManager : MonoBehaviour { /// /// True if time is frozen (Start, Pause Menu, Dead) /// /// Could be renamed appropriately [field: SerializeField] public bool Paused { get; private set; } = true; public BaseState CurrentState { get; private set; } = null!; bool lastStartButton; #region Unity Messages void Awake() => CurrentState = new StartFlowState(this); void Start() => CurrentState.EnterState(); #endregion void SetPause(bool value) { Paused = value; Time.timeScale = value ? 0f : 1f; } #region Inputs public void OnStart(InputAction.CallbackContext ctx) { //feels pretty redundant ^^' bool newValue = ctx.ReadValueAsButton(); bool wasJustPressed = !lastStartButton && newValue; lastStartButton = newValue; if (!wasJustPressed) return; if (CurrentState is StartFlowState) SwitchState(new GameplayFlowState(this)); } #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 StartFlowState : GameFlowState { public StartFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { base.EnterState(); Debug.Log("Press Start to start...!"); gameFlowManager.SetPause(true); } public override void LeaveState() { Debug.Log("Let the games begin!!"); } } class GameplayFlowState : GameFlowState { public GameplayFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { base.EnterState(); gameFlowManager.SetPause(false); } } class PauseMenuFlowState : GameFlowState { public PauseMenuFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { base.EnterState(); gameFlowManager.SetPause(true); } } class DeadFlowState : GameFlowState { public DeadFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { base.EnterState(); gameFlowManager.SetPause(true); } } #endregion }