CardUpLevelView.cs 9.9 KB

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