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

78 lines
2.5 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-04 15:24:11 +08:00
using Gameplay.AI;
using Framework;
2023-08-22 19:08:45 +08:00
namespace Code.Scripts.Gameplay.BT.Task
{
public class BTMoveTo : Action
{
public SharedBattleInfo SharedBattleInfo;
private GameUnit _owner;
2024-11-04 15:24:11 +08:00
private GameUnit _target;
2023-08-22 19:08:45 +08:00
private uint _lastTarget;
2024-12-20 19:56:53 +08:00
private bool needCheck;
2023-08-22 19:08:45 +08:00
public override void OnStart()
{
2024-07-15 16:51:32 +08:00
_owner = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.ownerId);
}
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();
2023-08-22 19:08:45 +08:00
if (SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID)
{
2024-07-15 16:51:32 +08:00
if (_lastTarget != SharedBattleInfo.Value.theChosenOneId && !AIUtils.IsNeedStop(_owner))
2023-08-22 19:08:45 +08:00
{
_lastTarget = SharedBattleInfo.Value.theChosenOneId;
2024-07-23 14:00:45 +08:00
if (GameUnitManager.instance.infightUnits.Contains(SharedBattleInfo.Value.theChosenOneId))
{
2024-12-20 19:56:53 +08:00
var target =
GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.theChosenOneId);
2024-07-23 14:00:45 +08:00
if (target != null)
2024-12-17 17:01:36 +08:00
{
2024-10-21 15:44:25 +08:00
AIUtils.CheckAndMove(_owner, target.transData.cellIndex);
2024-12-17 17:01:36 +08:00
}
2024-07-23 14:00:45 +08:00
}
2024-07-15 16:51:32 +08:00
}
2024-07-17 14:48:00 +08:00
2024-11-04 15:24:11 +08:00
if (_owner?.controlData.targetEnemy == null)
2024-07-15 16:51:32 +08:00
{
_lastTarget = Constants.INVALID_UINT_ID;
2023-08-22 19:08:45 +08:00
}
return TaskStatus.Running;
}
2024-12-20 19:56:53 +08:00
2023-08-22 19:08:45 +08:00
return TaskStatus.Failure;
2024-07-15 16:51:32 +08:00
}
2024-12-20 19:56:53 +08:00
private async void AvoidOtherUnit()
2024-07-15 16:51:32 +08:00
{
2024-12-20 19:56:53 +08:00
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.theChosenOneId);
}
2023-08-22 19:08:45 +08:00
}
}
}