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

78 lines
2.6 KiB
C#

using BehaviorDesigner.Runtime.Tasks;
using Cysharp.Threading.Tasks;
using Gameplay.BT.Variables;
using Gameplay.Unit;
using Gameplay.AI;
using Framework;
namespace Code.Scripts.Gameplay.BT.Task
{
public class BTMoveAway : Action
{
public SharedBattleInfo SharedBattleInfo;
private GameUnit _owner;
private bool lastMoveState;
private bool _needAvoidCheck;
private float _avoidCheckTimer;
private float _currentAvoidDelay;
private const float MIN_AVOID_DELAY = 0f;
private const float MAX_AVOID_DELAY = 1f;
public override void OnStart()
{
_owner = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.ownerID);
}
public override TaskStatus OnUpdate()
{
if (_owner == null) return TaskStatus.Failure;
// 避开其他AI
AvoidOtherUnit();
if (_owner != null && SharedBattleInfo.Value.targetID != Constants.INVALID_UINT_ID &&
!AIUtils.IsNeedStop(_owner))
{
if (GameUnitManager.instance.infightUnits.Contains(SharedBattleInfo.Value.targetID))
{
var enemy = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.targetID);
if (enemy != null && !enemy.statusData.isDead)
{
if (AIUtils.MoveAway(_owner, enemy))
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
}
}
else
{
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
}
}
return TaskStatus.Success;
}
private void AvoidOtherUnit()
{
if (AIUtils.IsNeedStop(_owner)) return;
if (_owner.statusData.isMoveing && !_needAvoidCheck)
{
_needAvoidCheck = true;
_currentAvoidDelay = UnityEngine.Random.Range(MIN_AVOID_DELAY, MAX_AVOID_DELAY);
_avoidCheckTimer = 0f;
}
if (_needAvoidCheck && !_owner.statusData.isMoveing &&
_owner.controlData.targetCellIndex == -1)
{
_avoidCheckTimer += UnityEngine.Time.deltaTime;
if(_avoidCheckTimer >= _currentAvoidDelay)
{
_needAvoidCheck = false;
AIUtils.CheckAreaIsOtherEnemyUnit(_owner, SharedBattleInfo.Value.targetID);
}
}
}
}
}