lcy
6 天以前 3b2a6bb9047cfce9f501593b3669a9c1af6c5df4
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()
    {
@@ -103,7 +153,8 @@
            
            if (hintConfig != null)
            {
                caster.heroInfoBar.ShowTips(((char)hintConfig.prefix).ToString(), true);
                caster.heroInfoBar.ShowTips(((char)hintConfig.prefix).ToString(), true, false, 1.25f);
                // Debug.Break();
            }
        }
@@ -113,6 +164,7 @@
        // 根据释放模式执行相应逻辑
        switch (skillConfig.castMode)
        {
            case SkillCastMode.None:
            case SkillCastMode.Self:
                CastImpl(OnAttackFinish);
                break;
@@ -148,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);
        });
    }
@@ -169,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);
        });
    }
@@ -179,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(() => 
@@ -210,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)
        {
@@ -270,6 +339,7 @@
    public void OnSkillStart()
    {
        HandleDead();
        skillEffect = SkillEffectFactory.CreateSkillEffect(caster, skillConfig, tagUseSkillAttack);
        skillEffect.Play(OnHitTargets);
        foreach (var subSkillPack in tagUseSkillAttack.subSkillList)
@@ -305,6 +375,7 @@
    // 技能后摇结束回调:通知技能效果处理后摇结束
    public virtual void OnFinalFrameEnd()
    {
        skillEffect?.OnFinalFrameEnd(); // 修复:添加空值检查
    }
@@ -350,6 +421,9 @@
    // 命中目标回调:处理所有被命中的目标
    protected virtual void OnHitTargets(int _hitIndex, List<HB427_tagSCUseSkill.tagSCUseSkillHurt> hitList)
    {
        //  造成伤害前先处理血量刷新包
        HandleRefreshHP();
        foreach (var hurt in hitList)
        {
            BattleObject target = caster.battleField.battleObjMgr.GetBattleObject((int)hurt.ObjID);
@@ -363,9 +437,10 @@
        }
    }
    // 处理单个目标被命中:应用伤害和施法者效果
    // 处理单个目标被命中:应用伤害和施法者效果
    protected virtual void OnHitEachTarget(int _hitIndex, BattleObject target, HB427_tagSCUseSkill.tagSCUseSkillHurt hurt)
    {
        // ============ 第一步:计算伤害分布 ============
        List<int> damageDivide = new List<int>();
        if (_hitIndex == 0 && skillConfig.DamageDivide.Length <= 0)
        {
@@ -384,19 +459,64 @@
            }
        }
        // 伤害分布计算和应用
        // 计算总伤害和分段伤害列表
        long totalDamage = GeneralDefine.GetFactValue(hurt.HurtHP, hurt.HurtHPEx);
        List<long> damageList = BattleUtility.DivideDamageToList(damageDivide.ToArray(), totalDamage);
        List<long> damageList = BattleUtility.DivideDamageToList(skillConfig.DamageDivide, _hitIndex, totalDamage);
        // 获取临时数据并应用伤害
        // ============ 第二步:刷新实际血量 ============
        long fromHp = target.teamHero.curHp;
        // 计算当前这一击的实际伤害(所有分段伤害之和)
        long currentHitDamage = 0;
        foreach (long dmg in damageList)
        {
            currentHitDamage += dmg;
        }
        long toHp = Math.Max(0, fromHp - currentHitDamage);
        // 更新目标血量
        target.teamHero.curHp = toHp;
#if UNITY_EDITOR
        BattleDebug.LogError(
            (caster.Camp == BattleCamp.Red ? "【红方行动】" : "【蓝方行动】") + "\n" +
            $"攻击者: {caster.teamHero.name}\n" +
            $"目标: {target.teamHero.name}\n" +
            $"技能: {skillConfig.SkillName} (第{_hitIndex}击)\n" +
            $"伤害: {currentHitDamage} (总伤害: {totalDamage})\n" +
            $"血量变化: {fromHp} -> {toHp}"
        );
#endif
        bool isLastHit = _hitIndex >= skillConfig.DamageDivide.Length - 1;
        // ============ 第三步:获取临时数据(掉落、死亡等) ============
        int objID = (int)target.ObjID;
        tempDropList.TryGetValue(objID, out BattleDrops battleDrops);
        tempDeadPackList.TryGetValue(objID, out HB422_tagMCTurnFightObjDead deadPack);
        target.Hurt(damageList, totalDamage, hurt, skillConfig, _hitIndex, battleDrops, deadPack);
        // 处理施法者相关效果
        // ============ 第四步:执行表现(飘字、动画等) ============
        target.Hurt(damageList, totalDamage, hurt, skillConfig, _hitIndex, battleDrops, deadPack, fromHp, toHp);
        // ============ 第五步:处理施法者相关效果 ============
        caster.SuckHp(hurt.SuckHP, skillConfig);
        caster.HurtByReflect(hurt.BounceHP, skillConfig);
    }
    // 处理HP刷新包(简化逻辑)
    private void HandleRefreshHP()
    {
        // 查找HP刷新包
        HB419_tagSCObjHPRefresh refreshPack = BattleUtility.FindObjHPRefreshPack(packList);
        if (refreshPack != null)
        {
            // 分发HP刷新包
            PackageRegedit.Distribute(refreshPack);
            packList.Remove(refreshPack);
        }
    }
    // 处理死亡相关逻辑:分配掉落和经验
@@ -645,72 +765,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;
                    otherSkillActionList.Add(otherSkillAction);
                    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;
    }
    // 添加清理方法:防止内存泄漏