GameManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. UpdateOrbUI();
  49. }
  50. }
  51. private void Update()
  52. {
  53. if(gameState == GameState.Playing)
  54. {
  55. HandleTimer();
  56. }
  57. }
  58. private void HandleTimer()
  59. {
  60. if(timeUntilNightfall > 0)
  61. {
  62. timeUntilNightfall -= Time.deltaTime;
  63. UpdateTimerUI();
  64. }
  65. else
  66. {
  67. timeUntilNightfall = 0;
  68. GameOver();
  69. }
  70. }
  71. private void UpdateTimerUI()
  72. {
  73. if (timerText)
  74. {
  75. int minutes = Mathf.FloorToInt(timeUntilNightfall / 60);
  76. int seconds = Mathf.FloorToInt(timeUntilNightfall % 60);
  77. timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
  78. }
  79. }
  80. public void IncreaseOrbCounter()
  81. {
  82. currentOrbCounter++;
  83. UpdateOrbUI();
  84. CheckAllOrbsCollected();
  85. }
  86. private void UpdateOrbUI()
  87. {
  88. if(orbCounterText != null)
  89. {
  90. orbCounterText.text = currentOrbCounter + "/" + maximumOrbs;
  91. }
  92. }
  93. private void CheckAllOrbsCollected()
  94. {
  95. if(currentOrbCounter >= maximumOrbs)
  96. {
  97. gameState = GameState.Won;
  98. SceneManager.LoadScene("VictoryScene");
  99. }
  100. }
  101. private void GameOver()
  102. {
  103. gameState = GameState.GameOver;
  104. SceneManager.LoadScene("GameOverScene");
  105. }
  106. public void StartGame()
  107. {
  108. currentOrbCounter = 0;
  109. gameState = GameState.Playing;
  110. SceneManager.LoadScene("Sandbox");
  111. }
  112. }