EntityBattleInfoView.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using FL.Battle;
  2. using FL.Battle.Buffs;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using XGame.Framework.Map;
  6. namespace FL.Map.UI
  7. {
  8. public interface IEntityBattleInfoView : IEntityView
  9. {
  10. ITarget Master { get; set; }
  11. bool IsMoveable { get; set; }
  12. void RefreshHp();
  13. void RefreshBuffs();
  14. }
  15. public interface IEntityBattleInfoVM
  16. {
  17. Image HpImg { get; }
  18. Image ShieldImg { get; }
  19. BattleInfoBuffList BuffList { get; }
  20. }
  21. public abstract class EntityBattleInfoView<TViewModel> : EntityView<TViewModel>, IEntityBattleInfoView where TViewModel : class, IEntityViewModel, IEntityBattleInfoVM
  22. {
  23. private ITarget _master;
  24. public ITarget Master
  25. {
  26. get => _master;
  27. set
  28. {
  29. _master = value;
  30. RefreshHp();
  31. }
  32. }
  33. private Vector2 _hpImgSize;
  34. /// <summary>
  35. /// 是否可以移动
  36. /// boss的血条位置固定
  37. /// </summary>
  38. public bool IsMoveable { get; set; }
  39. protected override void OnInited()
  40. {
  41. //if (_hpImgSize == Vector2.zero)
  42. _hpImgSize = VM.HpImg.rectTransform.sizeDelta;
  43. //Log.Debug($"EntityBattleInfoView size:{_hpImgSize}");
  44. }
  45. protected override void OnDispose()
  46. {
  47. VM.HpImg.rectTransform.sizeDelta = _hpImgSize;
  48. _master = null;
  49. }
  50. public void RefreshHp()
  51. {
  52. var hpImg = VM.HpImg;
  53. var shieldImg = VM.ShieldImg;
  54. var totalShield = Master.Buffs.TotalShieldVal;
  55. shieldImg.gameObject.SetActive(totalShield > 0);
  56. var totalHp = _master.Entity.Attr.HpLimit + totalShield;
  57. var curHp = _master.Entity.Attr.CurrentHp;
  58. if (hpImg.type == Image.Type.Filled)
  59. {
  60. hpImg.fillAmount = (float)curHp / totalHp;
  61. if (totalShield > 0)
  62. {
  63. shieldImg.fillAmount = (float)(curHp + totalShield) / totalHp;
  64. }
  65. }
  66. else
  67. {
  68. var size = _hpImgSize;
  69. size.x = size.x * curHp / totalHp;
  70. hpImg.rectTransform.sizeDelta = size;
  71. if (totalShield > 0)
  72. {
  73. size = _hpImgSize;
  74. size.x = size.x * (curHp + totalShield) / totalHp;
  75. shieldImg.rectTransform.sizeDelta = size;
  76. }
  77. }
  78. }
  79. public void RefreshBuffs()
  80. {
  81. if (Master.Buffs.IsShieldDirty)
  82. {
  83. Master.Buffs.IsShieldDirty = false;
  84. RefreshHp();
  85. }
  86. if (VM.BuffList == null || Master.Buffs.IsDirty == false)
  87. return;
  88. Master.Buffs.IsDirty = false;
  89. var buffs = Master.Buffs.GetBuffs();
  90. VM.BuffList.Refresh(buffs, Context);
  91. XGame.Framework.ListPool.Recycle(buffs);
  92. }
  93. }
  94. }