yyl
2025-10-31 bfa20b35bbe0407a93a0eb96d53463413d61504c
Main/System/Battle/Skill/SkillBase.cs
@@ -9,6 +9,13 @@
{
    const float moveTime = 0.5f;
    private static readonly Color colorGreen = new Color(33f / 255f,
                                                        133f / 255f,
                                                        6f / 255f);
    private static readonly Color colorBlue = new Color(40f / 255f,
                                                        87f / 255f,
                                                        189f / 255f);
    protected SkillEffect skillEffect;
    protected HB427_tagSCUseSkill tagUseSkillAttack;
    public SkillConfig skillConfig;
@@ -24,8 +31,13 @@
    public int fromSkillId;
    public bool isPlay = false;
    private float MoveSpeed = 750f;
    private Dictionary<int, BattleDrops> tempDropList = new Dictionary<int, BattleDrops>();
    private Dictionary<int, HB422_tagMCTurnFightObjDead> tempDeadPackList = new Dictionary<int, HB422_tagMCTurnFightObjDead>();
    protected List<HB428_tagSCBuffRefresh> buffCollections = new List<HB428_tagSCBuffRefresh>();
    // 构造函数:初始化技能基础数据
    public SkillBase(BattleObject _caster, SkillConfig _skillCfg, HB427_tagSCUseSkill vNetData, List<GameNetPackBasic> _packList, BattleField _battleField = null)
@@ -76,6 +88,44 @@
        }
    }
    protected void ShadowIllutionCreate(bool create)
    {
        if (create)
        {
            Color color = Color.white;
            //1-连击;2-反击;3-追击
            //  反击蓝色
            //  追击连击绿色
            bool change = false;
            if (tagUseSkillAttack.BattleType == 1)
            {
                color = colorGreen;
                change = true;
            }
            else if (tagUseSkillAttack.BattleType == 2)
            {
                color = colorBlue;
                change = true;
            }
            else if (tagUseSkillAttack.BattleType == 3)
            {
                color = colorGreen;
                change = true;
            }
            if (change)
            {
                MoveSpeed = 1125f;
                caster.motionBase.ShowIllusionShadow(true, color);
            }
        }
        else
        {
            MoveSpeed = 750f;
            caster.motionBase.ShowIllusionShadow(false);
        }
    }
    // 技能释放主逻辑:广播事件、高亮目标、执行释放
    public virtual void Cast()
    {
@@ -84,12 +134,37 @@
        TeamHero teamHero = caster.teamHero;
        EventBroadcast.Instance.Broadcast<string, SkillConfig, TeamHero>(EventName.BATTLE_CAST_SKILL, guid, skillConfig, teamHero);
        if (caster != null)
        {
            // 战斗类型 0-常规;1-连击;2-反击;3-追击;4-子技能;5-被动触发的
            DamageNumConfig hintConfig = null;
            if (tagUseSkillAttack.BattleType == 1)
            {
                hintConfig = DamageNumConfig.Get(BattleConst.BattleComboAttack);
            }
            else if (tagUseSkillAttack.BattleType == 2)
            {
                hintConfig = DamageNumConfig.Get(BattleConst.BattleCounterAttack);
            }
            else if (tagUseSkillAttack.BattleType == 3)
            {
                hintConfig = DamageNumConfig.Get(BattleConst.BattleChaseAttack);
            }
            if (hintConfig != null)
            {
                caster.heroInfoBar.ShowTips(((char)hintConfig.prefix).ToString(), true, false, 1.25f);
                // Debug.Break();
            }
        }
        // 高亮所有本次技能相关的目标
        HighLightAllTargets();
        // 根据释放模式执行相应逻辑
        switch (skillConfig.castMode)
        {
            case SkillCastMode.None:
            case SkillCastMode.Self:
                CastImpl(OnAttackFinish);
                break;
@@ -125,7 +200,12 @@
        RectTransform target = battleField.GetTeamNode(caster.GetEnemyCamp(), skillConfig);
        ExecuteMoveAndCastSequence(target, () =>
        {
            MoveToTarget(battleField.GetTeamNode(caster.Camp, caster.teamHero.positionNum), Vector2.zero, OnAttackFinish, 750F);
            // ShadowIllutionCreate(true);
            MoveToTarget(battleField.GetTeamNode(caster.Camp, caster.teamHero.positionNum), Vector2.zero, () =>
            {
                // ShadowIllutionCreate(false);
                OnAttackFinish();
            }, MoveSpeed);
        });
    }
@@ -146,7 +226,12 @@
        ExecuteMoveAndCastSequence(targetTrans, () =>
        {
            RectTransform rectTransform = battleField.GetTeamNode(caster.Camp, caster.teamHero.positionNum);
            MoveToTarget(rectTransform, Vector2.zero, OnAttackFinish, 750F);
            // ShadowIllutionCreate(true);
            MoveToTarget(rectTransform, Vector2.zero, () =>
            {
                // ShadowIllutionCreate(false);
                OnAttackFinish();
            }, MoveSpeed);
        });
    }
@@ -156,17 +241,24 @@
        RectTransform target = battleField.GetTeamNode(caster.Camp, skillConfig);
        ExecuteMoveAndCastSequence(target, () =>
        {
            MoveToTarget(battleField.GetTeamNode(caster.Camp, caster.teamHero.positionNum), Vector2.zero, OnAttackFinish, 750F);
            // ShadowIllutionCreate(true);
            MoveToTarget(battleField.GetTeamNode(caster.Camp, caster.teamHero.positionNum), Vector2.zero, () =>
            {
                // ShadowIllutionCreate(false);
                OnAttackFinish();
            }, MoveSpeed);
        });
    }
    // 执行移动-施法-返回序列:通用的移动攻击流程
    private void ExecuteMoveAndCastSequence(RectTransform target, Action onReturnComplete)
    {
        ShadowIllutionCreate(true);
        MoveToTarget(target, new Vector2(skillConfig.CastDistance, 0), () =>
        {
            TurnBack(() =>
            {
                ShadowIllutionCreate(false);
                CastImpl(() =>
                {
                    TurnBack(() => 
@@ -187,7 +279,7 @@
    }
    // 移动到目标位置:处理角色的移动动画和逻辑
    protected void MoveToTarget(RectTransform target, Vector2 offset, Action _onComplete = null, float speed = 500f)
    protected void MoveToTarget(RectTransform target, Vector2 offset, Action _onComplete = null, float speed = 750f)
    {
        if (skillConfig.CastDistance >= 9999)
        {
@@ -251,7 +343,8 @@
        skillEffect.Play(OnHitTargets);
        foreach (var subSkillPack in tagUseSkillAttack.subSkillList)
        {
            RecordAction recordAction = CustomHB426CombinePack.CreateSkillAction(battleField.guid, new List<GameNetPackBasic>() { subSkillPack });
            SkillRecordAction recordAction = CustomHB426CombinePack.CreateSkillAction(battleField.guid, new List<GameNetPackBasic>() { subSkillPack });
            otherSkillActionList.Add(recordAction);
            battleField.recordPlayer.ImmediatelyPlay(recordAction);
        }
        isPlay = true;
@@ -320,7 +413,7 @@
        }
        battleField.battleRootNode.skillMaskNode.SetActive(true);
        battleField.battleRootNode.SetSortingOrder();
        // battleField.battleRootNode.SetSortingOrder();
    }
    // 命中目标回调:处理所有被命中的目标
@@ -621,71 +714,119 @@
    public void OnSkillFinished()
    {
        // 修复:使用循环代替递归,避免栈溢出风险
        try
        // try
        // {
        while (true)
        {
            while (true)
            // 验证技能效果是否完成
            if (skillEffect != null && !skillEffect.IsFinished())
                return;
            if (skillEffect != null)
            {
                // 验证技能效果是否完成
                if (skillEffect != null && !skillEffect.IsFinished())
                    return;
                if (skillEffect != null)
                {
                    skillEffect = null;
                    continue; // 使用continue代替递归调用
                }
                // 验证其他技能动作是否完成
                if (otherSkillActionList.Count > 0)
                {
                    bool hasFinishedAction = false;
                    for (int i = otherSkillActionList.Count - 1; i >= 0; i--)
                    {
                        var action = otherSkillActionList[i];
                        if (action.IsFinished())
                        {
                            otherSkillActionList.RemoveAt(i);
                            hasFinishedAction = true;
                        }
                    }
                    if (hasFinishedAction)
                    {
                        continue; // 使用continue代替递归调用
                    }
                    return;
                }
                break; // 没有更多需要处理的,退出循环
                skillEffect = null;
                continue; // 使用continue代替递归调用
            }
            // 处理剩余包
            while (packList.Count > 0)
            // 验证其他技能动作是否完成
            if (otherSkillActionList.Count > 0)
            {
                var pack = packList[0];
                packList.RemoveAt(0);
                if (pack is CustomHB426CombinePack combinePack && combinePack.startTag.Tag.StartsWith("Skill_"))
                bool hasFinishedAction = false;
                for (int i = otherSkillActionList.Count - 1; i >= 0; i--)
                {
                    BattleDebug.LogError("other skill casting " + combinePack.startTag.Tag);
                    var otherSkillAction = combinePack.CreateSkillAction();
                    otherSkillAction.fromSkillId = skillConfig.SkillID;
                    return;
                    var action = otherSkillActionList[i];
                    if (action.IsFinished())
                    {
                        otherSkillActionList.RemoveAt(i);
                        hasFinishedAction = true;
                    }
                }
                if (hasFinishedAction)
                {
                    continue; // 使用continue代替递归调用
                }
                return;
            }
            break; // 没有更多需要处理的,退出循环
        }
        // 处理剩余包
        if (!ResolvePackList())
        {
            return;
        }
            // }
            // catch (Exception ex)
            // {
            //     Debug.LogError($"OnSkillFinished异常: {ex.Message},技能ID={skillConfig.SkillID}");
            //     // 确保状态一致性,即使出现异常也要标记完成
            //     isFinished = true;
            //     throw; // 重新抛出异常供上层处理
            // }
        isFinished = true;
    }
    protected virtual bool ResolvePackList()
    {
        while (packList.Count > 0)
        {
            var pack = packList[0];
            packList.RemoveAt(0);
            if (pack is CustomHB426CombinePack combinePack && combinePack.startTag.Tag.StartsWith("Skill_"))
            {
                BattleDebug.LogError("other skill casting " + combinePack.startTag.Tag);
                var otherSkillAction = combinePack.CreateSkillAction();
                otherSkillAction.fromSkillId = skillConfig.SkillID;
                otherSkillActionList.Add(otherSkillAction);
                return false;
            }
            else if (pack is HB428_tagSCBuffRefresh buffRefresh)
            {
                //   从找到第一个HB428开始 找出连续的HB428_tagSCBuffRefresh包 如果找到一个B428后 之后碰到非HB428包就停止
                buffCollections.Add(buffRefresh);
                while (packList.Count > 0)
                {
                    var nextPack = packList[0];
                    if (nextPack is HB428_tagSCBuffRefresh nextBuffRefresh)
                    {
                        buffCollections.Add(nextBuffRefresh);
                        packList.RemoveAt(0);
                    }
                    else
                    {
                        break;
                    }
                }
                if (pack is CustomB421ActionPack actionPack)
                    actionPack.Distribute();
                // 同时刷新所有对象的buff,不分组
                foreach (var buff in buffCollections)
                {
                    BattleObject battleObj = battleField.battleObjMgr.GetBattleObject((int)buff.ObjID);
                    if (battleObj != null)
                    {
                        battleObj.buffMgr.RefreshBuff(buff, true);
                    }
                }
                // 清空已处理的buff集合
                buffCollections.Clear();
                continue;
            }
            if (pack is CustomB421ActionPack actionPack)
            {
                actionPack.Distribute();
            }
            else
            {
                PackageRegedit.Distribute(pack);
            }
        }
        catch (Exception ex)
        {
            Debug.LogError($"OnSkillFinished异常: {ex.Message},技能ID={skillConfig.SkillID}");
            // 确保状态一致性,即使出现异常也要标记完成
            isFinished = true;
            throw; // 重新抛出异常供上层处理
        }
        isFinished = true;
        return true;
    }
    // 添加清理方法:防止内存泄漏