Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Cancel any current movement
- public bool CancelMoveTask()
- {
- if (_moveTask == null)
- return false;
- _moveTask = null;
- _moveTaskTokenSource.Cancel();
- _moveTaskTokenSource = null;
- return true;
- }
- // Begin moving to the specified position. The task completes when the actor has reached the position,
- // the destination is found to be unreachable, or movement is canceled.
- public Task<MoveResult> MoveToPosition(Vector2 position)
- {
- if (_movement == null)
- return Task.FromResult(MoveResult.Unreachable);
- CancelMoveTask();
- _moveTaskTokenSource = new CancellationTokenSource();
- _moveTask = MoveToPositionImpl(position, _moveTaskTokenSource.Token);
- return _moveTask;
- }
- private async Task<MoveResult> MoveToPositionImpl(Vector2 position, CancellationToken token)
- {
- while (!token.IsCancellationRequested)
- {
- if (ReachedPosition(position))
- return MoveResult.Done;
- if (IsUnreachable(position))
- return MoveResult.Unreachable;
- Vector2 delta = position - Collider.GetCenter(Actor);
- Debug.Assert(_movement.IsLimited); // for now
- _movement.Direction = MovementBehavior.GetDirection(delta);
- await AIDispatcher.Yield();
- }
- return MoveResult.Canceled;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement