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;

  for (var row = 0; row < rows; row++) {
    // 偶数行从左到右,奇数行从右到左
    if (row % 2 == 0) {
      for (var col = 0; col < columns; col++) {
        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 col = columns - 1; col >= 0; col--) {
        final coord = isPointy
            ? HexCoordinate.fromOffsetPointy(col, row)
            : HexCoordinate.fromOffsetFlat(col, row);
        positions.add(coord);

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

  return positions;
}