1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using Spine;
using System.Linq;
using System;
 
public class SkillBase
{
    const float moveTime = 0.5f;
 
    protected SkillEffect skillEffect;
    protected HB427_tagSCUseSkill tagUseSkillAttack;
    public SkillConfig skillConfig;
    protected bool isFinished = false;
    protected BattleField battleField = null; // 战场
    protected RectTransform targetNode = null; // 目标节点
    protected BattleObject caster = null; // 施法者
    protected List<GameNetPackBasic> packList;
    protected List<SkillRecordAction> otherSkillActionList = new List<SkillRecordAction>();
    protected List<H0704_tagRolePackRefresh> dropPackList = new List<H0704_tagRolePackRefresh>();
    protected List<HB405_tagMCAddExp> expPackList = new List<HB405_tagMCAddExp>();
    protected bool moveFinished = false;
    public int fromSkillId;
    public bool isPlay = false;
 
    private Dictionary<int, BattleDrops> tempDropList = new Dictionary<int, BattleDrops>();
    private Dictionary<int, HB422_tagMCTurnFightObjDead> tempDeadPackList = new Dictionary<int, HB422_tagMCTurnFightObjDead>();
 
    // 构造函数:初始化技能基础数据
    public SkillBase(BattleObject _caster, SkillConfig _skillCfg, HB427_tagSCUseSkill vNetData, List<GameNetPackBasic> _packList, BattleField _battleField = null)
    {
        caster = _caster;
        if (null == caster)
        {
            throw new Exception("SkillBase caster is null ");
        }
        skillConfig = _skillCfg;
        tagUseSkillAttack = vNetData;
        battleField = _battleField;
        packList = _packList;
    }
 
    // 技能运行主逻辑:处理技能效果和其他技能动作
    public virtual void Run()
    {
        if (skillEffect != null)
        {
            if (skillEffect.IsFinished())
            {
                skillEffect = null;
                OnSkillFinished();
            }
            else
            {
                skillEffect.Run();
            }
            return;
        }
 
        if (otherSkillActionList.Count > 0)
        {
            for (int i = otherSkillActionList.Count - 1; i >= 0; i--)
            {
                var action = otherSkillActionList[i];
                if (action.IsFinished())
                {
                    otherSkillActionList.RemoveAt(i);
                    OnSkillFinished();
                }
                else if (moveFinished)
                {
                    action.Run();
                }
            }
        }
    }
 
    // 技能释放主逻辑:广播事件、高亮目标、执行释放
    public virtual void Cast()
    {
        // 广播技能释放事件
        string guid = battleField.guid;
        TeamHero teamHero = caster.teamHero;
        EventBroadcast.Instance.Broadcast<string, SkillConfig, TeamHero>(EventName.BATTLE_CAST_SKILL, guid, skillConfig, teamHero);
 
        // 高亮所有本次技能相关的目标
        HighLightAllTargets();
 
        // 根据释放模式执行相应逻辑
        switch (skillConfig.castMode)
        {
            case SkillCastMode.Self:
                CastImpl(OnAttackFinish);
                break;
            case SkillCastMode.Enemy:
                CastToEnemy();
                break;
            case SkillCastMode.Target:
                CastToTarget();
                break;
            case SkillCastMode.Allies:
                CastToAllies();
                break;
            case SkillCastMode.DashCast:
                DashCast(OnAttackFinish);
                break;
            default:
                Debug.LogError("强制结束技能 暂时不支持其他的方式释放 有需求please联系策划 技能id:" + skillConfig.SkillID + " cast position " + skillConfig.CastPosition);
                ForceFinished();
                break;
        }
    }
 
    // 冲撞攻击模式(待实现)
    protected void DashCast(Action _onComplete)
    {
        Debug.LogError("DashCast 还没实现");
        ForceFinished();
    }
 
    // 对敌方释放技能:移动到敌方区域进行攻击
    protected void CastToEnemy()
    {
        RectTransform target = battleField.GetTeamNode(caster.GetEnemyCamp(), skillConfig);
        ExecuteMoveAndCastSequence(target, () =>
        {
            MoveToTarget(battleField.GetTeamNode(caster.Camp, caster.teamHero.positionNum), Vector2.zero, OnAttackFinish, 750F);
        });
    }
 
    // 对指定目标释放技能:移动到主要目标位置进行攻击
    protected void CastToTarget()
    {
        if (tagUseSkillAttack.HurtCount <= 0)
        {
            Debug.LogError("技能攻击包没有目标 HurtCount <= 0");
            OnSkillFinished();
            return;
        }
 
        int mainTargetPosNum = BattleUtility.GetMainTargetPositionNum(caster, tagUseSkillAttack.HurtList.ToList(), skillConfig);
        BattleCamp battleCamp = skillConfig.TagFriendly != 0 ? caster.Camp : caster.GetEnemyCamp();
        RectTransform targetTrans = battleField.GetTeamNode(battleCamp, mainTargetPosNum);
 
        ExecuteMoveAndCastSequence(targetTrans, () =>
        {
            RectTransform rectTransform = battleField.GetTeamNode(caster.Camp, caster.teamHero.positionNum);
            MoveToTarget(rectTransform, Vector2.zero, OnAttackFinish, 750F);
        });
    }
 
    // 对友方释放技能:移动到友方区域进行治疗或增益
    protected void CastToAllies()
    {
        RectTransform target = battleField.GetTeamNode(caster.Camp, skillConfig);
        ExecuteMoveAndCastSequence(target, () =>
        {
            MoveToTarget(battleField.GetTeamNode(caster.Camp, caster.teamHero.positionNum), Vector2.zero, OnAttackFinish, 750F);
        });
    }
 
    // 执行移动-施法-返回序列:通用的移动攻击流程
    private void ExecuteMoveAndCastSequence(RectTransform target, Action onReturnComplete)
    {
        MoveToTarget(target, new Vector2(skillConfig.CastDistance, 0), () =>
        {
            TurnBack(() =>
            {
                CastImpl(() =>
                {
                    TurnBack(() => 
                    {
                        try
                        {
                            onReturnComplete?.Invoke(); // 添加异常处理防止回调异常导致状态不完整
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError($"ExecuteMoveAndCastSequence回调异常: {ex.Message}");
                            throw;
                        }
                    }, -1f);
                });
            }, -1f);
        });
    }
 
    // 移动到目标位置:处理角色的移动动画和逻辑
    protected void MoveToTarget(RectTransform target, Vector2 offset, Action _onComplete = null, float speed = 500f)
    {
        if (skillConfig.CastDistance >= 9999)
        {
            _onComplete?.Invoke();
            return;
        }
 
        caster.motionBase.PlayAnimation(MotionName.run, true);
        var tweener = BattleUtility.MoveToTarget(caster.heroRectTrans, target, offset, () =>
        {
            caster.motionBase.PlayAnimation(MotionName.idle, true);
            _onComplete?.Invoke();
        }, speed);
        battleField.battleTweenMgr.OnPlayTween(tweener);
    }
 
    // 转身逻辑:根据技能配置处理角色转向
    protected void TurnBack(Action _onComplete, float forward)
    {
        if (skillConfig.CastDistance < 0)
        {
            Vector3 scale = caster.heroGo.transform.localScale;
            scale.x = Mathf.Abs(scale.x) * forward;
            caster.heroGo.transform.localScale = scale;
        }
        _onComplete?.Invoke();
    }
 
    // 攻击完成后的处理:转身、恢复状态、播放待机动画
    protected void OnAttackFinish()
    {
        TurnBack(null, 1f);
        OnAllAttackMoveFinished();
        caster.motionBase.PlayAnimation(MotionName.idle, true);
    }
 
    // 所有攻击移动完成后的处理:恢复UI显示状态
    protected virtual void OnAllAttackMoveFinished()
    {
        moveFinished = true;
        List<BattleObject> allList = battleField.battleObjMgr.allBattleObjDict.Values.ToList<BattleObject>();
        foreach (BattleObject bo in allList)
        {
            bo.layerMgr.SetFront();
            bo.heroInfoBar.SetActive(true);
        }
        battleField.battleRootNode.skillMaskNode.SetActive(false);
    }
 
    // 执行技能释放动画和逻辑:播放施法动作并提供回调
    protected TrackEntry CastImpl(Action onComplete = null)
    {
        return caster.motionBase.PlaySkillAnimation(skillConfig, this, tagUseSkillAttack.BattleType == 4, onComplete);
    }
 
    // 技能开始回调:处理死亡、子技能、技能效果初始化
    public void OnSkillStart()
    {
        HandleDead();
        skillEffect = SkillEffectFactory.CreateSkillEffect(caster, skillConfig, tagUseSkillAttack);
        skillEffect.Play(OnHitTargets);
        foreach (var subSkillPack in tagUseSkillAttack.subSkillList)
        {
            SkillRecordAction recordAction = CustomHB426CombinePack.CreateSkillAction(battleField.guid, new List<GameNetPackBasic>() { subSkillPack });
            otherSkillActionList.Add(recordAction);
            battleField.recordPlayer.ImmediatelyPlay(recordAction);
        }
        isPlay = true;
    }
 
    // 技能前摇结束回调
    public virtual void OnStartSkillFrameEnd() { }
 
    // 技能中摇开始回调:通知技能效果处理中摇开始
    public virtual void OnMiddleFrameStart(int times)
    {
        skillEffect?.OnMiddleFrameStart(times); // 修复:添加空值检查
    }
 
    // 技能中摇结束回调:通知技能效果处理中摇结束
    public virtual void OnMiddleFrameEnd(int times, int hitIndex)
    {
        skillEffect?.OnMiddleFrameEnd(times, hitIndex); // 修复:添加空值检查
    }
 
    // 技能后摇开始回调:通知技能效果处理后摇开始
    public virtual void OnFinalFrameStart()
    {
        skillEffect?.OnFinalFrameStart(); // 修复:添加空值检查
    }
 
    // 技能后摇结束回调:通知技能效果处理后摇结束
    public virtual void OnFinalFrameEnd()
    {
        skillEffect?.OnFinalFrameEnd(); // 修复:添加空值检查
    }
 
    // 高亮所有相关目标:设置施法者和目标的显示层级
    protected void HighLightAllTargets()
    {
        caster.layerMgr.SetSortingOrder(BattleConst.ActiveHeroActionSortingOrder);
 
        if (skillConfig.FuncType != 2)
            return;
 
        List<BattleObject> targetList = battleField.battleObjMgr.GetBattleObjList(tagUseSkillAttack);
        List<BattleObject> highlightList = new List<BattleObject>(targetList) { caster };
        List<BattleObject> allList = battleField.battleObjMgr.allBattleObjDict.Values.ToList<BattleObject>();
        
        // 修复:使用HashSet优化性能,避免重复设置
        var targetSet = new HashSet<BattleObject>(targetList);
        var highlightSet = new HashSet<BattleObject>(highlightList);
        
        caster.heroInfoBar.SetActive(false);
 
        foreach (BattleObject bo in allList)
        {
            bool isHighlight = highlightSet.Contains(bo);
            bool isTarget = targetSet.Contains(bo);
            
            if (isHighlight)
            {
                bo.layerMgr.SetFront();
            }
            else
            {
                bo.layerMgr.SetBack();
            }
 
            bo.heroInfoBar.SetActive(isTarget);
        }
 
        battleField.battleRootNode.skillMaskNode.SetActive(true);
        battleField.battleRootNode.SetSortingOrder();
    }
 
    // 命中目标回调:处理所有被命中的目标
    protected virtual void OnHitTargets(int _hitIndex, List<HB427_tagSCUseSkill.tagSCUseSkillHurt> hitList)
    {
        foreach (var hurt in hitList)
        {
            BattleObject target = caster.battleField.battleObjMgr.GetBattleObject((int)hurt.ObjID);
            if (target == null)
            {
                Debug.LogError("目标为空 target == null ObjId : " + hurt.ObjID);
                continue;
            }
 
            OnHitEachTarget(_hitIndex, target, hurt);
        }
    }
 
    // 处理单个目标被命中:应用伤害和施法者效果
    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)
        {
            damageDivide.Add(10000);
        }
        else
        {
            if (skillConfig.DamageDivide.Length <= _hitIndex)
            {
                Debug.LogError("技能伤害分布配置错误 skillId: " + skillConfig.SkillID + " hitIndex: " + _hitIndex);
                damageDivide.Add(10000);
            }
            else
            {
                damageDivide = skillConfig.DamageDivide[_hitIndex].ToList();
            }
        }
 
        // 伤害分布计算和应用
        long totalDamage = GeneralDefine.GetFactValue(hurt.HurtHP, hurt.HurtHPEx);
        List<long> damageList = BattleUtility.DivideDamageToList(damageDivide.ToArray(), totalDamage);
 
        // 获取临时数据并应用伤害
        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);
 
        // 处理施法者相关效果
        caster.SuckHp(hurt.SuckHP, skillConfig);
        caster.HurtByReflect(hurt.BounceHP, skillConfig);
    }
 
    // 处理死亡相关逻辑:分配掉落和经验
    protected void HandleDead()
    {
        var deadPackList = BattleUtility.FindDeadPack(packList);
        if (deadPackList.Count <= 0) return;
 
        CheckAfterDeadhPack();
 
        // 修复:先收集要删除的包,避免在foreach中修改集合
        var dropPacksToRemove = new List<H0704_tagRolePackRefresh>(dropPackList);
        foreach (var _dropPack in dropPacksToRemove)
        {
            PackageRegedit.Distribute(_dropPack);
            packList.Remove(_dropPack);
        }
 
        // 获取并分配掉落物品和经验
        var dropPack = PackManager.Instance.GetSinglePack(PackType.DropItem);
        var itemDict = dropPack.GetAllItems();
        List<ItemModel> itemList = new List<ItemModel>(itemDict.Values.Where(item => item != null && item.isAuction));
 
        var dropAssign = AssignDrops(itemList, deadPackList.Count);
        var expAssign = AssignExp(expPackList, deadPackList.Count);
 
        // 构造BattleDrops并缓存
        for (int i = 0; i < deadPackList.Count; i++)
        {
            int objID = (int)deadPackList[i].ObjID;
            BattleObject deadTarget = battleField.battleObjMgr.GetBattleObject(objID);
            
            // 修复:添加空值检查
            if (deadTarget == null)
            {
                Debug.LogError($"找不到死亡目标,ObjID: {objID}");
                continue;
            }
            
            List<int> itemIndexList = dropAssign[i].Select(item => item.gridIndex).ToList();
            
            BattleDrops battleDrops = new BattleDrops()
            {
                rectTransform = deadTarget.heroRectTrans,
                dropItemPackIndex = itemIndexList,
                expDrops = expAssign[i]
            };
 
            // 修复:避免字典键冲突,使用安全的添加方式
            if (!tempDropList.ContainsKey(objID))
            {
                tempDropList.Add(objID, battleDrops);
            }
            else
            {
                Debug.LogWarning($"tempDropList中已存在ObjID={objID}的记录,将覆盖原值");
                tempDropList[objID] = battleDrops; // 覆盖现有值
            }
 
            if (!tempDeadPackList.ContainsKey(objID))
            {
                tempDeadPackList.Add(objID, deadPackList[i]);
            }
            else
            {
                Debug.LogWarning($"tempDeadPackList中已存在ObjID={objID}的记录,将覆盖原值");
                tempDeadPackList[objID] = deadPackList[i]; // 覆盖现有值
            }
        }
 
        // 修复:避免在遍历时修改集合,先收集后删除
        var deadPacksToRemove = new List<GameNetPackBasic>(deadPackList.Cast<GameNetPackBasic>());
        foreach (var deadPack in deadPacksToRemove)
        {
            packList.Remove(deadPack);
        }
    }
 
    // 分配掉落物品:将掉落物品平均分配给死亡对象
    protected List<List<ItemModel>> AssignDrops(List<ItemModel> itemList, int deadCount)
    {
        var dropAssign = new List<List<ItemModel>>();
        for (int i = 0; i < deadCount; i++)
            dropAssign.Add(new List<ItemModel>());
        for (int i = 0; i < itemList.Count; i++)
            dropAssign[i % deadCount].Add(itemList[i]);
        return dropAssign;
    }
 
    // 分配经验值:将经验包平均分配给每个死亡对象
    protected List<List<HB405_tagMCAddExp>> AssignExp(List<HB405_tagMCAddExp> expList, int deadCount)
    {
        var expAssign = new List<List<HB405_tagMCAddExp>>();
        for (int i = 0; i < deadCount; i++)
            expAssign.Add(new List<HB405_tagMCAddExp>());
 
        // 修复:检查除零风险
        if (deadCount == 0)
        {
            Debug.LogWarning("AssignExp: deadCount为0,无法分配经验");
            return expAssign;
        }
 
        // 修复:先收集要删除的包,避免在foreach中修改packList
        var expPacksToRemove = new List<HB405_tagMCAddExp>();
 
        foreach (var expPack in expList)
        {
            long totalExp = GeneralDefine.GetFactValue(expPack.Exp, expPack.ExpPoint);
            long avgExp = totalExp / deadCount;
            long remain = totalExp % deadCount;
 
            for (int i = 0; i < deadCount; i++)
            {
                long assignExp = avgExp + (i < remain ? 1 : 0);
                var newPack = new HB405_tagMCAddExp
                {
                    Exp = (uint)(assignExp % 100000000),
                    ExpPoint = (uint)(assignExp / 100000000),
                    Source = expPack.Source
                };
                expAssign[i].Add(newPack);
            }
            expPacksToRemove.Add(expPack);
        }
        
        // 统一删除收集的包
        foreach (var pack in expPacksToRemove)
        {
            packList.Remove(pack);
        }
 
        return expAssign;
    }
 
    // 检查死亡后的包处理:处理技能包、掉落包、经验包
    protected void CheckAfterDeadhPack()
    {
        List<int> removeIndexList = new List<int>();
        
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
 
            // 复活基本都靠技能包
            if (pack is CustomHB426CombinePack combinePack && combinePack.startTag.Tag.StartsWith("Skill_"))
                break;
                
            if (pack is H0704_tagRolePackRefresh h0704Pack && h0704Pack.PackType == (byte)PackType.DropItem && h0704Pack.IsBind == 1)
            {
                dropPackList.Add(h0704Pack);
                removeIndexList.Add(i);
            }
            
            if (pack is HB405_tagMCAddExp h405Pack && h405Pack.Source == 2)
            {
                expPackList.Add(h405Pack);
                removeIndexList.Add(i);
            }
        }
 
        for (int i = removeIndexList.Count - 1; i >= 0; i--)
            packList.RemoveAt(removeIndexList[i]);
    }
 
    // 检查技能是否完成:综合检查所有完成条件
    public virtual bool IsFinished()
    {
        if (!isPlay) return false;
        
 
        // 检查技能效果是否完成
        if (skillEffect != null)
        {
            if (!skillEffect.IsFinished()) return false;
            skillEffect = null;
            OnSkillFinished();
            return false;
        }
 
        // 检查其他技能动作是否完成
        if (otherSkillActionList.Count > 0)
        {
            for (int i = otherSkillActionList.Count - 1; i >= 0; i--)
            {
                var action = otherSkillActionList[i];
                if (action.IsFinished())
                {
                    otherSkillActionList.RemoveAt(i);
                    OnSkillFinished();
                }
            }
            if (otherSkillActionList.Count > 0) return false;
        }
 
        // 检查最终完成状态
        if (isFinished && moveFinished)
        {
            if (packList.Count > 0)
            {
                OnSkillFinished();
                return false;
            }
 
            return true;
        }
 
        return false;
    }
 
 
    // 强制结束技能:立即结束所有技能相关的处理
    public virtual void ForceFinished()
    {
        skillEffect?.ForceFinished();
        
        otherSkillActionList.ForEach(action => action.ForceFinish());
        otherSkillActionList.Clear();
 
        isFinished = true;
        moveFinished = true;
        isPlay = true;
 
        // 处理所有剩余包
        while (packList.Count > 0)
        {
            var pack = packList[0];
            packList.RemoveAt(0);
 
            if (pack is CustomHB426CombinePack combinePack && combinePack.startTag.Tag.StartsWith("Skill_"))
            {
                var otherSkillAction = combinePack.CreateSkillAction();
                otherSkillAction.fromSkillId = skillConfig.SkillID;
                otherSkillAction.ForceFinish();
            }
            else
            {
                if (pack is CustomB421ActionPack actionPack)
                    actionPack.Distribute();
                PackageRegedit.Distribute(pack);
            }
        }
    }
 
    // 技能完成处理:正常完成时的清理工作
    public void OnSkillFinished()
    {
        // 修复:使用循环代替递归,避免栈溢出风险
        try
        {
            while (true)
            {
                // 验证技能效果是否完成
                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; // 没有更多需要处理的,退出循环
            }
 
            // 处理剩余包
            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;
                }
 
                if (pack is CustomB421ActionPack actionPack)
                    actionPack.Distribute();
                PackageRegedit.Distribute(pack);
            }
        }
        catch (Exception ex)
        {
            Debug.LogError($"OnSkillFinished异常: {ex.Message},技能ID={skillConfig.SkillID}");
            // 确保状态一致性,即使出现异常也要标记完成
            isFinished = true;
            throw; // 重新抛出异常供上层处理
        }
 
        isFinished = true;
    }
 
    // 添加清理方法:防止内存泄漏
    public virtual void Cleanup()
    {
        tempDropList?.Clear();
        tempDeadPackList?.Clear();
        otherSkillActionList?.Clear();
        dropPackList?.Clear();
        expPackList?.Clear();
        
        skillEffect = null;
        packList = null;
    }
}