CardUpLevelView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using FairyGUI;
  2. using UI.Card;
  3. using System.Collections;
  4. using UnityEngine;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using cfg.GfgCfg;
  9. using UI.CommonGame;
  10. using ET;
  11. namespace GFGGame
  12. {
  13. public class CardUpLevelView : BaseWindow
  14. {
  15. private UI_CardUpLevelUI _ui;
  16. private EffectUI _effectUI1;
  17. private EffectUI _effectUI2;
  18. private List<int> upgradeCardItemsArr = CommonDataManager.Tables.TblGlobalCfg.UpgradeCardItems;
  19. private CardData _cardData;
  20. private List<int> itemsCount;
  21. private Dictionary<int, int> _listItemChose = new Dictionary<int, int>();
  22. private List<GObject> _listItemObj = new List<GObject>();
  23. private int _countCanUpMaxLevel = 0;
  24. private int _nowCountCanUpLevel = 0;
  25. private int _oldCanUpLevel = 0;
  26. private long _goldsHasNum = 0;
  27. private int _cumulativeGold = 0;
  28. private int _cfgMaxLevel = 0;
  29. public override void Dispose()
  30. {
  31. EffectUIPool.Recycle(_effectUI1);
  32. _effectUI1 = null;
  33. EffectUIPool.Recycle(_effectUI2);
  34. _effectUI2 = null;
  35. if (_ui != null)
  36. {
  37. _ui.Dispose();
  38. _ui = null;
  39. }
  40. base.Dispose();
  41. }
  42. protected override void OnInit()
  43. {
  44. base.OnInit();
  45. _ui = UI_CardUpLevelUI.Create();
  46. this.viewCom = _ui.target;
  47. this.viewCom.Center();
  48. this.modal = true;
  49. viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
  50. _ui.m_btnUse.onClick.Add(OnClickBtnUse);
  51. _ui.m_btnReduce.onClick.Add(OnClickBtnReduce);
  52. _ui.m_btnAdd.onClick.Add(OnClickBtnAdd);
  53. _ui.m_list.itemRenderer = RenderListItem;
  54. _ui.m_slideUpLevel.onChanged.Add(OnChangeUpLevel);
  55. }
  56. protected override void OnShown()
  57. {
  58. base.OnShown();
  59. //防止引导检测之前触发点击事件
  60. GRoot.inst.touchable = false;
  61. _cardData = (CardData)this.viewData;
  62. AddEffect();
  63. ClearItemsCountList();
  64. _oldCanUpLevel = _cardData.lv;
  65. _cfgMaxLevel = CommonDataManager.Tables.TblCardLvlCfg.DataList
  66. .Where(a => a.Rarity == _cardData.itemCfg.Rarity).ToList().Count;
  67. tidyData(_cfgMaxLevel, "Max");
  68. _ui.m_slideUpLevel.value = 100;
  69. tidyData(_countCanUpMaxLevel); // _cardData.lv
  70. ReferViewInfo();
  71. Timers.inst.AddUpdate(CheckGuide);
  72. }
  73. private void tidyData(int setLevel, string tidyType = "")
  74. {
  75. _goldsHasNum = 0;
  76. _cumulativeGold = 0;
  77. int CumulativeExp = 0;
  78. long needExp = 0;
  79. long allNeedLevelExp = 0;
  80. if (setLevel > 0)
  81. {
  82. for (int i = _cardData.lv; i < setLevel; i++)
  83. {
  84. allNeedLevelExp += CommonDataManager.Tables.TblCardLvlCfg.Get(_cardData.itemCfg.Rarity, i).NeedExp;
  85. }
  86. needExp = allNeedLevelExp - _cardData.exp; //缺的经验值
  87. }
  88. //先计算出可以升满的最高级别
  89. for (int i = 0; i < upgradeCardItemsArr.Count; i++)
  90. {
  91. ItemParamProto cardUpLvGolds = CommonDataManager.Tables.TblItemCfg.GetOrDefault(upgradeCardItemsArr[i])
  92. .CardUpLvGolds[_cardData.itemCfg.Rarity - 1].ToGfgGameItemParam();
  93. _goldsHasNum = ItemDataManager.GetItemNum(cardUpLvGolds.ItemId);
  94. if (!_listItemChose.ContainsKey(i) || _listItemChose[i] == 1)
  95. {
  96. int itemId = upgradeCardItemsArr[i];
  97. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
  98. int num = 0;
  99. if (setLevel > _cardData.lv)
  100. num = (int)Math.Ceiling((needExp - CumulativeExp) / (decimal)itemCfg.CardUpLvExp); //需要购买的数量
  101. long itemNum = Math.Min(num, ItemDataManager.GetItemNum(itemId)); //物品可购买数量
  102. int goldNum =
  103. (int)Math.Floor((_goldsHasNum - _cumulativeGold) / (decimal)cardUpLvGolds.Count); //货币可购买数量
  104. int sumNum = (int)Math.Min(goldNum, itemNum);
  105. itemsCount[i] = sumNum;
  106. CumulativeExp = CumulativeExp + (sumNum * itemCfg.CardUpLvExp);
  107. _cumulativeGold = _cumulativeGold + (sumNum * cardUpLvGolds.Count);
  108. }
  109. }
  110. if (tidyType == "Max")
  111. _countCanUpMaxLevel = _cardData.lv;
  112. else
  113. _nowCountCanUpLevel = _cardData.lv;
  114. allNeedLevelExp = _cardData.exp + CumulativeExp;
  115. int lvLimit = CommonDataManager.Tables.TblRoleLevelCfg.GetOrDefault(RoleDataManager.lvl).CardLeverLimit;
  116. for (int i = _cardData.lv; i < setLevel; i++)
  117. {
  118. int needLevelExp = CommonDataManager.Tables.TblCardLvlCfg.Get(_cardData.itemCfg.Rarity, i).NeedExp;
  119. allNeedLevelExp -= needLevelExp;
  120. if (allNeedLevelExp >= 0)
  121. {
  122. if (tidyType == "Max")
  123. {
  124. if (_countCanUpMaxLevel < lvLimit)
  125. _countCanUpMaxLevel += 1;
  126. }
  127. else
  128. {
  129. if (_nowCountCanUpLevel < lvLimit)
  130. _nowCountCanUpLevel += 1;
  131. }
  132. }
  133. else
  134. break;
  135. }
  136. }
  137. private void OnChangeUpLevel()
  138. {
  139. float volumn = (float)_ui.m_slideUpLevel.value / 100;
  140. _nowCountCanUpLevel = _cardData.lv + (int)(volumn * (_countCanUpMaxLevel - _cardData.lv));
  141. tidyData(_nowCountCanUpLevel);
  142. ReferViewInfo();
  143. }
  144. private void ReferSlide()
  145. {
  146. _ui.m_slideUpLevel.value =
  147. (float)(_nowCountCanUpLevel - _cardData.lv) / (_countCanUpMaxLevel - _cardData.lv) * 100;
  148. }
  149. //清理itemsCount
  150. private void ClearItemsCountList()
  151. {
  152. if (itemsCount == null)
  153. {
  154. itemsCount = new List<int>();
  155. }
  156. itemsCount.Clear();
  157. for (int i = 0; i < upgradeCardItemsArr.Count; i++)
  158. {
  159. itemsCount.Add(0);
  160. }
  161. }
  162. protected override void OnHide()
  163. {
  164. GRoot.inst.touchable = true;
  165. base.OnHide();
  166. Timers.inst.Remove(CheckGuide);
  167. }
  168. private void AddEffect()
  169. {
  170. //边框左上角特效
  171. EffectUIPool.CreateEffectUI(_ui.m_holderLeftTop, "ui_Activity", "Com_window_L_up",
  172. onComplete: (effect) =>
  173. {
  174. if (effect != null)
  175. {
  176. _effectUI1 = effect;
  177. }
  178. });
  179. //边框右下角特效
  180. EffectUIPool.CreateEffectUI(_ui.m_holderRightDowm, "ui_Activity", "Com_window_R_Down",
  181. onComplete: (effect) =>
  182. {
  183. if (effect != null)
  184. {
  185. _effectUI2 = effect;
  186. }
  187. });
  188. }
  189. private void RenderListItem(int index, GObject obj)
  190. {
  191. UI_ComItem listItem = UI_ComItem.Proxy(obj);
  192. _listItemObj.Add(obj);
  193. CardLvlCfg cardLvCfg = CommonDataManager.Tables.TblCardLvlCfg.Get(_cardData.itemCfg.Rarity, _cardData.lv);
  194. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(upgradeCardItemsArr[index]);
  195. listItem.m_ButtonType.selectedIndex = 4;
  196. if (!_listItemChose.ContainsKey(index))
  197. _listItemChose.Add(index, 1);
  198. listItem.m_ImgpinkGot.visible = (_listItemChose[index] != 0);
  199. listItem.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg);
  200. if (listItem.target.data == null)
  201. listItem.target.onClick.Add(OnListItemClick);
  202. listItem.target.data = index;
  203. listItem.m_CountType.selectedIndex = 1;
  204. listItem.m_txtDecomCount.text = itemsCount[index].ToString();
  205. listItem.m_txtDecomHasCount.text = "/" + ItemDataManager.GetItemNum(itemCfg.Id).ToString();
  206. UI_ComItem.ProxyEnd();
  207. }
  208. private void OnListItemClick(EventContext context)
  209. {
  210. int index = (int)(context.sender as GObject).data;
  211. UI_ComItem listItem = UI_ComItem.Proxy(_listItemObj[index]);
  212. if (_listItemChose[index] == 0)
  213. _listItemChose[index] = 1;
  214. else
  215. _listItemChose[index] = 0;
  216. listItem.m_ImgpinkGot.visible = (_listItemChose[index] != 0);
  217. UI_ComItem.ProxyEnd();
  218. ClearItemsCountList();
  219. tidyData(_cfgMaxLevel, "Max");
  220. tidyData(_nowCountCanUpLevel);
  221. ReferViewInfo();
  222. ReferSlide();
  223. }
  224. private void ReferViewInfo()
  225. {
  226. _ui.m_txtChooseLevel.SetVar("value1", _nowCountCanUpLevel.ToString()).FlushVars();
  227. _ui.m_txtChooseLevel.SetVar("value2", _countCanUpMaxLevel.ToString()).FlushVars();
  228. _ui.m_txtCost.SetVar("value1", _cumulativeGold.ToString()).FlushVars();
  229. _ui.m_txtCost.SetVar("value2", _goldsHasNum.ToString()).FlushVars();
  230. _listItemObj.Clear();
  231. _ui.m_list.numItems = upgradeCardItemsArr.Count;
  232. }
  233. private async void OnClickBtnUse()
  234. {
  235. bool result = await CardSProxy.UpgradeCardLvl(_cardData.id, itemsCount);
  236. if (result)
  237. {
  238. this.Hide();
  239. if (_cardData.lv - _oldCanUpLevel >= 1)
  240. {
  241. ViewManager.Show<CardUpView>(new object[]
  242. { _cardData.scores, "lv", _oldCanUpLevel, _cardData.lv, _cardData.id });
  243. }
  244. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.CI_PAI, 2);
  245. }
  246. }
  247. private void OnClickBtnReduce()
  248. {
  249. _nowCountCanUpLevel -= 1;
  250. if (_nowCountCanUpLevel < _cardData.lv)
  251. _nowCountCanUpLevel = _cardData.lv;
  252. tidyData(_nowCountCanUpLevel);
  253. ReferViewInfo();
  254. ReferSlide();
  255. }
  256. private void OnClickBtnAdd()
  257. {
  258. _nowCountCanUpLevel += 1;
  259. if (_nowCountCanUpLevel > _countCanUpMaxLevel)
  260. _nowCountCanUpLevel = _countCanUpMaxLevel;
  261. tidyData(_nowCountCanUpLevel);
  262. ReferViewInfo();
  263. ReferSlide();
  264. }
  265. private void CheckGuide(object param)
  266. {
  267. GRoot.inst.touchable = true;
  268. if (GuideDataManager.IsGuideFinish(ConstGuideId.UP_CARD_LV) <= 0)
  269. {
  270. UpdateToCheckGuide(null);
  271. }
  272. else
  273. {
  274. Timers.inst.Remove(CheckGuide);
  275. }
  276. }
  277. protected override void UpdateToCheckGuide(object param)
  278. {
  279. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  280. GuideController.TryGuide(_ui.m_btnUse, ConstGuideId.UP_CARD_LV, 5, "");
  281. }
  282. }
  283. }