| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using UnityEngine;
- using UnityEngine.UIElements;
- using System.Collections.Generic;
- /// <summary>
- /// Manages the UI Toolkit-based Agent Stats display
- /// Shows total active agents and count that have reached an exit
- /// </summary>
- public class AgentStatsUIController : MonoBehaviour
- {
- [Header("UI Settings")]
- [SerializeField] private UIDocument uiDocument;
- [SerializeField] private float updateInterval = 0.25f;
- private Label totalAgentsLabel;
- private Label exitReachedLabel;
- private Label killedLabel;
- private Label monstersKilledLabel;
- private AIAgentManager agentManager;
- private MazeController mazeController;
- private float lastUpdateTime = 0f;
- private int lastTotalCount = -1;
- private int lastExitCount = -1;
- private int lastKilledCount = -1;
- private int lastMonstersKilledCount = -1;
- void Start()
- {
- // Find references in Start instead of Awake for better timing
- if (uiDocument == null)
- {
- uiDocument = GetComponent<UIDocument>();
- // If not on this GameObject, try to find it in the scene
- if (uiDocument == null)
- {
- uiDocument = FindAnyObjectByType<UIDocument>();
- Debug.Log("AgentStatsUIController: Found UIDocument in scene");
- }
- if (uiDocument == null)
- {
- Debug.LogError("AgentStatsUIController: UIDocument not found anywhere in scene!");
- enabled = false;
- return;
- }
- }
- // Get the root and find labels
- var root = uiDocument.rootVisualElement;
- if (root == null)
- {
- Debug.LogError("AgentStatsUIController: Root visual element is null!");
- return;
- }
- totalAgentsLabel = root.Q<Label>("TotalAgentsLabel");
- exitReachedLabel = root.Q<Label>("ExitReachedLabel");
- killedLabel = root.Q<Label>("KilledLabel");
- monstersKilledLabel = root.Q<Label>("MonstersKilledLabel");
- if (totalAgentsLabel == null)
- Debug.LogError("AgentStatsUIController: TotalAgentsLabel not found in UXML!");
- if (exitReachedLabel == null)
- Debug.LogError("AgentStatsUIController: ExitReachedLabel not found in UXML!");
- // killedLabel / monstersKilledLabel are optional – no error if absent
- // Find managers
- mazeController = FindAnyObjectByType<MazeController>();
- if (mazeController == null)
- {
- Debug.LogError("AgentStatsUIController: MazeController not found in scene!");
- return;
- }
- agentManager = mazeController.GetAgentManager();
- if (agentManager == null)
- {
- Debug.LogError("AgentStatsUIController: AgentManager not found in MazeController!");
- return;
- }
- Debug.Log("AgentStatsUIController: Initialized successfully");
- // Do first update immediately
- UpdateDisplay();
- }
- void Update()
- {
- if (agentManager == null || totalAgentsLabel == null) return;
- if (Time.time - lastUpdateTime > updateInterval)
- {
- UpdateDisplay();
- lastUpdateTime = Time.time;
- }
- }
- /// <summary>
- /// Updates the UI display with current agent stats
- /// </summary>
- private void UpdateDisplay()
- {
- int totalAgents = agentManager.GetActiveAgentCount();
- int agentsAtExit = GetAgentsAtExit();
- int agentsKilled = agentManager.AgentsKilledByMonsters;
- int monstersKilled = agentManager.MonstersKilledByAgents;
- // Only update if values changed
- if (totalAgents != lastTotalCount)
- {
- if (totalAgentsLabel != null)
- {
- totalAgentsLabel.text = totalAgents.ToString();
- lastTotalCount = totalAgents;
- }
- }
- if (agentsAtExit != lastExitCount)
- {
- if (exitReachedLabel != null)
- {
- exitReachedLabel.text = agentsAtExit.ToString();
- lastExitCount = agentsAtExit;
- }
- }
- if (agentsKilled != lastKilledCount)
- {
- if (killedLabel != null)
- {
- killedLabel.text = agentsKilled.ToString();
- lastKilledCount = agentsKilled;
- }
- }
- if (monstersKilled != lastMonstersKilledCount)
- {
- if (monstersKilledLabel != null)
- {
- monstersKilledLabel.text = monstersKilled.ToString();
- lastMonstersKilledCount = monstersKilled;
- }
- }
- }
- /// <summary>
- /// Counts how many agents have reached the goal/exit
- /// </summary>
- private int GetAgentsAtExit()
- {
- if (agentManager == null) return 0;
- List<AIAgent> allAgents = agentManager.GetActiveAgents();
- int count = 0;
- foreach (var agent in allAgents)
- {
- if (agent != null && agent.HasReachedGoal)
- {
- count++;
- }
- }
- return count;
- }
- }
|