|  |  | 
 |  |  | using System.Collections.Generic; | 
 |  |  | using UnityEngine; | 
 |  |  | using System; | 
 |  |  | using System.Linq; | 
 |  |  |  | 
 |  |  | public class HeroManager : GameSystemManager<HeroManager> | 
 |  |  | { | 
 |  |  |     //武将和图鉴的关系 | 
 |  |  |     //同一个武将图鉴,可能有多个武将(物品)数据,或者没有武将,但是有图鉴信息 | 
 |  |  |     //初始创建(0725),后续跟随背包事件增加删除  key = guid | 
 |  |  |     protected Dictionary<string, HeroInfo> heroInfoDict = new Dictionary<string, HeroInfo>(); | 
 |  |  |  | 
 |  |  |     //武将列表界面 | 
 |  |  |     public List<string> heroSortList = new List<string>(); | 
 |  |  |  | 
 |  |  |     public Action<HeroInfo> onNewHeroEvent; | 
 |  |  |  | 
 |  |  | 
 |  |  |     { | 
 |  |  |         base.Init(); | 
 |  |  |  | 
 |  |  |         //  注册一点事件 | 
 |  |  |         PackManager.Instance.refrechPackEvent += refrechPackEvent; | 
 |  |  |         PackManager.Instance.CreateItemEvent += CreateHero; | 
 |  |  |         PackManager.Instance.DeleteItemEvent += DeleteHero; | 
 |  |  |         DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public override void Release() | 
 |  |  |     { | 
 |  |  |         base.Release(); | 
 |  |  |         PackManager.Instance.refrechPackEvent -= refrechPackEvent; | 
 |  |  |         PackManager.Instance.CreateItemEvent -= CreateHero; | 
 |  |  |         PackManager.Instance.DeleteItemEvent -= DeleteHero; | 
 |  |  |         DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent -= OnBeforePlayerDataInitialize; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public override void RequestNessaryData() | 
 |  |  |     void OnBeforePlayerDataInitialize() | 
 |  |  |     { | 
 |  |  |         base.RequestNessaryData(); | 
 |  |  |  | 
 |  |  |          | 
 |  |  |         heroSortList.Clear(); | 
 |  |  |         heroInfoDict.Clear(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void refrechPackEvent(PackType packType) | 
 |  |  |     void refrechPackEvent(PackType packType) | 
 |  |  |     { | 
 |  |  |         if (packType == PackType.Hero) | 
 |  |  |         { | 
 |  |  |             // bool change = false; | 
 |  |  |             bool isNew = false; | 
 |  |  |             //  如果有分批次初始化可能还要麻烦一点?先看看 | 
 |  |  |             bool isInit = heroInfoDict.Count >= 0; | 
 |  |  |             InitHerosInfo(); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |             SinglePack heroPack = PackManager.Instance.GetSinglePack(packType); | 
 |  |  |  | 
 |  |  |             Dictionary<int, ItemModel> heroes = heroPack.GetAllItems(); | 
 |  |  |  | 
 |  |  |             foreach (ItemModel hero in heroes.Values) | 
 |  |  |     void CreateHero(PackType packType, string guid) | 
 |  |  |     { | 
 |  |  |         if (packType == PackType.Hero) | 
 |  |  |         { | 
 |  |  |             HeroInfo heroInfo = null; | 
 |  |  |             if (!heroInfoDict.TryGetValue(guid, out heroInfo)) | 
 |  |  |             { | 
 |  |  |                 HeroInfo heroInfo = null; | 
 |  |  |                 if (!heroInfoDict.TryGetValue(hero.guid, out heroInfo)) | 
 |  |  |                 { | 
 |  |  |                     heroInfo = new HeroInfo(hero); | 
 |  |  |                     heroInfoDict.Add(hero.guid, heroInfo); | 
 |  |  |                     if (!isInit) | 
 |  |  |                     { | 
 |  |  |                         isNew = true; | 
 |  |  |                         onNewHeroEvent?.Invoke(heroInfo); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 else | 
 |  |  |                 { | 
 |  |  |                     heroInfo.UpdateHero(hero); | 
 |  |  |                     //  也有可能不是change | 
 |  |  |                     // change = true; | 
 |  |  |                     // onHeroChangeEvent?.Invoke(heroInfo); | 
 |  |  |                 } | 
 |  |  |                 heroInfo = new HeroInfo(PackManager.Instance.GetItemByGuid(guid)); | 
 |  |  |                 heroInfoDict.Add(guid, heroInfo); | 
 |  |  |             } | 
 |  |  |             else | 
 |  |  |             { | 
 |  |  |                 heroInfo.UpdateHero(PackManager.Instance.GetItemByGuid(guid)); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     void DeleteHero(PackType packType, string guid) | 
 |  |  |     { | 
 |  |  |         if (packType == PackType.Hero) | 
 |  |  |         { | 
 |  |  |             if (heroInfoDict.ContainsKey(guid)) | 
 |  |  |             { | 
 |  |  |                 heroInfoDict.Remove(guid); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public HeroInfo GetHero(string guid) | 
 |  |  |     { | 
 |  |  |         if (!heroInfoDict.ContainsKey(guid)) | 
 |  |  |             return null; | 
 |  |  |         return heroInfoDict[guid]; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     void InitHerosInfo() | 
 |  |  |     {    | 
 |  |  |         heroInfoDict.Clear(); | 
 |  |  |         SinglePack heroPack = PackManager.Instance.GetSinglePack(PackType.Hero); | 
 |  |  |         Dictionary<int, ItemModel> heroes = heroPack.GetAllItems(); | 
 |  |  |  | 
 |  |  |         foreach (ItemModel hero in heroes.Values) | 
 |  |  |         { | 
 |  |  |             CreateHero(hero.packType, hero.guid); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |     { | 
 |  |  |         List<HeroInfo> heroList = new List<HeroInfo>(heroInfoDict.Values); | 
 |  |  |  | 
 |  |  |         heroList.Sort((a, b) =>  | 
 |  |  |         heroList.Sort((a, b) => | 
 |  |  |         { | 
 |  |  |             int power1 = a.CalculatePower(); | 
 |  |  |             int power2 = b.CalculatePower(); | 
 |  |  | 
 |  |  |         return retList; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     // public override bool IsNessaryDataReady() | 
 |  |  |     // { | 
 |  |  |     //     return true; | 
 |  |  |     // } | 
 |  |  |     public bool waitResortHeroList = false; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     //刷新时机, 打开武将界面 或者 关闭功能界面 | 
 |  |  |     public void SortHeroList() | 
 |  |  |     { | 
 |  |  |         heroSortList = heroInfoDict.Keys.ToList(); | 
 |  |  |         heroSortList.Sort(CmpHero); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     int CmpHero(string guidA, string guidB) | 
 |  |  |     { | 
 |  |  |         HeroInfo heroA = heroInfoDict[guidA]; | 
 |  |  |         HeroInfo heroB = heroInfoDict[guidB]; | 
 |  |  |         if (heroA == null || heroB == null) | 
 |  |  |         { | 
 |  |  |             return 0; | 
 |  |  |         } | 
 |  |  |         // 排序规则:上阵>武将等级>突破等级>武将觉醒阶级>武将品质>武将吞噬星级>武将ID | 
 |  |  |         if (heroA.isInMainBattle != heroB.isInMainBattle) | 
 |  |  |         { | 
 |  |  |             return heroA.isInMainBattle ? -1 : 1; | 
 |  |  |         } | 
 |  |  |         if (heroA.heroLevel != heroB.heroLevel) | 
 |  |  |         { | 
 |  |  |             return heroA.heroLevel > heroB.heroLevel ? -1 : 1; | 
 |  |  |         } | 
 |  |  |         if (heroA.breakLevel != heroB.breakLevel) | 
 |  |  |         { | 
 |  |  |             return heroA.breakLevel > heroB.breakLevel ? -1 : 1; | 
 |  |  |         } | 
 |  |  |         if (heroA.awakeLevel != heroB.awakeLevel) | 
 |  |  |         { | 
 |  |  |             return heroA.awakeLevel > heroB.awakeLevel ? -1 : 1; | 
 |  |  |         } | 
 |  |  |         if (heroA.Quality != heroB.Quality) | 
 |  |  |         { | 
 |  |  |             return heroA.Quality > heroB.Quality ? -1 : 1; | 
 |  |  |         } | 
 |  |  |         if (heroA.heroStar != heroA.heroStar) | 
 |  |  |         { | 
 |  |  |             return heroA.heroStar > heroB.heroStar ? -1 : 1; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return heroA.heroId.CompareTo(heroB.heroId); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |