yyl
2025-10-17 c124d98bdf9659cf764bebb799bee42c30eb152f
Main/System/Hero/HeroManager.cs
@@ -1,32 +1,155 @@
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
public class HeroManager : GameSystemManager<HeroManager>
{
    protected Dictionary<long, HeroInfo> heroInfoDict = new Dictionary<long, HeroInfo>();
    //武将和图鉴的关系
    //同一个武将图鉴,可能有多个武将(物品)数据,或者没有武将,但是有图鉴信息
    //初始创建(0725),后续跟随背包事件增加删除  key = guid
    protected Dictionary<string, HeroInfo> heroInfoDict = new Dictionary<string, HeroInfo>();
    public Action<HeroInfo> onHeroChangeEvent;
    public Action<int> onHeroDeleteEvent;
    public override void Init()
    {
        base.Init();
        //  注册一点事件
        PackManager.Instance.ChangeItemEvent += ChangeHero;
        PackManager.Instance.DeleteItemEvent += DeleteHero;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize;
    }
    public override void Release()
    {
        base.Release();
        PackManager.Instance.ChangeItemEvent -= ChangeHero;
        PackManager.Instance.DeleteItemEvent -= DeleteHero;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent -= OnBeforePlayerDataInitialize;
    }
    public override void RequestNessaryData()
    void OnBeforePlayerDataInitialize()
    {
        base.RequestNessaryData();
        heroInfoDict.Clear();
    }
    // public override bool IsNessaryDataReady()
    // {
    //     return true;
    // }
    // isCreate bool:true代表创建 false 刷新触发
    void ChangeHero(PackType packType, string guid, bool isCreate)
    {
        if (packType == PackType.Hero)
        {
            HeroInfo heroInfo = null;
            if (!heroInfoDict.TryGetValue(guid, out heroInfo))
            {
                heroInfo = new HeroInfo(PackManager.Instance.GetItemByGuid(guid));
                heroInfoDict.Add(guid, heroInfo);
            }
            else
            {
                heroInfo.UpdateHero(PackManager.Instance.GetItemByGuid(guid));
            }
            onHeroChangeEvent?.Invoke(heroInfo);
        }
    }
    void DeleteHero(PackType packType, string guid, int itemID, int index, int clearType)
    {
        if (packType == PackType.Hero)
        {
            HeroInfo heroInfo = null;
            heroInfoDict.TryGetValue(guid, out heroInfo);
            heroInfoDict.Remove(guid);
            onHeroDeleteEvent?.Invoke(itemID);
        }
    }
    public HeroInfo GetHero(string guid)
    {
        if (!heroInfoDict.ContainsKey(guid))
            return null;
        return heroInfoDict[guid];
    }
    public List<HeroInfo> GetHeroList()
    {
        return heroInfoDict.Values.ToList();
    }
    public List<string> GetHeroGuidList(int job = 0, int country = 0)
    {
        if (job == 0 && country == 0)
            return heroInfoDict.Keys.ToList();
        List<string> retGuidList = new List<string>();
        foreach (string guid in heroInfoDict.Keys)
        {
            HeroInfo heroInfo = heroInfoDict[guid];
            //0代表全部
            if (job == 0 || country == 0)
            {
                if (job != 0 && job == heroInfo.heroConfig.Class)
                    retGuidList.Add(guid);
                if (country != 0 && country == heroInfo.heroConfig.Country)
                    retGuidList.Add(guid);
            }
            else
            {
                if (job == heroInfo.heroConfig.Class && country == heroInfo.heroConfig.Country)
                    retGuidList.Add(guid);
            }
        }
        return retGuidList;
    }
    public List<HeroInfo> GetPowerfulHeroList()
    {
        List<HeroInfo> heroList = new List<HeroInfo>(heroInfoDict.Values);
        heroList.Sort((a, b) =>
        {
            long power1 = a.CalculatePower(false);
            long power2 = b.CalculatePower(false);
            if (power1 == power2)
            {
                return 0;
            }
            return power1 > power2 ? -1 : 1;
        });
        List<HeroInfo> retList = new List<HeroInfo>();
        for (int i = 0; i < TeamConst.MaxTeamHeroCount && i < heroList.Count; i++)
        {
            retList.Add(heroList[i]);
        }
        return retList;
    }
    public int GetHeroCount()
    {
        return heroInfoDict.Count;
    }
    public bool HasHero(int heroID)
    {
        return PackManager.Instance.GetSinglePack(PackType.Hero).HasItem(heroID);
    }
    public int GetHeroCountByID(int heroID)
    {
        return (int)PackManager.Instance.GetSinglePack(PackType.Hero).GetCountById(heroID);
    }
}