feat: Update enemy AI and movement systems

- Enhanced Physics2D layer collision report with new interactions between Player and Enemy layers.
- Refactored BD_InvestigateLastKnown to streamline animation handling and improve readability.
- Simplified BD_MaintainCombatDistance by consolidating movement stop logic.
- Updated BD_MoveToPlayer to set AI phase on start.
- Improved BD_Patrol logic with better handling of stuck states and path failures.
- Enhanced BD_PatrolWaypoints to manage stuck conditions and retry logic more effectively.
- Refined BD_ReturnToHome to remove unnecessary animation calls.
- Updated BD_WalkRandom to ensure AI phase is set correctly on start.
- Improved EnemyAbilityBase to delegate target facing to the movement system.
- Enhanced EnemyBase with new movement methods for better control.
- Refactored EnemyMovement to introduce a new input system for handling movement and facing.
- Added EnemyMoveInput struct to encapsulate movement intentions.
- Updated Physics2DSettings to reflect new layer collision matrix.
- Introduced RTK CLI instructions for optimized command usage.
This commit is contained in:
2026-05-29 17:01:59 +08:00
parent e24ecc9589
commit bcd8b0e90b
19 changed files with 179534 additions and 175 deletions

View File

@@ -9,34 +9,19 @@ namespace BaseGames.Enemies.AI
{
/// <summary>
/// BD Action来回踱步巡逻——持续向当前方向移动遇墙或悬崖时自动翻转方向。
/// 转向检测依赖 EnemySensorHub 的 "wall_ahead" / "ledge" 槽SensorToolkit
///
/// 若需要按预设路点顺序巡逻,请使用 <see cref="BD_PatrolWaypoints"/>(支持 Transform 引用和内联坐标)。
///
/// 转向检测优先级:
/// <list type="number">
/// <item>EnemySensorHub "wall_ahead" / "ledge" 槽SensorToolkit已配置时使用</item>
/// <item>Physics2D Raycast 兜底Prefab 未配置 Sensor 时自动启用)</item>
/// </list>
/// </summary>
[TaskName("Patrol (Pace)")]
[TaskCategory("BaseGames/Enemy/Movement")]
[TaskDescription("来回踱步巡逻:遇墙或悬崖自动翻转方向(SensorToolkit 优先")]
[TaskDescription("来回踱步巡逻:遇墙或悬崖自动翻转方向(需配置 EnemySensorHub wall_ahead / ledge 槽")]
public class BD_Patrol : Action
{
[Tooltip("兜底检测地面边缘的向下射线长度m")]
public float edgeCheckLength = 1.2f;
[Tooltip("兜底检测障碍物的水平射线长度m")]
public float wallCheckLength = 0.4f;
[Tooltip("兜底边缘检测射线起点相对角色的前向偏移m")]
public float edgeCheckFwdOffset = 0.3f;
[Tooltip("兜底边缘检测射线起点相对角色的向下偏移m")]
public float edgeCheckDownOffset = 0.1f;
[Tooltip("(兜底)地面/墙壁 LayerMask")]
public LayerMask groundLayer;
private EnemyBase _enemy;
private EnemySensorHub _hub;
private float _dir = 1f;
private float _flipCooldown; // 翻转后短暂冷却,等待 RaySensor2D 刷新到新朝向
// 缓存SensorHub 中对应槽位是否已配置Awake 时查询一次,避免每帧 Dictionary 查找)
private bool _hasWallSensor;
@@ -50,14 +35,27 @@ namespace BaseGames.Enemies.AI
_hasEdgeSensor = _hub != null && _hub.Get(SensorSlotNames.Ledge) != null;
}
public override void OnStart() => _enemy?.SetAiPhase(AiPhase.Patrol);
public override void OnStart()
{
_enemy?.SetAiPhase(AiPhase.Patrol);
// 与敌人实际朝向同步,防止任务重入时 _dir 与朝向不符(如战斗后朝向已改变)
if (_enemy?.Movement != null)
_dir = _enemy.Movement.FacingDirection;
_flipCooldown = 0f;
}
public override TaskStatus OnUpdate()
{
if (_enemy == null) return TaskStatus.Failure;
if (ShouldFlip())
// 翻转冷却期间跳过传感器检测(等待 RaySensor2D 在新朝向完成刷新)
if (_flipCooldown > 0f)
_flipCooldown -= Time.deltaTime;
else if (ShouldFlip())
{
_dir = -_dir;
_flipCooldown = 0.1f; // ~6 帧缓冲60 fps防止传感器残留信号导致抖动
}
_enemy.MoveInDirection(_dir);
return TaskStatus.Running;
@@ -67,27 +65,11 @@ namespace BaseGames.Enemies.AI
private bool ShouldFlip()
{
if (_hub != null)
{
// 有传感器配置:用传感器结果,完全跳过 Raycast
if (_hasWallSensor || _hasEdgeSensor)
{
bool wallHit = _hasWallSensor && _hub.HasAnyDetection(SensorSlotNames.WallAhead);
bool edgeHit = _hasEdgeSensor && _hub.HasAnyDetection(SensorSlotNames.Ledge);
return wallHit || edgeHit;
}
}
// Raycast 兜底:仅在未配置 Sensor 时执行
Transform t = _enemy.transform;
Vector2 pos = t.position;
Vector2 edgeOrigin = pos + Vector2.right * (_dir * edgeCheckFwdOffset) + Vector2.down * edgeCheckDownOffset;
bool hasGround = Physics2D.Raycast(edgeOrigin, Vector2.down, edgeCheckLength, groundLayer);
if (!hasGround) return true;
bool hitWall = Physics2D.Raycast(pos, Vector2.right * _dir, wallCheckLength, groundLayer);
return hitWall;
// 转身进行中时不重复检测,防止 _dir 在转身期间被传感器残留信号反复翻转
if (_enemy.Movement != null && _enemy.Movement.IsTurning) return false;
bool wallHit = _hasWallSensor && _hub.HasAnyDetection(SensorSlotNames.WallAhead);
bool edgeHit = _hasEdgeSensor && _hub.HasAnyDetection(SensorSlotNames.Ledge);
return wallHit || edgeHit;
}
}
}