GameManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System.Collections;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. public class GameManager : MonoBehaviour
  6. {
  7. public enum GameState
  8. {
  9. InMainMenu,
  10. Paused,
  11. Playing,
  12. GameOver,
  13. Won
  14. }
  15. public static GameManager gameManagerInstance { get; private set; }
  16. [SerializeField] int currentOrbCounter;
  17. [SerializeField] int maximumOrbs;
  18. [SerializeField] TextMeshProUGUI orbCounterText;
  19. private float initialTime;
  20. [SerializeField] float timeUntilNightfall;
  21. [SerializeField] TextMeshProUGUI timerText;
  22. [SerializeField] Transform playerTransform;
  23. public GameState gameState = GameState.InMainMenu;
  24. private void Awake()
  25. {
  26. if(gameManagerInstance == null)
  27. {
  28. gameManagerInstance = this;
  29. initialTime = timeUntilNightfall;
  30. SceneManager.sceneLoaded += OnSceneLoaded;
  31. DontDestroyOnLoad(gameObject);
  32. }
  33. else
  34. {
  35. Destroy(gameObject);
  36. }
  37. }
  38. private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  39. {
  40. if(scene.name == "MainMap")
  41. {
  42. currentOrbCounter = 0;
  43. timeUntilNightfall = initialTime;
  44. playerTransform = GameObject.Find("Player").transform;
  45. GameObject orbObject = GameObject.Find("Orb_Text");
  46. if (orbObject)
  47. {
  48. orbCounterText = orbObject.GetComponent<TextMeshProUGUI>();
  49. }
  50. GameObject timerObject = GameObject.Find("Timer");
  51. if (timerObject)
  52. {
  53. timerText = timerObject.GetComponent<TextMeshProUGUI>();
  54. }
  55. StartCoroutine(WaitAndSpawnOrbs());
  56. UpdateOrbUI();
  57. UpdateTimerUI();
  58. }
  59. else if(scene.name == "MainMenu")
  60. {
  61. currentOrbCounter = 0;
  62. timeUntilNightfall = initialTime;
  63. }
  64. else if(scene.name == "VictoryScene")
  65. {
  66. currentOrbCounter = 0;
  67. timeUntilNightfall = initialTime;
  68. }
  69. else if(scene.name == "GameOverScene")
  70. {
  71. currentOrbCounter = 0;
  72. timeUntilNightfall = initialTime;
  73. }
  74. }
  75. IEnumerator WaitAndSpawnOrbs()
  76. {
  77. yield return null;
  78. yield return new WaitForFixedUpdate();
  79. GenerateOrbs generator = GetComponent<GenerateOrbs>();
  80. if (generator) generator.StartGenerating(playerTransform);
  81. }
  82. private void Update()
  83. {
  84. if (gameState == GameState.Playing)
  85. {
  86. HandleTimer();
  87. }
  88. }
  89. private void HandleTimer()
  90. {
  91. if(timeUntilNightfall > 0)
  92. {
  93. timeUntilNightfall -= Time.deltaTime;
  94. UpdateTimerUI();
  95. }
  96. else
  97. {
  98. timeUntilNightfall = 0;
  99. GameOver();
  100. }
  101. }
  102. private void UpdateTimerUI()
  103. {
  104. if (timerText)
  105. {
  106. int minutes = Mathf.FloorToInt(timeUntilNightfall / 60);
  107. int seconds = Mathf.FloorToInt(timeUntilNightfall % 60);
  108. timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
  109. }
  110. }
  111. public void IncreaseOrbCounter()
  112. {
  113. currentOrbCounter++;
  114. UpdateOrbUI();
  115. CheckAllOrbsCollected();
  116. }
  117. private void UpdateOrbUI()
  118. {
  119. if(orbCounterText != null)
  120. {
  121. orbCounterText.text = currentOrbCounter + "/" + maximumOrbs;
  122. }
  123. }
  124. private void CheckAllOrbsCollected()
  125. {
  126. if(currentOrbCounter >= maximumOrbs)
  127. {
  128. gameState = GameState.Won;
  129. SceneManager.LoadScene("VictoryScene");
  130. }
  131. }
  132. private void GameOver()
  133. {
  134. gameState = GameState.GameOver;
  135. SceneManager.LoadScene("GameOverScene");
  136. }
  137. public void StartGame()
  138. {
  139. gameState = GameState.Playing;
  140. SceneManager.LoadScene("MainMap");
  141. }
  142. public void GoBackToMainMenu()
  143. {
  144. gameState = GameState.InMainMenu;
  145. SceneManager.LoadScene("MainMenu");
  146. }
  147. public int GetMaximumOrbs()
  148. {
  149. return maximumOrbs;
  150. }
  151. }