GameManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. using TMPro;
  3. using UnityEngine.SceneManagement;
  4. public class GameManager : MonoBehaviour
  5. {
  6. public static GameManager Instance; // Allows other scripts to find this one easily
  7. [Header("Game Settings")]
  8. public int totalOrbsToWin = 20;
  9. public float timeRemaining = 300f; // 5 minutes in seconds
  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. // Singleton pattern: ensures only one GameManager exists
  29. if (Instance == null) Instance = this;
  30. else Destroy(gameObject);
  31. }
  32. void Update()
  33. {
  34. if (isGameOver) return;
  35. // Handle Timer
  36. if (timeRemaining > 0)
  37. {
  38. timeRemaining -= Time.deltaTime;
  39. UpdateTimerUI();
  40. }
  41. else
  42. {
  43. GameOver(false); // Timer ran out
  44. }
  45. }
  46. public void AddOrb()
  47. {
  48. currentOrbs++;
  49. UpdateOrbUI();
  50. if (currentOrbs >= totalOrbsToWin)
  51. {
  52. GameOver(true); // Collected all orbs
  53. }
  54. }
  55. public void SaveProgress()
  56. {
  57. PlayerPrefs.SetInt("SavedOrbs", currentOrbs);
  58. PlayerPrefs.SetFloat("SavedTime", timeRemaining);
  59. PlayerPrefs.Save();
  60. Debug.Log("Game Saved! Orbs: " + currentOrbs);
  61. }
  62. public void LoadProgress()
  63. {
  64. if (PlayerPrefs.HasKey("SavedOrbs"))
  65. {
  66. currentOrbs = PlayerPrefs.GetInt("SavedOrbs");
  67. timeRemaining = PlayerPrefs.GetFloat("SavedTime");
  68. UpdateOrbUI();
  69. UpdateTimerUI();
  70. Debug.Log("Game Loaded!");
  71. }
  72. else
  73. {
  74. Debug.Log("No Save Data Found.");
  75. }
  76. }
  77. public void ClearSave()
  78. {
  79. PlayerPrefs.DeleteAll();
  80. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  81. }
  82. void UpdateOrbUI()
  83. {
  84. orbText.text = "Orbs: " + currentOrbs + " / " + totalOrbsToWin;
  85. }
  86. void UpdateTimerUI()
  87. {
  88. float minutes = Mathf.FloorToInt(timeRemaining / 60);
  89. float seconds = Mathf.FloorToInt(timeRemaining % 60);
  90. timerText.text = string.Format("Time: {0:00}:{1:00}", minutes, seconds);
  91. }
  92. public void GameOver(bool won)
  93. {
  94. isGameOver = true;
  95. Time.timeScale = 0f; // Freeze the game
  96. gameOverPanel.SetActive(true);
  97. if (won)
  98. {
  99. gameOverTitleText.text = "VICTORY!";
  100. gameOverTitleText.color = Color.green;
  101. Debug.Log("Winner!");
  102. }
  103. else
  104. {
  105. gameOverTitleText.text = "NIGHT HAS FALLEN...";
  106. gameOverTitleText.color = Color.red;
  107. Debug.Log("Game Over!");
  108. }
  109. // Unlock cursor so player can click buttons
  110. Cursor.lockState = CursorLockMode.None;
  111. Cursor.visible = true;
  112. }
  113. public void RestartGame()
  114. {
  115. Time.timeScale = 1f;
  116. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  117. }
  118. public void LoadMainMenu()
  119. {
  120. Time.timeScale = 1f;
  121. SceneManager.LoadScene("MainMenu"); // Make sure your menu scene is named this
  122. }
  123. }