hch
7 天以前 cb4ec28d83ba847f362392936b20e52e17b03081
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using UnityEngine;
using System;
using UnityEngine.UI;
 
/// <summary>
/// 战斗飘字UI组件
/// 职责:管理UI元素和与控制器的交互
/// </summary>
public class BattleTips : MonoBehaviour, IBattleFloatingUI
{
    #region Inspector字段
    
    [Header("UI Components")]
    public RectTransform rectTransform;
    public Text tipText;
    public Text artText;
    public Image background;
 
    [Header("Floating Config")]
    [Tooltip("飘字动画配置,请在Inspector中拖拽赋值")]
    public FloatingConfig floatingConfig;
    
    #endregion
 
    #region 公共字段
    
    public Action OnFinish;
    
    // Buff 颜色相关
    private bool useBuffColor = false;
    private bool isDebuff = false;
    
    #endregion
 
    #region 私有字段
    
    // 移除 [SerializeField],controller 不应该被序列化
    private BattleFloatingUIController controller;
    
    #endregion
 
 
    #region 公共方法
    
    /// <summary>
    /// 设置速度和缩放比例
    /// </summary>
    public void SetRatio(float speed, float scale)
    {
        EnsureControllerInitialized();
        controller?.SetRatio(speed, scale);
    }
 
    /// <summary>
    /// 设置运行时位置(用于不跟随角色的飘字)
    /// </summary>
    public void SetPosition(Vector2 beginPos, Vector2 endPos)
    {
        EnsureControllerInitialized();
        controller?.SetRuntimePosition(beginPos, endPos);
    }
    
    /// <summary>
    /// 运行时更新配置
    /// </summary>
    public void SetFloatingConfig(FloatingConfig config)
    {
        floatingConfig = config;
        
        if (controller != null)
        {
            controller.SetConfig(config);
        }
        else
        {
            InitController();
        }
    }
 
    /// <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)
    {
        EnsureControllerInitialized();
        
        if (controller == null) return;
        
        // 合并回调
        Action combinedCallback = () =>
        {
            OnFinish?.Invoke();
            OnFinish = null;
            onComplete?.Invoke();
        };
        
        controller.Play(isCrit, combinedCallback);
    }
 
    public void Run()
    {
        controller?.Run();
    }
 
    public void Stop()
    {
        controller?.Stop();
    }
 
    public void Resume()
    {
        controller?.Resume();
    }
    
    #endregion
 
    #region 私有方法
    
    /// <summary>
    /// 初始化控制器
    /// </summary>
    private void InitController()
    {
        if (controller != null) return;
 
        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)
            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;
    }
    
    #endregion
}