49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Linq;
|
|
using Animancer;
|
|
using UnityEngine;
|
|
|
|
namespace BaseGames.Enemies.Abilities
|
|
{
|
|
/// <summary>
|
|
/// 面向玩家能力:朝向玩家后播放转身/翻转动画并等待完成。
|
|
/// CanUse 重写:仅在无其他技能运行且玩家在背后时可用。
|
|
/// 可复用于任何需要"检测背后玩家并翻转"的敌人。
|
|
/// </summary>
|
|
public class FacePlayerAbility : EnemyAbilityBase
|
|
{
|
|
private FacePlayerAbilitySO _cfg;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
_cfg = ResolveConfig<FacePlayerAbilitySO>();
|
|
}
|
|
|
|
public override bool CanUse =>
|
|
base.CanUse
|
|
&& !_enemy.Abilities.All.Any(a => a != this && a.IsRunning)
|
|
&& IsPlayerBehind();
|
|
|
|
private bool IsPlayerBehind()
|
|
{
|
|
if (_enemy.PlayerTransform == null) return false;
|
|
float dx = _enemy.PlayerTransform.position.x - _enemy.transform.position.x;
|
|
int facing = _enemy.Movement?.FacingDirection ?? 1;
|
|
return (facing > 0 && dx < 0) || (facing < 0 && dx > 0);
|
|
}
|
|
|
|
protected override IEnumerator ExecuteCoroutine()
|
|
{
|
|
Phase = AbilityRunState.Active;
|
|
|
|
_enemy.FacePlayer();
|
|
|
|
if (_cfg == null || _cfg.faceClip.Clip == null) yield break;
|
|
|
|
_animancer.Play(_cfg.faceClip);
|
|
yield return EnemyAbilityWaits.Get(_cfg.faceClip.Clip.length);
|
|
}
|
|
}
|
|
}
|