yyl
2026-04-24 64d526e0f5b9f49fe5bd6dcf043f5071d996e7ad
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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Spine;
 
// SkillBase(Cast 部分):施法阶段——移动、动画、残影、高亮、攻击结束。
public partial class SkillBase
{
    // 技能释放主逻辑:广播事件、高亮目标、执行释放
    public virtual void Cast()
    {
#if UNITY_EDITOR
        //  [前冲诊断] Cast() 入口:记录 skillId/caster/BattleType/当前位置/当前是否还在 tween。
        //  用来判断新技能是否在上一个技能的回位 tween 还没完成时就进入 Cast()。
        {
            Vector2 castEntryPos = (caster != null && caster.GetRectTransform() != null)
                ? caster.GetRectTransform().anchoredPosition : Vector2.zero;
            bool casterTweening = caster != null && caster.GetRectTransform() != null
                && DG.Tweening.DOTween.IsTweening(caster.GetRectTransform());
            BattleDebug.LogError($"[前冲诊断] Cast 入口 skillId={skillConfig?.SkillID} caster={caster?.ObjID} battleType={tagUseSkillAttack?.BattleType} castMode={skillSkinConfig?.castMode} anchoredPos={castEntryPos} casterTweening={casterTweening}");
        }
#endif
        // 广播技能释放事件
        string guid = battleField.guid;
        // 获取释放者数据:Hero 传递 teamHero,Mingge 传递 null(因为事件监听器只处理 Hero 数据)
        TeamHero teamHero = null;
        if (caster is HeroBattleObject heroBattleObject)
        {
            teamHero = heroBattleObject.teamHero;
        }
        // 命格释放技能时 teamHero 为 null,监听器会正确处理(已有 null 检查)
        EventBroadcast.Instance.Broadcast<string, SkillConfig, TeamHero>(EventName.BATTLE_CAST_SKILL, guid, skillConfig, teamHero);
 
        if (skillSkinConfig.SkinllSFX1 != 0)
        {
            battleField.soundManager.PlayEffectSound(skillSkinConfig.SkinllSFX1, false);
        }
 
        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);
            }
 
            Hint(caster, hintConfig);
        }
 
        // 高亮所有本次技能相关的目标
        HighLightAllTargets();
 
        // 根据释放模式执行相应逻辑
        switch (skillSkinConfig.castMode)
        {
            case SkillCastMode.None:
            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 " + skillSkinConfig.CastPosition);
                ForceFinished();
                break;
        }
    }
 
    protected void Hint(BattleObject battleObject, DamageNumConfig hintConfig)
    {
        if (hintConfig != null)
        {
            battleObject.ShowTips(((char)hintConfig.prefix).ToString(), true, false, 1.25f);
        }
    }
 
    // 冲撞攻击模式(待实现)
    protected void DashCast(Action _onComplete)
    {
        Debug.LogError("DashCast 还没实现");
        ForceFinished();
    }
 
    // 对敌方释放技能:移动到敌方区域进行攻击
    protected void CastToEnemy()
    {
        RectTransform target = battleField.GetTeamNode(caster.GetEnemyCamp(), skillSkinConfig);
        ExecuteMoveAndCastSequence(target, () =>
        {
            if (skillConfig.ClientTriggerTiming == 1)
            {
                OnAttackFinish();
            }
            else
            {
                // ShadowIllutionCreate(true);
                MoveToTarget(battleField.GetTeamNode(caster.Camp, caster.GetPositionNum()), Vector2.zero, () =>
                {
                    // ShadowIllutionCreate(false);
                    OnAttackFinish();
                }, MoveSpeed);
            }
        });
    }
 
    // 对指定目标释放技能:移动到主要目标位置进行攻击
    protected void CastToTarget()
    {
        if (tagUseSkillAttack.HurtCount <= 0)
        {
            Debug.LogError("技能攻击包没有目标 HurtCount <= 0");
            OnSkillFinished();
            return;
        }
 
        int mainTargetPosNum = BattleUtility.GetMainTargetPositionNum(this, 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.GetPositionNum());
            // ShadowIllutionCreate(true);
            MoveToTarget(rectTransform, Vector2.zero, () =>
            {
                // ShadowIllutionCreate(false);
                OnAttackFinish();
            }, MoveSpeed);
        });
    }
 
    // 对友方释放技能:移动到友方区域进行治疗或增益
    protected void CastToAllies()
    {
        RectTransform target = battleField.GetTeamNode(caster.Camp, skillSkinConfig);
        ExecuteMoveAndCastSequence(target, () =>
        {
            if (skillConfig.ClientTriggerTiming == 1)
            {
                OnAttackFinish();
            }
            else
            {
                // ShadowIllutionCreate(true);
                MoveToTarget(battleField.GetTeamNode(caster.Camp, caster.GetPositionNum()), Vector2.zero, () =>
                {
                    // ShadowIllutionCreate(false);
                    OnAttackFinish();
                }, MoveSpeed);
            }
        });
    }
 
    // 执行移动-施法-返回序列:通用的移动攻击流程
    private void ExecuteMoveAndCastSequence(RectTransform target, Action onReturnComplete)
    {
#if UNITY_EDITOR
        BattleDebug.LogError($"[前冲诊断] ExecuteMoveAndCastSequence 开始 skillId={skillConfig?.SkillID} caster={caster?.ObjID} battleType={tagUseSkillAttack?.BattleType} CastDistance={skillSkinConfig?.CastDistance} castMode={skillSkinConfig?.castMode}");
#endif
        ShadowIllutionCreate(true);
        MoveToTarget(target, new Vector2(skillSkinConfig.CastDistance, 0), () =>
        {
#if UNITY_EDITOR
            BattleDebug.LogError($"[前冲诊断] 前冲完成 skillId={skillConfig?.SkillID} caster={caster?.ObjID} 准备 CastImpl");
#endif
            if (skillSkinConfig.CastDistance < 9999 && skillSkinConfig.SkinllSFX2 != 0)
            {
                battleField.soundManager.PlayEffectSound(skillSkinConfig.SkinllSFX2, false);
            }
 
            TurnBack(() =>
            {
                ShadowIllutionCreate(false);
 
                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 = 750f)
    {
#if UNITY_EDITOR
        //  [前冲诊断] 记录入口参数:CastDistance、offset、speed;以及当前 caster 的锚点位置。
        Vector2 fromPos = caster != null && caster.GetRectTransform() != null
            ? caster.GetRectTransform().anchoredPosition : Vector2.zero;
        bool mttTweening = caster != null && caster.GetRectTransform() != null
            && DG.Tweening.DOTween.IsTweening(caster.GetRectTransform());
        BattleDebug.LogError($"[前冲诊断] MoveToTarget 入口 skillId={skillConfig?.SkillID} caster={caster?.ObjID} battleType={tagUseSkillAttack?.BattleType} CastDistance={skillSkinConfig?.CastDistance} offset={offset} speed={speed} fromPos={fromPos} casterTweening={mttTweening}");
#endif
        if (skillSkinConfig.CastDistance >= 9999)
        {
#if UNITY_EDITOR
            BattleDebug.LogError($"[前冲诊断] CastDistance>=9999 直接跳过移动 skillId={skillConfig?.SkillID} caster={caster?.ObjID}");
#endif
            _onComplete?.Invoke();
            return;
        }
 
        caster.PlayAnimation(MotionName.run, true);
#if UNITY_EDITOR
        //  [前冲诊断] 记录 target 的名字/世界坐标/父节点 scale,便于定位镜像坐标系导致 offset 方向反转
        string targetName = target != null ? target.name : "(null)";
        Vector3 targetWorld = target != null ? target.position : Vector3.zero;
        Vector3 targetLossyScale = target != null ? (Vector3)target.lossyScale : Vector3.one;
        Vector2 targetAnchored = target != null ? target.anchoredPosition : Vector2.zero;
        BattleDebug.LogError($"[前冲诊断] target信息 skillId={skillConfig?.SkillID} caster={caster?.ObjID} casterCamp={caster?.Camp} target.name={targetName} target.anchoredPos={targetAnchored} target.worldPos={targetWorld} target.lossyScale={targetLossyScale}");
#endif
        var tweener = BattleUtility.MoveToTarget(caster.GetRectTransform(), target, offset, () =>
        {
#if UNITY_EDITOR
            Vector2 toPos = caster != null && caster.GetRectTransform() != null
                ? caster.GetRectTransform().anchoredPosition : Vector2.zero;
            BattleDebug.LogError($"[前冲诊断] MoveToTarget 完成 skillId={skillConfig?.SkillID} caster={caster?.ObjID} toPos={toPos}");
#endif
            //  tween 完成时清除 caster 上的 activeMoveTween 句柄,放开 CanCastSkillAnimation 的闸门。
            if (caster != null)
            {
                caster.activeMoveTween = null;
            }
            caster.PlayAnimation(MotionName.idle, true);
            _onComplete?.Invoke();
        }, speed);
        //  记录到 caster,让 CanCastSkillAnimation 能精确等待这一个 tween(而不是 caster 身上任意 tween)。
        if (caster != null)
        {
            caster.activeMoveTween = tweener;
        }
        battleField.battleTweenMgr.OnPlayTween(tweener);
    }
 
    // 转身逻辑:根据技能配置处理角色转向
    protected void TurnBack(Action _onComplete, float forward)
    {
        if (skillSkinConfig.CastDistance < 0)
        {
            caster.SetFacing(forward);
        }
        _onComplete?.Invoke();
    }
 
    // 攻击完成后的处理:转身、恢复状态、播放待机动画
    protected void OnAttackFinish()
    {
#if UNITY_EDITOR
        //  [前冲诊断] OnAttackFinish 入口:记录帧号和当前位置,和 Cast()/MoveToTarget 日志对齐。
        {
            Vector2 finPos = (caster != null && caster.GetRectTransform() != null)
                ? caster.GetRectTransform().anchoredPosition : Vector2.zero;
            bool finTweening = caster != null && caster.GetRectTransform() != null
                && DG.Tweening.DOTween.IsTweening(caster.GetRectTransform());
            BattleDebug.LogError($"[前冲诊断] OnAttackFinish skillId={skillConfig?.SkillID} caster={caster?.ObjID} battleType={tagUseSkillAttack?.BattleType} anchoredPos={finPos} casterTweening={finTweening}");
        }
#endif
        TurnBack(null, 1f);
        OnAllAttackMoveFinished();
        caster.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.GetHeroInfoBar()?.SetActive(true);
        }
        battleField.battleRootNode.skillMaskNode.SetActive(false);
    }
 
    // 执行技能释放动画和逻辑:播放施法动作并提供回调
    protected TrackEntry CastImpl(Action onComplete = null)
    {
        return caster.PlaySkillAnimation(skillConfig, skillSkinConfig, this, tagUseSkillAttack.BattleType == 4, onComplete);
    }
 
    // 残影效果开关:连击/反击/追击时开启彩色残影并加速
    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.ShowIllusionShadow(true, color);
            }
        }
        else
        {
            MoveSpeed = 750f;
            caster.ShowIllusionShadow(false);
        }
    }
 
    // 高亮所有相关目标:设置施法者和目标的显示层级
    protected void HighLightAllTargets()
    {
        caster.layerMgr.SetSortingOrder(BattleConst.SkillMaskOrder + 1);// offset是3 英雄层级 +1就是 active级别 
 
        if (skillConfig.FuncType != 2)
            return;
 
        // 收集所有目标(包含 HurtList、每个 Hurt 的 HurtListEx、以及顶层 HurtListEx)
        var targetSet = new HashSet<BattleObject>();
        if (tagUseSkillAttack != null)
        {
            // 主目标列表
            if (tagUseSkillAttack.HurtList != null)
            {
                foreach (var hurt in tagUseSkillAttack.HurtList)
                {
                    var bo = battleField.battleObjMgr.GetBattleObject((int)hurt.ObjID);
                    if (bo != null) targetSet.Add(bo);
 
                    // 主目标的额外目标(弹射/平摊)
                    if (hurt.HurtListEx != null)
                    {
                        foreach (var hurtEx in hurt.HurtListEx)
                        {
                            var exBo = battleField.battleObjMgr.GetBattleObject((int)hurtEx.ObjID);
                            if (exBo != null) targetSet.Add(exBo);
                        }
                    }
                }
            }
 
            // 技能包顶层的 HurtListEx(如溅射、顶层平摊)
            if (tagUseSkillAttack.HurtListEx != null)
            {
                foreach (var hurtEx in tagUseSkillAttack.HurtListEx)
                {
                    var exBo = battleField.battleObjMgr.GetBattleObject((int)hurtEx.ObjID);
                    if (exBo != null) targetSet.Add(exBo);
                }
            }
        }
 
        // 确保施法者也被高亮(原逻辑)
        var highlightList = new List<BattleObject>(targetSet) { caster };
 
        var allList = battleField.battleObjMgr.allBattleObjDict.Values.ToList();
 
        // 构造集合便于判断
        var targetSetLookup = new HashSet<BattleObject>(targetSet);
        var highlightSet = new HashSet<BattleObject>(highlightList);
 
        // 先把施法者的 InfoBar 隐藏(原逻辑保留)
        caster.GetHeroInfoBar()?.SetActive(false);
 
        foreach (BattleObject bo in allList)
        {
            bool isHighlight = highlightSet.Contains(bo);
            bool isTarget = targetSetLookup.Contains(bo);
 
            if (isHighlight)
            {
                bo.layerMgr.SetFront();
            }
            else
            {
                bo.layerMgr.SetBack();
            }
 
            // 目标(含 HurtListEx)都应显示 InfoBar
            bo.GetHeroInfoBar()?.SetActive(isTarget);
        }
 
        battleField.battleRootNode.skillMaskNode.SetActive(true);
    }
}