hch
22 小时以前 742387b0573b11b9edea35c9254139ed385ceb79
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Cysharp.Threading.Tasks;
using DG.Tweening;
 
public class DamageContent : MonoBehaviour, IBattleFloatingUI
{
    public GameObject line;
    public RectTransform parent;
 
    [Header("Floating Config")]
    [Tooltip("请在Inspector中拖拽FloatingConfig资源")]
    public FloatingConfig floatingConfig;
 
    protected List<DamageLine> damageLineList = new List<DamageLine>();
    private BattleDmgInfo battleDmgInfo;
    private BattleFloatingUIController controller;
 
    #region Unity Lifecycle
 
    void Awake()
    {
        line.SetActive(false);
    }
 
    #endregion
 
    #region Controller Management
 
    private void InitController()
    {
        if (controller != null) return;
 
        if (floatingConfig == null)
        {
            Debug.LogError($"[DamageContent] FloatingConfig 未配置,请在Inspector中拖拽赋值! GameObject: {gameObject.name}");
            return;
        }
 
        RectTransform rectTransform = GetComponent<RectTransform>();
        controller = new BattleFloatingUIController(rectTransform, gameObject, ApplyColor, floatingConfig);
    }
 
    public void SetRatio(float speed, float scale)
    {
        InitController();
        controller?.SetRatio(speed, scale);
    }
 
    public void SetFloatingConfig(FloatingConfig config)
    {
        floatingConfig = config;
        if (controller != null)
        {
            controller.SetConfig(config);
        }
    }
 
    #endregion
 
    #region Position Management
 
    /// <summary>
    /// 设置飘字的起点和终点位置(运行时动态设置)
    /// </summary>
    public void SetPosition(Vector2 beginPos, Vector2 endPos)
    {
        InitController();
        controller?.SetRuntimePosition(beginPos, endPos);
    }
 
    #endregion
 
    #region Damage Display
 
    public void SetDamage(BattleDmgInfo _battleDmgInfo, List<BattleDmg> damages, Action _onComplete)
    {
        battleDmgInfo = _battleDmgInfo;
        
        EnsureDamageLineCapacity(damages.Count);
        DisplayDamageLines(damages);
        HideExcessDamageLines(damages.Count);
        
        bool isCrit = battleDmgInfo.IsCrit();
        Play(isCrit, _onComplete);
    }
 
    /// <summary>
    /// 确保有足够的DamageLine对象
    /// </summary>
    private void EnsureDamageLineCapacity(int requiredCount)
    {
        RectTransform lineTemplate = line.GetComponent<RectTransform>();
        Vector2 templateAnchorMin = lineTemplate.anchorMin;
        Vector2 templateAnchorMax = lineTemplate.anchorMax;
        Vector2 templatePivot = lineTemplate.pivot;
        
        for (int i = damageLineList.Count; i < requiredCount; i++)
        {
            GameObject newLine = GameObject.Instantiate(line, parent);
            DamageLine damageLine = newLine.GetComponent<DamageLine>();
            
            RectTransform newLineRect = newLine.GetComponent<RectTransform>();
            if (newLineRect != null)
            {
                newLineRect.anchorMin = templateAnchorMin;
                newLineRect.anchorMax = templateAnchorMax;
                newLineRect.pivot = templatePivot;
                newLineRect.anchoredPosition = Vector2.zero;
                newLineRect.localScale = Vector3.one;
            }
            
            damageLineList.Add(damageLine);
        }
    }
 
    /// <summary>
    /// 显示伤害行并设置位置
    /// </summary>
    private void DisplayDamageLines(List<BattleDmg> damages)
    {
        for (int i = 0; i < damages.Count; i++)
        {
            DamageLine damageLine = damageLineList[i];
            SetDamageLinePosition(damageLine, i);
            damageLine.SetActive(true);
            damageLine.SetDamage(damages[i]);
        }
    }
 
    /// <summary>
    /// 设置单个伤害行的位置(使用Y轴偏移)
    /// </summary>
    private void SetDamageLinePosition(DamageLine damageLine, int index)
    {
        RectTransform lineRect = damageLine.GetComponent<RectTransform>();
        if (lineRect == null) return;
 
        RectTransform lineTemplate = line.GetComponent<RectTransform>();
        Vector2 basePos = lineTemplate.anchoredPosition;
        
        Vector2 pos = basePos;
        pos.y += 60f * index;
        lineRect.anchoredPosition = pos;
    }
 
    /// <summary>
    /// 隐藏多余的伤害行
    /// </summary>
    private void HideExcessDamageLines(int displayCount)
    {
        for (int i = displayCount; i < damageLineList.Count; i++)
        {
            damageLineList[i].SetActive(false);
        }
    }
 
    #endregion
 
    #region Animation Control
 
    public void Play(bool isCrit, Action onComplete = null)
    {
        InitController();
        controller?.Play(isCrit, onComplete);
    }
 
    public void Run()
    {
        if (controller == null) return;
        controller.Run();
    }
 
    public void Stop()
    {
        if (controller == null) return;
        controller.Stop();
    }
 
    public void Resume()
    {
        if (controller == null) return;
        controller.Resume();
    }
 
    #endregion
 
    #region Visual Effects
 
    private void ApplyColor(Color color)
    {
        for (int i = 0; i < damageLineList.Count; i++)
        {
            if (damageLineList[i].gameObject.activeSelf)
            {
                damageLineList[i].SetColor(color);
            }
        }
    }
 
    #endregion
}