lcy
2025-10-31 227c6aa58384fb8b48f8f9cccae52115fb5cfda3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using UnityEngine;
using System;
using UnityEngine.UI;
 
public class BattleTips : MonoBehaviour, IBattleFloatingUI
{
    public Vector2 beginPos = Vector2.zero;
    public Vector2 endPos = new Vector2(0, 150);
 
    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);
 
    public Action OnFinish; // 保留 OnFinish
 
    [SerializeField]
    private BattleFloatingUIController controller;
 
    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;
    }
 
    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;
            onComplete?.Invoke();
        };
        
        controller.Play(isCrit, combinedCallback);
    }
 
    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;
    }
}