CardUpLevelView.cs 11 KB

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