Lorenzo501

GameManager of my Unity game architecture

Feb 10th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.54 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityStandardAssets.Characters.FirstPerson;
  4.  
  5. namespace UnityGame.Architecture.Managers
  6. {
  7.     public class GameManager : MonoBehaviour
  8.     {
  9.         #region Fields
  10.         private static Manager[] managers;
  11.  
  12.         private static GameManager instance;
  13.  
  14.         private static bool isInGame;
  15.         private static bool isPaused;
  16.  
  17.         [SerializeField]
  18.         private FirstPersonController firstPersonController;
  19.         [SerializeField]
  20.         private GameObject shooterGameObject;
  21.         [SerializeField]
  22.         private GameObject hudGameObject;
  23.         #endregion
  24.  
  25.         #region Properties
  26.         public static GameManager Instance { get { return instance; } }
  27.  
  28.         public static bool IsInGame { get { return isInGame; } }
  29.         public static bool IsPaused { get { return isPaused; } }
  30.         #endregion
  31.  
  32.         // Use this for direct initialization
  33.         private void Awake()
  34.         {
  35.             instance = this;
  36.  
  37.             managers = new Manager[]
  38.             {
  39.                 new ActorManager(),
  40.                 new LevelManager(),
  41.                 new GUIManager()
  42.             };
  43.  
  44.             // Wake all the managers
  45.             foreach (Manager manager in managers)
  46.                 manager.Awake();
  47.         }
  48.  
  49.         // Use this for late initialization
  50.         private void Start()
  51.         {
  52.             // Start all the managers
  53.             foreach (Manager manager in managers)
  54.                 manager.Start();
  55.         }
  56.  
  57.         // Update is called once per frame
  58.         private void Update()
  59.         {
  60.             // Update all the managers
  61.             foreach (Manager manager in managers)
  62.                 manager.Update();
  63.  
  64.             if (isInGame && !isPaused && Input.GetKeyDown(KeyCode.Escape))
  65.                 PauseGameplay();
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Gets the manager of the specified type.
  70.         /// </summary>
  71.         /// <typeparam name="T">type</typeparam>
  72.         /// <returns>The manager in question</returns>
  73.         public static T GetManager<T>() where T : Manager
  74.         {
  75.             foreach (Manager manager in managers)
  76.                 if (manager.GetType() == typeof(T))
  77.                     return manager as T;
  78.  
  79.             Debug.LogError(string.Format("Manager {0} doesn't exist", typeof(T)));
  80.  
  81.             return default(T);
  82.         }
  83.  
  84.         public void StartGameplay(string difficultyChoice)
  85.         {
  86.             if (!Enum.IsDefined(typeof(DifficultyType), difficultyChoice))
  87.             {
  88.                 Debug.LogErrorFormat("Parse: Can't convert {0} to DifficultyType enum, check the spelling.", difficultyChoice);
  89.                 return;
  90.             }
  91.  
  92.             GetManager<GUIManager>().HideActiveMenu();
  93.             TogglePlayerControl();
  94.             GetManager<LevelManager>().StartLevel(1, (DifficultyType)Enum.Parse(typeof(DifficultyType), difficultyChoice));
  95.             isInGame = true;
  96.         }
  97.  
  98.         public void StopGameplay(bool isGameWon)
  99.         {
  100.             GetManager<GUIManager>().ShowMenu(isGameWon ? MenuType.Highscore : MenuType.GameOver);
  101.         }
  102.  
  103.         public void RestartGameplay()
  104.         {
  105.             StartGameplay(LevelManager.CurrentDifficulty.ToString());
  106.         }
  107.  
  108.         public void PauseGameplay()
  109.         {
  110.             isPaused = true;
  111.  
  112.             Time.timeScale = 0.0f;
  113.             TogglePlayerControl();
  114.             GetManager<GUIManager>().ShowMenu(MenuType.Pause);
  115.  
  116.             foreach (Manager manager in managers)
  117.                 manager.Pause();
  118.         }
  119.  
  120.         public void ResumeGameplay()
  121.         {
  122.             isPaused = false;
  123.  
  124.             Time.timeScale = 1.0f;
  125.             TogglePlayerControl();
  126.             GetManager<GUIManager>().HideActiveMenu();
  127.  
  128.             foreach (Manager manager in managers)
  129.                 manager.Resume();
  130.         }
  131.  
  132.         public void QuitGame()
  133.         {
  134.             Application.Quit();
  135.         }
  136.  
  137.         private void TogglePlayerControl()
  138.         {
  139.             ResetCursorPosition();
  140.             Cursor.visible = !Cursor.visible;
  141.             hudGameObject.SetActive(!hudGameObject.activeSelf);
  142.             firstPersonController.enabled = !firstPersonController.enabled;
  143.             shooterGameObject.SetActive(!shooterGameObject.activeSelf);
  144.         }
  145.  
  146.         private void ResetCursorPosition()
  147.         {
  148.             // Both resets and locks the cursor position
  149.             Cursor.lockState = CursorLockMode.Locked;
  150.  
  151.             // Unlocks the cursor position
  152.             Cursor.lockState = CursorLockMode.Confined;
  153.         }
  154.  
  155.         public void ShowMenu(string menuChoice)
  156.         {
  157.             if (!Enum.IsDefined(typeof(MenuType), menuChoice))
  158.             {
  159.                 Debug.LogErrorFormat("Parse: Can't convert {0} to MenuType enum, check the spelling.", menuChoice);
  160.                 return;
  161.             }
  162.  
  163.             GetManager<GUIManager>().ShowMenu((MenuType)Enum.Parse(typeof(MenuType), menuChoice));
  164.         }
  165.  
  166.         public void RefreshHighscore(string difficultyChoice)
  167.         {
  168.             if (!Enum.IsDefined(typeof(DifficultyType), difficultyChoice))
  169.             {
  170.                 Debug.LogErrorFormat("Parse: Can't convert {0} to DifficultyType enum, check the spelling.", difficultyChoice);
  171.                 return;
  172.             }
  173.  
  174.             // change shown score data to the chosen difficulty
  175.             GetManager<GUIManager>().RefreshHighscore((DifficultyType)Enum.Parse(typeof(DifficultyType), difficultyChoice));
  176.         }
  177.     }
  178. }
Add Comment
Please, Sign In to add comment