HeroController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System.Collections.Generic;
  2. using Common.Utility.CombatEvent;
  3. using Core.Utility;
  4. using Excel2Json;
  5. using Fort23.Core;
  6. using Fort23.UTool;
  7. using GameLogic.Bag;
  8. namespace GameLogic.Hero
  9. {
  10. public class HeroController
  11. {
  12. /// <summary>
  13. /// 所有英雄
  14. /// </summary>
  15. public Dictionary<int, HeroInfo> allHeroDic = new Dictionary<int, HeroInfo>();
  16. /// <summary>
  17. /// 也可以叫主力(afk的共鸣英雄),替补英雄等级=主力英雄的最低等级(和afk一样的)
  18. /// 主力英雄不一定等于上阵英雄,主力英雄只是决定了共鸣等级
  19. /// </summary>
  20. public Dictionary<int, HeroInfo> heroDicInLead = new Dictionary<int, HeroInfo>(4);
  21. /// <summary>
  22. /// 后备英雄(共享等级)
  23. /// </summary>
  24. public Dictionary<int, HeroInfo> heroDicInBack = new Dictionary<int, HeroInfo>();
  25. public int mainLevel => m_MainLevel;
  26. private int m_MainLevel;
  27. private void CalMainLevel()
  28. {
  29. //计算主力英雄的最低等级
  30. int tmpLv = 9999;
  31. foreach (var keyValuePair in heroDicInLead)
  32. {
  33. if (keyValuePair.Value.level.Value < tmpLv)
  34. {
  35. tmpLv = keyValuePair.Value.level.Value;
  36. }
  37. }
  38. m_MainLevel = tmpLv;
  39. EventManager.Instance.Dispatch(CustomEventType.MainLvUp, new SimpleEventData(){intData = m_MainLevel});
  40. }
  41. public HeroUpResultType CanPromote(HeroInfo heroInfo)
  42. {
  43. long curHeroCount = BagController.Instance.GetItemInfo(heroInfo.modelConfig.itemID).count.Value;
  44. int costHeroCount = heroInfo.promoteConfig.costCount;
  45. if (curHeroCount < costHeroCount)
  46. {
  47. return HeroUpResultType.ResNotEnough;
  48. }
  49. return HeroUpResultType.Success;
  50. }
  51. public HeroUpResultType CanUpgrade(HeroInfo heroInfo, bool isOpenPre = false)
  52. {
  53. if (PlayerManager.Instance.gameConstantConfig.maxLv <= heroInfo.level.Value)
  54. {
  55. LogTool.Log("已到达最高等级:" + PlayerManager.Instance.gameConstantConfig.maxLv);
  56. return HeroUpResultType.MaxLv;
  57. }
  58. //计算主力英雄等级差, 如果再升一级,就大于最大等级差了,就不允许升了.
  59. if (heroInfo.level.Value - m_MainLevel >= GlobalParam.Max_Main_Level_Difference)
  60. {
  61. LogTool.Log(heroInfo.modelID + "主力英雄等级差不能超过:" + GlobalParam.Max_Main_Level_Difference);
  62. return HeroUpResultType.MainLevelNotEnough;
  63. }
  64. //每10级,要打开预览界面后,再升级(如果已经打开,就不判断了)
  65. if (!isOpenPre && (heroInfo.level.Value + 1) % 10 == 1)
  66. {
  67. return HeroUpResultType.NeedOpenPreUI;
  68. }
  69. if (BagController.Instance.IsEnough(GlobalParam.Item_HeroExp_ID, heroInfo.powerUpConfig.levelUpExp))
  70. {
  71. if (isOpenPre)
  72. {
  73. return HeroUpResultType.PlayUpgradeEftFirst;
  74. }
  75. return HeroUpResultType.Success;
  76. }
  77. else
  78. {
  79. return HeroUpResultType.ExpNotEnough;
  80. }
  81. }
  82. private void DoUpgrade(HeroInfo heroInfo)
  83. {
  84. heroInfo.Upgrade();
  85. PlayerManager.Instance.SaveHeroData(heroInfo);
  86. //如果当前英雄等级等于主力等级,则升级时需要重新计算主力等级
  87. if (heroInfo.level.Value - 1 == m_MainLevel)
  88. {
  89. CalMainLevel();
  90. }
  91. // SendEvent(HeroUpType.Level, heroInfo, HeroUpResultType.Success);
  92. }
  93. public HeroUpResultType PromoteHeroLogic(HeroInfo heroInfo)
  94. {
  95. HeroUpResultType resultType = CanPromote(heroInfo);
  96. if (resultType == HeroUpResultType.Success)
  97. {
  98. //扣除碎片
  99. bool isOk = BagController.Instance.DeductItem(heroInfo.modelConfig.itemID, heroInfo.promoteConfig.costCount);
  100. if (isOk)
  101. {
  102. if (resultType == HeroUpResultType.Success)
  103. {
  104. heroInfo.Promote();
  105. PlayerManager.Instance.SaveHeroData(heroInfo);
  106. // SendEvent(HeroUpType.Promote, heroInfo, resultType);
  107. }
  108. }
  109. }
  110. return resultType;
  111. }
  112. /// <summary>
  113. /// 英雄升级逻辑,主要是改数据,不在这里做UI表现
  114. /// </summary>
  115. /// <param name="heroInfo"></param>
  116. public HeroUpResultType UpgradeHeroLogic(HeroInfo heroInfo, bool isOpenPre = false)
  117. {
  118. HeroUpResultType resultType = CanUpgrade(heroInfo, isOpenPre);
  119. if (resultType == HeroUpResultType.Success || resultType == HeroUpResultType.PlayUpgradeEftFirst)
  120. {
  121. //扣除经验
  122. bool isOk = BagController.Instance.DuctHeroExp(heroInfo.powerUpConfig.levelUpExp);
  123. if (isOk)
  124. {
  125. if (resultType == HeroUpResultType.Success)
  126. {
  127. DoUpgrade(heroInfo);
  128. }
  129. else if (resultType == HeroUpResultType.PlayUpgradeEftFirst)
  130. {
  131. //执行逻辑,返回播放特效的类型
  132. DoUpgrade(heroInfo);
  133. return resultType;
  134. }
  135. return HeroUpResultType.Success;
  136. }
  137. }
  138. return resultType;
  139. }
  140. // /// <summary>
  141. // /// 发送英雄提升的事件
  142. // /// </summary>
  143. // /// <param name="upType">提升类型:升级、升星等.</param>
  144. // public void SendEvent(HeroUpType upType, HeroInfo heroInfo, HeroUpResultType resultType = HeroUpResultType.None)
  145. // {
  146. // HeroPowerUpEventData data = new HeroPowerUpEventData();
  147. // data.heroModelID = heroInfo.modelID;
  148. // data.upType = upType;
  149. // data.ResultType = resultType;
  150. //
  151. // PlayerManager.Instance.lastHeroInfo = heroInfo;
  152. //
  153. // if (upType == HeroUpType.Level)
  154. // {
  155. // SkillUpConfig upConfig = PlayerManager.Instance.heroController
  156. // .GetSkillUpConfig4Lv(heroInfo.level.Value);
  157. //
  158. //
  159. // if (upConfig.ID > 0)
  160. // {
  161. // heroInfo.SkillData.UpdateSkills();
  162. // data.isSkillUp = true;
  163. // }
  164. //
  165. // EventManager.Instance.Dispatch(CustomEventType.HeroLvUp, data);
  166. // }
  167. // else if (upType == HeroUpType.Promote)
  168. // {
  169. // // SkillUpConfig upConfig = PlayerManager.Instance.heroController
  170. // // .IsStarUpGetNewSkill(heroInfo.star.Value);
  171. //
  172. // // data.isSkillUp = upConfig.ID > 0;
  173. //
  174. // // SkillUpConfig upConfig = PlayerManager.Instance.heroController.GetSkillUpConfig4Star(heroInfo.star.Value);
  175. // //
  176. // // if (upConfig.ID > 0)
  177. // // {
  178. // // heroInfo.SkillData.UpdateSkills();
  179. // // data.isSkillUp = true;
  180. // // }
  181. //
  182. // EventManager.Instance.Dispatch(CustomEventType.HeroPromote, data);
  183. // }
  184. // EventManager.Instance.Dispatch(CustomEventType.HeroPowerUp, data);
  185. // }
  186. //
  187. public void InitHeroes()
  188. {
  189. CalMainLevel();
  190. }
  191. /// <summary>
  192. /// 获取英雄
  193. /// </summary>
  194. /// <param name="modelID">英雄ID modelID</param>
  195. /// <returns></returns>
  196. public CombatHeroInfo GetHeroInfo(int modelID)
  197. {
  198. if (allHeroDic.ContainsKey(modelID))
  199. {
  200. return allHeroDic[modelID];
  201. }
  202. LogTool.Error("没有这个英雄" + modelID);
  203. return null;
  204. }
  205. /// <summary>
  206. /// 添加英雄
  207. /// </summary>
  208. /// <param name="heroInfo"></param>
  209. public void AddHero(HeroInfo heroInfo)
  210. {
  211. if (heroInfo.isLead)
  212. {
  213. DeployHeroToLead(heroInfo);
  214. }
  215. else
  216. {
  217. DeployHeroToBack(heroInfo);
  218. }
  219. }
  220. /// <summary>
  221. /// 进入主力队伍
  222. /// </summary>
  223. /// <param name="heroInfo"></param>
  224. public void DeployHeroToLead(HeroInfo heroInfo)
  225. {
  226. if (heroDicInLead.Count >= GlobalParam.Max_Deploy_HERO)
  227. {
  228. return;
  229. }
  230. if (heroDicInLead.ContainsKey(heroInfo.modelID))
  231. {
  232. LogTool.Error("heroDicInLead不应该出现相同的英雄ID=" + heroInfo.modelID);
  233. return;
  234. }
  235. heroDicInLead.Add(heroInfo.modelID, heroInfo);
  236. heroInfo.isLead = true;
  237. allHeroDic.Add(heroInfo.modelID, heroInfo);
  238. CalMainLevel();
  239. }
  240. /// <summary>
  241. /// 更换主力
  242. /// </summary>
  243. /// <param name="heroInfo"></param>
  244. public void ChangeLeadHero(HeroInfo backHero, HeroInfo leadHero)
  245. {
  246. if (!heroDicInLead.ContainsKey(leadHero.modelID))
  247. {
  248. LogTool.Error("不是主力" + leadHero.modelID);
  249. return;
  250. }
  251. if (!heroDicInBack.ContainsKey(backHero.modelID))
  252. {
  253. LogTool.Error("不是替补" + backHero.modelID);
  254. return;
  255. }
  256. //从主力中移除,并加入后补
  257. heroDicInLead.Remove(leadHero.modelID);
  258. DeployHeroToBack(leadHero);
  259. //从后补中移除,并加入主力
  260. heroDicInBack.Remove(backHero.modelID);
  261. DeployHeroToLead(backHero);
  262. }
  263. /// <summary>
  264. /// 进入后备队伍(英雄背包)
  265. /// </summary>
  266. /// <param name="heroInfo"></param>
  267. public void DeployHeroToBack(HeroInfo heroInfo)
  268. {
  269. if (heroDicInBack.ContainsKey(heroInfo.modelID))
  270. {
  271. LogTool.Error("heroDicInBack不应该出现相同的英雄ID=" + heroInfo.modelID);
  272. return;
  273. }
  274. heroDicInBack.Add(heroInfo.modelID, heroInfo);
  275. heroInfo.isLead = false;
  276. allHeroDic.Add(heroInfo.modelID, heroInfo);
  277. }
  278. }
  279. }