101 lines
3.0 KiB
C#
101 lines
3.0 KiB
C#
|
|
using TMPro;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
namespace WarGame.Units
|
||
|
|
{
|
||
|
|
public class UnitHealthBar : MonoBehaviour
|
||
|
|
{
|
||
|
|
[Header("UI References")]
|
||
|
|
[SerializeField] private Slider healthSlider;
|
||
|
|
[SerializeField] private Image fillImage;
|
||
|
|
[SerializeField] private TMP_Text levelText;
|
||
|
|
[SerializeField] private TMP_Text tacticStatusText;
|
||
|
|
[SerializeField] private TMP_Text unitNameText;
|
||
|
|
|
||
|
|
[Header("Colors")]
|
||
|
|
[SerializeField] private Color playerColor = new Color(0.2f, 0.7f, 1f);
|
||
|
|
[SerializeField] private Color enemyColor = new Color(0.95f, 0.25f, 0.25f);
|
||
|
|
|
||
|
|
[Header("Settings")]
|
||
|
|
[SerializeField] private Vector3 offset = new Vector3(0, 2.2f, 0);
|
||
|
|
[SerializeField] private bool billboardToCamera = true;
|
||
|
|
|
||
|
|
private Transform targetTransform;
|
||
|
|
private Camera mainCamera;
|
||
|
|
private float tacticTextTimer = 0f;
|
||
|
|
|
||
|
|
public void Initialize(Transform target, bool isPlayerTeam, string unitName, int level)
|
||
|
|
{
|
||
|
|
targetTransform = target;
|
||
|
|
mainCamera = Camera.main;
|
||
|
|
|
||
|
|
if (fillImage != null)
|
||
|
|
{
|
||
|
|
fillImage.color = isPlayerTeam ? playerColor : enemyColor;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (unitNameText != null)
|
||
|
|
{
|
||
|
|
unitNameText.text = unitName;
|
||
|
|
}
|
||
|
|
|
||
|
|
UpdateLevel(level);
|
||
|
|
|
||
|
|
if (tacticStatusText != null)
|
||
|
|
{
|
||
|
|
tacticStatusText.gameObject.SetActive(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void LateUpdate()
|
||
|
|
{
|
||
|
|
if (targetTransform != null)
|
||
|
|
{
|
||
|
|
transform.position = targetTransform.position + offset;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (billboardToCamera && mainCamera != null)
|
||
|
|
{
|
||
|
|
transform.rotation = Quaternion.LookRotation(transform.position - mainCamera.transform.position);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (tacticTextTimer > 0)
|
||
|
|
{
|
||
|
|
tacticTextTimer -= Time.deltaTime;
|
||
|
|
if (tacticTextTimer <= 0 && tacticStatusText != null)
|
||
|
|
{
|
||
|
|
tacticStatusText.gameObject.SetActive(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void UpdateHealth(float currentHealth, float maxHealth)
|
||
|
|
{
|
||
|
|
if (healthSlider != null && maxHealth > 0)
|
||
|
|
{
|
||
|
|
healthSlider.value = Mathf.Clamp01(currentHealth / maxHealth);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void UpdateLevel(int level)
|
||
|
|
{
|
||
|
|
if (levelText != null)
|
||
|
|
{
|
||
|
|
levelText.text = $"Lv.{level}";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ShowTacticAlert(string text, Color color, float duration = 2.0f)
|
||
|
|
{
|
||
|
|
if (tacticStatusText != null)
|
||
|
|
{
|
||
|
|
tacticStatusText.text = text;
|
||
|
|
tacticStatusText.color = color;
|
||
|
|
tacticStatusText.gameObject.SetActive(true);
|
||
|
|
tacticTextTimer = duration;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|