using System.Collections.Generic; using System.Linq; using UnityEngine; // SkillBase(Finish 部分):完成判定与强制结束。 public partial class SkillBase { public virtual bool IsActionCompleted() { if (!isPlay) return false; if (skillEffect != null) { if (!skillEffect.IsFinished()) return false; } if (moveFinished) { // 如果技能有动画(SkillMotionName不为空),需要等待动画播放完成 if (skillSkinConfig != null && !string.IsNullOrEmpty(skillSkinConfig.SkillMotionName)) { if (!isMotionCompleted) { BattleDebug.LogError($"SkillBase.IsActionCompleted: 技能 {skillConfig.SkillID} 等待动画播放完成"); return false; } } return true; } return false; } // 检查技能是否完成:综合检查所有完成条件 public virtual bool IsFinished() { if (!isPlay) { ReportStuckIfNeeded("isPlay=false(OnSkillStart 未被调用或提前退出)"); return false; } bool tempRetValue = true; // 检查技能效果是否完成 if (skillEffect != null) { if (!skillEffect.IsFinished()) { ReportStuckIfNeeded("skillEffect 未完成"); return false; } skillEffect = null; OnSkillFinished(); tempRetValue = false; } // 检查其他技能动作是否完成 if (currentWaitingSkill.Count > 0) { if (currentWaitingSkill.Any(s => s.IsFinished())) { currentWaitingSkill.RemoveAll(s => s.IsFinished()); OnSkillFinished(); } else { tempRetValue = false; } } if (!tempRetValue) { if (currentWaitingSkill.Count > 0) { ReportStuckIfNeeded($"currentWaitingSkill 仍有 {currentWaitingSkill.Count} 个子动作"); } return false; } // 检查最终完成状态 if (isFinished && moveFinished) { if (packList.Count > 0) { OnSkillFinished(); ReportStuckIfNeeded($"packList 仍剩 {packList.Count} 个包,ResolvePackList 未消费完"); return false; } // 如果自己内部的recora action的 inner record player还有没执行完的包 也是返回false if (ownRecordAction != null && ownRecordAction.GetInnerRecordPlayer().IsPlaying()) { ReportStuckIfNeeded("ownRecordAction.innerRecordPlayer.IsPlaying() == true(子 RecordAction 未播完)"); return false; } // 技能完全结束,移除技能注册并触发延迟的死亡判定 // useInnerPlayer=true:DeathRecordAction 投到 ownRecordAction.innerRecordPlayer,等待当前技能完成 // clearEvenIfNotDispatched=false:保持原有行为,OnObjsDead 返回 null 时不清空缓存 if (FlushPendingDeathActions(useInnerPlayer: true, clearEvenIfNotDispatched: false)) { ReportStuckIfNeeded("FlushPendingDeathActions 投递了 DeathRecordAction,等待其播放完成"); return false; } bool done = !ownRecordAction.GetInnerRecordPlayer().IsPlaying(); if (!done) ReportStuckIfNeeded("末尾 innerRecordPlayer.IsPlaying() == true"); else ResetStuckCounter(); return done; } ReportStuckIfNeeded($"isFinished={isFinished} moveFinished={moveFinished}(还没到末段)"); return false; } #if UNITY_EDITOR // 卡死侦测计数器:IsFinished 连续多少次返回 false。 // 第 StuckThreshold 次首次 dump,之后每 StuckRepeatInterval 次再 dump,避免刷屏。 private int _stuckCheckCount = 0; private string _lastStuckReason = null; private const int StuckThreshold = 120; // ~2 秒(60fps) private const int StuckRepeatInterval = 180; // ~3 秒 #endif /// /// 当 IsFinished 持续返回 false 时打一次详细诊断。reason 描述阻塞原因。 /// 只在 UNITY_EDITOR + BattleDebug 开关下输出。 /// private void ReportStuckIfNeeded(string reason) { #if UNITY_EDITOR _stuckCheckCount++; // 阻塞原因切换了 → 立刻打一次,并重置计数 bool reasonChanged = _lastStuckReason != reason; if (reasonChanged) { _lastStuckReason = reason; _stuckCheckCount = 1; } bool firstHit = _stuckCheckCount == StuckThreshold; bool repeat = _stuckCheckCount > StuckThreshold && (_stuckCheckCount - StuckThreshold) % StuckRepeatInterval == 0; if (!firstHit && !repeat) return; int skillId = skillConfig != null ? skillConfig.SkillID : 0; ulong casterId = tagUseSkillAttack != null ? tagUseSkillAttack.ObjID : 0UL; string subSkillDump = " (无)"; if (currentWaitingSkill.Count > 0) { var lines = new List(); for (int i = 0; i < currentWaitingSkill.Count; i++) { var s = currentWaitingSkill[i]; lines.Add($" [{i}] type={s.GetType().Name} IsFinished={s.IsFinished()}"); } subSkillDump = string.Join("\n", lines); } string packListDump = " (无)"; if (packList != null && packList.Count > 0) { var lines = new List(); for (int i = 0; i < packList.Count && i < 8; i++) { var p = packList[i]; string tag = p is CustomHB426CombinePack cb ? $" tag={cb.startTag?.Tag}" : ""; lines.Add($" [{i}] {p.GetType().Name}{tag}"); } if (packList.Count > 8) lines.Add($" ... 还有 {packList.Count - 8} 个"); packListDump = string.Join("\n", lines); } bool innerPlaying = ownRecordAction != null && ownRecordAction.GetInnerRecordPlayer().IsPlaying(); string innerPlayerDump = " (innerRecordPlayer: 无)"; if (ownRecordAction != null) { innerPlayerDump = " innerRecordPlayer: " + ownRecordAction.GetInnerRecordPlayer().DumpPlayingState(); } // 额外诊断:caster 动画状态 + SkillEffect 内部标志位 + 技能动作名 string casterAnim = " (caster 信息不可用)"; if (caster is HeroBattleObject hbo && hbo.motionBase != null) { casterAnim = $" caster.motionBase: playingSkillWithAnim={hbo.motionBase.PlayingSkillWithAnimForDebug}"; // [卡死诊断] 当 playingSkillWithAnim=true 但本 skillBase 还没 OnSkillStart, // 一次性把 MotionBase 的现场也 dump 出来:锁是谁加的 + 活跃轨道列表 + 加锁时的调用栈。 // 这样 SkillBase 的 120 帧卡死报告就能直接定位到 MotionBase 侧的 owner skill。 if (hbo.motionBase.PlayingSkillWithAnimForDebug) { int ownerSid = hbo.motionBase.PlayingSkillAnimOwnerSkillIdForDebug; int ownerFrame = hbo.motionBase.PlayingSkillAnimOwnerFrameForDebug; int elapsed = ownerFrame > 0 ? (UnityEngine.Time.frameCount - ownerFrame) : -1; casterAnim += $"\n MotionBase锁持有者: skillId={ownerSid} setFrame={ownerFrame} 已持有{elapsed}帧"; casterAnim += $"\n MotionBase现场: {hbo.motionBase.DumpActiveTracksForDebug()}"; string ownerStack = hbo.motionBase.PlayingSkillAnimOwnerStackForDebug; if (!string.IsNullOrEmpty(ownerStack)) { casterAnim += $"\n MotionBase锁加锁调用栈:\n{ownerStack}"; } } } string skinInfo = $" skillSkinConfig.SkillMotionName={(skillSkinConfig == null ? "null" : (string.IsNullOrEmpty(skillSkinConfig.SkillMotionName) ? "(空)" : skillSkinConfig.SkillMotionName))}"; string skillEffectDump = skillEffect == null ? " skillEffect=null" : $" skillEffect: {skillEffect.DumpState()}"; BattleDebug.LogError( "SkillBase.IsFinished 疑似卡死 (持续 " + _stuckCheckCount + " 次未完成)\n" + $" skillId={skillId} caster={casterId} 原因: {reason}\n" + $" StateFlags={_stateFlags}\n" + $"{skillEffectDump}\n" + $"{skinInfo}\n" + $"{casterAnim}\n" + $" currentWaitingSkill.Count={currentWaitingSkill.Count}\n{subSkillDump}\n" + $" packList.Count={(packList?.Count ?? 0)}\n{packListDump}\n" + $" innerRecordPlayer.IsPlaying={innerPlaying}\n" + $"{innerPlayerDump}\n" + $" tempDeadPackList.Count={tempDeadPackList.Count}\n" + $" buffPackCollections.Count={buffPackCollections.Count}"); #endif } private void ResetStuckCounter() { #if UNITY_EDITOR _stuckCheckCount = 0; _lastStuckReason = null; #endif } // 强制结束技能:立即结束所有技能相关的处理 public virtual void ForceFinished() { if (isFinished) return; // 强制结束路径:移除注册 + 投递死亡动作(不等技能完成,直接播放)+ 始终清空缓存。 // useInnerPlayer=false 保持原 ForceFinished 的行为(不指定 _playSkillRecordAction)。 // clearEvenIfNotDispatched=true 保持 ForceFinished 原有的"无论是否投递都 Clear"语义。 FlushPendingDeathActions(useInnerPlayer: false, clearEvenIfNotDispatched: true); // 1. 强制结束技能效果 skillEffect?.ForceFinished(); skillEffect = null; // 2. 强制结束所有子技能动作 if (currentWaitingSkill.Count > 0) { foreach (var skill in currentWaitingSkill) { skill.ForceFinish(); } currentWaitingSkill.Clear(); } // 3. 清理 DOTween 动画(防止移动回调在战斗结束后执行) if (caster != null) { caster.StopMoveAnimation(); } // 4. 重置施法者状态 if (caster != null) { // 重置位置到原点 caster.ResetPosition(); // 重置朝向 caster.ResetFacing(); // 取消幻影效果 caster.ShowIllusionShadow(false); } // 5. 恢复 UI 状态 if (battleField != null) { // 恢复所有角色的显示层级和血条 var allList = battleField.battleObjMgr?.allBattleObjDict?.Values; if (allList != null) { foreach (BattleObject bo in allList) { bo.layerMgr?.SetFront(); bo.GetHeroInfoBar()?.SetActive(true); } } // 关闭技能遮罩 if (battleField.battleRootNode != null && battleField.battleRootNode.skillMaskNode != null) { battleField.battleRootNode.skillMaskNode.SetActive(false); } } isFinished = true; moveFinished = true; isPlay = true; // 强制结束时,无论是否有动画,都标记动画完成 isMotionCompleted = true; // 6. 处理所有剩余包(包括 buff 包) // 先处理 buffPackCollections DistributeBuffPacks(buffPackCollections); buffPackCollections.Clear(); // 处理剩余的 packList 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.fromSkill = this; otherSkillAction.ForceFinish(); } else { // 【使用 parentRecordAction.innerRecordPlayer】 // 原因:ForceFinished时剩余的包也是技能内部产生的,应该由innerRecordPlayer管理 // 这样可以确保即使强制结束,包的处理也在正确的上下文中 PackageRegedit.Distribute(pack); } } } // 技能完成处理:正常完成时的清理工作 public void OnSkillFinished() { // 修复:使用循环代替递归,避免栈溢出风险 while (true) { // 验证技能效果是否完成 if (skillEffect != null && !skillEffect.IsFinished()) return; if (skillEffect != null) { skillEffect = null; continue; // 使用continue代替递归调用 } // 验证其他技能动作是否完成 if (currentWaitingSkill.Count > 0) { bool hasFinishedAction = currentWaitingSkill.All(s => s.IsFinished()); if (hasFinishedAction) { // 修复死循环:完成后需要清空 currentWaitingSkill currentWaitingSkill.Clear(); continue; // 使用continue代替递归调用 } return; } break; // 没有更多需要处理的,退出循环 } // 处理剩余包 if (!ResolvePackList()) { return; } isFinished = true; } protected virtual bool ResolvePackList() { if (currentWaitingSkill.Count > 0) { return false; } while (packList.Count > 0) { var pack = packList[0]; packList.RemoveAt(0); if (pack is CustomHB426CombinePack combinePack && combinePack.startTag.Tag.StartsWith("Skill_")) { var skillRecordAction = combinePack.CreateSkillAction(); skillRecordAction.fromSkill = this; currentWaitingSkill.Add(skillRecordAction); // 需要给真正parent播的 if (skillRecordAction.useParentRecordPlayer && skillRecordAction.parentSkillAction != null) { skillRecordAction.parentSkillAction.GetInnerRecordPlayer().PlayRecord(skillRecordAction); } else { ownRecordAction.GetInnerRecordPlayer().PlayRecord(skillRecordAction); } return false; } else if (IsBuffPack(pack)) { // 从找到第一个 Buff 包开始,收集连续的 HB428/HB429 包 buffPackCollections.Add(pack); while (packList.Count > 0) { var nextPack = packList[0]; if (IsBuffPack(nextPack)) { buffPackCollections.Add(nextPack); packList.RemoveAt(0); } else { break; } } // 处理所有收集到的 buff 包 ProcessBuffPacks(buffPackCollections); // 清空已处理的 buff 集合 buffPackCollections.Clear(); continue; } else { // 【使用 parentRecordAction.innerRecordPlayer】 // 原因:技能执行过程中的包(Buff、属性刷新等)是技能效果的一部分 // 应该由SkillRecordAction的innerRecordPlayer管理,确保与技能生命周期一致 PackageRegeditEx.DistributeToRecordAction(pack, ownRecordAction); } } return true; } // 添加清理方法:防止内存泄漏 public virtual void Cleanup() { tempDropList?.Clear(); tempDeadPackList?.Clear(); currentWaitingSkill?.Clear(); dropPackList?.Clear(); expPackList?.Clear(); buffPackCollections?.Clear(); skillEffect = null; packList = null; } public virtual bool CanStartExecution() { if (null == caster) { return false; } if (null == skillConfig) { return false; } if (string.IsNullOrEmpty(skillSkinConfig.SkillMotionName)) { return true; } return !battleField.IsCastingSkill(caster.ObjID); } }