2024-12-04 18:49:20 +08:00
|
|
|
using BehaviorDesigner.Runtime.Tasks;
|
2024-12-20 19:56:53 +08:00
|
|
|
using Cysharp.Threading.Tasks;
|
2024-12-04 18:49:20 +08:00
|
|
|
using Gameplay.BT.Variables;
|
|
|
|
|
using Gameplay.Unit;
|
|
|
|
|
using Gameplay.AI;
|
|
|
|
|
using Framework;
|
|
|
|
|
|
|
|
|
|
public class BTDefendMoveTo : Action
|
|
|
|
|
{
|
2024-12-20 19:56:53 +08:00
|
|
|
public SharedBattleInfo SharedBattleInfo;
|
2024-12-04 18:49:20 +08:00
|
|
|
private GameUnit _owner;
|
|
|
|
|
private GameUnit _target;
|
|
|
|
|
private uint _lastTarget;
|
2024-12-20 19:56:53 +08:00
|
|
|
private bool needCheck;
|
2024-12-04 18:49:20 +08:00
|
|
|
|
|
|
|
|
public override void OnStart()
|
|
|
|
|
{
|
2025-02-19 13:44:13 +08:00
|
|
|
_owner = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.ownerID);
|
2024-12-04 18:49:20 +08:00
|
|
|
}
|
2024-12-06 18:59:22 +08:00
|
|
|
|
2024-12-04 18:49:20 +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 (SharedBattleInfo.Value.targetID != Constants.INVALID_UINT_ID)
|
2024-12-04 18:49:20 +08:00
|
|
|
{
|
|
|
|
|
// 超出守卫范围
|
2024-12-06 18:59:22 +08:00
|
|
|
if (AIUtils.CheckDistanceIsOver(_owner))
|
2024-12-04 18:49:20 +08:00
|
|
|
{
|
2025-02-19 13:44:13 +08:00
|
|
|
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
|
2024-12-06 18:59:22 +08:00
|
|
|
return TaskStatus.Failure;
|
2024-12-04 18:49:20 +08:00
|
|
|
}
|
|
|
|
|
|
2024-12-06 18:59:22 +08:00
|
|
|
if (_owner.controlData.targetEnemy == null && !AIUtils.IsNeedStop(_owner))
|
2024-12-04 18:49:20 +08:00
|
|
|
{
|
2025-02-19 13:44:13 +08:00
|
|
|
if (GameUnitManager.instance.infightUnits.Contains(SharedBattleInfo.Value.targetID))
|
2024-12-04 18:49:20 +08:00
|
|
|
{
|
2025-02-19 13:44:13 +08:00
|
|
|
var target = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.targetID);
|
2024-12-04 18:49:20 +08:00
|
|
|
if (target != null)
|
|
|
|
|
{
|
2024-12-06 18:59:22 +08:00
|
|
|
_owner.controlData.targetEnemy = target;
|
2024-12-04 18:49:20 +08:00
|
|
|
AIUtils.CheckAndMove(_owner, target.transData.cellIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-06 18:59:22 +08:00
|
|
|
if (_owner.controlData.targetEnemy != null &&
|
2025-02-19 13:44:13 +08:00
|
|
|
_owner.controlData.targetEnemy.GetID() != SharedBattleInfo.Value.targetID &&
|
2024-12-06 18:59:22 +08:00
|
|
|
!AIUtils.IsNeedStop(_owner))
|
2024-12-04 18:49:20 +08:00
|
|
|
{
|
2025-02-19 13:44:13 +08:00
|
|
|
if (GameUnitManager.instance.infightUnits.Contains(SharedBattleInfo.Value.targetID))
|
2024-12-06 18:59:22 +08:00
|
|
|
{
|
2025-02-19 13:44:13 +08:00
|
|
|
var target = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.targetID);
|
2024-12-06 18:59:22 +08:00
|
|
|
if (target != null)
|
|
|
|
|
{
|
|
|
|
|
_owner.controlData.targetEnemy = target;
|
|
|
|
|
AIUtils.CheckAndMove(_owner, target.transData.cellIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-04 18:49:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TaskStatus.Running;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TaskStatus.Failure;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-20 19:56:53 +08:00
|
|
|
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));
|
|
|
|
|
|
2025-02-19 13:44:13 +08:00
|
|
|
AIUtils.CheckAreaIsOtherEnemyUnit(_owner, SharedBattleInfo.Value.targetID);
|
2024-12-20 19:56:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 18:49:20 +08:00
|
|
|
public override void OnEnd()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|