EntityBattleInfoView.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. protected override void OnInited()
  23. {
  24. //if (_hpImgSize == Vector2.zero)
  25. _hpImgSize = VM.HpImg.rectTransform.sizeDelta;
  26. //Log.Debug($"EntityBattleInfoView size:{_hpImgSize}");
  27. }
  28. protected override void OnEnable(object intent)
  29. {
  30. }
  31. protected override void OnDisable()
  32. {
  33. }
  34. protected override void OnDispose()
  35. {
  36. VM.HpImg.rectTransform.sizeDelta = _hpImgSize;
  37. _master = null;
  38. }
  39. public void RefreshHp()
  40. {
  41. var hpImg = VM.HpImg;
  42. var shieldImg = VM.ShieldImg;
  43. var totalShield = Master.Buffs.TotalShieldVal;
  44. shieldImg.gameObject.SetActive(totalShield > 0);
  45. var totalHp = _master.Entity.Attr.HpLimit + totalShield;
  46. var curHp = _master.Entity.Attr.CurrentHp;
  47. if (hpImg.type == Image.Type.Filled)
  48. {
  49. hpImg.fillAmount = (float)curHp / totalHp;
  50. if (totalShield > 0)
  51. {
  52. shieldImg.fillAmount = (float)(curHp + totalShield) / totalHp;
  53. }
  54. }
  55. else
  56. {
  57. var size = _hpImgSize;
  58. size.x = size.x * curHp / totalHp;
  59. hpImg.rectTransform.sizeDelta = size;
  60. if (totalShield > 0)
  61. {
  62. size = _hpImgSize;
  63. size.x = size.x * (curHp + totalShield) / totalHp;
  64. shieldImg.rectTransform.sizeDelta = size;
  65. }
  66. }
  67. }
  68. public void AddHp(int addVal)
  69. {
  70. //TODO 动画
  71. RefreshHp();
  72. }
  73. public void RefreshBuffs()
  74. {
  75. if (Master.Buffs.IsShieldDirty)
  76. {
  77. Master.Buffs.IsShieldDirty = false;
  78. RefreshHp();
  79. }
  80. if (VM.BuffList == null || Master.Buffs.IsDirty == false)
  81. return;
  82. Master.Buffs.IsDirty = false;
  83. var buffs = XGame.Framework.ListPool.Acquire<IBuff>();
  84. Master.Buffs.GetBuffs(ref buffs);
  85. VM.BuffList.Refresh(buffs, Context);
  86. XGame.Framework.ListPool.Recycle(buffs);
  87. }
  88. }
  89. public class EntityBattleInfoVM : EntityViewModel
  90. {
  91. public Image HpImg { get; private set; }
  92. public Image ShieldImg { get; private set; }
  93. internal BattleInfoBuffList BuffList { get; private set; }
  94. protected override void OnInit(IObjectCollector collector)
  95. {
  96. HpImg = collector.GetComponent<Image>("HpImg");
  97. ShieldImg = collector.GetComponent<Image>("ShieldImg");
  98. BuffList = collector.GetComponent<BattleInfoBuffList>("BuffList");
  99. }
  100. }
  101. }