using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using TheIsland.Models;
using TheIsland.Network;
namespace TheIsland.UI
{
///
/// Creates and manages floating UI above an agent (name, HP/Energy bars, speech bubble).
/// Attach this to the Agent prefab - it will create all UI elements automatically.
///
public class AgentUI : MonoBehaviour
{
#region Configuration
[Header("UI Settings")]
[SerializeField] private Vector3 uiOffset = new Vector3(0, 2.5f, 0);
[SerializeField] private float uiScale = 0.01f;
[SerializeField] private float speechDuration = 5f;
[Header("Colors")]
[SerializeField] private Color hpHighColor = new Color(0.3f, 0.9f, 0.3f);
[SerializeField] private Color hpLowColor = new Color(0.9f, 0.3f, 0.3f);
[SerializeField] private Color energyHighColor = new Color(1f, 0.8f, 0.2f);
[SerializeField] private Color energyLowColor = new Color(1f, 0.5f, 0.1f);
#endregion
#region UI References
private Canvas _canvas;
private TextMeshProUGUI _nameLabel;
private TextMeshProUGUI _personalityLabel;
private Image _hpBarFill;
private Image _energyBarFill;
private TextMeshProUGUI _hpText;
private TextMeshProUGUI _energyText;
private GameObject _speechBubble;
private TextMeshProUGUI _speechText;
private GameObject _deathOverlay;
#endregion
#region State
private int _agentId;
private AgentData _currentData;
private Coroutine _speechCoroutine;
private Camera _mainCamera;
#endregion
#region Properties
public int AgentId => _agentId;
public AgentData CurrentData => _currentData;
public bool IsAlive => _currentData?.IsAlive ?? false;
#endregion
#region Initialization
private void Awake()
{
_mainCamera = Camera.main;
CreateUI();
}
private void LateUpdate()
{
// Make UI face the camera (billboard effect)
if (_canvas != null && _mainCamera != null)
{
_canvas.transform.LookAt(
_canvas.transform.position + _mainCamera.transform.rotation * Vector3.forward,
_mainCamera.transform.rotation * Vector3.up
);
}
}
public void Initialize(AgentData data)
{
_agentId = data.id;
_currentData = data;
gameObject.name = $"Agent_{data.id}_{data.name}";
// Set name and personality
if (_nameLabel != null) _nameLabel.text = data.name;
if (_personalityLabel != null) _personalityLabel.text = $"({data.personality})";
UpdateStats(data);
Debug.Log($"[AgentUI] Initialized {data.name}");
}
#endregion
#region UI Creation
private void CreateUI()
{
// Create World Space Canvas
var canvasObj = new GameObject("AgentCanvas");
canvasObj.transform.SetParent(transform);
canvasObj.transform.localPosition = uiOffset;
canvasObj.transform.localScale = Vector3.one * uiScale;
_canvas = canvasObj.AddComponent