78 lines
2.4 KiB
C#
78 lines
2.4 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 BTMoveTo : Action
|
|
{
|
|
public SharedBattleInfo SharedBattleInfo;
|
|
private GameUnit _owner;
|
|
private GameUnit _target;
|
|
private uint _lastTarget;
|
|
private bool needCheck;
|
|
|
|
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 (SharedBattleInfo.Value.targetID != Constants.INVALID_UINT_ID)
|
|
{
|
|
if (_lastTarget != SharedBattleInfo.Value.targetID && !AIUtils.IsNeedStop(_owner))
|
|
{
|
|
_lastTarget = SharedBattleInfo.Value.targetID;
|
|
if (GameUnitManager.instance.infightUnits.Contains(SharedBattleInfo.Value.targetID))
|
|
{
|
|
var target =
|
|
GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.targetID);
|
|
if (target != null)
|
|
{
|
|
AIUtils.CheckAndMove(_owner, target.transData.cellIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (_owner?.controlData.targetEnemy == null)
|
|
{
|
|
_lastTarget = Constants.INVALID_UINT_ID;
|
|
}
|
|
|
|
return TaskStatus.Running;
|
|
}
|
|
|
|
return TaskStatus.Failure;
|
|
}
|
|
|
|
private async void AvoidOtherUnit()
|
|
{
|
|
if (AIUtils.IsNeedStop(_owner)) return;
|
|
|
|
if (_owner.statusData.isMoveing && !needCheck)
|
|
{
|
|
needCheck = true;
|
|
}
|
|
|
|
if (needCheck && !_owner.statusData.isMoveing &&
|
|
_owner.controlData.targetCellIndex == -1)
|
|
{
|
|
needCheck = false;
|
|
// 随机等待 防止同步
|
|
var randomDelay = UnityEngine.Random.Range(0f, 1f);
|
|
await UniTask.Delay((int)(randomDelay * 1000));
|
|
|
|
AIUtils.CheckAreaIsOtherEnemyUnit(_owner, SharedBattleInfo.Value.targetID);
|
|
}
|
|
}
|
|
}
|
|
} |