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);
|
}
|
}
|
}
|