UIBossHpBarController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DG.Tweening;
  5. using DG.Tweening.Core;
  6. using DG.Tweening.Plugins.Options;
  7. using UnityEngine;
  8. public class UIBossHpBarController : MonoBehaviour
  9. {
  10. private void Awake()
  11. {
  12. this.cursorTweenAlpha = this.cursorSprite.GetComponent<TweenAlpha>();
  13. base.StartCoroutine(this.HPValueHistoryRecorder());
  14. base.StartCoroutine(this.HPChangeAnim());
  15. }
  16. private void Update()
  17. {
  18. if (UIBossHpBarController.boss == null)
  19. {
  20. return;
  21. }
  22. this.BindData();
  23. if (this.bossData.FullCurrentHp <= 0)
  24. {
  25. this.Disappear();
  26. }
  27. }
  28. private IEnumerator HPChangeAnim()
  29. {
  30. for (;;)
  31. {
  32. if (this.play)
  33. {
  34. this.play = false;
  35. this.cursorTweenAlpha.enabled = true;
  36. this.cursorSprite.enabled = true;
  37. float start = this.hp2Bar.value;
  38. float targe = this.hp1Bar.value;
  39. for (float x = 0f; x < 1f; x += 0.02f)
  40. {
  41. float current = start + (targe - start) * x;
  42. this.hp2Bar.value = current;
  43. yield return new WaitForSeconds(0.02f);
  44. }
  45. this.cursorTweenAlpha.enabled = false;
  46. this.cursorSprite.alpha = 0f;
  47. }
  48. yield return null;
  49. }
  50. yield break;
  51. }
  52. private IEnumerator HPValueHistoryRecorder()
  53. {
  54. for (;;)
  55. {
  56. this.hpValueHistory.Enqueue(this.hp1Bar.value);
  57. if (this.hpValueHistory.Count > 10)
  58. {
  59. this.hpValueHistory.Dequeue();
  60. }
  61. yield return new WaitForSeconds(0.1f);
  62. }
  63. yield break;
  64. }
  65. private void BindData()
  66. {
  67. this.bossData.Update();
  68. this.hp1Bar.value = (float)this.bossData.CurrentAppearHp / Math.Max(1f, (float)this.bossData.MaxHps[this.bossData.CurrentHpBarIndex]);
  69. this.hp1Bar.value = (float)this.bossData.CurrentAppearHp / (float)Mathf.Max(this.bossData.MaxHps[this.bossData.CurrentHpBarIndex], 1);
  70. if (this.hpValueHistory.Count >= 10 && (double)Mathf.Abs(this.hp1Bar.value - this.hpValueHistory.Peek()) < 0.0001 && (double)Mathf.Abs(this.hp1Bar.value - this.hpValueWhenLastPlayAnim) > 0.0001)
  71. {
  72. this.hpValueWhenLastPlayAnim = this.hp1Bar.value;
  73. this.play = true;
  74. }
  75. if ((double)Math.Abs(this.hp1Bar.value - 1f) < 0.0001)
  76. {
  77. this.hp2Bar.value = 1f;
  78. this.cursorTweenAlpha.enabled = false;
  79. this.cursorSprite.alpha = 0f;
  80. this.hpValueWhenLastPlayAnim = 1f;
  81. }
  82. int num = this.bossData.HpBarCount - this.bossData.CurrentHpBarIndex;
  83. this._numLabel.alpha = (float)((num != 1) ? 1 : 0);
  84. this._numLabel.text = StringTools.Int2String[this.bossData.HpBarCount - this.bossData.CurrentHpBarIndex];
  85. }
  86. public void Create(EnemyAttribute enemy, List<int> phaseHp)
  87. {
  88. if (!this.Visible)
  89. {
  90. UIBossHpBarController.boss = enemy;
  91. this.bossData = new UIBossHpBarController.BossHpBarData(phaseHp);
  92. this.FadeTo(1f, 1f, delegate()
  93. {
  94. this.Visible = true;
  95. });
  96. }
  97. else
  98. {
  99. Log.Error("重复生成Boss血条");
  100. }
  101. }
  102. public void Disappear()
  103. {
  104. if (this.Visible)
  105. {
  106. this.Visible = false;
  107. this.FadeTo(0f, 1f);
  108. UIBossHpBarController.boss = null;
  109. base.StopCoroutine(this.HPValueHistoryRecorder());
  110. base.StopCoroutine(this.HPChangeAnim());
  111. }
  112. else
  113. {
  114. Log.Error("重复隐藏Boss血条");
  115. }
  116. }
  117. public YieldInstruction FadeTo(float endValue, float duration)
  118. {
  119. return DOTween.To(() => this._widget.alpha, delegate(float alpha)
  120. {
  121. this._widget.alpha = alpha;
  122. }, endValue, duration).WaitForCompletion();
  123. }
  124. public YieldInstruction FadeTo(float endValue, float duration, TweenCallback onComplete)
  125. {
  126. return DOTween.To(() => this._widget.alpha, delegate(float alpha)
  127. {
  128. this._widget.alpha = alpha;
  129. }, endValue, duration).OnComplete(onComplete).WaitForCompletion();
  130. }
  131. private static EnemyAttribute boss;
  132. private UIBossHpBarController.BossHpBarData bossData;
  133. [SerializeField]
  134. private UIWidget _widget;
  135. [SerializeField]
  136. private UILabel _numLabel;
  137. [SerializeField]
  138. private UIProgressBar hp1Bar;
  139. [SerializeField]
  140. private UIProgressBar hp2Bar;
  141. [SerializeField]
  142. private UISprite cursorSprite;
  143. private TweenAlpha cursorTweenAlpha;
  144. private bool play;
  145. public bool Visible;
  146. private readonly Queue<float> hpValueHistory = new Queue<float>();
  147. private float hpValueWhenLastPlayAnim = 1f;
  148. private class BossHpBarData
  149. {
  150. public BossHpBarData(List<int> phaseHp)
  151. {
  152. this.MaxHps = phaseHp;
  153. this.HpBarCount = phaseHp.Count;
  154. }
  155. public int FullMaxHp
  156. {
  157. get
  158. {
  159. return UIBossHpBarController.boss.maxHp;
  160. }
  161. }
  162. public int FullCurrentHp
  163. {
  164. get
  165. {
  166. return UIBossHpBarController.boss.currentHp;
  167. }
  168. }
  169. public void Update()
  170. {
  171. int num = 0;
  172. for (int i = this.HpBarCount - 1; i >= 0; i--)
  173. {
  174. num += this.MaxHps[i];
  175. if (this.FullCurrentHp <= num)
  176. {
  177. this.CurrentHpBarIndex = i;
  178. this.CurrentAppearHp = this.FullCurrentHp - (num - this.MaxHps[i]);
  179. break;
  180. }
  181. }
  182. }
  183. public readonly int HpBarCount;
  184. public readonly List<int> MaxHps;
  185. public int CurrentHpBarIndex;
  186. public int CurrentAppearHp;
  187. }
  188. }