GameManager.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. private int currentOrbs = 0;
  14. private bool isGameOver = false;
  15. void Awake()
  16. {
  17. // Singleton pattern: ensures only one GameManager exists
  18. if (Instance == null) Instance = this;
  19. else Destroy(gameObject);
  20. }
  21. void Update()
  22. {
  23. if (isGameOver) return;
  24. // Handle Timer
  25. if (timeRemaining > 0)
  26. {
  27. timeRemaining -= Time.deltaTime;
  28. UpdateTimerUI();
  29. }
  30. else
  31. {
  32. GameOver(false); // Timer ran out
  33. }
  34. }
  35. public void AddOrb()
  36. {
  37. currentOrbs++;
  38. UpdateOrbUI();
  39. if (currentOrbs >= totalOrbsToWin)
  40. {
  41. GameOver(true); // Collected all orbs
  42. }
  43. }
  44. void UpdateOrbUI()
  45. {
  46. orbText.text = "Orbs: " + currentOrbs + " / " + totalOrbsToWin;
  47. }
  48. void UpdateTimerUI()
  49. {
  50. float minutes = Mathf.FloorToInt(timeRemaining / 60);
  51. float seconds = Mathf.FloorToInt(timeRemaining % 60);
  52. timerText.text = string.Format("Time: {0:00}:{1:00}", minutes, seconds);
  53. }
  54. void GameOver(bool won)
  55. {
  56. isGameOver = true;
  57. if (won) Debug.Log("VICTORY! All orbs collected.");
  58. else Debug.Log("GAME OVER! Night has fallen.");
  59. // Later we will trigger Victory/Loss screens here
  60. }
  61. }