UIController.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Controls the game UI, updating score and timer displays.
  5. /// </summary>
  6. [RequireComponent(typeof(UIDocument))]
  7. public class UIController : MonoBehaviour
  8. {
  9. private UIDocument uiDocument;
  10. // UI Elements
  11. private Label homeTeamLabel;
  12. private Label homeScoreLabel;
  13. private Label awayTeamLabel;
  14. private Label awayScoreLabel;
  15. private Label timerLabel;
  16. private Label periodLabel;
  17. [Header("Team Names")]
  18. [SerializeField] private string homeTeamName = "Home Team";
  19. [SerializeField] private string awayTeamName = "Away Team";
  20. void Awake()
  21. {
  22. uiDocument = GetComponent<UIDocument>();
  23. }
  24. void OnEnable()
  25. {
  26. // Get root visual element
  27. var root = uiDocument.rootVisualElement;
  28. // Query UI elements
  29. homeTeamLabel = root.Q<Label>("HomeTeamLabel");
  30. homeScoreLabel = root.Q<Label>("HomeScoreLabel");
  31. awayTeamLabel = root.Q<Label>("AwayTeamLabel");
  32. awayScoreLabel = root.Q<Label>("AwayScoreLabel");
  33. timerLabel = root.Q<Label>("TimerLabel");
  34. periodLabel = root.Q<Label>("PeriodLabel");
  35. // Set team names
  36. if (homeTeamLabel != null)
  37. homeTeamLabel.text = homeTeamName;
  38. if (awayTeamLabel != null)
  39. awayTeamLabel.text = awayTeamName;
  40. // Subscribe to GameManager events
  41. if (GameManager.Instance != null)
  42. {
  43. GameManager.Instance.OnScoreChanged += UpdateScore;
  44. GameManager.Instance.OnTimeChanged += UpdateTimer;
  45. GameManager.Instance.OnPeriodChanged += UpdatePeriod;
  46. // Initialize display
  47. UpdateScore(GameManager.Instance.HomeScore, GameManager.Instance.AwayScore);
  48. UpdateTimer(GameManager.Instance.TimeRemaining);
  49. UpdatePeriod(GameManager.Instance.CurrentPeriod);
  50. }
  51. }
  52. void OnDisable()
  53. {
  54. // Unsubscribe from events
  55. if (GameManager.Instance != null)
  56. {
  57. GameManager.Instance.OnScoreChanged -= UpdateScore;
  58. GameManager.Instance.OnTimeChanged -= UpdateTimer;
  59. GameManager.Instance.OnPeriodChanged -= UpdatePeriod;
  60. }
  61. }
  62. private void UpdateScore(int homeScore, int awayScore)
  63. {
  64. if (homeScoreLabel != null)
  65. homeScoreLabel.text = homeScore.ToString();
  66. if (awayScoreLabel != null)
  67. awayScoreLabel.text = awayScore.ToString();
  68. }
  69. private void UpdateTimer(float timeRemaining)
  70. {
  71. if (timerLabel != null)
  72. {
  73. int minutes = Mathf.FloorToInt(timeRemaining / 60f);
  74. int seconds = Mathf.FloorToInt(timeRemaining % 60f);
  75. timerLabel.text = $"{minutes:00}:{seconds:00}";
  76. }
  77. }
  78. private void UpdatePeriod(int period)
  79. {
  80. if (periodLabel != null)
  81. {
  82. periodLabel.text = $"Period {period}";
  83. }
  84. }
  85. }