CardUpLevelView.cs 9.9 KB

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