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 _pool = new Queue(); 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(); 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); }); } } /// /// Các loại damage để đổi màu & text /// public enum DamageType { Normal, Critical, Heal, Miss, Immune }