using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using UnityEngine; using Core.Utilities; using DG.Tweening; /** * 无尽模式boss血量管理器 * @Author: chenxin * @Date: 2020-10-16 20:00:56 */ namespace KTGMGemClient { public class EndlessBossHPManager : Singleton { /// /// 血条1 /// public Image HPImage1; /// /// 血条2 /// public Image HPImage2; private Color[] colorArr = { new Color(1, 0, 0), // "#FF0000" new Color(1, 165 / 255f, 0), // "#FFA500" new Color(1, 1, 0), // "#FFFF00" new Color(0, 1, 0), // "#00FF00" new Color(0, 1, 1), // "#00FFFF" new Color(0, 0, 1), // "#0000FF" new Color(128 / 255f, 0, 128 / 255f) // "#800080" }; /// /// 当前的进度值 /// private float current; /// /// 目标进度值 /// private float target; /// /// 血条消失速度 /// public float Speed = 50f; /// /// 当前颜色索引 /// private int index; private Tween tween; public Text WaveNumText; // 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 % colorArr.Length; } /// /// 切换血条显示 /// /// public void SwitchHP(bool end = false) { tween.Pause(); HPImage2.fillAmount = 0; index = ++index % colorArr.Length; ChangeHPImageIndex(); ResetProgress(); SetHPColor(end); } /// /// 初始化血条 /// public void InitHP() { index = 0; ResetProgress(); SetHPColor(); } /// /// 更新波次 /// /// public void UpdateWave(int wave) { WaveNumText.text = $"×{wave}"; } /// /// 切换两血条的显示层级 /// 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.color = colorArr[index]; HPImage1.color = Color.white; } else { HPImage2.color = colorArr[index]; HPImage1.color = colorArr[(index + 1) % colorArr.Length]; } } } }