GameManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using UnityEngine;
  2. /// <summary>
  3. /// Central game manager that controls game state, timer, score, and puck in play status.
  4. /// </summary>
  5. public class GameManager : MonoBehaviour
  6. {
  7. public static GameManager Instance { get; private set; }
  8. [Header("Game Settings")]
  9. [SerializeField] private float periodDuration = 1200f; // 20:00 in seconds
  10. [SerializeField] private int totalPeriods = 3;
  11. [Header("Game State")]
  12. private int homeScore = 0;
  13. private int awayScore = 0;
  14. private int currentPeriod = 1;
  15. private float timeRemaining = 1200f; // Start at 20:00
  16. private bool isPuckInPlay = false;
  17. private bool isGameRunning = false;
  18. public bool IsPuckInPlay => isPuckInPlay;
  19. public bool IsGameRunning => isGameRunning;
  20. public int HomeScore => homeScore;
  21. public int AwayScore => awayScore;
  22. public int CurrentPeriod => currentPeriod;
  23. public float TimeRemaining => timeRemaining;
  24. // Events for UI updates
  25. public delegate void ScoreChangedHandler(int homeScore, int awayScore);
  26. public event ScoreChangedHandler OnScoreChanged;
  27. public delegate void TimeChangedHandler(float timeRemaining);
  28. public event TimeChangedHandler OnTimeChanged;
  29. public delegate void PeriodChangedHandler(int period);
  30. public event PeriodChangedHandler OnPeriodChanged;
  31. public delegate void PuckInPlayChangedHandler(bool inPlay);
  32. public event PuckInPlayChangedHandler OnPuckInPlayChanged;
  33. void Awake()
  34. {
  35. // Singleton pattern
  36. if (Instance == null)
  37. {
  38. Instance = this;
  39. }
  40. else
  41. {
  42. Destroy(gameObject);
  43. return;
  44. }
  45. }
  46. void Start()
  47. {
  48. timeRemaining = periodDuration;
  49. StartGame();
  50. Debug.Log("Game started. All players frozen by default. Press F to start faceoff.");
  51. }
  52. void Update()
  53. {
  54. if (isGameRunning && isPuckInPlay && timeRemaining > 0)
  55. {
  56. timeRemaining -= Time.deltaTime;
  57. if (timeRemaining <= 0)
  58. {
  59. timeRemaining = 0;
  60. EndPeriod();
  61. }
  62. OnTimeChanged?.Invoke(timeRemaining);
  63. }
  64. }
  65. public void StartGame()
  66. {
  67. isGameRunning = true;
  68. currentPeriod = 1;
  69. timeRemaining = periodDuration;
  70. homeScore = 0;
  71. awayScore = 0;
  72. OnScoreChanged?.Invoke(homeScore, awayScore);
  73. OnTimeChanged?.Invoke(timeRemaining);
  74. OnPeriodChanged?.Invoke(currentPeriod);
  75. Debug.Log("Game Started!");
  76. }
  77. public void SetPuckInPlay(bool inPlay)
  78. {
  79. if (isPuckInPlay != inPlay)
  80. {
  81. isPuckInPlay = inPlay;
  82. OnPuckInPlayChanged?.Invoke(isPuckInPlay);
  83. Debug.Log($"Puck in play: {isPuckInPlay}");
  84. }
  85. }
  86. public void AddHomeScore(int points = 1)
  87. {
  88. homeScore += points;
  89. OnScoreChanged?.Invoke(homeScore, awayScore);
  90. Debug.Log($"Home Team scores! {homeScore} - {awayScore}");
  91. // Stop play after goal
  92. SetPuckInPlay(false);
  93. }
  94. public void AddAwayScore(int points = 1)
  95. {
  96. awayScore += points;
  97. OnScoreChanged?.Invoke(homeScore, awayScore);
  98. Debug.Log($"Away Team scores! {homeScore} - {awayScore}");
  99. // Stop play after goal
  100. SetPuckInPlay(false);
  101. }
  102. private void EndPeriod()
  103. {
  104. SetPuckInPlay(false);
  105. isGameRunning = false;
  106. Debug.Log($"End of Period {currentPeriod}");
  107. if (currentPeriod < totalPeriods)
  108. {
  109. // Start next period
  110. currentPeriod++;
  111. timeRemaining = periodDuration;
  112. OnPeriodChanged?.Invoke(currentPeriod);
  113. OnTimeChanged?.Invoke(timeRemaining);
  114. // Game will resume after faceoff
  115. isGameRunning = true;
  116. }
  117. else
  118. {
  119. EndGame();
  120. }
  121. }
  122. private void EndGame()
  123. {
  124. Debug.Log($"Game Over! Final Score: {homeScore} - {awayScore}");
  125. if (homeScore > awayScore)
  126. {
  127. Debug.Log("Home Team Wins!");
  128. }
  129. else if (awayScore > homeScore)
  130. {
  131. Debug.Log("Away Team Wins!");
  132. }
  133. else
  134. {
  135. Debug.Log("Game is tied!");
  136. }
  137. }
  138. public void PauseGame()
  139. {
  140. isGameRunning = false;
  141. }
  142. public void ResumeGame()
  143. {
  144. isGameRunning = true;
  145. }
  146. public string GetFormattedTime()
  147. {
  148. int minutes = Mathf.FloorToInt(timeRemaining / 60f);
  149. int seconds = Mathf.FloorToInt(timeRemaining % 60f);
  150. return $"{minutes:00}:{seconds:00}";
  151. }
  152. }