Reputation: 1
I am a game programming student early on I learned about the difference between pausing games with Time.timeScale and a singleton state machine. I really liked learning how to use a singleton and implementing them in my projects. The only issue with it is that if I want to pause the gravity, animation and stuff like that (essentially anything other than stopping the movement itself )I have to implement them individually.
What I want to do is instead update my state machine to check if the object contains anything that would need an additional check. As right now calling pause can potentially be quite bloated. I will post the state machine as well as a chunk of bloated code. Stuff like the gameover enum and cases are commented out since they were not needed for my most recent project.
#region GameStateHandler
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
/*
This is a gamestate handler that uses the singleton design pattern. Its primary function for this project is for pausing and resuming from pause.
I chose to use a state machine for handling the pause functionality in our game due to the fact that it enables a larger amount of
control than in comparison to something like TimeScale = 0.
In order for something to be paused a reference to this script would be called like so:
if (GameStateHandler.Instance != null && GameStateHandler.Instance.CurrentState == GameState.Paused)
{
return;
}
This will stop the object once was pause has been activated.
*/
public enum GameState
{
Playing,
Paused,
//GameOver
}
public class GameStateHandler : MonoBehaviour
{
public static GameStateHandler Instance { get; private set; }
public GameState CurrentState { get; private set; } = GameState.Playing;
public Animator[] animators;
public Health health;
private PlayerMovement playerMovement;
public static bool Paused;
public bool isGameOver = false;
private UIManager uiManager;
//While Asynchronistic loading isn't exactly necessary for handling pause/playing state I have included it to ensure smooth loading between the menu and levels.
public void SceneChange(string name)
{
StartCoroutine(LoadSceneAsync(name));
}
private IEnumerator LoadSceneAsync(string sceneName)
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
while (!asyncLoad.isDone)
{
yield return null;
}
}
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
uiManager = GetComponent<UIManager>();
if (uiManager == null)
{
Debug.Log("UIManager not found in scene, please attach.");
}
}
void Start()
{
if (uiManager != null)
{
uiManager.OnResumePress();
}
}
public void ChangeState(GameState newState) //Handlles the transition between states.
{
if (CurrentState == newState) return;
CurrentState = newState;
switch (newState)
{
case GameState.Paused:
PauseGame();
break;
case GameState.Playing:
ResumeGame();
break;
//case GameState.GameOver:
// GameOver();
// break;
}
}
public void TogglePause()
{
if (CurrentState == GameState.Playing)
{
ChangeState(GameState.Paused);
}
else if (CurrentState == GameState.Paused)
{
ChangeState(GameState.Playing);
}
}
public void PauseGame()
{
Debug.Log("Game Paused");
//stop animations
foreach (Animator animator in animators)
{
animator.enabled = false; //pauses all animations
}
}
public void ResumeGame()
{
if (uiManager != null)
{
uiManager.OnResumePress();
}
Debug.Log("Game Resumed");
ChangeState(GameState.Playing);
}
public void GoToMainMenu()
{
if (GameStateHandler.Instance.CurrentState == GameState.Paused)
{
return;
}
SceneManager.LoadScene(0); //Assuming menu = scene 0
}
public void QuitGame()
{
Application.Quit();
Debug.Log("See ya later!");
}
//The Game Over state ended up not being used in this project.
//public void GameOver()
//{
// if (CurrentState == GameState.Paused)
// {
// ResumeGame();
// }
// ChangeState(GameState.GameOver);
// isGameOver = true;
// Debug.Log("Game Over");
// if (playerMovement != null)
// {
// playerMovement.enabled = false;
// }
// Paused = false;
//}
}
#endregion
void Update()
{
if (GameStateHandler.Instance != null && GameStateHandler.Instance.CurrentState == GameState.Paused)
{
if (ani != null && ani.speed != 0)
{
previousAnimatorSpeed = ani.speed;
ani.speed = 0;
}
if (rb.useGravity)
{
isGravityEnabled = true;
rb.useGravity = false;
previousLinearVelocity = rb.linearVelocity; //store previous velocity otherwise you can spam pause and keep going forward
rb.linearVelocity = Vector3.zero; // Stop all movement
}
transform.position = previousPosition;
return;
}
else
{
// Restore gravity when resumed
if (!isGravityEnabled && rb.useGravity == false)
{
rb.useGravity = true;
}
// Restore animation speed when resumed
if (ani != null && ani.speed == 0 && previousAnimatorSpeed != 0)
{
ani.speed = previousAnimatorSpeed;
}
//restore velocity when resumed otherwise you can spam pause and keep going forward
if (rb.linearVelocity == Vector3.zero)
{
rb.linearVelocity = previousLinearVelocity;
}
}
{
rb.useGravity = isGravityEnabled;
if (ani.speed == 0 && previousAnimatorSpeed != 0)
{
ani.speed = previousAnimatorSpeed;
}
}
previousPosition = transform.position; ```
I haven't tried anything as of now however I am thinking that if the game state handler included all the relevant variables (animation, gravity etc) then it could check if the script its being called has them and in that case it would pause all relevant fields.
Upvotes: 0
Views: 26