HeroController.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System.Collections.Generic;
  2. using Common.Utility.CombatEvent;
  3. using Core.Utility;
  4. using Fort23.Core;
  5. using Fort23.UTool;
  6. using GameLogic.Bag;
  7. namespace GameLogic.Hero
  8. {
  9. public class HeroController
  10. {
  11. /// <summary>
  12. /// 所有英雄
  13. /// </summary>
  14. public Dictionary<int, HeroInfo> allHeroDic = new Dictionary<int, HeroInfo>();
  15. /// <summary>
  16. /// 也可以叫主力(afk的共鸣英雄),替补英雄等级=主力英雄的最低等级(和afk一样的)
  17. /// 主力英雄不一定等于上阵英雄,主力英雄只是决定了共鸣等级
  18. /// </summary>
  19. public Dictionary<int, HeroInfo> heroDicInLead = new Dictionary<int, HeroInfo>(4);
  20. /// <summary>
  21. /// 后备英雄(共享等级)
  22. /// </summary>
  23. public Dictionary<int, HeroInfo> heroDicInBack = new Dictionary<int, HeroInfo>();
  24. public int mainLevel => m_MainLevel;
  25. private int m_MainLevel;
  26. private void CalMainLevel()
  27. {
  28. //主力英雄不足4个时,就没有主力等级的说法
  29. if (heroDicInLead.Count < GlobalParam.Max_Deploy_HERO)
  30. {
  31. m_MainLevel = -1;
  32. return;
  33. }
  34. //计算主力英雄的最低等级
  35. int tmpLv = 9999;
  36. foreach (var keyValuePair in heroDicInLead)
  37. {
  38. if (keyValuePair.Value.level.Value < tmpLv)
  39. {
  40. tmpLv = keyValuePair.Value.level.Value;
  41. }
  42. }
  43. m_MainLevel = tmpLv;
  44. }
  45. public HeroUpResultType CanUpgrade(HeroInfo heroInfo)
  46. {
  47. if (PlayerManager.Instance.gameConstantConfig.maxLv <= heroInfo.level.Value)
  48. {
  49. LogTool.Log("已到达最高等级:" + PlayerManager.Instance.gameConstantConfig.maxLv);
  50. return HeroUpResultType.MaxLv;
  51. }
  52. //计算主力英雄等级差, 如果再升一级,就大于最大等级差了,就不允许升了.
  53. if ((heroInfo.level.Value) - m_MainLevel >= GlobalParam.Max_Main_Level_Difference)
  54. {
  55. LogTool.Log(heroInfo.modelID + "主力英雄等级差不能超过:" + GlobalParam.Max_Main_Level_Difference);
  56. return HeroUpResultType.MainLevelNotEnough;
  57. }
  58. if (BagController.Instance.IsEnough(GlobalParam.Item_HeroExp_ID, heroInfo.powerUpConfig.levelUpExp))
  59. {
  60. return HeroUpResultType.Success;
  61. }
  62. else
  63. {
  64. return HeroUpResultType.ExpNotEnough;
  65. }
  66. }
  67. /// <summary>
  68. /// 英雄升级逻辑,主要是改数据,不在这里做UI表现
  69. /// </summary>
  70. /// <param name="heroInfo"></param>
  71. public HeroUpResultType UpgradeHeroLogic(HeroInfo heroInfo)
  72. {
  73. HeroUpResultType resultType = CanUpgrade(heroInfo);
  74. if (resultType == HeroUpResultType.Success &&
  75. BagController.Instance.DuctHeroExp(heroInfo.powerUpConfig.levelUpExp))
  76. {
  77. heroInfo.Upgrade();
  78. //如果当前英雄等级等于主力等级,则升级时需要重新计算主力等级
  79. if (heroInfo.level.Value - 1 == m_MainLevel)
  80. {
  81. CalMainLevel();
  82. }
  83. SendEvent(HeroUpType.Level, heroInfo, HeroUpResultType.Success);
  84. return HeroUpResultType.Success;
  85. }
  86. return resultType;
  87. }
  88. /// <summary>
  89. /// 发送英雄提升的事件
  90. /// </summary>
  91. /// <param name="upType">提升类型:升级、升星等.</param>
  92. private void SendEvent(HeroUpType upType, HeroInfo heroInfo, HeroUpResultType resultType = HeroUpResultType.None)
  93. {
  94. HeroPowerUpEventData data = new HeroPowerUpEventData();
  95. data.heroModelID = heroInfo.modelID;
  96. data.upType = upType;
  97. data.ResultType = resultType;
  98. PlayerManager.Instance.lastHeroInfo = heroInfo;
  99. EventManager.Instance.Dispatch(CustomEventType.HeroPowerUp, data);
  100. if (upType == HeroUpType.Level)
  101. {
  102. EventManager.Instance.Dispatch(CustomEventType.HeroLvUp, data);
  103. }
  104. else if (upType == HeroUpType.Promote)
  105. {
  106. EventManager.Instance.Dispatch(CustomEventType.HeroPromote, data);
  107. }
  108. }
  109. public void InitHeroes()
  110. {
  111. for (int i = 0; i < AccountFileInfo.Instance.playerData.HeroListData.Count; i++)
  112. {
  113. AccountFileInfo.HeroData heroData = AccountFileInfo.Instance.playerData.HeroListData[i];
  114. HeroInfo heroInfo = new HeroInfo();
  115. heroInfo.InitHero(heroData);
  116. AddHero(heroInfo);
  117. }
  118. CalMainLevel();
  119. }
  120. /// <summary>
  121. /// 获取英雄
  122. /// </summary>
  123. /// <param name="modelID">英雄ID modelID</param>
  124. /// <returns></returns>
  125. public CombatHeroInfo GetHeroInfo(int modelID)
  126. {
  127. if (allHeroDic.ContainsKey(modelID))
  128. {
  129. return allHeroDic[modelID];
  130. }
  131. LogTool.Error("没有这个英雄" + modelID);
  132. return null;
  133. }
  134. /// <summary>
  135. /// 添加英雄
  136. /// </summary>
  137. /// <param name="heroInfo"></param>
  138. public void AddHero(HeroInfo heroInfo)
  139. {
  140. if (heroInfo.isLead)
  141. {
  142. DeployHeroToLead(heroInfo);
  143. }
  144. else
  145. {
  146. DeployHeroToBack(heroInfo);
  147. }
  148. }
  149. /// <summary>
  150. /// 进入主力队伍
  151. /// </summary>
  152. /// <param name="heroInfo"></param>
  153. public void DeployHeroToLead(HeroInfo heroInfo)
  154. {
  155. if (heroDicInLead.Count >= GlobalParam.Max_Deploy_HERO)
  156. {
  157. return;
  158. }
  159. if (heroDicInLead.ContainsKey(heroInfo.modelID))
  160. {
  161. LogTool.Error("heroDicInLead不应该出现相同的英雄ID=" + heroInfo.modelID);
  162. return;
  163. }
  164. heroDicInLead.Add(heroInfo.modelID, heroInfo);
  165. heroInfo.isLead = true;
  166. allHeroDic.Add(heroInfo.modelID, heroInfo);
  167. }
  168. /// <summary>
  169. /// 更换主力
  170. /// </summary>
  171. /// <param name="heroInfo"></param>
  172. public void ChangeLeadHero(HeroInfo backHero, HeroInfo leadHero)
  173. {
  174. if (!heroDicInLead.ContainsKey(leadHero.modelID))
  175. {
  176. LogTool.Error("不是主力" + leadHero.modelID);
  177. return;
  178. }
  179. if (!heroDicInBack.ContainsKey(backHero.modelID))
  180. {
  181. LogTool.Error("不是替补" + backHero.modelID);
  182. return;
  183. }
  184. //从主力中移除,并加入后补
  185. heroDicInLead.Remove(leadHero.modelID);
  186. DeployHeroToBack(leadHero);
  187. //从后补中移除,并加入主力
  188. heroDicInBack.Remove(backHero.modelID);
  189. DeployHeroToLead(backHero);
  190. }
  191. /// <summary>
  192. /// 进入后备队伍(英雄背包)
  193. /// </summary>
  194. /// <param name="heroInfo"></param>
  195. public void DeployHeroToBack(HeroInfo heroInfo)
  196. {
  197. if (heroDicInBack.ContainsKey(heroInfo.modelID))
  198. {
  199. LogTool.Error("heroDicInBack不应该出现相同的英雄ID=" + heroInfo.modelID);
  200. return;
  201. }
  202. heroDicInBack.Add(heroInfo.modelID, heroInfo);
  203. heroInfo.isLead = false;
  204. allHeroDic.Add(heroInfo.modelID, heroInfo);
  205. }
  206. }
  207. }