MountTrainNestedCtrl.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using FL.Data;
  8. using FL.Network;
  9. using System.Collections.Generic;
  10. using XGame.Database;
  11. using XGame.Framework.UI;
  12. namespace FL.FGUI
  13. {
  14. /// <summary>
  15. /// UI逻辑处理类
  16. /// </summary>
  17. /// <typeparam name=""></typeparam>
  18. public partial class MountTrainNestedCtrl : UIController<MountTrainNestedVM>
  19. {
  20. private bool _bInitData = true;
  21. private int _trainCostItemId; // 驯养需要的道具id
  22. private int _trainCostNum; // 驯养需要的道具数量
  23. private List<MountTrainingAreaNestedView> _trainPartsList; // 训练部位列表
  24. protected override void OnEnable(object intent)
  25. {
  26. AddUIListenres();
  27. AddEventListener();
  28. }
  29. protected override void OnDisable()
  30. {
  31. RemoveUIListenres();
  32. RemoveEventListener();
  33. }
  34. #region UI事件
  35. private void AddUIListenres()
  36. {
  37. VM.TrainBtn.onClick.Add(OnClickTrainBtnBtn);
  38. VM.OneClickTrainBtn.onClick.Add(OnClickOneClickTrainBtn);
  39. }
  40. private void RemoveUIListenres()
  41. {
  42. VM.TrainBtn.onClick.Remove(OnClickTrainBtnBtn);
  43. VM.OneClickTrainBtn.onClick.Remove(OnClickOneClickTrainBtn);
  44. }
  45. private void OnClickTrainBtnBtn(EventContext context)
  46. {
  47. if (ItemService.Instance.IsEnough(_trainCostItemId, _trainCostNum, true))
  48. {
  49. MountService.Instance.SendToTrainingMountPart();
  50. }
  51. }
  52. private void OnClickOneClickTrainBtn(EventContext context)
  53. {
  54. if (ItemService.Instance.IsEnough(_trainCostItemId, _trainCostNum, true))
  55. {
  56. MountService.Instance.SendToOneKeyTraining();
  57. }
  58. }
  59. #endregion
  60. private void AddEventListener()
  61. {
  62. Context.AddListener(EventDefine.UpdataItemData, OnUpdateItemNum);
  63. Context.AddListener(EventDefine.TrainMountSucessful, TrainMountSucessful);
  64. }
  65. private void RemoveEventListener()
  66. {
  67. Context.RemoveListener(EventDefine.UpdataItemData, OnUpdateItemNum);
  68. Context.RemoveListener(EventDefine.TrainMountSucessful, TrainMountSucessful);
  69. }
  70. public void ClearData()
  71. {
  72. _trainPartsList?.Clear();
  73. _trainPartsList = null;
  74. _bInitData = true;
  75. }
  76. private void Init()
  77. {
  78. if (_trainPartsList == null)
  79. {
  80. _trainPartsList = new List<MountTrainingAreaNestedView>() { VM.TrainingTheHead, VM.TrainingTheBody, VM.TrainingFeet };
  81. }
  82. }
  83. public void ShowTrainMountUI(bool bShowSpine)
  84. {
  85. if (_bInitData)
  86. {
  87. Init();
  88. _bInitData = false;
  89. }
  90. int totalLv = 0; // 驯养总等级
  91. var mountsPartsMap = MountData.Instance.MountsPartsMap;
  92. foreach (var item in mountsPartsMap)
  93. {
  94. totalLv += item.Value;
  95. }
  96. VM.TrainTotalLvLabel.text = string.Format(StringDefine.TotalTrainLevel, totalLv);
  97. var curTrainInfo = zuoqiBuweiProbTableRepo.Get(totalLv);
  98. if (curTrainInfo != null)
  99. {
  100. ShowTrainExp(curTrainInfo.Success);
  101. _trainCostItemId = curTrainInfo.LevelUpCost[0];
  102. _trainCostNum = curTrainInfo.LevelUpCost.Length > 1 ? curTrainInfo.LevelUpCost[1] : 1;
  103. VM.TrainRateLabel.text = string.Format(StringDefine.TrainSuccessRate, curTrainInfo.Prob.ToRealFloatPercentage());
  104. }
  105. ShowTrainAttrUI(mountsPartsMap);
  106. if (zuoqiBuweiProbTableRepo.Get(totalLv + 1) == null)
  107. {
  108. ShowMaxUpgradeStep(true);
  109. ShowCostItemNestedUI(false);
  110. }
  111. else
  112. ShowTrainCostUI();
  113. if (bShowSpine)
  114. {
  115. ShowMountSpine(GetMountAppearance(), true);
  116. }
  117. }
  118. /// <summary>
  119. /// 优先获取骑乘的坐骑皮肤,其次获取解锁的最高阶的通用坐骑皮肤
  120. /// </summary>
  121. /// <returns></returns>
  122. private string GetMountAppearance()
  123. {
  124. string mountSpine = MountData.Instance.GetRidingAppearance();
  125. if (string.IsNullOrEmpty(mountSpine))
  126. {
  127. var mountInfo = zuoqiInfoTableRepo.Get(MountData.Instance.UniversalId);
  128. if (mountInfo != null)
  129. mountSpine = mountInfo.Spine;
  130. }
  131. return mountSpine;
  132. }
  133. /// <summary>
  134. /// 坐骑的立绘
  135. /// </summary>
  136. /// <param name="mountPicture"></param>
  137. private void ShowMountSpine(string spineName, bool bShowTraining = false)
  138. {
  139. VM.MountSpine.LoadSpine(spineName, "stand", true);
  140. }
  141. /// <summary>
  142. /// 幸运值
  143. /// </summary>
  144. /// <param name="total"></param>
  145. private void ShowTrainExp(int total)
  146. {
  147. VM.TrainLuckyBar.max = total;
  148. VM.TrainLuckyBar.value = MountData.Instance.MountPartExp;
  149. }
  150. /// <summary>
  151. /// 训练部位等级加成属性
  152. /// </summary>
  153. /// <param name="mountsPartsMap"></param>
  154. private void ShowTrainAttrUI(Dictionary<int, int> mountsPartsMap)
  155. {
  156. for (int i = 0; i < _trainPartsList.Count; i++)
  157. {
  158. _trainPartsList[i].Ctrl.ShowUI(GetTrainInfo(i + 1, mountsPartsMap));
  159. }
  160. }
  161. private TrainParam GetTrainInfo(int part, Dictionary<int, int> mountsPartsMap)
  162. {
  163. int trainLv = 0;
  164. zuoqiBuweiTable trainInfo = null;
  165. if (mountsPartsMap.ContainsKey(part))
  166. {
  167. trainLv = mountsPartsMap[part];
  168. //部位*1000+部位等级
  169. trainInfo = zuoqiBuweiTableRepo.Get(part * 1000 + mountsPartsMap[part]);
  170. }
  171. return new TrainParam()
  172. {
  173. level = trainLv,
  174. trainInfo = trainInfo,
  175. };
  176. }
  177. /// <summary>
  178. /// 坐骑驯养消耗
  179. /// </summary>
  180. private void ShowTrainCostUI()
  181. {
  182. VM.TrainCostItem.Ctrl.ShowCostUI(_trainCostItemId, _trainCostNum);
  183. }
  184. /// <summary>
  185. ///
  186. /// </summary>
  187. /// <param name="bShow"></param>
  188. private void ShowMaxUpgradeStep(bool bShow)
  189. {
  190. VM.MaxUpgradeStep.selectedIndex = bShow ? 1 : 0;
  191. }
  192. /// <summary>
  193. ///
  194. /// </summary>
  195. /// <param name="bShow"></param>
  196. private void ShowCostItemNestedUI(bool bShow)
  197. {
  198. if (bShow)
  199. VM.TrainCostItem.Enable(null);
  200. else
  201. VM.TrainCostItem.Disable();
  202. }
  203. /// <summary>
  204. /// 道具数据变化
  205. /// </summary>
  206. /// <param name="eventId"></param>
  207. /// <param name="args"></param>
  208. private void OnUpdateItemNum(int eventId, object args)
  209. {
  210. var itemDataList = args as List<ActItem>;
  211. if (itemDataList?.Count > 0)
  212. {
  213. for (int i = 0; i < itemDataList.Count; i++)
  214. {
  215. if (itemDataList[i].id == _trainCostItemId)
  216. {
  217. // 通用坐骑驯养消耗
  218. ShowTrainCostUI();
  219. }
  220. }
  221. }
  222. }
  223. /// <summary>
  224. /// 坐骑训练成功
  225. /// </summary>
  226. /// <param name="eventId"></param>
  227. /// <param name="args"></param>
  228. private void TrainMountSucessful(int eventId, object args)
  229. {
  230. ShowTrainMountUI(false);
  231. }
  232. }
  233. }