CardUpLevelView.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using FairyGUI;
  2. using UI.Card;
  3. using System.Collections;
  4. using UnityEngine;
  5. using System;
  6. using System.Collections.Generic;
  7. using UI.CommonGame;
  8. using ET;
  9. namespace GFGGame
  10. {
  11. public class CardUpLevelView : BaseWindow
  12. {
  13. private UI_CardUpLevelUI _ui;
  14. private EffectUI _effectUI1;
  15. private EffectUI _effectUI2;
  16. private int[] upgradeCardItemsArr = GlobalCfgArray.globalCfg.upgradeCardItemsArr;
  17. private CardData _cardData;
  18. private List<int> itemsCount;
  19. private int _countCanUpMaxLevel = 0;
  20. private int _nowCountCanUpLevel = 0;
  21. private long _goldsHasNum = 0;
  22. private int _cumulativeGold = 0;
  23. public override void Dispose()
  24. {
  25. EffectUIPool.Recycle(_effectUI1);
  26. _effectUI1 = null;
  27. EffectUIPool.Recycle(_effectUI2);
  28. _effectUI2 = null;
  29. if (_ui != null)
  30. {
  31. _ui.Dispose();
  32. _ui = null;
  33. }
  34. base.Dispose();
  35. }
  36. protected override void OnInit()
  37. {
  38. base.OnInit();
  39. _ui = UI_CardUpLevelUI.Create();
  40. this.viewCom = _ui.target;
  41. this.viewCom.Center();
  42. this.modal = true;
  43. viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
  44. _ui.m_btnUse.onClick.Add(OnClickBtnUse);
  45. _ui.m_btnReduce.onClick.Add(OnClickBtnReduce);
  46. _ui.m_btnAdd.onClick.Add(OnClickBtnAdd);
  47. _ui.m_list.itemRenderer = RenderListItem;
  48. _ui.m_slideUpLevel.onChanged.Add(OnChangeUpLevel);
  49. }
  50. private void OnChangeUpLevel()
  51. {
  52. float volumn = (float)_ui.m_slideUpLevel.value / 100;
  53. _nowCountCanUpLevel = (int)(volumn * (_countCanUpMaxLevel - _cardData.lv + 1));
  54. tidyData(_nowCountCanUpLevel);
  55. ReferViewInfo();
  56. }
  57. private void ReferSlide()
  58. {
  59. _ui.m_slideUpLevel.value = (float)_nowCountCanUpLevel / (_countCanUpMaxLevel - _cardData.lv + 1)*100;
  60. }
  61. protected override void OnShown()
  62. {
  63. base.OnShown();
  64. _cardData = (CardData)this.viewData;
  65. AddEffect();
  66. ClearItemsCountList();
  67. _ui.m_slideUpLevel.value = 0;
  68. int cfgMaxLevel = CardLvlCfgArray.Instance.GetCfgsByrarity(_cardData.itemCfg.rarity).Count - 1;
  69. tidyData(cfgMaxLevel);
  70. _countCanUpMaxLevel = _nowCountCanUpLevel;
  71. tidyData(_cardData.lv);
  72. ReferViewInfo();
  73. }
  74. private void tidyData(int setLevel)
  75. {
  76. _nowCountCanUpLevel = 0;
  77. _goldsHasNum = 0;
  78. _cumulativeGold = 0;
  79. int CumulativeExp = 0;
  80. int needExp = CardLvlCfgArray.Instance.GetCfgByrarityAndcardLvl(_cardData.itemCfg.rarity, setLevel).needExp; //需要升级到的经验值
  81. needExp = needExp - _cardData.exp; //缺的经验值
  82. //先计算出可以升满的最高级别
  83. for (int i = 0; i < upgradeCardItemsArr.Length; i++)
  84. {
  85. int itemId = upgradeCardItemsArr[i];
  86. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  87. int num = 0;
  88. if(setLevel> _cardData.lv)
  89. num = (int)Math.Ceiling((needExp - CumulativeExp) / (decimal)itemCfg.cardUpLvExp); //需要购买的数量
  90. long itemNum = Math.Min(num, ItemDataManager.GetItemNum(itemId)); //物品可购买数量
  91. int[] cardUpLvGolds = ItemCfgArray.Instance.GetCfg(upgradeCardItemsArr[i]).cardUpLvGoldsArr[_cardData.itemCfg.rarity - 1];
  92. _goldsHasNum = ItemDataManager.GetItemNum(cardUpLvGolds[0]);
  93. int goldNum = (int)Math.Floor((_goldsHasNum - _cumulativeGold) / (decimal)cardUpLvGolds[1]);//货币可购买数量
  94. int sumNum = (int)Math.Min(goldNum, itemNum);
  95. itemsCount[i] = sumNum;
  96. CumulativeExp = CumulativeExp + (sumNum * itemCfg.cardUpLvExp);
  97. _cumulativeGold = _cumulativeGold + (sumNum * cardUpLvGolds[1]);
  98. }
  99. _nowCountCanUpLevel = _cardData.lv;
  100. for (int i = _cardData.lv; i < setLevel; i++)
  101. {
  102. int needLevelExp = CardLvlCfgArray.Instance.GetCfgByrarityAndcardLvl(_cardData.itemCfg.rarity, i).needExp;
  103. if (_cardData.exp + CumulativeExp >= needLevelExp)
  104. _nowCountCanUpLevel += 1;
  105. else
  106. break;
  107. }
  108. }
  109. //清理itemsCount
  110. private void ClearItemsCountList()
  111. {
  112. if (itemsCount == null)
  113. {
  114. itemsCount = new List<int>();
  115. }
  116. itemsCount.Clear();
  117. for (int i = 0; i < upgradeCardItemsArr.Length; i++)
  118. {
  119. itemsCount.Add(0);
  120. }
  121. }
  122. protected override void OnHide()
  123. {
  124. base.OnHide();
  125. }
  126. private void AddEffect()
  127. {
  128. //邊框左上角特效
  129. _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_holderLeftTop, "ui_Activity", "Com_window_L_up");
  130. //邊框右下角特效
  131. _effectUI2 = EffectUIPool.CreateEffectUI(_ui.m_holderRightDowm, "ui_Activity", "Com_window_R_Down");
  132. }
  133. private void RenderListItem(int index, GObject obj)
  134. {
  135. UI_ComItem listItem = UI_ComItem.Proxy(obj);
  136. CardLvlCfg cardLvCfg = CardLvlCfgArray.Instance.GetCfgByrarityAndcardLvl(_cardData.itemCfg.rarity, _cardData.lv);
  137. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(upgradeCardItemsArr[index]);
  138. listItem.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg);
  139. listItem.m_CountType.selectedIndex = 1;
  140. listItem.m_txtDecomCount.text = itemsCount[index].ToString();
  141. listItem.m_txtDecomHasCount.text = "/" + ItemDataManager.GetItemNum(itemCfg.id).ToString();
  142. UI_ComItem.ProxyEnd();
  143. }
  144. private void ReferViewInfo()
  145. {
  146. _ui.m_txtChooseLevel.SetVar("value1", _nowCountCanUpLevel.ToString()).FlushVars();
  147. _ui.m_txtChooseLevel.SetVar("value2", _countCanUpMaxLevel.ToString()).FlushVars();
  148. _ui.m_txtCost.SetVar("value1", _cumulativeGold.ToString()).FlushVars();
  149. _ui.m_txtCost.SetVar("value2", _goldsHasNum.ToString()).FlushVars();
  150. _ui.m_list.numItems = upgradeCardItemsArr.Length;
  151. }
  152. private async void OnClickBtnUse()
  153. {
  154. bool result = await CardSProxy.UpgradeCardLvl(_cardData.id, itemsCount);
  155. if (result)
  156. {
  157. this.Hide();
  158. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.CI_PAI, 2);
  159. }
  160. }
  161. private void OnClickBtnReduce()
  162. {
  163. _nowCountCanUpLevel -= 1;
  164. if (_nowCountCanUpLevel < _cardData.lv)
  165. _nowCountCanUpLevel = _cardData.lv;
  166. tidyData(_nowCountCanUpLevel);
  167. ReferViewInfo();
  168. ReferSlide();
  169. }
  170. private void OnClickBtnAdd()
  171. {
  172. _nowCountCanUpLevel += 1;
  173. if (_nowCountCanUpLevel > _countCanUpMaxLevel)
  174. _nowCountCanUpLevel = _countCanUpMaxLevel;
  175. tidyData(_nowCountCanUpLevel);
  176. ReferViewInfo();
  177. ReferSlide();
  178. }
  179. }
  180. }