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(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 allList = battleField.battleObjMgr.allBattleObjDict.Values.ToList(); 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(); 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(targetSet) { caster }; var allList = battleField.battleObjMgr.allBattleObjDict.Values.ToList(); // 构造集合便于判断 var targetSetLookup = new HashSet(targetSet); var highlightSet = new HashSet(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); } }