| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using UnityEngine;
- using UnityEngine.UIElements;
- /// <summary>
- /// Displays agent information in a popup panel using UI Toolkit
- /// Singleton pattern to ensure only one panel is shown at a time
- /// </summary>
- public class AgentInfoPanel : MonoBehaviour
- {
- private static AgentInfoPanel instance;
- private static UIDocument uiDocument;
- private static VisualElement panelRoot;
- private static AIAgent currentAgent;
- void Awake()
- {
- if (instance != null && instance != this)
- {
- Destroy(gameObject);
- return;
- }
- instance = this;
- uiDocument = GetComponent<UIDocument>();
- if (uiDocument == null)
- {
- Debug.LogError("AgentInfoPanel: UIDocument component not found!");
- return;
- }
- }
- void OnEnable()
- {
- if (uiDocument == null) return;
- panelRoot = uiDocument.rootVisualElement.Q<VisualElement>("AgentInfoPanelRoot");
- if (panelRoot == null)
- {
- Debug.LogError("AgentInfoPanel: AgentInfoPanelRoot not found in UXML!");
- return;
- }
- // Hide panel initially
- panelRoot.style.display = DisplayStyle.None;
- // Setup close button
- var closeButton = panelRoot.Q<Button>("CloseButton");
- if (closeButton != null)
- {
- closeButton.clicked += HidePanel;
- }
- }
- /// <summary>
- /// Shows agent information in the info panel
- /// </summary>
- public static void ShowAgentInfo(AIAgent agent)
- {
- if (instance == null)
- {
- Debug.LogWarning("AgentInfoPanel: Instance not found!");
- return;
- }
- if (agent == null) return;
- currentAgent = agent;
- instance.DisplayAgentInfo(agent);
- }
- /// <summary>
- /// Displays the agent info in the UI
- /// </summary>
- private void DisplayAgentInfo(AIAgent agent)
- {
- if (panelRoot == null) return;
- // Show panel
- panelRoot.style.display = DisplayStyle.Flex;
- // Set agent name
- var nameLabel = panelRoot.Q<Label>("AgentNameLabel");
- if (nameLabel != null)
- {
- nameLabel.text = agent.AgentName;
- }
- // Set stats
- var stats = agent.Stats;
- if (stats != null)
- {
- UpdateStatDisplay("StrengthBar", "StrengthValue", stats.Strength);
- UpdateStatDisplay("SpeedBar", "SpeedValue", stats.Speed);
- UpdateStatDisplay("MagicBar", "MagicValue", stats.Magic);
- UpdateStatDisplay("DexterityBar", "DexterityValue", stats.Dexterity);
- var totalLabel = panelRoot.Q<Label>("TotalStatsValue");
- if (totalLabel != null)
- {
- totalLabel.text = stats.GetTotalStats().ToString();
- }
- }
- // Set status
- var statusLabel = panelRoot.Q<Label>("StatusLabel");
- if (statusLabel != null)
- {
- if (agent.HasReachedGoal)
- {
- statusLabel.text = "✓ Reached Exit";
- }
- else
- {
- statusLabel.text = "Exploring maze...";
- }
- }
- Debug.Log($"Displaying info for: {agent.AgentName}");
- }
- /// <summary>
- /// Updates a stat display (progress bar and label)
- /// </summary>
- private void UpdateStatDisplay(string barName, string labelName, int value)
- {
- var bar = panelRoot.Q<ProgressBar>(barName);
- if (bar != null)
- {
- bar.value = value;
- bar.highValue = 100;
- }
- var label = panelRoot.Q<Label>(labelName);
- if (label != null)
- {
- label.text = value.ToString();
- }
- }
- /// <summary>
- /// Hides the info panel
- /// </summary>
- private static void HidePanel()
- {
- if (panelRoot != null)
- {
- panelRoot.style.display = DisplayStyle.None;
- currentAgent = null;
- }
- }
- /// <summary>
- /// Hides panel when clicking outside of it
- /// </summary>
- void Update()
- {
- if (panelRoot == null || panelRoot.style.display == DisplayStyle.None) return;
- if (Input.GetMouseButtonDown(0))
- {
- // Check if click is outside the panel
- var clickPos = Input.mousePosition;
- var panelWorldRect = panelRoot.worldBound;
- if (!panelWorldRect.Contains(new Vector2(clickPos.x, Screen.height - clickPos.y)))
- {
- HidePanel();
- }
- }
- }
- }
|