private void calculateDistances(Position position) {
        Queue<Position> positionsToProcess = new ArrayDeque<>();

        distance = new int[map.getWidth()][map.getHeight()];
        boolean[][] checked = new boolean[distance.length][distance[0].length];

        for (int i = 0; i < distance.length; i++)
            for (int j = 0; j < distance[0].length; j++)
                distance[i][j] = distanceLimit; // Initializing array of distances with maximal value

        checked[position.getX()][position.getY()] = true;
        distance[position.getX()][position.getY()] = 0;

        Position p;

        while (position != null) {

            if ( distance[position.getX()][position.getY()] >= distanceLimit ) {
                position = positionsToProcess.poll();
                continue;
            }

            for (int i = position.getX() - 1; i <= position.getX() + 1; i++)
                for (int j = position.getY() - 1; j <= position.getY() + 1; j++)
                    if ( insideArray(i, j) && !checked[i][j] ) {
                        p = Position.getInstance(i, j);
                        if (map.isCellPassable(p)) {
                            positionsToProcess.add(p);
                            distance[i][j] = distance[position.getX()][position.getY()] + 1;
                            checked[i][j] = true;
                        }
                    }

            position = positionsToProcess.poll();
        }
    }