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

1 line
5.7 KiB
C#
Raw Normal View History

2024-11-06 14:22:53 +08:00
using System; using Gameplay; using Gameplay.AI; using UnityEngine; using Gameplay.Unit; using Gameplay.Level; using Gameplay.BT.Variables; using BehaviorDesigner.Runtime.Tasks; using Action = BehaviorDesigner.Runtime.Tasks.Action; public class BTMoveAroundTo : Action { public SharedBattleInfo SharedBattleInfo; private EMoveDirection MoveDirection = EMoveDirection.Clockwise; private GameUnit _owner; private GameUnit _target; private Map _map; private EStage _stage = EStage.None; private int _stageTargetCellIndex = -1; private int _targetCellIndex = -1; private Vector3 _baseDirection = Vector3.zero; private enum EMoveDirection { Clockwise, AntiClockwise, } private enum EStage { None, Stage1, Stage2, MoveToTarget } public override void OnStart() { var level = LevelManager.Instance.CurrentLevel; _owner = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.ownerId); _target = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.theChosenOneId); _map = level?.Map; _stage = EStage.None; _stageTargetCellIndex = -1; _targetCellIndex = -1; if (_map != null && _target != null && _owner != null) { var targetPosition = _map.TGSCellIndex2WorldPosition(_target.transData.cellIndex); var selfPosition = _map.TGSCellIndex2WorldPosition(_owner.transData.cellIndex); _baseDirection = targetPosition - selfPosition; } } public override TaskStatus OnUpdate() { if (_target == null) { return TaskStatus.Failure; } var attackDistance = _owner.fightData.attackDistance; var selfAsideDistance = attackDistance / 2; var targetAsideDistance = attackDistance; if (HasTargetMoved()) { RememberTarget(); switch (_stage) { case EStage.None: _stageTargetCellIndex = GetStage1TargetCell(selfAsideDistance); _stage = EStage.Stage1; AIUtils.CheckAndMove(_owner, _stageTargetCellIndex); break; case EStage.Stage1: _stageTargetCellIndex = GetStage1TargetCell(selfAsideDistance); AIUtils.CheckAndMove(_owner, _stageTargetCellIndex); break; case EStage.Stage2: _stageTargetCellIndex = GetStage2TargetCell(targetAsideDistance); AIUtils.CheckAndMove(_owner, _stageTargetCellIndex); break; case EStage.MoveToTarget: break; default: throw new ArgumentOutOfRangeException(); } return TaskStatus.Running; } if (HasStageArrived() && !AIUtils.IsNeedStop(_owner)) { switch (_stage) { case EStage.None: DebugUtil.LogError(" BTMoveAroundTo OnUpdate error, stage is none, owner: {0}", _owner?.GetID()); break; case EStage.Stage1: _stageTargetCellIndex = GetStage2TargetCell(targetAsideDistance); _stage = EStage.Stage2; AIUtils.CheckAndMove(_owner, _stageTargetCellIndex); return TaskStatus.Running; case EStage.Stage2: _stage = EStage.MoveToTarget; AIUtils.CheckAndMove(_owner, _stageTargetCellIndex, false); return TaskStatus.Success; } } return TaskStatus.Running; } private int GetStage1TargetCell(int distance) { var targetPosition = _map.TGSCellIndex2WorldPosition(_target.transData.cellIndex); var supposedStartPosition = targetPosition - _baseDirection; Vector3 move;