GameManager.cs 4.1 KB

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