Files
test/Assets/PerfectWorld/Scripts/Managers/DamageTextManager.cs
T
2025-10-09 15:31:53 +07:00

89 lines
2.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using TMPro;
using DG.Tweening;
public class DamageTextPool : MonoBehaviour
{
public static DamageTextPool Instance { get; private set; }
[SerializeField] private GameObject textPrefab;
[SerializeField] private int poolSize = 50;
private Queue<TextMeshPro> _pool = new Queue<TextMeshPro>();
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
return;
}
Instance = this;
InitializePool();
}
private void InitializePool()
{
for (int i = 0; i < poolSize; i++)
{
var obj = Instantiate(textPrefab, transform);
var tmp = obj.GetComponent<TextMeshPro>();
obj.SetActive(false);
_pool.Enqueue(tmp);
}
}
public void Show(Vector3 worldPos, int value, DamageType type = DamageType.Normal)
{
if (_pool.Count == 0)
InitializePool(); // mở rộng nếu thiếu
var tmp = _pool.Dequeue();
tmp.gameObject.SetActive(true);
tmp.text = type switch
{
DamageType.Critical => $"CRIT -{value}",
DamageType.Heal => $"+{value}",
DamageType.Miss => "MISS",
DamageType.Immune => "IMMUNE",
_ => $"-{value}"
};
tmp.color = type switch
{
DamageType.Critical => Color.yellow,
DamageType.Heal => Color.green,
DamageType.Miss => Color.gray,
DamageType.Immune => Color.white,
_ => Color.red
};
tmp.transform.position = worldPos + Vector3.up * 2f;
tmp.transform.localScale = Vector3.one;
// Animation bay lên rồi fade ra
var seq = DOTween.Sequence();
seq.Append(tmp.transform.DOMoveY(worldPos.y + 3f, 1f).SetEase(Ease.OutQuad));
seq.Join(tmp.DOFade(0, 1f));
seq.OnComplete(() =>
{
tmp.alpha = 1;
tmp.gameObject.SetActive(false);
_pool.Enqueue(tmp);
});
}
}
/// <summary>
/// Các loại damage để đổi màu & text
/// </summary>
public enum DamageType
{
Normal,
Critical,
Heal,
Miss,
Immune
}