match-three/lib/game/match_three_game.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

77 lines
2.0 KiB
Dart

import 'dart:ui';
import 'package:flame/game.dart';
import 'package:match_three/game/systems/match_detector.dart';
import 'components/grid_component.dart';
import 'components/background_component.dart';
import 'models/gem.dart';
import '../bloc/game_bloc.dart';
const gridSize = 8 * 64.0 + 32.0; // 8 gems * 64px + padding
class MatchThreeGame extends FlameGame {
late BackgroundComponent backgroundComponent;
late GameBloc gameBloc;
final int levelId;
final int gridHeight;
final int gridWidth;
GridComponent? gridComponent;
Gem? selectedGem;
MatchThreeGame(
{required this.levelId,
required this.gridHeight,
required this.gridWidth})
: super();
@override
Future<void> onLoad() async {
backgroundComponent = BackgroundComponent();
add(backgroundComponent);
gridComponent = GridComponent(this, levelId, gridHeight, gridWidth);
add(gridComponent!);
}
@override
void onGameResize(Vector2 size) {
super.onGameResize(size);
// Center the grid on screen
if (gridComponent != null && hasLayout) {
gridComponent!.position = Vector2(
(size.x - gridSize) / 2,
(size.y - gridSize) / 2,
);
}
}
void selectGem(Gem gem) {
print(">>> Selecting gem ${gem.name}(${gem.type}) ${gem.row}, ${gem.col}");
if (selectedGem == null) {
selectedGem = gem;
return;
}
if (MatchDetector.isValidSwap(gridComponent!.gameGrid, selectedGem!.row,
selectedGem!.col, gem.row, gem.col)) {
// Attempt swap
print(
")) Valid swapping ${selectedGem!.row}, ${selectedGem!.col} with ${gem.row}, ${gem.col}");
gameBloc.add(SwapGems(
selectedGem!.row,
selectedGem!.col,
gem.row,
gem.col,
));
} else {
print(
"(( Invalid swapping ${selectedGem!.row}, ${selectedGem!.col} with ${gem.row}, ${gem.col}");
}
selectedGem = null;
}
void setGameBloc(GameBloc bloc) {
gameBloc = bloc;
}
}