65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using UnityEngine;
|
||
using PathBerserker2d;
|
||
using BaseGames.Enemies;
|
||
|
||
namespace BaseGames.Enemies.Navigation
|
||
{
|
||
/// <summary>
|
||
/// PathBerserker2d 导航代理包装器(架构 07_EnemyModule §5)。
|
||
/// 实现 IPathAgent 接口,使 EnemyBase 和 BD Task 无需直接依赖 PB2d 类型。
|
||
/// PB2d API:UpdatePath(Vector2)、Stop()、TransformBasedMovement.movementSpeed、IsFollowingAPath。
|
||
/// </summary>
|
||
[RequireComponent(typeof(NavAgent))]
|
||
[RequireComponent(typeof(TransformBasedMovement))]
|
||
public class EnemyNavAgent : MonoBehaviour, IPathAgent
|
||
{
|
||
private NavAgent _navAgent;
|
||
private TransformBasedMovement _movement;
|
||
|
||
/// <summary>正在沿路径移动时为 true。</summary>
|
||
public bool IsMoving => _navAgent != null && _navAgent.IsFollowingAPath;
|
||
|
||
public event System.Action OnNavPathFailed;
|
||
|
||
private void Awake()
|
||
{
|
||
_navAgent = GetComponent<NavAgent>();
|
||
_movement = GetComponent<TransformBasedMovement>();
|
||
}
|
||
|
||
public void RequestMoveTo(Vector2 target)
|
||
{
|
||
_navAgent?.UpdatePath(target);
|
||
}
|
||
|
||
public void StopNavigation()
|
||
{
|
||
_navAgent?.Stop();
|
||
}
|
||
|
||
public bool IsAtDestination()
|
||
{
|
||
if (_navAgent == null) return true;
|
||
// 已停止 OR 在目标线段上且不再跟随路径
|
||
return _navAgent.IsIdle;
|
||
}
|
||
|
||
public void SetSpeed(float speed)
|
||
{
|
||
if (_movement != null) _movement.movementSpeed = speed;
|
||
}
|
||
|
||
public bool IsNearEdge()
|
||
{
|
||
// 双射线检测:脚下前方是否有地面
|
||
if (_navAgent == null) return false;
|
||
var origin = (Vector2)transform.position;
|
||
var facing = transform.localScale.x >= 0f ? Vector2.right : Vector2.left;
|
||
var groundMask = ~0; // 检测所有层;可收窄至 Ground 层
|
||
bool groundAhead = Physics2D.Raycast(origin + facing * 0.3f, Vector2.down, 0.5f, groundMask);
|
||
return !groundAhead;
|
||
}
|
||
}
|
||
}
|
||
|