Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityStandardAssets.Characters.FirstPerson;
- namespace UnityGame.Architecture.Managers
- {
- public class GameManager : MonoBehaviour
- {
- #region Fields
- private static Manager[] managers;
- private static GameManager instance;
- private static bool isInGame;
- private static bool isPaused;
- [SerializeField]
- private FirstPersonController firstPersonController;
- [SerializeField]
- private GameObject shooterGameObject;
- [SerializeField]
- private GameObject hudGameObject;
- #endregion
- #region Properties
- public static GameManager Instance { get { return instance; } }
- public static bool IsInGame { get { return isInGame; } }
- public static bool IsPaused { get { return isPaused; } }
- #endregion
- // Use this for direct initialization
- private void Awake()
- {
- instance = this;
- managers = new Manager[]
- {
- new ActorManager(),
- new LevelManager(),
- new GUIManager()
- };
- // Wake all the managers
- foreach (Manager manager in managers)
- manager.Awake();
- }
- // Use this for late initialization
- private void Start()
- {
- // Start all the managers
- foreach (Manager manager in managers)
- manager.Start();
- }
- // Update is called once per frame
- private void Update()
- {
- // Update all the managers
- foreach (Manager manager in managers)
- manager.Update();
- if (isInGame && !isPaused && Input.GetKeyDown(KeyCode.Escape))
- PauseGameplay();
- }
- /// <summary>
- /// Gets the manager of the specified type.
- /// </summary>
- /// <typeparam name="T">type</typeparam>
- /// <returns>The manager in question</returns>
- public static T GetManager<T>() where T : Manager
- {
- foreach (Manager manager in managers)
- if (manager.GetType() == typeof(T))
- return manager as T;
- Debug.LogError(string.Format("Manager {0} doesn't exist", typeof(T)));
- return default(T);
- }
- public void StartGameplay(string difficultyChoice)
- {
- if (!Enum.IsDefined(typeof(DifficultyType), difficultyChoice))
- {
- Debug.LogErrorFormat("Parse: Can't convert {0} to DifficultyType enum, check the spelling.", difficultyChoice);
- return;
- }
- GetManager<GUIManager>().HideActiveMenu();
- TogglePlayerControl();
- GetManager<LevelManager>().StartLevel(1, (DifficultyType)Enum.Parse(typeof(DifficultyType), difficultyChoice));
- isInGame = true;
- }
- public void StopGameplay(bool isGameWon)
- {
- GetManager<GUIManager>().ShowMenu(isGameWon ? MenuType.Highscore : MenuType.GameOver);
- }
- public void RestartGameplay()
- {
- StartGameplay(LevelManager.CurrentDifficulty.ToString());
- }
- public void PauseGameplay()
- {
- isPaused = true;
- Time.timeScale = 0.0f;
- TogglePlayerControl();
- GetManager<GUIManager>().ShowMenu(MenuType.Pause);
- foreach (Manager manager in managers)
- manager.Pause();
- }
- public void ResumeGameplay()
- {
- isPaused = false;
- Time.timeScale = 1.0f;
- TogglePlayerControl();
- GetManager<GUIManager>().HideActiveMenu();
- foreach (Manager manager in managers)
- manager.Resume();
- }
- public void QuitGame()
- {
- Application.Quit();
- }
- private void TogglePlayerControl()
- {
- ResetCursorPosition();
- Cursor.visible = !Cursor.visible;
- hudGameObject.SetActive(!hudGameObject.activeSelf);
- firstPersonController.enabled = !firstPersonController.enabled;
- shooterGameObject.SetActive(!shooterGameObject.activeSelf);
- }
- private void ResetCursorPosition()
- {
- // Both resets and locks the cursor position
- Cursor.lockState = CursorLockMode.Locked;
- // Unlocks the cursor position
- Cursor.lockState = CursorLockMode.Confined;
- }
- public void ShowMenu(string menuChoice)
- {
- if (!Enum.IsDefined(typeof(MenuType), menuChoice))
- {
- Debug.LogErrorFormat("Parse: Can't convert {0} to MenuType enum, check the spelling.", menuChoice);
- return;
- }
- GetManager<GUIManager>().ShowMenu((MenuType)Enum.Parse(typeof(MenuType), menuChoice));
- }
- public void RefreshHighscore(string difficultyChoice)
- {
- if (!Enum.IsDefined(typeof(DifficultyType), difficultyChoice))
- {
- Debug.LogErrorFormat("Parse: Can't convert {0} to DifficultyType enum, check the spelling.", difficultyChoice);
- return;
- }
- // change shown score data to the chosen difficulty
- GetManager<GUIManager>().RefreshHighscore((DifficultyType)Enum.Parse(typeof(DifficultyType), difficultyChoice));
- }
- }
- }
Add Comment
Please, Sign In to add comment