NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/AI/Task/BTMoveAway.cs

78 lines
2.6 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using BehaviorDesigner.Runtime.Tasks;
2024-12-20 19:56:53 +08:00
using Cysharp.Threading.Tasks;
2023-08-22 19:08:45 +08:00
using Gameplay.BT.Variables;
2023-12-13 16:20:41 +08:00
using Gameplay.Unit;
2024-11-06 14:22:53 +08:00
using Gameplay.AI;
using Framework;
2023-08-22 19:08:45 +08:00
namespace Code.Scripts.Gameplay.BT.Task
{
public class BTMoveAway : Action
{
public SharedBattleInfo SharedBattleInfo;
private GameUnit _owner;
2024-12-20 19:56:53 +08:00
private bool lastMoveState;
2025-03-10 14:19:47 +08:00
private bool _needAvoidCheck;
private float _avoidCheckTimer;
private float _currentAvoidDelay;
private const float MIN_AVOID_DELAY = 0f;
private const float MAX_AVOID_DELAY = 1f;
2023-08-22 19:08:45 +08:00
public override void OnStart()
{
2025-02-19 13:44:13 +08:00
_owner = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.ownerID);
2023-08-22 19:08:45 +08:00
}
2023-08-22 19:08:45 +08:00
public override TaskStatus OnUpdate()
{
2024-12-20 19:56:53 +08:00
if (_owner == null) return TaskStatus.Failure;
// 避开其他AI
AvoidOtherUnit();
2025-02-19 13:44:13 +08:00
if (_owner != null && SharedBattleInfo.Value.targetID != Constants.INVALID_UINT_ID &&
2024-12-02 18:53:43 +08:00
!AIUtils.IsNeedStop(_owner))
2023-08-22 19:08:45 +08:00
{
2025-02-19 13:44:13 +08:00
if (GameUnitManager.instance.infightUnits.Contains(SharedBattleInfo.Value.targetID))
2024-11-04 15:24:11 +08:00
{
2025-02-19 13:44:13 +08:00
var enemy = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.targetID);
2024-11-04 15:24:11 +08:00
if (enemy != null && !enemy.statusData.isDead)
{
2024-12-02 18:53:43 +08:00
if (AIUtils.MoveAway(_owner, enemy))
2025-02-19 13:44:13 +08:00
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
2024-11-04 15:24:11 +08:00
}
}
else
{
2025-02-19 13:44:13 +08:00
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
2024-11-04 15:24:11 +08:00
}
2023-08-22 19:08:45 +08:00
}
2023-10-19 15:00:19 +08:00
return TaskStatus.Success;
2023-08-22 19:08:45 +08:00
}
2024-12-20 19:56:53 +08:00
2025-03-10 14:19:47 +08:00
private void AvoidOtherUnit()
2024-12-20 19:56:53 +08:00
{
if (AIUtils.IsNeedStop(_owner)) return;
2025-03-10 14:19:47 +08:00
if (_owner.statusData.isMoveing && !_needAvoidCheck)
2024-12-20 19:56:53 +08:00
{
2025-03-10 14:19:47 +08:00
_needAvoidCheck = true;
_currentAvoidDelay = UnityEngine.Random.Range(MIN_AVOID_DELAY, MAX_AVOID_DELAY);
_avoidCheckTimer = 0f;
2024-12-20 19:56:53 +08:00
}
2025-03-10 14:19:47 +08:00
if (_needAvoidCheck && !_owner.statusData.isMoveing &&
2024-12-20 19:56:53 +08:00
_owner.controlData.targetCellIndex == -1)
{
2025-03-10 14:19:47 +08:00
_avoidCheckTimer += UnityEngine.Time.deltaTime;
if(_avoidCheckTimer >= _currentAvoidDelay)
{
_needAvoidCheck = false;
AIUtils.CheckAreaIsOtherEnemyUnit(_owner, SharedBattleInfo.Value.targetID);
}
2024-12-20 19:56:53 +08:00
}
}
2023-08-22 19:08:45 +08:00
}
}