using UnityEngine; using TMPro; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { public static GameManager Instance; // Allows other scripts to find this one easily [Header("Game Settings")] public int totalOrbsToWin = 20; public float timeRemaining = 300f; // 5 minutes in seconds [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() { // Singleton pattern: ensures only one GameManager exists if (Instance == null) Instance = this; else Destroy(gameObject); } void Update() { if (isGameOver) return; // Handle Timer if (timeRemaining > 0) { timeRemaining -= Time.deltaTime; UpdateTimerUI(); } else { GameOver(false); // Timer ran out } } public void AddOrb() { currentOrbs++; UpdateOrbUI(); if (currentOrbs >= totalOrbsToWin) { GameOver(true); // Collected all orbs } } public void SaveProgress() { PlayerPrefs.SetInt("SavedOrbs", currentOrbs); PlayerPrefs.SetFloat("SavedTime", timeRemaining); PlayerPrefs.Save(); Debug.Log("Game Saved! Orbs: " + currentOrbs); } public void LoadProgress() { if (PlayerPrefs.HasKey("SavedOrbs")) { currentOrbs = PlayerPrefs.GetInt("SavedOrbs"); timeRemaining = PlayerPrefs.GetFloat("SavedTime"); UpdateOrbUI(); UpdateTimerUI(); Debug.Log("Game Loaded!"); } else { Debug.Log("No Save Data Found."); } } public void ClearSave() { PlayerPrefs.DeleteAll(); SceneManager.LoadScene(SceneManager.GetActiveScene().name); } void UpdateOrbUI() { orbText.text = "Orbs: " + currentOrbs + " / " + totalOrbsToWin; } void UpdateTimerUI() { 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; // Freeze the game gameOverPanel.SetActive(true); if (won) { gameOverTitleText.text = "VICTORY!"; gameOverTitleText.color = Color.green; Debug.Log("Winner!"); } else { gameOverTitleText.text = "NIGHT HAS FALLEN..."; gameOverTitleText.color = Color.red; Debug.Log("Game Over!"); } // Unlock cursor so player can click buttons 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"); // Make sure your menu scene is named this } }