using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; using DG.Tweening; public class BattleHeroInfoBar : MonoBehaviour { protected BattleObject battleObject; public Slider sliderHp; public Slider sliderXp; //怒气 protected float timer = 0f; public float PopUpInterval = 0.2f; // public List buffCells = new List(); protected List messages = new List(); public BasicHeroInfoContainer heroInfoContainer; public BattleTips textTips; protected Tween hpTween; protected Tween xpTween; protected List tipsList = new List(); protected List buffList = new List(); public ScrollerController scroller; public void SetBattleObject(BattleObject _battleObject) { battleObject = _battleObject; heroInfoContainer.SetHeroInfo(battleObject.teamHero); RefreshBuff(buffList); UpdateHP(battleObject.teamHero.curHp, battleObject.teamHero.curHp, battleObject.teamHero.maxHp, false); UpdateXP(battleObject.teamHero.rage, battleObject.teamHero.rage, 100, false); } public void RefreshBuff(List datas) { buffList = datas; // 更新buff图标 or 创建新的buff图标 scroller.Refresh(); for (int i = 0; i < buffList.Count; i++) { if (i % 5 == 0) { scroller.AddCell(ScrollerDataType.Header, i); } } scroller.Restart(); } protected void OnEnable() { scroller.OnRefreshCell += OnRefreshCell; } protected void OnDisable() { scroller.OnRefreshCell -= OnRefreshCell; // 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(); } protected void OnRefreshCell(ScrollerDataType type, CellView cell) { var _cell = cell as BattleBuffLineCell; _cell.Display(buffList, cell.index); } public void ShowTips(string message) { messages.Add(message); } public void SetActive(bool active) { gameObject.SetActive(active); } public void PopUpTipsDirectly(string message) { GameObject prefab = textTips.gameObject; GameObject go = GameObject.Instantiate(prefab, transform); BattleTips tips = go.GetComponent(); tips.SetText(message); tips.OnFinish = () => { // TODO YYL 考虑池化 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 += Time.deltaTime; if (messages.Count > 0 && timer >= PopUpInterval) { // 播放飘字 string message = messages[0]; messages.RemoveAt(0); PopUpTipsDirectly(message); timer = 0f; } } }