| | |
| | | using UnityEngine; |
| | | using System; |
| | | using Cysharp.Threading.Tasks; |
| | | using DG.Tweening; |
| | | |
| | | public class DamageContent : MonoBehaviour, IBattleFloatingUI |
| | | { |
| | |
| | | private BattleDmgInfo battleDmgInfo; |
| | | private BattleFloatingUIController controller; |
| | | |
| | | #region Unity Lifecycle |
| | | |
| | | void Awake() |
| | | { |
| | | line.SetActive(false); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region Controller Management |
| | | |
| | | private void InitController() |
| | | { |
| | |
| | | controller?.SetRatio(speed, scale); |
| | | } |
| | | |
| | | public void SetFloatingConfig(FloatingConfig config) |
| | | { |
| | | floatingConfig = config; |
| | | if (controller != null) |
| | | { |
| | | controller.SetConfig(config); |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region Position Management |
| | | |
| | | /// <summary> |
| | | /// 设置飘字的起点和终点位置(运行时动态设置) |
| | | /// </summary> |
| | |
| | | controller?.SetRuntimePosition(beginPos, endPos); |
| | | } |
| | | |
| | | public async void SetDamage(BattleDmgInfo _battleDmgInfo, List<BattleDmg> damages, Action _onComplete) |
| | | #endregion |
| | | |
| | | #region Damage Display |
| | | |
| | | public void SetDamage(BattleDmgInfo _battleDmgInfo, List<BattleDmg> damages, Action _onComplete) |
| | | { |
| | | battleDmgInfo = _battleDmgInfo; |
| | | for (int i = damages.Count; i < damageLineList.Count; i++) |
| | | |
| | | EnsureDamageLineCapacity(damages.Count); |
| | | DisplayDamageLines(damages); |
| | | HideExcessDamageLines(damages.Count); |
| | | |
| | | bool isCrit = battleDmgInfo.IsCrit(); |
| | | Play(isCrit, _onComplete); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 确保有足够的DamageLine对象 |
| | | /// </summary> |
| | | private void EnsureDamageLineCapacity(int requiredCount) |
| | | { |
| | | RectTransform lineTemplate = line.GetComponent<RectTransform>(); |
| | | Vector2 templateAnchorMin = lineTemplate.anchorMin; |
| | | Vector2 templateAnchorMax = lineTemplate.anchorMax; |
| | | Vector2 templatePivot = lineTemplate.pivot; |
| | | |
| | | for (int i = damageLineList.Count; i < requiredCount; i++) |
| | | { |
| | | GameObject newLine = GameObject.Instantiate(line, parent); |
| | | DamageLine damageLine = newLine.GetComponent<DamageLine>(); |
| | | |
| | | RectTransform newLineRect = newLine.GetComponent<RectTransform>(); |
| | | if (newLineRect != null) |
| | | { |
| | | newLineRect.anchorMin = templateAnchorMin; |
| | | newLineRect.anchorMax = templateAnchorMax; |
| | | newLineRect.pivot = templatePivot; |
| | | newLineRect.anchoredPosition = Vector2.zero; |
| | | newLineRect.localScale = Vector3.one; |
| | | } |
| | | |
| | | damageLineList.Add(damageLine); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 显示伤害行并设置位置 |
| | | /// </summary> |
| | | private void DisplayDamageLines(List<BattleDmg> damages) |
| | | { |
| | | for (int i = 0; i < damages.Count; i++) |
| | | { |
| | | DamageLine damageLine = damageLineList[i]; |
| | | SetDamageLinePosition(damageLine, i); |
| | | damageLine.SetActive(true); |
| | | damageLine.SetDamage(damages[i]); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 设置单个伤害行的位置(使用Y轴偏移) |
| | | /// </summary> |
| | | private void SetDamageLinePosition(DamageLine damageLine, int index) |
| | | { |
| | | RectTransform lineRect = damageLine.GetComponent<RectTransform>(); |
| | | if (lineRect == null) return; |
| | | |
| | | RectTransform lineTemplate = line.GetComponent<RectTransform>(); |
| | | Vector2 basePos = lineTemplate.anchoredPosition; |
| | | |
| | | Vector2 pos = basePos; |
| | | pos.y += 60f * index; |
| | | lineRect.anchoredPosition = pos; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 隐藏多余的伤害行 |
| | | /// </summary> |
| | | private void HideExcessDamageLines(int displayCount) |
| | | { |
| | | for (int i = displayCount; i < damageLineList.Count; i++) |
| | | { |
| | | damageLineList[i].SetActive(false); |
| | | } |
| | | |
| | | // 使用控制器的Play方法 |
| | | bool isCrit = battleDmgInfo.IsCrit(); |
| | | Play(isCrit, _onComplete); |
| | | |
| | | for (int i = 0; i < damages.Count; i++) |
| | | { |
| | | if (i >= damageLineList.Count) |
| | | { |
| | | GameObject newLine = GameObject.Instantiate(line, parent); |
| | | damageLineList.Add(newLine.GetComponent<DamageLine>()); |
| | | } |
| | | damageLineList[i].SetActive(true); |
| | | damageLineList[i].SetDamage(damages[i]); |
| | | await UniTask.Delay(100); |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region Animation Control |
| | | |
| | | public void Play(bool isCrit, Action onComplete = null) |
| | | { |
| | |
| | | controller.Resume(); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region Visual Effects |
| | | |
| | | private void ApplyColor(Color color) |
| | | { |
| | | for (int i = 0; i < damageLineList.Count; i++) |
| | |
| | | } |
| | | } |
| | | |
| | | // 运行时更新配置 |
| | | public void SetFloatingConfig(FloatingConfig config) |
| | | { |
| | | floatingConfig = config; |
| | | if (controller != null) |
| | | { |
| | | controller.SetConfig(config); |
| | | } |
| | | } |
| | | #endregion |
| | | } |