yyl
2025-10-31 39001a600fcae2bcf27c225df8752d75fb92fef4
Main/System/Battle/UIComp/BattleTips.cs
@@ -1,45 +1,137 @@
using UnityEngine;
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, 100);
    public float showTime = 0.4f;
    public float timer = 0f;
    public Vector2 endPos = new Vector2(0, 150);
    public RectTransform rectTransform;
    public Text tipText;
    public Text artText;
    public Action OnFinish;
    public Image background;
    public void SetText(string text)
    public Vector3 normalBeginScale = new Vector3(2f, 2f, 2f);
    public Vector3 normalEndScale = new Vector3(1f, 1f, 1f);
    public Action OnFinish; // 保留 OnFinish
    [SerializeField]
    private BattleFloatingUIController controller;
    void Awake()
    {
        tipText.text = text;
        rectTransform.anchoredPosition = Vector2.zero;
        timer = 0f;
        gameObject.SetActive(true);
        InitController();
    }
    //  不要使用update
    public void Run()
    private void InitController()
    {
        if (!gameObject.activeSelf)
            return;
        if (controller != null) return;
        if (timer >= showTime)
        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);
    }
    public void SetText(string text, bool useArtText = false, bool isCrit = false, Color textColor = default)
    {
        if (textColor == default)
        {
            textColor = Color.white;
        }
        InitController();
        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);
        }
        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);
    }
    public void Play(bool isCrit, Action onComplete = null)
    {
        InitController(); // 确保 controller 已初始化
        // 合并 OnFinish 和 onComplete
        Action combinedCallback = () =>
        {
            OnFinish?.Invoke();
            OnFinish = null;
            gameObject.SetActive(false);
            return;
        }
            onComplete?.Invoke();
        };
        controller.Play(isCrit, combinedCallback);
    }
        rectTransform.anchoredPosition = Vector2.Lerp(beginPos, endPos, timer / showTime);
        timer += Time.deltaTime;
    public void Run()
    {
        if (controller == null) return; // 防止在 Awake 前调用
        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)
    {
        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;
    }
}