GameManager.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using TMPro;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4. public class GameManager : MonoBehaviour
  5. {
  6. public enum GameState
  7. {
  8. InMainMenu,
  9. Paused,
  10. Playing,
  11. GameOver,
  12. Won
  13. }
  14. public static GameManager gameManagerInstance { get; private set; }
  15. [SerializeField] int currentOrbCounter = 0;
  16. [SerializeField] int maximumOrbs = 10;
  17. [SerializeField] TextMeshProUGUI orbCounterText;
  18. [SerializeField] float timeUntilNightfall = 180f;
  19. [SerializeField] TextMeshProUGUI timerText;
  20. public GameState gameState = GameState.InMainMenu;
  21. private void Awake()
  22. {
  23. if(gameManagerInstance == null)
  24. {
  25. gameManagerInstance = this;
  26. DontDestroyOnLoad(gameObject);
  27. SceneManager.sceneLoaded += OnSceneLoaded;
  28. }
  29. else
  30. {
  31. Destroy(gameObject);
  32. }
  33. }
  34. private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  35. {
  36. if(scene.name == "Sandbox")
  37. {
  38. GameObject orbObject = GameObject.Find("Orb_Text");
  39. if (orbObject)
  40. {
  41. orbCounterText = orbObject.GetComponent<TextMeshProUGUI>();
  42. }
  43. GameObject timerObject = GameObject.Find("Timer");
  44. if (timerObject)
  45. {
  46. timerText = timerObject.GetComponent<TextMeshProUGUI>();
  47. }
  48. GenerateOrbs generator = GetComponent<GenerateOrbs>();
  49. if (generator) generator.GenerateOrb();
  50. UpdateOrbUI();
  51. }
  52. }
  53. private void Update()
  54. {
  55. if(gameState == GameState.Playing)
  56. {
  57. HandleTimer();
  58. }
  59. }
  60. private void HandleTimer()
  61. {
  62. if(timeUntilNightfall > 0)
  63. {
  64. timeUntilNightfall -= Time.deltaTime;
  65. UpdateTimerUI();
  66. }
  67. else
  68. {
  69. timeUntilNightfall = 0;
  70. GameOver();
  71. }
  72. }
  73. private void UpdateTimerUI()
  74. {
  75. if (timerText)
  76. {
  77. int minutes = Mathf.FloorToInt(timeUntilNightfall / 60);
  78. int seconds = Mathf.FloorToInt(timeUntilNightfall % 60);
  79. timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
  80. }
  81. }
  82. public void IncreaseOrbCounter()
  83. {
  84. currentOrbCounter++;
  85. UpdateOrbUI();
  86. CheckAllOrbsCollected();
  87. }
  88. private void UpdateOrbUI()
  89. {
  90. if(orbCounterText != null)
  91. {
  92. orbCounterText.text = currentOrbCounter + "/" + maximumOrbs;
  93. }
  94. }
  95. private void CheckAllOrbsCollected()
  96. {
  97. if(currentOrbCounter >= maximumOrbs)
  98. {
  99. gameState = GameState.Won;
  100. SceneManager.LoadScene("VictoryScene");
  101. }
  102. }
  103. private void GameOver()
  104. {
  105. gameState = GameState.GameOver;
  106. SceneManager.LoadScene("GameOverScene");
  107. }
  108. public void StartGame()
  109. {
  110. currentOrbCounter = 0;
  111. gameState = GameState.Playing;
  112. SceneManager.LoadScene("Sandbox");
  113. }
  114. public int GetMaximumOrbs()
  115. {
  116. return maximumOrbs;
  117. }
  118. }