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:
@@ -79,6 +79,11 @@ namespace BaseGames.Enemies.AI
|
||||
|
||||
Vector2 playerPos = _enemy.PlayerTransform.position;
|
||||
|
||||
// 若配置了巡逻区域,且玩家超出追击边界 → 放弃追击(优先级高于纯距离限制)
|
||||
var zone = _enemy.PatrolZone;
|
||||
if (zone != null && !zone.ContainsChase(playerPos))
|
||||
return TaskStatus.Failure;
|
||||
|
||||
if (_enemy.IsPlayerVisible())
|
||||
{
|
||||
// 视线恢复:Searching → Tracking,恢复奔跑速度
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace BaseGames.Enemies.AI
|
||||
/// </summary>
|
||||
[TaskName("Is Near Edge?")]
|
||||
[TaskCategory("BaseGames/Enemy/State")]
|
||||
[TaskDescription("检查前方是否有悬崖边缘(基于 SensorToolkit 或 Raycast)")]
|
||||
[TaskDescription("检查前方是否有悬崖边缘(基于 EnemyNavAgent Raycast 检测)")]
|
||||
public class BD_IsNearEdge : Conditional
|
||||
{
|
||||
private EnemyBase _enemy;
|
||||
|
||||
61
Assets/_Game/Scripts/Enemies/AI/BD_IsOutsideZone.cs
Normal file
61
Assets/_Game/Scripts/Enemies/AI/BD_IsOutsideZone.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
#if GRAPH_DESIGNER
|
||||
using UnityEngine;
|
||||
using Opsive.BehaviorDesigner.Runtime.Tasks;
|
||||
using Opsive.BehaviorDesigner.Runtime.Tasks.Conditionals;
|
||||
using BaseGames.Enemies;
|
||||
|
||||
namespace BaseGames.Enemies.AI
|
||||
{
|
||||
/// <summary>
|
||||
/// BD Conditional:判断目标坐标是否超出指定区域边界。
|
||||
///
|
||||
/// <list type="bullet">
|
||||
/// <item>未配置 PatrolZone 时返回 Failure(表示"无限制",等同于不超界)。</item>
|
||||
/// <item>超界 → Success;区域内 → Failure。</item>
|
||||
/// </list>
|
||||
///
|
||||
/// 典型用法:在 Patrol BT 子树中用 BD_IsOutsideZone 检查敌人坐标,
|
||||
/// 超出巡逻区域时触发归位序列。
|
||||
/// </summary>
|
||||
[TaskName("Is Outside Zone")]
|
||||
[TaskCategory("BaseGames/Enemy/Zone")]
|
||||
[TaskDescription("判断敌人/玩家坐标是否超出巡逻或追击区域;无 Zone 时返回 Failure(不限制)")]
|
||||
public sealed class BD_IsOutsideZone : Conditional
|
||||
{
|
||||
[Tooltip("true = 检查追击区域,false = 检查巡逻区域")]
|
||||
[SerializeField] private bool m_CheckChaseZone = false;
|
||||
|
||||
[Tooltip("true = 检查敌人自身坐标,false = 检查玩家坐标")]
|
||||
[SerializeField] private bool m_CheckEnemy = true;
|
||||
|
||||
private EnemyBase _enemy;
|
||||
|
||||
public override void OnAwake() => _enemy = gameObject.GetComponent<EnemyBase>();
|
||||
|
||||
public override TaskStatus OnUpdate()
|
||||
{
|
||||
if (_enemy == null) return TaskStatus.Failure;
|
||||
|
||||
var zone = _enemy.PatrolZone;
|
||||
if (zone == null) return TaskStatus.Failure; // 无区域 = 不限制
|
||||
|
||||
Vector2 pos;
|
||||
if (m_CheckEnemy)
|
||||
{
|
||||
pos = _enemy.transform.position;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_enemy.PlayerTransform == null) return TaskStatus.Failure;
|
||||
pos = _enemy.PlayerTransform.position;
|
||||
}
|
||||
|
||||
bool inside = m_CheckChaseZone
|
||||
? zone.ContainsChase(pos)
|
||||
: zone.ContainsPatrol(pos);
|
||||
|
||||
return inside ? TaskStatus.Failure : TaskStatus.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
11
Assets/_Game/Scripts/Enemies/AI/BD_IsOutsideZone.cs.meta
Normal file
11
Assets/_Game/Scripts/Enemies/AI/BD_IsOutsideZone.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bff87912e6defb849bea247df2801172
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -7,23 +7,23 @@ using BaseGames.Enemies.Perception;
|
||||
namespace BaseGames.Enemies.AI
|
||||
{
|
||||
/// <summary>
|
||||
/// 条件:EnemySensorHub 中名为 slotName 的 Sensor 是否检测到目标。
|
||||
/// 条件:<see cref="IPerceptionSystem"/> 中名为 slotName 的槽位是否检测到目标。
|
||||
/// 若 m_Target 为空则使用 EnemyBase.PlayerTransform。
|
||||
/// </summary>
|
||||
[TaskName("Is Sensor Detecting?")]
|
||||
[TaskCategory("BaseGames/Enemy/Perception")]
|
||||
[TaskDescription("检查 EnemySensorHub 中指定 Sensor 槽是否检测到目标")]
|
||||
[TaskDescription("检查 PhysicsPerceptionSystem 中指定 Sensor 槽是否检测到目标")]
|
||||
public sealed class BD_IsSensorDetecting : Conditional
|
||||
{
|
||||
[SerializeField] private string m_SlotName = "aggro";
|
||||
[SerializeField] private bool m_AnyTarget = false;
|
||||
|
||||
private EnemySensorHub _hub;
|
||||
private EnemyBase _enemy;
|
||||
private IPerceptionSystem _hub;
|
||||
private EnemyBase _enemy;
|
||||
|
||||
public override void OnAwake()
|
||||
{
|
||||
_hub = gameObject.GetComponent<EnemySensorHub>();
|
||||
_hub = gameObject.GetComponent<IPerceptionSystem>();
|
||||
_enemy = gameObject.GetComponent<EnemyBase>();
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ namespace BaseGames.Enemies.AI
|
||||
private bool _reached;
|
||||
private bool _pathFailed;
|
||||
private bool _subscribed;
|
||||
private Vector2 _returnTarget;
|
||||
|
||||
public override void OnAwake() => _enemy = gameObject.GetComponent<EnemyBase>();
|
||||
|
||||
@@ -54,7 +55,12 @@ namespace BaseGames.Enemies.AI
|
||||
// 切换为行走速度
|
||||
float walkSpeed = _enemy.Stats?.WalkSpeed ?? 2f;
|
||||
_enemy.Nav?.SetSpeed(walkSpeed);
|
||||
_enemy.MoveTo(_enemy.HomePosition);
|
||||
|
||||
// 优先归位到巡逻区域中心;无区域时退回出生点
|
||||
_returnTarget = _enemy.PatrolZone != null
|
||||
? _enemy.PatrolZone.PatrolCenter
|
||||
: _enemy.HomePosition;
|
||||
_enemy.MoveTo(_returnTarget);
|
||||
}
|
||||
|
||||
public override TaskStatus OnUpdate()
|
||||
@@ -66,7 +72,7 @@ namespace BaseGames.Enemies.AI
|
||||
|
||||
// 兜底距离判断(事件可能因帧序问题延迟一帧)
|
||||
float radius = m_ArriveRadius > 0f ? m_ArriveRadius : (_enemy.Stats?.HomeRadius ?? 0.5f);
|
||||
float sqr = ((Vector2)_enemy.transform.position - _enemy.HomePosition).sqrMagnitude;
|
||||
float sqr = ((Vector2)_enemy.transform.position - _returnTarget).sqrMagnitude;
|
||||
if (sqr <= radius * radius)
|
||||
return CompleteReturn();
|
||||
|
||||
|
||||
@@ -13,8 +13,7 @@
|
||||
"BaseGames.Enemies",
|
||||
"BaseGames.Enemies.Boss.Patterns",
|
||||
"Opsive.BehaviorDesigner.Runtime",
|
||||
"Kybernetik.Animancer",
|
||||
"Micosmo.SensorToolkit"
|
||||
"Kybernetik.Animancer"
|
||||
],
|
||||
"autoReferenced": true,
|
||||
"overrideReferences": false,
|
||||
|
||||
Reference in New Issue
Block a user