| | |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | using System; |
| | | using Cysharp.Threading.Tasks; |
| | | |
| | | public class DamageContent : MonoBehaviour |
| | | public class DamageContent : MonoBehaviour, IBattleFloatingUI |
| | | { |
| | | public GameObject line; |
| | | public GameObject line; |
| | | public RectTransform parent; |
| | | |
| | | public RectTransform parent; |
| | | [Header("Floating Config")] |
| | | [Tooltip("请在Inspector中拖拽FloatingConfig资源")] |
| | | public FloatingConfig floatingConfig; |
| | | |
| | | protected List<DamageLine> damageLineList = new List<DamageLine>(); |
| | | protected List<DamageLine> damageLineList = new List<DamageLine>(); |
| | | private BattleDmgInfo battleDmgInfo; |
| | | private BattleFloatingUIController controller; |
| | | |
| | | public PositionTween posTween; |
| | | void Awake() |
| | | { |
| | | line.SetActive(false); |
| | | } |
| | | |
| | | void Awake() |
| | | { |
| | | line.SetActive(false); |
| | | } |
| | | private void InitController() |
| | | { |
| | | if (controller != null) return; |
| | | |
| | | public void SetDamage(List<long> damages, Action _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]); |
| | | } |
| | | if (floatingConfig == null) |
| | | { |
| | | Debug.LogError($"[DamageContent] FloatingConfig 未配置,请在Inspector中拖拽赋值! GameObject: {gameObject.name}"); |
| | | return; |
| | | } |
| | | |
| | | for (int i = damages.Count; i < damageLineList.Count; i++) |
| | | { |
| | | damageLineList[i].SetActive(false); |
| | | } |
| | | RectTransform rectTransform = GetComponent<RectTransform>(); |
| | | controller = new BattleFloatingUIController(rectTransform, gameObject, ApplyColor, floatingConfig); |
| | | } |
| | | |
| | | posTween.Play(_onComplete); |
| | | } |
| | | public void SetRatio(float speed, float scale) |
| | | { |
| | | InitController(); |
| | | controller?.SetRatio(speed, scale); |
| | | } |
| | | |
| | | public void Stop() |
| | | { |
| | | posTween.Stop(); |
| | | } |
| | | /// <summary> |
| | | /// 设置飘字的起点和终点位置(运行时动态设置) |
| | | /// </summary> |
| | | public void SetPosition(Vector2 beginPos, Vector2 endPos) |
| | | { |
| | | InitController(); |
| | | controller?.SetRuntimePosition(beginPos, endPos); |
| | | } |
| | | |
| | | public void Resume() |
| | | { |
| | | posTween.Resume(); |
| | | } |
| | | public async void SetDamage(BattleDmgInfo _battleDmgInfo, List<BattleDmg> damages, Action _onComplete) |
| | | { |
| | | battleDmgInfo = _battleDmgInfo; |
| | | for (int i = damages.Count; 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); |
| | | } |
| | | } |
| | | |
| | | public void Play(bool isCrit, Action onComplete = null) |
| | | { |
| | | InitController(); |
| | | controller?.Play(isCrit, onComplete); |
| | | } |
| | | |
| | | public void Run() |
| | | { |
| | | if (controller == null) return; |
| | | controller.Run(); |
| | | } |
| | | |
| | | public void Stop() |
| | | { |
| | | if (controller == null) return; |
| | | controller.Stop(); |
| | | } |
| | | |
| | | public void Resume() |
| | | { |
| | | if (controller == null) return; |
| | | controller.Resume(); |
| | | } |
| | | |
| | | private void ApplyColor(Color color) |
| | | { |
| | | for (int i = 0; i < damageLineList.Count; i++) |
| | | { |
| | | if (damageLineList[i].gameObject.activeSelf) |
| | | { |
| | | damageLineList[i].SetColor(color); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 运行时更新配置 |
| | | public void SetFloatingConfig(FloatingConfig config) |
| | | { |
| | | floatingConfig = config; |
| | | if (controller != null) |
| | | { |
| | | controller.SetConfig(config); |
| | | } |
| | | } |
| | | } |