GameManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using UnityEngine;
  2. using TMPro;
  3. using UnityEngine.SceneManagement;
  4. public class GameManager : MonoBehaviour
  5. {
  6. public static GameManager Instance;
  7. [Header("Game Settings")]
  8. public int totalOrbsToWin = 20;
  9. public float timeRemaining = 300f;
  10. [Header("UI References")]
  11. public TextMeshProUGUI orbText;
  12. public TextMeshProUGUI timerText;
  13. [Header("Game Over UI")]
  14. public GameObject gameOverPanel;
  15. public TextMeshProUGUI gameOverTitleText;
  16. private int currentOrbs = 0;
  17. private bool isGameOver = false;
  18. public int GetOrbsInternal() { return currentOrbs; }
  19. public void SetDataInternal(int orbs, float time)
  20. {
  21. currentOrbs = orbs;
  22. timeRemaining = time;
  23. UpdateOrbUI();
  24. UpdateTimerUI();
  25. }
  26. void Awake()
  27. {
  28. if (Instance == null) Instance = this;
  29. else Destroy(gameObject);
  30. }
  31. void Start()
  32. {
  33. // Check if the static flag from MainMenu is set to true
  34. if (MainMenu.shouldLoadSave)
  35. {
  36. Debug.Log("GameManager: Continue detected. Finding SaveSystem to load data...");
  37. SaveSystem saveSys = FindFirstObjectByType<SaveSystem>();
  38. if (saveSys != null)
  39. {
  40. saveSys.LoadGame();
  41. }
  42. // Reset the flag so future restarts are "fresh" unless clicked from Menu again
  43. MainMenu.shouldLoadSave = false;
  44. }
  45. else
  46. {
  47. Debug.Log("GameManager: Starting Fresh Game.");
  48. UpdateOrbUI();
  49. UpdateTimerUI();
  50. }
  51. }
  52. void Update()
  53. {
  54. if (isGameOver) return;
  55. if (timeRemaining > 0)
  56. {
  57. timeRemaining -= Time.deltaTime;
  58. UpdateTimerUI();
  59. }
  60. else
  61. {
  62. GameOver(false);
  63. }
  64. }
  65. public void AddOrb()
  66. {
  67. currentOrbs++;
  68. UpdateOrbUI();
  69. if (currentOrbs >= totalOrbsToWin)
  70. {
  71. GameOver(true);
  72. }
  73. }
  74. // Cleaned up to use the SaveSystem instead of PlayerPrefs
  75. public void SaveProgress()
  76. {
  77. SaveSystem saveSys = FindFirstObjectByType<SaveSystem>();
  78. if (saveSys != null) saveSys.SaveGame();
  79. }
  80. public void LoadProgress()
  81. {
  82. SaveSystem saveSys = FindFirstObjectByType<SaveSystem>();
  83. if (saveSys != null) saveSys.LoadGame();
  84. }
  85. public void ClearSave()
  86. {
  87. string path = Application.persistentDataPath + "/savegame.json";
  88. if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
  89. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  90. }
  91. void UpdateOrbUI()
  92. {
  93. if (orbText != null)
  94. orbText.text = "Orbs: " + currentOrbs + " / " + totalOrbsToWin;
  95. }
  96. void UpdateTimerUI()
  97. {
  98. if (timerText != null)
  99. {
  100. float minutes = Mathf.FloorToInt(timeRemaining / 60);
  101. float seconds = Mathf.FloorToInt(timeRemaining % 60);
  102. timerText.text = string.Format("Time: {0:00}:{1:00}", minutes, seconds);
  103. }
  104. }
  105. public void GameOver(bool won)
  106. {
  107. isGameOver = true;
  108. Time.timeScale = 0f;
  109. gameOverPanel.SetActive(true);
  110. if (won)
  111. {
  112. gameOverTitleText.text = "VICTORY!";
  113. gameOverTitleText.color = Color.green;
  114. }
  115. else
  116. {
  117. gameOverTitleText.text = "NIGHT HAS FALLEN...";
  118. gameOverTitleText.color = Color.red;
  119. }
  120. Cursor.lockState = CursorLockMode.None;
  121. Cursor.visible = true;
  122. }
  123. public void RestartGame()
  124. {
  125. Time.timeScale = 1f;
  126. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  127. }
  128. public void LoadMainMenu()
  129. {
  130. Time.timeScale = 1f;
  131. SceneManager.LoadScene("MainMenu");
  132. }
  133. }