(client) feat:添加临时动画组件,添加逃跑逻辑

This commit is contained in:
m0_75251201
2025-09-06 12:25:55 +08:00
parent f43aeffebf
commit 15cdd2b244
73 changed files with 3420 additions and 6055 deletions

View File

@ -7,7 +7,7 @@ namespace AI
public abstract class JobBase
{
public Entity.Entity entity;
protected int timeoutTicks = 300;
protected int timeoutTicks = 150;
public bool Running => timeoutTicks > 0;
protected abstract void UpdateJob();
@ -41,7 +41,6 @@ namespace AI
Vector3 move = new(Random.Range(-10, 10), Random.Range(-10, 10));
var targetPosition = entity.transform.position + move;
entity.SetTarget(targetPosition);
entity.IsChase = false;
}
protected override void UpdateJob()
@ -52,7 +51,6 @@ namespace AI
override public void StopJob()
{
base.StopJob();
entity.IsChase = true;
}
}
@ -87,7 +85,7 @@ namespace AI
{
base.StartJob(performerEntityContext);
// 1. 任务执行者自身有效性检查
if (entity == null)
if (!entity)
{
StopJob(); // 调用StopJob来结束任务
return;
@ -95,7 +93,7 @@ namespace AI
attackTarget = FindNewHostileTarget();
if (attackTarget == null)
if (!attackTarget)
{
StopJob(); // 调用StopJob来结束任务
}
@ -104,16 +102,16 @@ namespace AI
protected override void UpdateJob()
{
// 1. 任务执行者的基本检查
if (entity == null || entity.IsDead)
if (!entity || entity.IsDead)
{
StopJob();
return;
}
if (attackTarget == null || attackTarget.IsDead)
if (!attackTarget || attackTarget.IsDead)
{
attackTarget = FindNewHostileTarget(); // 尝试寻找新的攻击目标
if (attackTarget == null)
if (!attackTarget)
{
StopJob();
return;
@ -277,4 +275,190 @@ namespace AI
}
}
public class FleeJob : JobBase
{
// 逃跑时在远离方向上设置的目标点偏移距离。让AI尝试跑向更远的点。
private const float FLEE_BUFFER_DISTANCE = 10.0f;
// 远程单位在逃跑时,如果敌人距离已超出其攻击范围的多少倍,可以认为已经足够安全。
// 此时实体将停止紧急移动可以等待timeoutTicks结束或进行其他决策。
private const float RANGED_SAFE_DISTANCE_MULTIPLIER = 2.0f;
// 对于近战或无武器单位,敌人必须在这个距离内,才触发紧急逃跑。
// 超过这个距离,实体可能认为已经足够安全,可以停止紧急移动。
private const float MELEE_PERIL_DISTANCE = 15.0f;
private const float MELEE_PERIL_DISTANCE_SQUARED = MELEE_PERIL_DISTANCE * MELEE_PERIL_DISTANCE;
// **新增**:目标刷新间隔 (以Ticks计)
private const int TARGET_REFRESH_INTERVAL_TICKS = 30; // 每30帧Ticks刷新一次目标
// **新增**缓存的敌对目标避免频繁调用FindNearestHostileTarget
private Entity.Entity _cachedHostileTarget;
// **新增**:目标刷新计时器
private int _targetRefreshTimer;
// **新增**:侧走参数 (用于躲避远程攻击)
private const float EVASION_SIDE_STEP_STRENGTH = 0.5f; // 侧走分量强度,与主方向叠加,需要调整
private int _sideStepDirection = 1; // 1 for right, -1 for left
private int _sideStepTimer;
private const int EVASION_SIDE_STEP_INTERVAL_TICKS = 20; // 每20帧切换一次侧走方向
public FleeJob(int initialTimeoutTicks = 150)
{
timeoutTicks = initialTimeoutTicks;
_targetRefreshTimer = 0; // 初始化计时器让第一次UpdateJob立即刷新目标
_sideStepTimer = 0;
_sideStepDirection = Random.Range(0, 2) * 2 - 1; // 随机初始化左右方向
}
public override void StartJob(Entity.Entity performerEntityContext)
{
base.StartJob(performerEntityContext);
_cachedHostileTarget = null; // 任务开始时清空缓存,确保第一次更新会查找新目标
_targetRefreshTimer = 0; // 确保立即查找目标
_sideStepTimer = 0;
_sideStepDirection = Random.Range(0, 2) * 2 - 1; // 随机初始化左右方向
}
protected override void UpdateJob()
{
// 1. 任务执行者的基本检查
if (!entity || entity.IsDead)
{
StopJob();
return;
}
// 2. 目标刷新逻辑 (避免每帧都调用FindNearestHostileTarget)
_targetRefreshTimer--;
if (!_cachedHostileTarget || _cachedHostileTarget.IsDead || _targetRefreshTimer <= 0)
{
_cachedHostileTarget = FindNearestHostileTarget();
_targetRefreshTimer = TARGET_REFRESH_INTERVAL_TICKS; // 重置计时器
}
// 使用缓存的目标
var hostileTarget = _cachedHostileTarget;
if (!hostileTarget)
{
// 如果没有敌对目标可逃跑,则停止此工作。
StopJob();
return;
}
// 3. 计算与敌对目标的距离
var distanceSq = (entity.Position - hostileTarget.Position).sqrMagnitude;
// 4. 获取敌方目标武器信息,判断敌方是否为远程单位
var enemyWeapon = hostileTarget.GetCurrentWeapon();
var isEnemyRanged = enemyWeapon is { Type: WeaponType.Ranged };
// 获取自己的武器信息 (虽然逃跑主要看敌方武器,但自身安全距离判断需要)
var selfWeapon = entity.GetCurrentWeapon();
var isSelfRanged = selfWeapon is { Type: WeaponType.Ranged };
// 5. 计算一个远离目标的基础方向
var directionAway = (entity.Position - hostileTarget.Position).normalized;
var moveTargetDirection = directionAway; // 默认是纯远离方向
// 6. 如果敌方是远程单位,叠加侧走逻辑
if (isEnemyRanged)
{
_sideStepTimer--;
if (_sideStepTimer <= 0)
{
_sideStepDirection *= -1; // 切换侧走方向 (左或右)
_sideStepTimer = EVASION_SIDE_STEP_INTERVAL_TICKS;
}
// 计算侧走方向 (在X-Z平面上垂直于远离方向)
var perpendicularDirection = GetPerpendicularVectorXZ(directionAway) * _sideStepDirection;
// 侧走策略:在远离方向上叠加一个侧走分量
// 让侧走分量与远离分量结合,形成一个弧线或斜向移动
moveTargetDirection = (directionAway * (1 - EVASION_SIDE_STEP_STRENGTH) +
perpendicularDirection * EVASION_SIDE_STEP_STRENGTH).normalized;
// 或者更简单地moveTargetDirection = (directionAway + perpendicularDirection * EVASION_SIDE_STEP_STRENGTH).normalized;
// 需要根据实际效果调整EVASION_SIDE_STEP_STRENGTH的数值和结合方式。
}
// 7. 确定最终移动目标点
var fleePosition = entity.Position + moveTargetDirection * FLEE_BUFFER_DISTANCE;
// 8. 结合自己的武器类型,决定何时停止紧急移动 (和之前的逻辑类似)
if (isSelfRanged)
{
var selfAttackRange = 0f;
if (selfWeapon != null && selfWeapon.Attributes != null)
{
selfAttackRange = selfWeapon.Attributes.attackRange;
}
var safeDistanceSq = (selfAttackRange * RANGED_SAFE_DISTANCE_MULTIPLIER) *
(selfAttackRange * RANGED_SAFE_DISTANCE_MULTIPLIER);
// 如果目标在“安全距离”内,则实体需要继续远离
if (distanceSq < safeDistanceSq)
{
entity.SetTarget(fleePosition);
entity.TryMove();
}
else
{
// 已经达到足够的安全距离,此时实体可以停止紧急移动。
// FleeJob会继续运行直到timeoutTicks耗尽给予一定的缓冲时间。
entity.SetTarget(entity.Position); // 设定目标为当前位置,使其停止移动
}
}
else // 自己是近战单位或没有武器
{
// 只要目标在“危险距离”内,就一直向外逃跑
if (distanceSq < MELEE_PERIL_DISTANCE_SQUARED)
{
entity.SetTarget(fleePosition);
entity.TryMove();
}
else
{
// 已经拉开足够远的距离,停止紧急逃跑.
// 同样FleeJob会继续运行直到timeoutTicks耗尽。
entity.SetTarget(entity.Position); // 设定目标为当前位置,使其停止移动
}
}
}
override public void StopJob()
{
base.StopJob();
if (entity != null)
{
entity.SetTarget(entity.Position); // 确保实体停止任何正在进行的逃跑移动
}
_cachedHostileTarget = null; // 清除缓存目标以便下次StartJob时重新查找
}
/// <summary>
/// 查找执行实体最近的敌对目标。此方法可能耗时,应避免频繁调用。
/// </summary>
/// <returns>找到的敌对实体如果没有则返回null。</returns>
private Entity.Entity FindNearestHostileTarget()
{
if (entity == null) return null;
return EntityManage.Instance.FindNearestEntityByRelation(
entity.currentDimensionId, // 搜索维度ID
entity.entityPrefab, // 执行实体的Prefab ID用于关系判断
Relation.Hostile)?.entity; // 寻找敌对关系的目标
}
/// <summary>
/// 获取给定向量在X-Z平面上的垂直向量。
/// 假定Y轴是世界向上方向。
/// </summary>
/// <param name="direction">原始方向向量。</param>
/// <returns>在X-Z平面上垂直于原始向量的向量顺时针旋转90度。</returns>
private Vector3 GetPerpendicularVectorXZ(Vector3 direction)
{
// 创建一个只保留XZ分量的向量
var flatDirection = new Vector3(direction.x, 0, direction.z).normalized;
// 顺时针旋转90度 (x,z) => (z,-x)
return new Vector3(flatDirection.z, 0, -flatDirection.x);
}
}
}