using UnityEngine; using TMPro; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { public static GameManager Instance; [Header("Game Settings")] public int totalOrbsToWin = 20; public float timeRemaining = 300f; [Header("UI References")] public TextMeshProUGUI orbText; public TextMeshProUGUI timerText; [Header("Game Over UI")] public GameObject gameOverPanel; public TextMeshProUGUI gameOverTitleText; private int currentOrbs = 0; private bool isGameOver = false; public int GetOrbsInternal() { return currentOrbs; } public void SetDataInternal(int orbs, float time) { currentOrbs = orbs; timeRemaining = time; UpdateOrbUI(); UpdateTimerUI(); } void Awake() { if (Instance == null) Instance = this; else Destroy(gameObject); } void Start() { // Check if the static flag from MainMenu is set to true if (MainMenu.shouldLoadSave) { Debug.Log("GameManager: Continue detected. Finding SaveSystem to load data..."); SaveSystem saveSys = FindFirstObjectByType(); if (saveSys != null) { saveSys.LoadGame(); } // Reset the flag so future restarts are "fresh" unless clicked from Menu again MainMenu.shouldLoadSave = false; } else { Debug.Log("GameManager: Starting Fresh Game."); UpdateOrbUI(); UpdateTimerUI(); } } void Update() { if (isGameOver) return; if (timeRemaining > 0) { timeRemaining -= Time.deltaTime; UpdateTimerUI(); } else { GameOver(false); } } public void AddOrb() { currentOrbs++; UpdateOrbUI(); if (currentOrbs >= totalOrbsToWin) { GameOver(true); } } // Cleaned up to use the SaveSystem instead of PlayerPrefs public void SaveProgress() { SaveSystem saveSys = FindFirstObjectByType(); if (saveSys != null) saveSys.SaveGame(); } public void LoadProgress() { SaveSystem saveSys = FindFirstObjectByType(); if (saveSys != null) saveSys.LoadGame(); } public void ClearSave() { string path = Application.persistentDataPath + "/savegame.json"; if (System.IO.File.Exists(path)) System.IO.File.Delete(path); SceneManager.LoadScene(SceneManager.GetActiveScene().name); } void UpdateOrbUI() { if (orbText != null) orbText.text = "Orbs: " + currentOrbs + " / " + totalOrbsToWin; } void UpdateTimerUI() { if (timerText != null) { float minutes = Mathf.FloorToInt(timeRemaining / 60); float seconds = Mathf.FloorToInt(timeRemaining % 60); timerText.text = string.Format("Time: {0:00}:{1:00}", minutes, seconds); } } public void GameOver(bool won) { isGameOver = true; Time.timeScale = 0f; gameOverPanel.SetActive(true); if (won) { gameOverTitleText.text = "VICTORY!"; gameOverTitleText.color = Color.green; } else { gameOverTitleText.text = "NIGHT HAS FALLEN..."; gameOverTitleText.color = Color.red; } Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } public void RestartGame() { Time.timeScale = 1f; SceneManager.LoadScene(SceneManager.GetActiveScene().name); } public void LoadMainMenu() { Time.timeScale = 1f; SceneManager.LoadScene("MainMenu"); } }