AgentStatsUIController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using System.Collections.Generic;
  4. /// <summary>
  5. /// Manages the UI Toolkit-based Agent Stats display
  6. /// Shows total active agents and count that have reached an exit
  7. /// </summary>
  8. public class AgentStatsUIController : MonoBehaviour
  9. {
  10. [Header("UI Settings")]
  11. [SerializeField] private UIDocument uiDocument;
  12. [SerializeField] private float updateInterval = 0.25f;
  13. private Label totalAgentsLabel;
  14. private Label exitReachedLabel;
  15. private AIAgentManager agentManager;
  16. private MazeController mazeController;
  17. private float lastUpdateTime = 0f;
  18. private int lastTotalCount = -1;
  19. private int lastExitCount = -1;
  20. void Start()
  21. {
  22. // Find references in Start instead of Awake for better timing
  23. if (uiDocument == null)
  24. {
  25. uiDocument = GetComponent<UIDocument>();
  26. // If not on this GameObject, try to find it in the scene
  27. if (uiDocument == null)
  28. {
  29. uiDocument = FindAnyObjectByType<UIDocument>();
  30. Debug.Log("AgentStatsUIController: Found UIDocument in scene");
  31. }
  32. if (uiDocument == null)
  33. {
  34. Debug.LogError("AgentStatsUIController: UIDocument not found anywhere in scene!");
  35. enabled = false;
  36. return;
  37. }
  38. }
  39. // Get the root and find labels
  40. var root = uiDocument.rootVisualElement;
  41. if (root == null)
  42. {
  43. Debug.LogError("AgentStatsUIController: Root visual element is null!");
  44. return;
  45. }
  46. totalAgentsLabel = root.Q<Label>("TotalAgentsLabel");
  47. exitReachedLabel = root.Q<Label>("ExitReachedLabel");
  48. if (totalAgentsLabel == null)
  49. {
  50. Debug.LogError("AgentStatsUIController: TotalAgentsLabel not found in UXML!");
  51. }
  52. if (exitReachedLabel == null)
  53. {
  54. Debug.LogError("AgentStatsUIController: ExitReachedLabel not found in UXML!");
  55. }
  56. // Find managers
  57. mazeController = FindAnyObjectByType<MazeController>();
  58. if (mazeController == null)
  59. {
  60. Debug.LogError("AgentStatsUIController: MazeController not found in scene!");
  61. return;
  62. }
  63. agentManager = mazeController.GetAgentManager();
  64. if (agentManager == null)
  65. {
  66. Debug.LogError("AgentStatsUIController: AgentManager not found in MazeController!");
  67. return;
  68. }
  69. Debug.Log("AgentStatsUIController: Initialized successfully");
  70. // Do first update immediately
  71. UpdateDisplay();
  72. }
  73. void Update()
  74. {
  75. if (agentManager == null || totalAgentsLabel == null) return;
  76. if (Time.time - lastUpdateTime > updateInterval)
  77. {
  78. UpdateDisplay();
  79. lastUpdateTime = Time.time;
  80. }
  81. }
  82. /// <summary>
  83. /// Updates the UI display with current agent stats
  84. /// </summary>
  85. private void UpdateDisplay()
  86. {
  87. int totalAgents = agentManager.GetActiveAgentCount();
  88. int agentsAtExit = GetAgentsAtExit();
  89. // Only update if values changed
  90. if (totalAgents != lastTotalCount)
  91. {
  92. if (totalAgentsLabel != null)
  93. {
  94. totalAgentsLabel.text = totalAgents.ToString();
  95. lastTotalCount = totalAgents;
  96. }
  97. }
  98. if (agentsAtExit != lastExitCount)
  99. {
  100. if (exitReachedLabel != null)
  101. {
  102. exitReachedLabel.text = agentsAtExit.ToString();
  103. lastExitCount = agentsAtExit;
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Counts how many agents have reached the goal/exit
  109. /// </summary>
  110. private int GetAgentsAtExit()
  111. {
  112. if (agentManager == null) return 0;
  113. List<AIAgent> allAgents = agentManager.GetActiveAgents();
  114. int count = 0;
  115. foreach (var agent in allAgents)
  116. {
  117. if (agent != null && agent.HasReachedGoal)
  118. {
  119. count++;
  120. }
  121. }
  122. return count;
  123. }
  124. }