match-three/lib/game/systems/gravity_system.dart
savinmax 3f12ce8d3f Add dynamic grid sizing support for levels
- Add gridWidth and gridHeight properties to level configuration
- Update GameGrid to accept custom dimensions instead of using constants
- Modify GridComponent to calculate gem size based on grid dimensions
- Update MatchThreeGame constructor to pass grid dimensions
- Ensure proper scaling and positioning for variable grid sizes
2025-09-21 18:06:00 +02:00

56 lines
1.4 KiB
Dart

import 'dart:math';
import '../models/gem.dart';
import '../models/grid.dart';
import '../../utils/constants.dart';
class GravitySystem {
static void applyGravity(GameGrid grid) {
for (int col = 0; col < grid.width; col++) {
_dropColumn(grid, col);
}
}
static List<Gem> generateGems(GameGrid grid) {
// Fill empty spaces with new gems
final random = Random();
final List<Gem> newGems = [];
for (int row = 0; row < grid.height; row++) {
for (int col = 0; col < grid.width; col++) {
if (grid.getGem(row, col) == null) {
final gem = Gem(
row: row,
col: col,
type: random.nextInt(GameConstants.gemTypes.length),
);
newGems.add(gem);
grid.setGem(row, col, gem);
}
}
}
return newGems;
}
static void _dropColumn(GameGrid grid, int col) {
final gems = <Gem>[];
// Collect non-null gems from bottom to top
for (int row = grid.height - 1; row >= 0; row--) {
final gem = grid.getGem(row, col);
if (gem != null) {
gems.add(gem);
}
}
// Clear column
for (int row = 0; row < grid.height; row++) {
grid.setGem(row, col, null);
}
// Place gems at bottom
for (int i = 0; i < gems.length; i++) {
final newRow = grid.height - 1 - i;
grid.setGem(newRow, col, gems[i].copyWith(row: newRow, col: col));
}
}
}