using UnityEngine.UI; using UnityEngine; using Core.Utilities; using DG.Tweening; using TMPro; /** * 无尽模式boss血量管理器 * @Author: chenxin * @Date: 2020-10-16 20:00:56 */ namespace KTGMGemClient { public class EndlessBossHPManager : Singleton { /// /// 血条1 /// public Image HPImage1; /// /// 血条2 /// public Image HPImage2; /// /// boss形象 /// public Image BossImage; private string path = "UI/Endless/Blood/"; private string bossPath = "UI/Endless/Boss/"; /// /// 血条底图 /// public Sprite BaseBlood; /// /// 当前的进度值 /// private float current; /// /// 目标进度值 /// private float target; /// /// 血条消失速度 /// public float Speed = 50f; /// /// 当前颜色索引 /// private int index; private Tween tween; public TextMeshProUGUI WaveNumText; /// /// 血条7种颜色 /// private int count = 7; /// /// boss信息,名字和等级 /// public Text BossInfo; // Start is called before the first frame update private void Start() { InitHP(); } /// /// 显示血条 /// public void ShowHP() { gameObject.SetActive(true); } /// /// 隐藏血条 /// public void HideHP() { gameObject.SetActive(false); } /// /// 设置当前进度百分比 /// /// public void SetCurrentProgress(float progress) { target = progress; tween = DOTween.To(() => HPImage2.fillAmount, (v) => HPImage2.fillAmount = v, target, 0.3f); tween.Play(); } /// /// 重置进度 /// private void ResetProgress() { current = 1f; target = 1f; HPImage1.fillAmount = 1; HPImage2.fillAmount = 1; index = index % count; } /// /// 切换血条显示 /// /// public void SwitchHP(bool end = false) { tween.Pause(); HPImage2.fillAmount = 0; index = ++index % count; ChangeHPImageIndex(); ResetProgress(); SetHPColor(end); } /// /// 初始化血条 /// public void InitHP() { index = 0; ResetProgress(); SetHPColor(); } /// /// 更新波次 /// /// public void UpdateWave(int wave) { WaveNumText.text = $"x{wave}"; } /// /// 设置boss信息 /// /// public void SetBossInfo(string info) { BossInfo.text = info; } /// /// 切换两血条的显示层级 /// private void ChangeHPImageIndex() { int index1 = HPImage1.transform.GetSiblingIndex(); int index2 = HPImage2.transform.GetSiblingIndex(); HPImage1.transform.SetSiblingIndex(index2); HPImage2.transform.SetSiblingIndex(index1); // 始终保持 HPImage2在上面 Image tmp = HPImage1; HPImage1 = HPImage2; HPImage2 = tmp; } /// /// 设置血条颜色 /// /// 是否是最终的血条显示 private void SetHPColor(bool end = false) { if (end) { HPImage2.sprite = GetSprite(index); HPImage1.sprite = Resources.Load($"{path}di", typeof(Sprite)) as Sprite; } else { HPImage2.sprite = GetSprite(index); HPImage1.sprite = GetSprite((index + 1) % count); } } /// /// 根据索引获得sprite /// /// private Sprite GetSprite(int index) { return Resources.Load($"{path}{index}", typeof(Sprite)) as Sprite; } /// /// 设置boss形象 /// /// public void SetBossImage(int resId) { resId = (resId + 1) % 2 + 1; BossImage.sprite = Resources.Load($"{bossPath}{resId}"); BossImage.SetNativeSize(); } } }