hch
2025-10-30 dc7922d80c1d133b6261b8af1d521567d2c0a35d
Main/System/Battle/UIComp/BattleTips.cs
@@ -2,106 +2,136 @@
using System;
using UnityEngine.UI;
public class BattleTips : MonoBehaviour
public class BattleTips : MonoBehaviour, IBattleFloatingUI
{
    public Vector2 beginPos = Vector2.zero;
    public Vector2 endPos = new Vector2(0, 150);
    public float scaleChangeTime = 0.2667f; // 7~8帧 (8/30=0.2667秒)
    public float totalShowTime = 0.8f; // 总时间约24帧 (8+16=24帧)
    public float timer = 0f;
    public Vector3 beginScale = new Vector3(4f, 4f, 4f);
    public Vector3 endScale = new Vector3(2f, 2f, 2f);
    public Color beginColor = new Color(1f, 1f, 1f, 0.5f);
    public Color endColor = new Color(1f, 1f, 1f, 1f);
    public RectTransform rectTransform;
    public Text tipText;
    public Text artText;
    public Action OnFinish;
    public Image background;
    private float speedRatio = 1f;
    public Vector3 normalBeginScale = new Vector3(2f, 2f, 2f);
    public Vector3 normalEndScale = new Vector3(1f, 1f, 1f);
    public void SetSpeedRatio(float ratio)
    public Action OnFinish; // 保留 OnFinish
    [SerializeField]
    private BattleFloatingUIController controller;
    void Awake()
    {
        speedRatio = ratio;
        InitController();
    }
    public void SetText(string text, bool useArtText = false)
    private void InitController()
    {
        //  初始放大200% 透明度50% 7~8帧内缩回100% 透明度到100% 再往上飘14~16帧 然后消失 (30帧/秒)
        if (controller != null) return;
        // 8+16/30=0.8秒
        controller = new BattleFloatingUIController(rectTransform, gameObject, ApplyColor);
        controller.beginPos = beginPos;
        controller.endPos = endPos;
        controller.normalBeginScale =   normalBeginScale;
        controller.normalEndScale = normalEndScale;
    }
        
    public void SetRatio(float speed, float scale)
    {
        InitController(); // 确保 controller 已初始化
        controller.SetRatio(speed, scale);
    }
        rectTransform.anchoredPosition = beginPos;
        rectTransform.localScale = beginScale;
    public void SetText(string text, bool useArtText = false, bool isCrit = false, Color textColor = default)
    {
        if (textColor == default)
        {
            textColor = Color.white;
        }
        timer = 0f;
        gameObject.SetActive(true);
        InitController();
        if (useArtText)
        {
            artText.text = text;
            artText.color = beginColor;
            tipText.gameObject.SetActive(false);
            artText.gameObject.SetActive(true);
        }
        else
        {
            tipText.text = text;
            tipText.color = beginColor;
            artText.gameObject.SetActive(false);
            tipText.gameObject.SetActive(true);
        }
        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);
        Play(isCrit);
    }
    //  不要使用update
    public void Run()
    public void Play(bool isCrit, Action onComplete = null)
    {
        if (!gameObject.activeSelf)
            return;
        InitController(); // 确保 controller 已初始化
        if (timer >= totalShowTime)
        // 合并 OnFinish 和 onComplete
        Action combinedCallback = () =>
        {
            gameObject.SetActive(false);
            OnFinish?.Invoke();
            OnFinish = null;
            return;
            onComplete?.Invoke();
        };
        controller.Play(isCrit, combinedCallback);
        }
        // 整个过程都往上飘
        float moveProgress = timer / totalShowTime;
        rectTransform.anchoredPosition = Vector2.Lerp(beginPos, endPos, moveProgress);
        // 阶段1: 7~8帧内缩放从beginScale到endScale,透明度从50%到100%
        if (timer < scaleChangeTime)
    public void Run()
        {
            float scaleProgress = timer / scaleChangeTime;
            rectTransform.localScale = Vector3.Lerp(beginScale, endScale, scaleProgress);
            Color currentColor = Color.Lerp(beginColor, endColor, scaleProgress);
            if (tipText.gameObject.activeSelf)
                tipText.color = currentColor;
            if (artText.gameObject.activeSelf)
                artText.color = currentColor;
        if (controller == null) return; // 防止在 Awake 前调用
        controller.Run();
        }
        // 阶段2: 缩放完成后,保持endScale和100%透明度,继续往上飘
        else
    public void Stop()
        {
            rectTransform.localScale = endScale;
            if (tipText.gameObject.activeSelf)
                tipText.color = endColor;
            if (artText.gameObject.activeSelf)
                artText.color = endColor;
        if (controller == null) return;
        controller.Stop();
        }
        timer += 1f / (float)BattleConst.skillMotionFps * speedRatio;
    public void Resume()
    {
        if (controller == null) return;
        controller.Resume();
    }
    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;
    }
}