lcy
6 分钟以前 f5794dc392d61a664668926e5edf06dbe06d4fc9
Main/System/Battle/UIComp/BattleTips.cs
@@ -2,79 +2,135 @@
using System;
using UnityEngine.UI;
/// <summary>
/// 战斗飘字UI组件
/// 职责:管理UI元素和与控制器的交互
/// </summary>
public class BattleTips : MonoBehaviour, IBattleFloatingUI
{
    public Vector2 beginPos = Vector2.zero;
    public Vector2 endPos = new Vector2(0, 150);
    #region Inspector字段
    [Header("UI Components")]
    public RectTransform rectTransform;
    public Text tipText;
    public Text artText;
    public Image background;
    public Vector3 normalBeginScale = new Vector3(2f, 2f, 2f);
    public Vector3 normalEndScale = new Vector3(1f, 1f, 1f);
    [Header("Floating Config")]
    [Tooltip("飘字动画配置,请在Inspector中拖拽赋值")]
    public FloatingConfig floatingConfig;
    #endregion
    public Action OnFinish; // 保留 OnFinish
    #region 公共字段
    public Action OnFinish;
    // Buff 颜色相关
    private bool useBuffColor = false;
    private bool isDebuff = false;
    #endregion
    [SerializeField]
    private BattleFloatingUIController controller;
    #region 私有字段
    // 移除 [SerializeField],controller 不应该被序列化
    public BattleFloatingUIController controller;
    #endregion
    void Awake()
    {
        InitController();
    }
    private void InitController()
    {
        if (controller != null) return;
        controller = new BattleFloatingUIController(rectTransform, gameObject, ApplyColor);
        controller.beginPos = beginPos;
        controller.endPos = endPos;
        controller.normalBeginScale =   normalBeginScale;
        controller.normalEndScale = normalEndScale;
    }
    #region 公共方法
    /// <summary>
    /// 设置速度和缩放比例
    /// </summary>
    public void SetRatio(float speed, float scale)
    {
        InitController(); // 确保 controller 已初始化
        controller.SetRatio(speed, scale);
        EnsureControllerInitialized();
        controller?.SetRatio(speed, scale);
    }
    public void SetText(string text, bool useArtText = false, bool isCrit = false, Color textColor = default)
    /// <summary>
    /// 设置运行时位置(用于不跟随角色的飘字)
    /// </summary>
    public void SetPosition(Vector2 beginPos, Vector2 endPos)
    {
        if (textColor == default)
        EnsureControllerInitialized();
        controller?.SetRuntimePosition(beginPos, endPos);
    }
    /// <summary>
    /// 运行时更新配置
    /// </summary>
    public void SetFloatingConfig(FloatingConfig config)
    {
        floatingConfig = config;
        if (controller != null)
        {
            textColor = Color.white;
        }
        InitController();
        if (useArtText)
        {
            artText.text = text;
            tipText.gameObject.SetActive(false);
            artText.gameObject.SetActive(true);
            controller.SetConfig(config);
        }
        else
        {
            tipText.text = text;
            artText.gameObject.SetActive(false);
            tipText.gameObject.SetActive(true);
            InitController();
        }
    }
        controller.beginColor = new Color(textColor.r, textColor.g, textColor.b, controller.beginColor.a);
        controller.endColor = new Color(textColor.r, textColor.g, textColor.b, controller.endColor.a);
        ApplyColor(controller.beginColor);
    /// <summary>
    /// 设置是否使用 Buff 颜色
    /// </summary>
    public void SetBuffColor(bool useBuff, bool isDebuffBuff)
    {
        useBuffColor = useBuff;
        isDebuff = isDebuffBuff;
        // 如果使用 buff 颜色,立即设置运行时颜色覆盖
        if (useBuffColor && floatingConfig != null)
        {
            Color buffColor = isDebuff ? floatingConfig.debuffColor : floatingConfig.gainBuffColor;
            Color beginColor = new Color(buffColor.r, buffColor.g, buffColor.b, floatingConfig.beginColor.a);
            Color endColor = new Color(buffColor.r, buffColor.g, buffColor.b, floatingConfig.endColor.a);
            EnsureControllerInitialized();
            controller?.SetRuntimeColor(beginColor, endColor);
        }
    }
    /// <summary>
    /// 设置文本内容和样式
    /// </summary>
    public void SetText(string text, bool useArtText = false, bool isCrit = false)
    {
        EnsureControllerInitialized();
        // 切换文本显示类型
        SwitchTextDisplay(useArtText, text);
        // 开始播放
        Play(isCrit);
    }
    /// <summary>
    /// 显示/隐藏背景
    /// </summary>
    public void ShowBackground(bool show)
    {
        if (background != null)
            background.enabled = show;
    }
    #endregion
    #region IBattleFloatingUI接口实现
    public void Play(bool isCrit, Action onComplete = null)
    {
        InitController(); // 确保 controller 已初始化
        EnsureControllerInitialized();
        
        // 合并 OnFinish 和 onComplete
        if (controller == null) return;
        // 合并回调
        Action combinedCallback = () =>
        {
            OnFinish?.Invoke();
@@ -87,51 +143,98 @@
    public void Run()
    {
        if (controller == null) return; // 防止在 Awake 前调用
        controller.Run();
        controller?.Run();
    }
    public void Stop()
    {
        if (controller == null) return;
        controller.Stop();
        controller?.Stop();
    }
    public void Resume()
    {
        if (controller == null) return;
        controller.Resume();
        controller?.Resume();
    }
    #endregion
    #region 私有方法
    /// <summary>
    /// 初始化控制器
    /// </summary>
    private void InitController()
    {
        if (floatingConfig == null)
        {
            Debug.LogError($"[BattleTips] FloatingConfig 未配置! GameObject: {gameObject.name}");
            return;
        }
        controller = new BattleFloatingUIController(
            rectTransform,
            gameObject,
            ApplyColor,
            floatingConfig
        );
    }
    /// <summary>
    /// 确保控制器已初始化
    /// </summary>
    private void EnsureControllerInitialized()
    {
        if (controller == null || !controller.IsValid())
            InitController();
    }
    /// <summary>
    /// 切换文本显示类型
    /// </summary>
    private void SwitchTextDisplay(bool useArtText, string text)
    {
        if (useArtText)
        {
            artText.text = text;
            tipText.gameObject.SetActive(false);
            artText.gameObject.SetActive(true);
        }
        else
        {
            tipText.text = text;
            artText.gameObject.SetActive(false);
            tipText.gameObject.SetActive(true);
        }
    }
    /// <summary>
    /// 应用文本颜色(保留配置的透明度)
    /// </summary>
    private void ApplyTextColor(Color textColor)
    {
        if (floatingConfig != null)
        {
            Color colorWithAlpha = new Color(
                textColor.r,
                textColor.g,
                textColor.b,
                floatingConfig.beginColor.a
            );
            ApplyColor(colorWithAlpha);
        }
    }
    /// <summary>
    /// 应用颜色到激活的文本组件
    /// </summary>
    private void ApplyColor(Color color)
    {
        if (tipText.gameObject.activeSelf)
            tipText.color = color;
        if (artText.gameObject.activeSelf)
            artText.color = color;
    }
    public void ShowBackground(bool showBackground)
    {
        // Implement the logic to show or hide the background
        background.enabled = showBackground;
    }
    public void UpdatePositions(Vector2 begin, Vector2 end)
    {
        InitController();
        beginPos = begin;
        endPos = end;
        controller.beginPos = begin;
        controller.endPos = end;
    }
    public void UpdateScales(Vector3 beginScale, Vector3 endScale)
    {
        InitController();
        normalBeginScale = beginScale;
        normalEndScale = endScale;
        controller.normalBeginScale = beginScale;
        controller.normalEndScale = endScale;
    }
    #endregion
}