generatePositions method

  1. @override
List<HexCoordinate> generatePositions({
  1. required int columns,
  2. required int rows,
  3. required HexOrientation orientation,
  4. int? count,
})
override

根据策略生成坐标序列

columns 网格列数 rows 网格行数 orientation 六边形方向 count 需要的坐标数量(如果为null,返回所有可用坐标)

Implementation

@override
List<HexCoordinate> generatePositions({
  required int columns,
  required int rows,
  required HexOrientation orientation,
  int? count,
}) {
  final positions = <HexCoordinate>[];
  final isPointy = orientation == HexOrientation.pointy;

  if (reverseDirection) {
    // 从右上到左下
    for (var sum = 0; sum < columns + rows - 1; sum++) {
      for (var col = columns - 1; col >= 0; col--) {
        final row = sum - col;
        if (row >= 0 && row < rows) {
          final coord = isPointy
              ? HexCoordinate.fromOffsetPointy(col, row)
              : HexCoordinate.fromOffsetFlat(col, row);
          positions.add(coord);

          if (count != null && positions.length >= count) {
            return positions;
          }
        }
      }
    }
  } else {
    // 从左上到右下
    for (var sum = 0; sum < columns + rows - 1; sum++) {
      for (var col = 0; col < columns; col++) {
        final row = sum - col;
        if (row >= 0 && row < rows) {
          final coord = isPointy
              ? HexCoordinate.fromOffsetPointy(col, row)
              : HexCoordinate.fromOffsetFlat(col, row);
          positions.add(coord);

          if (count != null && positions.length >= count) {
            return positions;
          }
        }
      }
    }
  }

  return positions;
}