CardUpLevelView.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 Dictionary<int,int> _listItemChose = new Dictionary<int, int>();
  20. private List<GObject> _listItemObj = new List<GObject>();
  21. private int _countCanUpMaxLevel = 0;
  22. private int _nowCountCanUpLevel = 0;
  23. private long _goldsHasNum = 0;
  24. private int _cumulativeGold = 0;
  25. public override void Dispose()
  26. {
  27. EffectUIPool.Recycle(_effectUI1);
  28. _effectUI1 = null;
  29. EffectUIPool.Recycle(_effectUI2);
  30. _effectUI2 = null;
  31. if (_ui != null)
  32. {
  33. _ui.Dispose();
  34. _ui = null;
  35. }
  36. base.Dispose();
  37. }
  38. protected override void OnInit()
  39. {
  40. base.OnInit();
  41. _ui = UI_CardUpLevelUI.Create();
  42. this.viewCom = _ui.target;
  43. this.viewCom.Center();
  44. this.modal = true;
  45. viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
  46. _ui.m_btnUse.onClick.Add(OnClickBtnUse);
  47. _ui.m_btnReduce.onClick.Add(OnClickBtnReduce);
  48. _ui.m_btnAdd.onClick.Add(OnClickBtnAdd);
  49. _ui.m_list.itemRenderer = RenderListItem;
  50. _ui.m_slideUpLevel.onChanged.Add(OnChangeUpLevel);
  51. }
  52. private void OnChangeUpLevel()
  53. {
  54. float volumn = (float)_ui.m_slideUpLevel.value / 100;
  55. _nowCountCanUpLevel = _cardData.lv + (int)(volumn * (_countCanUpMaxLevel - _cardData.lv));
  56. tidyData(_nowCountCanUpLevel);
  57. ReferViewInfo();
  58. }
  59. private void ReferSlide()
  60. {
  61. _ui.m_slideUpLevel.value = (float)(_nowCountCanUpLevel - _cardData.lv ) / (_countCanUpMaxLevel - _cardData.lv)*100;
  62. }
  63. protected override void OnShown()
  64. {
  65. base.OnShown();
  66. _cardData = (CardData)this.viewData;
  67. AddEffect();
  68. ClearItemsCountList();
  69. _ui.m_slideUpLevel.value = 0;
  70. int cfgMaxLevel = CardLvlCfgArray.Instance.GetCfgsByrarity(_cardData.itemCfg.rarity).Count - 1;
  71. tidyData(cfgMaxLevel);
  72. _countCanUpMaxLevel = _nowCountCanUpLevel;
  73. tidyData(_cardData.lv);
  74. ReferViewInfo();
  75. }
  76. private void tidyData(int setLevel)
  77. {
  78. _nowCountCanUpLevel = 0;
  79. _goldsHasNum = 0;
  80. _cumulativeGold = 0;
  81. int CumulativeExp = 0;
  82. long needExp = 0;
  83. long allNeedLevelExp = 0;
  84. if (setLevel > 0){
  85. for (int i = _cardData.lv; i < setLevel; i++)
  86. {
  87. allNeedLevelExp += CardLvlCfgArray.Instance.GetCfgByrarityAndcardLvl(_cardData.itemCfg.rarity, i).needExp;
  88. }
  89. needExp = allNeedLevelExp - _cardData.exp; //缺的经验值
  90. }
  91. //先计算出可以升满的最高级别
  92. for (int i = 0; i < upgradeCardItemsArr.Length; i++)
  93. {
  94. if (!_listItemChose.ContainsKey(i) || _listItemChose[i] == 1) {
  95. int itemId = upgradeCardItemsArr[i];
  96. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  97. int num = 0;
  98. if(setLevel> _cardData.lv)
  99. num = (int)Math.Ceiling((needExp - CumulativeExp) / (decimal)itemCfg.cardUpLvExp); //需要购买的数量
  100. long itemNum = Math.Min(num, ItemDataManager.GetItemNum(itemId)); //物品可购买数量
  101. int[] cardUpLvGolds = ItemCfgArray.Instance.GetCfg(upgradeCardItemsArr[i]).cardUpLvGoldsArr[_cardData.itemCfg.rarity - 1];
  102. _goldsHasNum = ItemDataManager.GetItemNum(cardUpLvGolds[0]);
  103. int goldNum = (int)Math.Floor((_goldsHasNum - _cumulativeGold) / (decimal)cardUpLvGolds[1]);//货币可购买数量
  104. int sumNum = (int)Math.Min(goldNum, itemNum);
  105. itemsCount[i] = sumNum;
  106. CumulativeExp = CumulativeExp + (sumNum * itemCfg.cardUpLvExp);
  107. _cumulativeGold = _cumulativeGold + (sumNum * cardUpLvGolds[1]);
  108. }
  109. }
  110. _nowCountCanUpLevel = _cardData.lv;
  111. allNeedLevelExp = _cardData.exp + CumulativeExp;
  112. for (int i = _cardData.lv; i < setLevel; i++)
  113. {
  114. int needLevelExp = CardLvlCfgArray.Instance.GetCfgByrarityAndcardLvl(_cardData.itemCfg.rarity, i).needExp;
  115. allNeedLevelExp -= needLevelExp;
  116. if (allNeedLevelExp >= 0)
  117. _nowCountCanUpLevel += 1;
  118. else
  119. break;
  120. }
  121. }
  122. //清理itemsCount
  123. private void ClearItemsCountList()
  124. {
  125. if (itemsCount == null)
  126. {
  127. itemsCount = new List<int>();
  128. }
  129. itemsCount.Clear();
  130. for (int i = 0; i < upgradeCardItemsArr.Length; i++)
  131. {
  132. itemsCount.Add(0);
  133. }
  134. }
  135. protected override void OnHide()
  136. {
  137. base.OnHide();
  138. }
  139. private void AddEffect()
  140. {
  141. //边框左上角特效
  142. _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_holderLeftTop, "ui_Activity", "Com_window_L_up");
  143. //边框右下角特效
  144. _effectUI2 = EffectUIPool.CreateEffectUI(_ui.m_holderRightDowm, "ui_Activity", "Com_window_R_Down");
  145. }
  146. private void RenderListItem(int index, GObject obj)
  147. {
  148. UI_ComItem listItem = UI_ComItem.Proxy(obj);
  149. _listItemObj.Add(obj);
  150. CardLvlCfg cardLvCfg = CardLvlCfgArray.Instance.GetCfgByrarityAndcardLvl(_cardData.itemCfg.rarity, _cardData.lv);
  151. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(upgradeCardItemsArr[index]);
  152. listItem.m_ButtonType.selectedIndex = 4;
  153. if (!_listItemChose.ContainsKey(index))
  154. _listItemChose.Add(index,1);
  155. listItem.m_ImgpinkGot.visible = (_listItemChose[index] != 0);
  156. listItem.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg);
  157. if (listItem.target.data == null)
  158. listItem.target.onClick.Add(OnListItemClick);
  159. listItem.target.data = index;
  160. listItem.m_CountType.selectedIndex = 1;
  161. listItem.m_txtDecomCount.text = itemsCount[index].ToString();
  162. listItem.m_txtDecomHasCount.text = "/" + ItemDataManager.GetItemNum(itemCfg.id).ToString();
  163. UI_ComItem.ProxyEnd();
  164. }
  165. private void OnListItemClick(EventContext context)
  166. {
  167. int index = (int)(context.sender as GObject).data;
  168. UI_ComItem listItem = UI_ComItem.Proxy(_listItemObj[index]);
  169. if (_listItemChose[index] == 0)
  170. _listItemChose[index] = 1;
  171. else
  172. _listItemChose[index] = 0;
  173. listItem.m_ImgpinkGot.visible = (_listItemChose[index] != 0);
  174. UI_ComItem.ProxyEnd();
  175. ClearItemsCountList();
  176. tidyData(_nowCountCanUpLevel);
  177. ReferViewInfo();
  178. ReferSlide();
  179. }
  180. private void ReferViewInfo()
  181. {
  182. _ui.m_txtChooseLevel.SetVar("value1", _nowCountCanUpLevel.ToString()).FlushVars();
  183. _ui.m_txtChooseLevel.SetVar("value2", _countCanUpMaxLevel.ToString()).FlushVars();
  184. _ui.m_txtCost.SetVar("value1", _cumulativeGold.ToString()).FlushVars();
  185. _ui.m_txtCost.SetVar("value2", _goldsHasNum.ToString()).FlushVars();
  186. _listItemObj.Clear();
  187. _ui.m_list.numItems = upgradeCardItemsArr.Length;
  188. }
  189. private async void OnClickBtnUse()
  190. {
  191. bool result = await CardSProxy.UpgradeCardLvl(_cardData.id, itemsCount);
  192. if (result)
  193. {
  194. this.Hide();
  195. if (_cardData.lv - _nowCountCanUpLevel >= 1)
  196. {
  197. ViewManager.Show<CardUpView>(new object[] { _cardData.scores, "lv", _nowCountCanUpLevel, _cardData.lv, _cardData.id });
  198. }
  199. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.CI_PAI, 2);
  200. }
  201. }
  202. private void OnClickBtnReduce()
  203. {
  204. _nowCountCanUpLevel -= 1;
  205. if (_nowCountCanUpLevel < _cardData.lv)
  206. _nowCountCanUpLevel = _cardData.lv;
  207. tidyData(_nowCountCanUpLevel);
  208. ReferViewInfo();
  209. ReferSlide();
  210. }
  211. private void OnClickBtnAdd()
  212. {
  213. _nowCountCanUpLevel += 1;
  214. if (_nowCountCanUpLevel > _countCanUpMaxLevel)
  215. _nowCountCanUpLevel = _countCanUpMaxLevel;
  216. tidyData(_nowCountCanUpLevel);
  217. ReferViewInfo();
  218. ReferSlide();
  219. }
  220. }
  221. }