hch
2025-11-20 ed98029a88cd89702980ac7c40b711afddc5aeb2
Main/System/Battle/BattleManager.cs
@@ -2,6 +2,7 @@
using UnityEngine;
using LitJson;
using System;
using System.Linq;
public class BattleManager : GameSystemManager<BattleManager>
{
@@ -23,10 +24,17 @@
            QuickSetting.Instance.SendPackage();
        }
    }
    public readonly int[] speedIndexfuncIdArr = new int[] { 34, 35, 36 };  // 战斗倍数对应的功能ID
    public readonly int passFuncId = 33;    // 跳过战斗对应的功能ID
    public int passRound;   // 超过X回合可跳过
    public int fightGuideID;
    public int fightGuideMainLevelLimit;
    public int fightGuideNoClickSeconds;
    public int[] challengeBossGuides;
    public Action<string, BattleField> onBattleFieldCreate;
    public Action<string, BattleField> onBattleFieldDestroy;
    public Action onBattleFieldDestroy;
    public bool isWaitServerStory = false;  //主线等服务端回报 0425
@@ -45,6 +53,15 @@
    {
        var config = FuncConfigConfig.Get("AutoGuaji");
        speedGear = JsonMapper.ToObject<float[]>(config.Numerical4);
        config = FuncConfigConfig.Get("FightGuide");
        fightGuideID = int.Parse(config.Numerical1);
        fightGuideMainLevelLimit = int.Parse(config.Numerical2);
        fightGuideNoClickSeconds = int.Parse(config.Numerical3);
        challengeBossGuides = JsonMapper.ToObject<int[]>(config.Numerical4);
        config = FuncConfigConfig.Get("BattleButton");
        passRound= int.Parse(config.Numerical1);
    }
@@ -58,8 +75,8 @@
    protected void OnPlayerLoginOk()
    {
        long exAttr1 = PlayerDatas.Instance.GetPlayerDataByType(PlayerDataType.ExAttr1);
        long exAttr2 = PlayerDatas.Instance.GetPlayerDataByType(PlayerDataType.ExAttr2);
        long exAttr1 = PlayerDatas.Instance.baseData.ExAttr1;
        long exAttr2 = PlayerDatas.Instance.baseData.ExAttr2;
        int MapID = 1;
        int FuncLineID = (int)exAttr2;
@@ -84,7 +101,11 @@
            redTeamList.Add(storyTeam);
            CreateBattleField(string.Empty, MapID, FuncLineID, extendData, redTeamList, blueTeamList);
            HB424_tagSCTurnFightInit vNetData = new HB424_tagSCTurnFightInit();
            vNetData.MapID = (uint)MapID;
            vNetData.FuncLineID = (uint)FuncLineID;
            CreateBattleField(string.Empty, vNetData, extendData, redTeamList, blueTeamList);
        }
        else
        {
@@ -126,6 +147,7 @@
        {
            if (!allow)
            {
                _package.socketType = ServerType.MainFight;
                packQueue.Enqueue(_package);
            }
        }
@@ -148,6 +170,7 @@
        List<GameNetPackBasic> newPackList = ParseBattlePackList(string.Empty, packQueueSnapshot);
#if UNITY_EDITOR
        Action printNewPack = () =>
        {
            string temp = "After AnalysisPackQueueAndDistribute newPackList count: " + newPackList.Count + "\n";
@@ -170,7 +193,7 @@
        };
        printNewPack();
#endif
        // HashSet<int> skipIndexes = new HashSet<int>();
        // // 这里已经是按照Dequeue的顺序了
@@ -256,12 +279,12 @@
    public static List<GameNetPackBasic> ParseBattlePackList(string guid, List<GameNetPackBasic> packQueueSnapshot)
    {
        var list = CustomHB426CombinePack.CombineToSkillPackFromList(guid, packQueueSnapshot);
        string str = "ParseBattlePackList \n";
        for (int i = 0; i < list.Count; i++)
        {
            str += "  " + list[i].GetType().Name + "\n";
        }
        BattleDebug.LogError(str);
        // string str = "ParseBattlePackList \n";
        // for (int i = 0; i < list.Count; i++)
        // {
        //     str += "  " + list[i].GetType().Name + "\n";
        // }
        // BattleDebug.LogError(str);
        return list;
    }
@@ -380,11 +403,18 @@
        {
            uidList.Add(packUID);
        }
    }
    public BattleField GetBattleField(ulong packUID)
    {
        return GetBattleField(GetGUID(packUID));
        string guid = GetGUID(packUID);
        BattleField battleField = GetBattleField(GetGUID(packUID));
        if (battleField == null || battleField.rejectNewPackage)
        {
            return null;
        }
        return battleField;
    }
    public BattleField GetBattleField(string guid)
@@ -417,6 +447,11 @@
            return;
        }
        if (queue.Count <= 0)
        {
            return;
        }
        var pack = queue.Dequeue();
        // Debug.LogError("DistributeNextReportPackage for guid : " + guid + " pack type : " + pack.GetType());
@@ -439,25 +474,16 @@
        catch (Exception ex)
        {
            Debug.LogError("DistributeNextPackage: 分发包异常 " + ex);
            // 出错时主动移除当前包,防止死循环
            if (packQueue.Count > 0)
            {
                packQueue.Dequeue();
            }
        }
        if (queue.Count <= 0)
        {
            battleReportDict.Remove(guid);
            battlePackRelationList.Remove(guid);
        }
    }
    #endregion
    public BattleField CreateBattleField(string guid, int MapID, int FuncLineID, JsonData extendData, List<TeamBase> redTeamList, List<TeamBase> blueTeamList)
    public BattleField CreateBattleField(string guid, HB424_tagSCTurnFightInit vNetData, JsonData extendData, List<TeamBase> redTeamList, List<TeamBase> blueTeamList)
    {
        BattleField battleField = null;
        int MapID = (int)vNetData.MapID;
        int FuncLineID = (int)vNetData.FuncLineID;
        bool isCreate = true;
        if (battleFields.TryGetValue(guid, out battleField))
@@ -468,14 +494,21 @@
                isCreate = false;
            }
            else
            {
            {
                BattleDebug.LogError("战场已存在 先进行销毁");
                battleField.Destroy();
            }
        }
        var bf = GetBattleFieldByMapID(MapID);
        if (bf != null && !string.IsNullOrEmpty(guid))
        {
            BattleDebug.LogError("相同地图ID的战场已存在 先进行销毁");
            bf.Destroy();
        }
        if (isCreate)
        {
        {
            battleField = BattleFieldFactory.CreateBattleField(guid, MapID, FuncLineID, extendData, redTeamList, blueTeamList);
            if (string.IsNullOrEmpty(guid))
@@ -488,7 +521,7 @@
        battleField.Init(MapID, FuncLineID, extendData, redTeamList, blueTeamList);
        battleField.Init(MapID, FuncLineID, extendData, redTeamList, blueTeamList, vNetData.TurnMax);
        return battleField;
    }
@@ -516,7 +549,7 @@
        GameObject.DestroyImmediate(battleField.battleRootNode.gameObject);
        onBattleFieldDestroy?.Invoke(battleField.guid, battleField);
        onBattleFieldDestroy?.Invoke();
    }
@@ -569,4 +602,76 @@
        return null;
    }
    //发进入战斗包
    private float lastTime = 0f;
    private float turnCoolDown = 0.5f; // 冷却时间
    public void SendTurnFight(uint mapID, uint funcLineID = 0, byte tagType = 0, uint tagID = 0, uint[] valueList = null)
    {
        // 连续频繁发包,触发提示
        float currentTime = Time.time;
        if (currentTime - lastTime < turnCoolDown)
        {
            SysNotifyMgr.Instance.ShowTip("BattleCoolDown");
            return;
        }
        lastTime = currentTime;
        CB410_tagCMTurnFight pack = new CB410_tagCMTurnFight();
        pack.MapID = mapID;
        pack.FuncLineID = funcLineID;
        pack.TagType = tagType;
        pack.TagID = tagID;
        if (!valueList.IsNullOrEmpty())
        {
            pack.ValueList = valueList;
            pack.ValueCount = (byte)valueList.Length;
        }
        GameNetSystem.Instance.SendInfo(pack);
    }
    // 获取当前正在显示的战斗场景名称,如果没有则返回空字符串
    public string GetActiveBattleName()
    {
        foreach (var kvp in battleFields)
        {
            BattleField battleField = kvp.Value;
            if (battleField == null)
                continue;
            var battleName = battleField.ToString();
            if (BattleConst.battleNameToWinName.ContainsKey(battleName)
            && UIManager.Instance.IsOpened(BattleConst.battleNameToWinName[battleName]))
                return battleName;
        }
        return "";
    }
    // 获取当前正在显示的战斗场景,如果没有则返回null
    public BattleField GetActiveBattleFieldByName(string battleName)
    {
        foreach (var kvp in battleFields)
        {
            BattleField battleField = kvp.Value;
            if (battleField == null)
                continue;
            var name = battleField.ToString();
            if (name == battleName)
                return battleField;
        }
        return null;
    }
    public void DestroyAllBattleField()
    {
        var battleFieldsList = battleFields.Values.ToList();
        foreach (var battleField in battleFieldsList)
        {
            if (battleField == null)
                continue;
            battleField.Destroy();
        }
    }
}