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 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 (_owner != null && SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID && !AIUtils.IsNeedStop(_owner)) { if (GameUnitManager.instance.infightUnits.Contains(SharedBattleInfo.Value.theChosenOneId)) { var enemy = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.theChosenOneId); if (enemy != null && !enemy.statusData.isDead) { if (AIUtils.MoveAway(_owner, enemy)) SharedBattleInfo.Value.theChosenOneId = Constants.INVALID_UINT_ID; } } else { SharedBattleInfo.Value.theChosenOneId = Constants.INVALID_UINT_ID; } } return TaskStatus.Success; } 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.theChosenOneId); } } } }