match-three/lib/game/match_three_game.dart
savinmax ea3f0c4e18 Add level system with progression and selection screen
- Add Level model with constraints, objectives, and star ratings
- Create LevelService for loading levels from JSON configuration
- Implement LevelSelectionScreen with visual progress tracking
- Update GameBloc to handle level-based gameplay
- Add 10 predefined levels with varying difficulty and objectives
- Integrate level progression system into game flow
2025-09-21 17:08:44 +02:00

71 lines
1.8 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;
GridComponent? gridComponent;
Gem? selectedGem;
MatchThreeGame(this.levelId) : super();
@override
Future<void> onLoad() async {
backgroundComponent = BackgroundComponent();
add(backgroundComponent);
gridComponent = GridComponent(this, levelId);
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;
}
}