| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- 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
- }
- }
|