using System; 
 | 
using System.Collections; 
 | 
using System.Collections.Generic; 
 | 
using System.Text; 
 | 
using LitJson; 
 | 
  
 | 
using UnityEngine; 
 | 
public class AutoFightModel : GameSystemManager<AutoFightModel> 
 | 
{ 
 | 
    //战斗倍数:值越大越快,影响战斗表现,掉落速度等,这里的倍数是索引,对应配置里的实际速率 
 | 
    public int fightSpeed 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            int value = QuickSetting.Instance.GetQuickSettingValue<int>(QuickSettingType.AutoFight_Speed, 0); 
 | 
            return Math.Min(Math.Max(value, 1), maxSpeed); 
 | 
        } 
 | 
        set 
 | 
        { 
 | 
            var num = Math.Max(1, value); 
 | 
            BattleManager.Instance.storyBattleField.SetSpeedRatio(BattleManager.Instance.speedGear[num - 1]); 
 | 
            QuickSetting.Instance.SetQuickSetting(QuickSettingType.AutoFight_Speed, num); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    //消耗倍数 
 | 
    public int fightCost 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            int value = QuickSetting.Instance.GetQuickSettingValue<int>(QuickSettingType.AutoFight_Cost, 0); 
 | 
            return Math.Min(Math.Max(value, 1), maxCost); 
 | 
        } 
 | 
        set 
 | 
        { 
 | 
            QuickSetting.Instance.SetQuickSetting(QuickSettingType.AutoFight_Cost, value); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    //自动模式, 真正点击战锤消耗开启,和休息(或无材料)停止 
 | 
    public bool isPause = false;    //如打BOSS的情况,暂停自动战斗 后续可以补充每X秒检测下是否有异常 
 | 
    bool m_IsAutoAttack = false; 
 | 
    public bool isAutoAttack 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            return m_IsAutoAttack; 
 | 
        } 
 | 
        set 
 | 
        { 
 | 
            if (m_IsAutoAttack == value) 
 | 
                return; 
 | 
            m_IsAutoAttack = value; 
 | 
            Debug.Log("isAutoAttack:" + m_IsAutoAttack); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    //是否开启自动战斗设置 
 | 
    public bool isAutoAttackSet 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            return QuickSetting.Instance.GetQuickSettingBool(QuickSettingType.AutoFight_Open, 0); 
 | 
        } 
 | 
        set 
 | 
        { 
 | 
            QuickSetting.Instance.SetQuickSetting(QuickSettingType.AutoFight_Open, value); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    //更好装备停止战斗 
 | 
    public bool isStopFightByBetterEquip 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            return QuickSetting.Instance.GetQuickSettingBool(QuickSettingType.AutoFight_FightPower, 0); 
 | 
        } 
 | 
        set 
 | 
        { 
 | 
            QuickSetting.Instance.SetQuickSetting(QuickSettingType.AutoFight_FightPower, value); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    public event Action ChangeAutoEvent; 
 | 
  
 | 
    public int maxSpeed = 3; //最高速度 索引 
 | 
    public int maxCost; //最高消耗 
 | 
    public int[] autoCostWithBlessLV; //自动战斗消耗倍数关联祝福等级 
 | 
    public int speed2UnlockMissionID; 
 | 
    public int speed3UnlockCTGID; 
 | 
     
 | 
    public override void Init() 
 | 
    { 
 | 
        ParseConfig(); 
 | 
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += BeforePlayerInit; 
 | 
        BattleManager.Instance.onBattleFieldCreate += OnCreateBattleField; 
 | 
  
 | 
        EventBroadcast.Instance.AddListener<string, SkillConfig, TeamHero>(EventName.BATTLE_CAST_SKILL, OnSkillCast); 
 | 
  
 | 
    } 
 | 
  
 | 
    public override void Release() 
 | 
    { 
 | 
        BattleManager.Instance.onBattleFieldCreate -= OnCreateBattleField; 
 | 
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent -= BeforePlayerInit; 
 | 
         
 | 
  
 | 
    } 
 | 
  
 | 
    void ParseConfig() 
 | 
    { 
 | 
        var config = FuncConfigConfig.Get("AutoGuaji"); 
 | 
        autoCostWithBlessLV = JsonMapper.ToObject<int[]>(config.Numerical1); 
 | 
        speed2UnlockMissionID = int.Parse(config.Numerical2); 
 | 
        speed3UnlockCTGID = int.Parse(config.Numerical3); 
 | 
        maxCost = autoCostWithBlessLV.Length; 
 | 
    } 
 | 
  
 | 
  
 | 
    void BeforePlayerInit() 
 | 
    { 
 | 
        fightingHeroSkinID = 0; 
 | 
        heroGuid = ""; 
 | 
    } 
 | 
  
 | 
    public void SaveAutoFightSetting() 
 | 
    { 
 | 
        if (PlayerDatas.Instance.baseData.UseHarmerCount != fightCost) 
 | 
        { 
 | 
            PlayerDatas.Instance.baseData.UseHarmerCount = fightCost; 
 | 
            BattleManager.Instance.MainFightRequest(1, (uint)fightCost); 
 | 
        } 
 | 
  
 | 
        StoryBattleField storyBattleField = BattleManager.Instance.storyBattleField; 
 | 
  
 | 
        if (storyBattleField != null && storyBattleField.GetBattleMode() != BattleMode.Stop) 
 | 
        { 
 | 
            //战斗中改变模式 
 | 
            isAutoAttack = isAutoAttackSet; 
 | 
            storyBattleField.AutoSetBattleMode(); 
 | 
        } 
 | 
  
 | 
        QuickSetting.Instance.SendPackage(); 
 | 
        ChangeAutoEvent?.Invoke(); 
 | 
    } 
 | 
  
 | 
  
 | 
    //自动处理装备,需要等待穿戴返回false,其他情况返回true 
 | 
    public bool TryAutoFightDoEquip(ItemModel item) 
 | 
    { 
 | 
        if (!isAutoAttack) 
 | 
            return false; 
 | 
  
 | 
        if (item == null) 
 | 
            return true; 
 | 
  
 | 
        long showFightPower = FightPowerManager.Instance.GetFightPowerChange(item); 
 | 
  
 | 
        if (showFightPower < 0) 
 | 
        { 
 | 
            EquipModel.Instance.SendEquipOP(new ushort[] { (ushort)item.gridIndex }, 1); 
 | 
            return true; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            if (isStopFightByBetterEquip) 
 | 
                return false; 
 | 
  
 | 
            EquipModel.Instance.SendEquipOP(new ushort[] { (ushort)item.gridIndex }, 1); 
 | 
            return true; 
 | 
  
 | 
        } 
 | 
  
 | 
    } 
 | 
  
 | 
    #region 主线战斗(自动和手动) 
 | 
  
 | 
    public void StartFight() 
 | 
    { 
 | 
        if (isAutoAttack) 
 | 
            return; 
 | 
  
 | 
        StoryBattleField storyBattleField = BattleManager.Instance.storyBattleField; 
 | 
        if (storyBattleField == null) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        if (!UIHelper.CheckMoneyCount(41, PlayerDatas.Instance.baseData.UseHarmerCount, 2)) 
 | 
        { 
 | 
            if (storyBattleField.GetBattleMode() != BattleMode.Stop) 
 | 
                storyBattleField.HaveRest(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        BattleManager.Instance.storyBattleField.SetSpeedRatio(BattleManager.Instance.speedGear[fightSpeed - 1]); 
 | 
        isAutoAttack = isAutoAttackSet; 
 | 
  
 | 
        //手动会一直进入这个逻辑, 自动触发一次 
 | 
        storyBattleField.AutoSetBattleMode(); 
 | 
        storyBattleField.operationAgent.DoNext(); 
 | 
    } 
 | 
  
 | 
    void OnCreateBattleField(string guid, BattleField battleField) 
 | 
    { 
 | 
        if (string.IsNullOrEmpty(guid) && BattleManager.Instance.storyBattleField != null) 
 | 
        { 
 | 
            BattleManager.Instance.storyBattleField.ChangeBattleModeEvent -= ChangeBattleModeEvent; 
 | 
            BattleManager.Instance.storyBattleField.ChangeBattleModeEvent += ChangeBattleModeEvent; 
 | 
        } 
 | 
  
 | 
    } 
 | 
  
 | 
  
 | 
    void ChangeBattleModeEvent(BattleMode _battleMode) 
 | 
    { 
 | 
        if (_battleMode == BattleMode.Stop) 
 | 
        { 
 | 
            isAutoAttack = false; 
 | 
        } 
 | 
        OnFightEvent?.Invoke(false); 
 | 
    } 
 | 
  
 | 
    public int fightingHeroSkinID;  //当前战斗的英雄皮肤ID 
 | 
    public string heroGuid; //战斗中的武将 
 | 
    public event Action<bool> OnFightEvent; //战斗模式变更通知 战斗释放技能通知 
 | 
  
 | 
  
 | 
    /// <summary> 
 | 
    /// 技能释放 通知UI处理 
 | 
    /// </summary> 
 | 
    /// <param name="guid">空为主线</param> 
 | 
    /// <param name="skillConfig">用于怒气等显示</param> 
 | 
    /// <param name="teamHero">战斗中的武将</param> 
 | 
    void OnSkillCast(string guid, SkillConfig skillConfig, TeamHero teamHero) 
 | 
    { 
 | 
        if (!string.IsNullOrEmpty(guid)) 
 | 
            return; 
 | 
  
 | 
        //防范回收报错 
 | 
        if (teamHero == null) 
 | 
            return; 
 | 
  
 | 
        //只通知玩家武将的战斗 
 | 
        if (teamHero.NPCID != 0) 
 | 
            return; 
 | 
  
 | 
        fightingHeroSkinID = teamHero.SkinID; 
 | 
        //战斗时没有GUID ,通过heroid查找 
 | 
        var hero = TeamManager.Instance.GetTeam(TeamType.Story).GetHeroByHeroID(teamHero.heroId); 
 | 
        if (hero != null) 
 | 
        { 
 | 
            heroGuid = hero.guid; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            heroGuid = ""; 
 | 
        } 
 | 
        OnFightEvent?.Invoke(true); 
 | 
    } 
 | 
  
 | 
    public bool CanChallengeBoss() 
 | 
    { 
 | 
        //最后一波通过,且当前波还是最后一波;倒退只倒退波不会倒退关卡 
 | 
        if (PlayerDatas.Instance.baseData.ExAttr2 != PlayerDatas.Instance.baseData.ExAttr1) 
 | 
        { 
 | 
            return false; 
 | 
        } 
 | 
        var value = PlayerDatas.Instance.baseData.ExAttr2; 
 | 
        var chapterID = value / 10000; 
 | 
        var levelNum = value % 10000 / 100; 
 | 
        var waveID = value % 100; 
 | 
  
 | 
        var config = MainLevelConfig.GetMainLevelConfig(chapterID, levelNum); 
 | 
        if (waveID < MainLevelConfig.GetwaveCount(config)) 
 | 
        { 
 | 
            return false; 
 | 
        } 
 | 
  
 | 
        return true; 
 | 
    } 
 | 
  
 | 
    #endregion 
 | 
  
 | 
  
 | 
} 
 |