private void MoveTowards(Position targetPosition) { if (currentPath == null || currentPath.Count == 0 || !targetPosition.Equals(currentTargetPosition) || !CanMove(currentPath.First()) || (position.Distance(targetPosition) % 11 == 0)) // Every 11 steps make sure that a new best path hasn't magically appeared { // Try to create a path if the current one is invalid currentPath = Pathfinder.FindPath(engine, this, engine.map.GetTileAt(position), engine.map.GetTileAt(targetPosition)); currentTargetPosition = targetPosition; // Remove the first element because we are already there if (currentPath != null) { currentPath.RemoveAt(0); } } if (currentPath != null && currentPath.Count > 0 && currentTargetPosition.Equals(targetPosition) && CanMove(currentPath.First())) { Position nextStep = currentPath.First(); currentPath.RemoveAt(0); TryToMove(nextStep); } else { status = Status.Idle; // Retry after moveRetryCooldown ticks engine.ScheduleUpdate(moveRetryCooldown, this); } }