hch
10 天以前 dc7922d80c1d133b6261b8af1d521567d2c0a35d
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
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using DG.Tweening;
 
 
public class BattleHeroInfoBar : MonoBehaviour
{
    public class TipsInfo
    {
        public string message;
        public bool useArtText;
        public bool followCharacter;
        public float scaleRatio;
 
        public Color textColor = Color.white;
 
        public bool showBackground = false;
    }
 
    protected BattleObject battleObject;
 
    public Slider sliderHp;
 
    public Slider sliderXp; //怒气
 
    protected float timer = 0f;
 
    public float PopUpInterval = 0.2f;
 
 
    [SerializeField] public List<BattleBuffCell> buffCells = new List<BattleBuffCell>();
 
    protected List<TipsInfo> messages = new List<TipsInfo>();
 
    public BasicHeroInfoContainer heroInfoContainer;
 
    public BattleTips textTips;
 
    protected Tween hpTween;
 
    protected Tween xpTween;
 
    protected List<BattleTips> tipsList = new List<BattleTips>();
 
    protected List<HB428_tagSCBuffRefresh> buffList = new List<HB428_tagSCBuffRefresh>();
 
 
    public void SetBattleObject(BattleObject _battleObject)
    {
        battleObject = _battleObject;
        heroInfoContainer.SetHeroInfo(battleObject.teamHero);
        RefreshBuff(battleObject.buffMgr.GetBuffList());
        UpdateHP(battleObject.teamHero.curHp, battleObject.teamHero.curHp, battleObject.teamHero.maxHp, false);
        UpdateXP(battleObject.teamHero.rage, battleObject.teamHero.rage, 100, false);
    }
 
    public void RefreshBuff(List<HB428_tagSCBuffRefresh> datas)
    {
        if (buffCells.IsNullOrEmpty())
            return;
 
 
        for (int i = 0; i < buffCells.Count; i++)
        {
            if (i < datas.Count)
            {
                buffCells[i].SetActive(true);
                HB428_tagSCBuffRefresh buffData = datas[i];
                buffCells[i].Init(buffData, () =>
                {
                    //  点击buff图标 显示buff描述/当前身上所有buff
                });
            }
            else
            {
                buffCells[i].SetActive(false);
            }
        }
    }
 
    protected void OnDisable()
    {
        //  TODO YYL 考虑池化
        messages.Clear();
        for (int i = 0; i < tipsList.Count; i++)
        {
            var tip = tipsList[i];
            tip.OnFinish = null;
            GameObject.DestroyImmediate(tip.gameObject);
        }
        tipsList.Clear();
    }
    public void ShowTips(string message, bool useArtText = false, bool followCharacter = true, float scaleRatio = 1f)
    {
        messages.Add(new TipsInfo
        {
            message = message,
            useArtText = useArtText,
            followCharacter = followCharacter,
            scaleRatio = scaleRatio
        });
    }
 
    public void ShowTips(TipsInfo tipsInfo)
    {
        messages.Add(tipsInfo);
    }
 
    public void SetActive(bool active)
    {
        gameObject.SetActive(active);
    }
 
    public void PopUpTipsDirectly(TipsInfo tipsInfo)
    {
        GameObject prefab = textTips.gameObject;
 
        GameObject go = GameObject.Instantiate(prefab, tipsInfo.followCharacter ? transform : battleObject.battleField.battleRootNode.transform);
 
        BattleTips tips = go.GetComponent<BattleTips>();
 
        if (!tipsInfo.followCharacter)
        {
            var contentRect = go.GetComponent<RectTransform>();
            var contentParentRect = contentRect.parent as RectTransform;
            var infoBarRect = GetComponent<RectTransform>();
 
            Vector3 worldTargetPos = infoBarRect.transform.TransformPoint(infoBarRect.rect.center);
 
            Vector2 anchoredPos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                contentParentRect,
                RectTransformUtility.WorldToScreenPoint(null, worldTargetPos),
                null,
                out anchoredPos);
 
            tips.UpdatePositions(anchoredPos, anchoredPos + new Vector2(0, 150));
            
            // 同时更新缩放
            Vector3 newBeginScale = tips.normalBeginScale * tipsInfo.scaleRatio;
            Vector3 newEndScale = tips.normalEndScale * tipsInfo.scaleRatio;
            tips.UpdateScales(newBeginScale, newEndScale);
        }
 
        tips.SetRatio(battleObject.battleField.speedRatio, 1f);
 
        tips.SetText(tipsInfo.message, tipsInfo.useArtText, false, tipsInfo.textColor);
 
        tips.ShowBackground(tipsInfo.showBackground);
 
        tips.OnFinish = () =>
        {
            tipsList.Remove(tips);
            GameObject.DestroyImmediate(tips.gameObject);
        };
 
        tipsList.Add(tips);
    }
 
 
    public void UpdateHP(long fromHp, long toHp, long maxHp,  bool tween = true)
    {
        //  做hp增加或者减少的动画
        // sliderHp.value = ((float)fromHp) / ((float)maxHp);
        if (hpTween != null)
        {
            battleObject.battleField.battleTweenMgr.OnKillTween(hpTween);
        }
 
        if (tween)
        {
            hpTween = sliderHp.DOValue((float)toHp / (float)maxHp, 0.3f);
            battleObject.battleField.battleTweenMgr.OnPlayTween(hpTween);
        }
        else
        {
            sliderHp.value = ((float)toHp) / ((float)maxHp);
        }
 
        // BattleDebug.LogError("update hp from " + fromHp + " to " + toHp + " maxHp " + maxHp);
    }
 
    public void UpdateXP(long fromXp, long toXp, long maxXp, bool tween = true)
    {
        //  做Xp增加或者减少的动画
        // sliderXp.value = ((float)fromXp) / ((float)maxXp);
        if (xpTween != null)
        {
            battleObject.battleField.battleTweenMgr.OnKillTween(xpTween);
        }
 
        if (tween)
        {
            xpTween = sliderXp.DOValue((float)toXp / (float)maxXp, 0.2f);
            battleObject.battleField.battleTweenMgr.OnPlayTween(xpTween);
        }
        else
        {
            sliderXp.value = ((float)toXp) / ((float)maxXp);
        }
 
        // BattleDebug.LogError("update xp from " + fromXp + " to " + toXp + " maxXp " + maxXp);
    }
 
    public void Run()
    {
        // 倒序遍历 删除.run里删除元素不受影响
        for (int i = tipsList.Count - 1; i >= 0; i--)
        {
            tipsList[i].Run();
        }
 
        timer += 1f / (float)BattleConst.skillMotionFps * battleObject.battleField.speedRatio;
 
        if (messages.Count > 0 && timer >= PopUpInterval)
        {
            // 播放飘字
            TipsInfo tipsInfo = messages[0];
            messages.RemoveAt(0);
 
            PopUpTipsDirectly(tipsInfo);
 
            timer = 0f;
        }
    }
 
    public void SetSpeedRatio(float ratio)
    {
        for (int i = 0; i < tipsList.Count; i++)
        {
            tipsList[i].SetRatio(ratio, 1f);
        }
    }
}