EntityBattleInfoView.cs 3.4 KB

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