| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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<SaveSystem>();
- 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<SaveSystem>();
- if (saveSys != null) saveSys.SaveGame();
- }
- public void LoadProgress()
- {
- SaveSystem saveSys = FindFirstObjectByType<SaveSystem>();
- 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");
- }
- }
|