hch
2026-02-02 04ffe31b6a2b2fbcfecc83abb44a8aa233f2e53f
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
using UnityEngine;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using System;
 
 
//  如果这个Action卡住了 大概率是因为 动作被其他动作中断了 导致没有回调
public class DeathRecordAction : RecordAction
{
    protected List<BattleDeadPack> deadPackList = new List<BattleDeadPack>();
 
    protected Dictionary<BattleDeadPack, SkillRecordAction> deathActionDict = new Dictionary<BattleDeadPack, SkillRecordAction>();
 
    protected Dictionary<BattleDeadPack, Func<bool>> deadActionStatesDict = new Dictionary<BattleDeadPack, Func<bool>>();
 
    protected Dictionary<int, bool> dropStateDict = new Dictionary<int, bool>();
 
    protected bool hasDeathTriggerSkill = false;
 
    // 标记是否已经分发了死亡后的包
    protected bool hasDistributedPacksAfterDeath = false;
 
    private SkillRecordAction parentSkillAction = null;
    
    private SkillRecordAction playSkillRecordAction = null;
 
    public DeathRecordAction(BattleField _battleField, List<BattleDeadPack> _deadPackList, SkillRecordAction _parentSkillAction = null, SkillRecordAction _playSkillRecordAction = null)
        : base(RecordActionType.Death, _battleField, null)
    {
        deadPackList = _deadPackList;
        CheckHasDeathTriggerSkill();
 
        // for (int i = 0; i < deadPackList.Count; i++)
        // {
        //     BattleObject battleObject = battleField.battleObjMgr.GetBattleObject((int)deadPackList[i].deadPack.ObjID);
        //     Debug.LogError($"DeathRecordAction: 初始化死亡动作,死亡对象名字={battleObject?.GetName()}, hasDeathTriggerSkill={deadPackList[i].deadTriggerSkill != null}");
        // }
 
        parentSkillAction = _parentSkillAction;
        playSkillRecordAction = _playSkillRecordAction;
 
    }
 
    protected void CheckHasDeathTriggerSkill()
    {
        foreach (var battleDeadPack in deadPackList)
        {
            if (battleDeadPack.deadTriggerSkill != null)
            {
                hasDeathTriggerSkill = true;
                break;
            }
        }
    }
 
 
    public override void Run()
    {
        base.Run();
        
        //  该死的正常死
        //  有技能的则按照顺序播放死亡技能 后再正常死
        if (isFinish)
            return;
 
 
        if (!isRunOnce)
        {
            isRunOnce = true;
 
            SkillRecordAction waitingAnimeAction = playSkillRecordAction;
 
            foreach (var battleDeadPack in deadPackList)
            {
                if (battleDeadPack.deadTriggerSkill != null)
                {
                    SkillRecordAction skillAction = battleDeadPack.deadTriggerSkill.CreateSkillAction();
                    if (null != skillAction)
                    {
                        deathActionDict.Add(battleDeadPack, skillAction);
                        
                        // 【使用 BattleField.recordPlayer】
                        // 原因:死亡触发技能是顶层的RecordAction,不是在某个RecordAction内部产生的
                        // 虽然可能是因为技能导致的死亡,但死亡触发的技能本身应该是独立的
                        // 使用ImmediatelyPlay并设置WaitingPlay=true,可以让死亡技能等待导致死亡的技能完成
                        // 如果DeathRecordAction有父节点(导致死亡的技能),则等待那个父节点
                        // 否则等待DeathRecordAction本身
                        if (waitingAnimeAction == null)
                        {
                            battleField.recordPlayer.ImmediatelyPlay(skillAction, this, true);
                        }
                        else
                        {
                            waitingAnimeAction.GetInnerRecordPlayer().ImmediatelyPlay(skillAction, waitingAnimeAction);
                        }
 
                        if (battleDeadPack.deadTriggerSkill.NeedWaiting() || waitingAnimeAction != null)
                        {
                            waitingAnimeAction = skillAction;
                        }
                    }
                }
                else
                {
                    deadActionStatesDict.Add(battleDeadPack, CreateDeadActionState(battleDeadPack));
                }
            }
        }
        else
        {
            List<BattleDeadPack> finishedKeys = new List<BattleDeadPack>();
            foreach (var kv in deathActionDict)
            {
                if (!kv.Key.isPlaySkill)
                {
                    // battleField.recordPlayer.ImmediatelyPlay(kv.Value);
                    kv.Key.isPlaySkill = true;
                }
 
                if (!kv.Value.IsFinished())
                {
                    kv.Value.Run();
                }
                else
                {
                    deadActionStatesDict.Add(kv.Key, CreateDeadActionState(kv.Key, true));
                    finishedKeys.Add(kv.Key);
                }
                break;
            }
 
            foreach (var key in finishedKeys)
            {
                deathActionDict.Remove(key);
            }
 
            if (0 == deathActionDict.Count)
            {
                hasDeathTriggerSkill = false;
            }
 
            int completeNum = 0;
 
            foreach (var kv in deadActionStatesDict)
            {
                if (kv.Value())
                {
                    completeNum++;
                }
            }
 
            if (completeNum == deadPackList.Count)
            {
                // 死亡处理完成后,分发死亡后的包
                if (!hasDistributedPacksAfterDeath)
                {
                    hasDistributedPacksAfterDeath = true;
                    DistributePacksAfterDeath();
                }
                
                isActionCompleted = true;
                isFinish = true;
            }
        }
    }
 
 
 
    //  重写IsActionCompleted,死亡动作是否完成
    public override bool IsActionCompleted()
    {
        return isActionCompleted || isFinish;
    }
 
    private Func<bool> CreateDeadActionState(BattleDeadPack deadPack, bool withoutAnime = false)
    {
        BattleObject deadObj = battleField.battleObjMgr.GetBattleObject((int)deadPack.deadPack.ObjID);
        if (null == deadObj)
        {
            return () => true;
        }
 
        bool playDeath = false;
 
        bool isComplete = false;
 
        //  如果没有释放技能 则直接死亡
        if (deadObj.CanStartDeath())
        {
            PerformDrop(deadObj);
 
            deadObj.OnDeath(() => {
                isComplete = true;
            }, withoutAnime);
 
            playDeath = true;
        }
 
 
 
        return () =>
        {
            //  还没播放死亡 并且没释放其他技能
            if (!playDeath && deadObj.CanStartDeath())
            {
                PerformDrop(deadObj);
 
                deadObj.OnDeath(() => {
                    isComplete = true;
                }, withoutAnime);
 
                playDeath = true;
            }
            
            if (deadObj.IsReborning())
            {
                isComplete = true;
            }
 
            return isComplete;
        };
    }
 
    public override void ForceFinish()
    {
        if (isFinish)
            return;
 
        isFinish = true;
 
        // 强制结束所有死亡触发技能
        foreach (var kv in deathActionDict)
        {
            kv.Value.ForceFinish();
        }
        deathActionDict.Clear();
 
        // 清理状态字典,防止回调继续等待
        deadActionStatesDict.Clear();
 
        //  直接结束所有死亡对象
        foreach (var deadPack in deadPackList)
        {
            BattleObject deadObj = battleField.battleObjMgr.GetBattleObject((int)deadPack.deadPack.ObjID);
            if (null != deadObj)
            {
                PerformDrop(deadObj);
                deadObj.SetDeath();
            }
        }
 
        // 强制分发死亡后的包
        if (!hasDistributedPacksAfterDeath)
        {
            hasDistributedPacksAfterDeath = true;
            DistributePacksAfterDeath();
        }
 
        base.ForceFinish();
    }
 
    private void PerformDrop(BattleObject deadObj)
    {
        //  只有主线掉落物品
        if (battleField.MapID == 1 || battleField.MapID == 2)
        {
            if (dropStateDict.ContainsKey(deadObj.ObjID))
            {
                return;
            }
            dropStateDict.Add(deadObj.ObjID, true);
            deadObj.PerformDrop();
        }
    }
 
    protected bool HasDeathTriggerSkill()
    {
        return hasDeathTriggerSkill;
    }
 
    // 分发死亡后的包
    protected void DistributePacksAfterDeath()
    {
        // 遍历所有死亡包,分发它们的 packListAfterDeath
        foreach (var deadPack in deadPackList)
        {
            if (deadPack.packListAfterDeath != null && deadPack.packListAfterDeath.Count > 0)
            {
                foreach (var pack in deadPack.packListAfterDeath)
                {
                    // 获取包的类型和UID用于调试
                    string packType = pack.GetType().Name;
                    ulong packUID = 0;
                    var packUIDField = pack.GetType().GetField("packUID");
                    if (packUIDField != null)
                    {
                        packUID = (ulong)packUIDField.GetValue(pack);
                    }
                    
                    
                    // 特殊处理 CustomHB426CombinePack:使用其自己的 Distribute 方法
                    if (pack is CustomHB426CombinePack combinePack)
                    {
                        combinePack.Distribute(playSkillRecordAction);
                    }
                    // 特殊处理 HB427_tagSCUseSkill:创建技能包并分发
                    else if (pack is HB427_tagSCUseSkill skillPack)
                    {
                        var skillAction = CustomHB426CombinePack.CreateSkillAction(battleField.guid, new List<GameNetPackBasic>() { skillPack });
                        if (skillAction != null)
                        {
                            if (playSkillRecordAction != null)
                            {
                                playSkillRecordAction.GetInnerRecordPlayer().PlayRecord(skillAction);
                            }
                            else
                            {
                                battleField.PlayRecord(skillAction);
                            }
                        }
                    }
                    else
                    {
                        // 【使用 causingRecordAction 或 battleField.recordPlayer】
                        // 原因:死亡后的包应该回到导致死亡的技能所在的上下文
                        // 如果有 parentSkillAction(导致死亡的技能),则分发到它的 innerRecordPlayer
                        // 否则分发到 battleField.recordPlayer(顶层)
                        if (playSkillRecordAction != null)
                        {
                            PackageRegeditEx.DistributeToRecordAction(pack, playSkillRecordAction);
                        }
                        else
                        {
                            // 如果没有 causingRecordAction,直接分发(使用原始的 PackageRegedit)
                            PackageRegedit.Distribute(pack);
                        }
                    }
                }
                
                // 分发完成后清理列表,防止重复分发和内存泄漏
                deadPack.packListAfterDeath.Clear();
            }
        }
    }
 
    public override bool CanStartExecution()
    {
        //  如果不是WaitingPlay模式,直接可以执行
        if (!isWaitingPlay)
        {
            return true;
        }
        
        //  如果没有父节点,也可以执行
        if (parentSkillAction == null)
        {
            return true;
        }
 
        //  可以先执行没有死亡触发技能的死亡动作
        if (!HasDeathTriggerSkill())
        {
            return true;
        }
        
        //  WaitingPlay模式下,需要等待直接父节点的动作完成(不管父节点是否WaitingPlay)
        if (!parentSkillAction.IsActionCompleted())
        {
            BattleDebug.LogError($"RecordAction.CanStartExecution: {this.GetType().Name} 等待父节点 {parentSkillAction.GetType().Name} 动作完成");
            return false;
        }
        
        return true;
    }
 
    public override bool NeedWaitSibling()
    {
        return HasDeathTriggerSkill();
    }
 
#if UNITY_EDITOR
    /// <summary>
    /// 首次运行时打印日志(仅编辑器)
    /// 打印死亡对象的名字
    /// </summary>
    protected override void PrintFirstRunLog()
    {
        if (deadPackList != null && deadPackList.Count > 0)
        {
            string deadNames = "";
            foreach (var deadPack in deadPackList)
            {
                BattleObject deadObj = battleField.battleObjMgr.GetBattleObject((int)deadPack.deadPack.ObjID);
                string name = deadObj?.GetName() ?? "Unknown";
                deadNames += name + ",";
            }
            deadNames = deadNames.TrimEnd(',');
            Debug.LogError($"[DeathRecordAction首次Run] 死亡对象:{deadNames} 数量:{deadPackList.Count}");
        }
        else
        {
            base.PrintFirstRunLog();
        }
    }
#endif
}