public AutonomousPathfinderMover(Player player)
 {
     TeleportPlayerToCenterOfCurrentTile(player);
     _lastTargetReached = new Input.AutonomousPathfinderMover.PathFinderInfo()
     {
         Direction = DirectionData.DegreesToDirectionData((int)player.ViewingAngle),
         Target    = player.Position
     };
     GetNextTarget(player);
 }
        private PathFinderInfo MoveCandidateForwardUntilBlocked(PathFinderInfo startingPointInfo, int[,] map)
        {
            var direction     = startingPointInfo.Direction;
            var lastUnblocked = new Point((int)startingPointInfo.Target.X, (int)startingPointInfo.Target.Y);

            while (IsTileUnblocked(new Point(lastUnblocked.X + (int)direction.RelativePosition.X, lastUnblocked.Y + (int)direction.RelativePosition.Y), map))
            {
                lastUnblocked = new Point(lastUnblocked.X + (int)direction.RelativePosition.X, lastUnblocked.Y + (int)direction.RelativePosition.Y);
            }
            return(new PathFinderInfo()
            {
                Direction = direction, Target = new Vector2(lastUnblocked.X + .5f, lastUnblocked.Y + .5f)
            });
        }
        public void Update(GameTime gameTime, Player player)
        {
            if (!_nextTarget.HasValue)
            {
                GetNextTarget(player);
            }

            if (HasDestinationTileBeenReached(player))
            {
                var turn = GetAmountToTurn(_lastTargetReached.Direction, _nextTarget.Value.Direction);
                _reversing         = (turn == 180);
                _amountLeftToTurn += turn;

                _lastTargetReached = _nextTarget.Value;
                _nextTarget        = null;
            }
            if (_amountLeftToTurn != 0)
            {
                var amountToTurn = 1;// (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 6);// Math.Abs(_amountLeftToTurn) > 90 ? 3 : 1;
                if (_amountLeftToTurn < 0)
                {
                    player.ViewingAngle -= amountToTurn; _amountLeftToTurn += amountToTurn;
                }
                else
                {
                    player.ViewingAngle += amountToTurn; _amountLeftToTurn -= amountToTurn;
                }
                if (Math.Abs(_amountLeftToTurn) < 15)
                {
                    NudgePathTowardsCenterOfTiles(player);
                }
            }
            else
            {
                if (_reversing)
                {
                    _reversing = false;
                }
            }
            if (!_reversing)
            {
                player.MoveForward();
            }
        }
        private PathFinderInfo AddBestTurnDirection(PathFinderInfo info, int[,] map)
        {
            var candidates = DirectionData.GetRightAndLeftInRandomOrder(_lastTargetReached.Direction).ToList();

            candidates.Add(DirectionData.GetOppositeDirection(_lastTargetReached.Direction));
            foreach (var directionCandidate in candidates)
            {
                var pointToTest = info.Target + directionCandidate.RelativePosition;
                var tileToTest  = new Point((int)pointToTest.X, (int)pointToTest.Y);
                if (IsTileUnblocked(tileToTest, map))
                {
                    return(new PathFinderInfo()
                    {
                        Direction = directionCandidate, Target = info.Target
                    });
                }
            }
            throw new ArgumentException("Unable to find way out of tile: " + info.Target + " going direction " + info.Direction);
        }