feat: Add HurtBoxOwnerGuard to prevent multiple damage registrations from the same HitBox activation
- Implemented HurtBoxOwnerGuard to ensure that multiple HurtBoxes on the same character do not register damage multiple times during a single HitBox activation. - Added custom editor for HitBox to facilitate the creation of shape colliders with HitBoxColliderProxy. - Introduced PhysicsPerceptionSystem for enemy perception, supporting multiple detection modes including RangeCircle, BatchLOS, FanCast, and BoxCast. - Created EnemyPatrolZone to define patrol and chase areas for enemies, allowing for shared zones among multiple enemies. - Added BD_IsOutsideZone conditional task for Behavior Designer to check if an enemy or player is outside a defined patrol zone.
This commit is contained in:
@@ -3,44 +3,38 @@ using UnityEngine;
|
||||
using Opsive.BehaviorDesigner.Runtime.Tasks;
|
||||
using Opsive.BehaviorDesigner.Runtime.Tasks.Actions;
|
||||
using BaseGames.Enemies;
|
||||
using BaseGames.Enemies.Perception;
|
||||
|
||||
namespace BaseGames.Enemies.AI
|
||||
{
|
||||
/// <summary>
|
||||
/// BD Action:来回踱步巡逻——持续向当前方向移动,遇墙或悬崖时自动翻转方向。
|
||||
/// 转向检测依赖 EnemySensorHub 的 "wall_ahead" / "ledge" 槽(SensorToolkit)。
|
||||
/// 转向检测通过 <see cref="EnemyMovement.IsWallAhead"/> / <see cref="EnemyMovement.IsLedgeAhead"/>
|
||||
/// 进行;这两项是 EnemyMovement 内置的物理射线检测,不属于感知系统(PhysicsPerceptionSystem)。
|
||||
///
|
||||
/// 若需要按预设路点顺序巡逻,请使用 <see cref="BD_PatrolWaypoints"/>(支持 Transform 引用和内联坐标)。
|
||||
/// </summary>
|
||||
[TaskName("Patrol (Pace)")]
|
||||
[TaskCategory("BaseGames/Enemy/Movement")]
|
||||
[TaskDescription("来回踱步巡逻:遇墙或悬崖自动翻转方向(需配置 EnemySensorHub wall_ahead / ledge 槽)")]
|
||||
[TaskDescription("来回踱步巡逻:遇墙或悬崖自动翻转方向(通过 EnemyMovement 物理射线检测,无需感知槽)")]
|
||||
public class BD_Patrol : Action
|
||||
{
|
||||
private EnemyBase _enemy;
|
||||
private EnemySensorHub _hub;
|
||||
private float _dir = 1f;
|
||||
private float _flipCooldown; // 翻转后短暂冷却,等待 RaySensor2D 刷新到新朝向
|
||||
|
||||
// 缓存:SensorHub 中对应槽位是否已配置(Awake 时查询一次,避免每帧 Dictionary 查找)
|
||||
private bool _hasWallSensor;
|
||||
private bool _hasEdgeSensor;
|
||||
private EnemyBase _enemy;
|
||||
private EnemyMovement _movement;
|
||||
private float _dir = 1f;
|
||||
private float _flipCooldown; // 翻转后短暂冷却,等待射线刷新到新朝向
|
||||
|
||||
public override void OnAwake()
|
||||
{
|
||||
_enemy = GetComponent<EnemyBase>();
|
||||
_hub = GetComponent<EnemySensorHub>();
|
||||
_hasWallSensor = _hub != null && _hub.Get(SensorSlotNames.WallAhead) != null;
|
||||
_hasEdgeSensor = _hub != null && _hub.Get(SensorSlotNames.Ledge) != null;
|
||||
_enemy = GetComponent<EnemyBase>();
|
||||
_movement = _enemy?.Movement;
|
||||
}
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
_enemy?.SetAiPhase(AiPhase.Patrol);
|
||||
// 与敌人实际朝向同步,防止任务重入时 _dir 与朝向不符(如战斗后朝向已改变)
|
||||
if (_enemy?.Movement != null)
|
||||
_dir = _enemy.Movement.FacingDirection;
|
||||
if (_movement != null)
|
||||
_dir = _movement.FacingDirection;
|
||||
_flipCooldown = 0f;
|
||||
}
|
||||
|
||||
@@ -48,13 +42,13 @@ namespace BaseGames.Enemies.AI
|
||||
{
|
||||
if (_enemy == null) return TaskStatus.Failure;
|
||||
|
||||
// 翻转冷却期间跳过传感器检测(等待 RaySensor2D 在新朝向完成刷新)
|
||||
// 翻转冷却期间跳过物理检测(等待射线在新朝向完成刷新)
|
||||
if (_flipCooldown > 0f)
|
||||
_flipCooldown -= Time.deltaTime;
|
||||
else if (ShouldFlip())
|
||||
{
|
||||
_dir = -_dir;
|
||||
_flipCooldown = 0.1f; // ~6 帧缓冲(60 fps),防止传感器残留信号导致抖动
|
||||
_flipCooldown = 0.1f; // ~6 帧缓冲(60 fps),防止射线残留信号导致抖动
|
||||
}
|
||||
|
||||
_enemy.MoveInDirection(_dir);
|
||||
@@ -65,11 +59,9 @@ namespace BaseGames.Enemies.AI
|
||||
|
||||
private bool ShouldFlip()
|
||||
{
|
||||
// 转身进行中时不重复检测,防止 _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;
|
||||
// 转身进行中时不重复检测,防止 _dir 在转身期间被残留信号反复翻转
|
||||
if (_movement == null || _movement.IsTurning) return false;
|
||||
return _movement.IsWallAhead || _movement.IsLedgeAhead;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user