match_magic/lib/game/match_magic_game.dart

214 lines
6.5 KiB
Dart
Raw Normal View History

2024-08-02 13:53:40 +03:00
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
2024-08-13 17:50:11 +03:00
import 'package:match_magic/game/sprite_loader.dart';
2024-08-05 11:04:54 +03:00
import 'package:provider/provider.dart';
import 'board.dart';
import 'swap_notifier.dart';
2024-08-02 13:53:40 +03:00
2024-08-13 17:50:11 +03:00
class MatchMagicGameScreen extends StatefulWidget {
const MatchMagicGameScreen({Key? key}) : super(key: key);
@override
_MatchMagicGameScreenState createState() => _MatchMagicGameScreenState();
}
class _MatchMagicGameScreenState extends State<MatchMagicGameScreen> {
late Future<List<Sprite>> _spritesFuture;
Board? board;
@override
void initState() {
super.initState();
_spritesFuture = SpriteLoader.loadSprites();
}
void _restartGame() {
context.read<SwapNotifier>().resetScore();
setState(() {
board = null;
_spritesFuture = SpriteLoader.loadSprites();
});
}
void _showSettingsDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Settings'),
content: const Text('The settings have not yet been implemented.'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
_showExitConfirmationDialog();
},
child: const Text('Exit to Main Menu'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Close'),
),
],
);
},
);
}
void _showExitConfirmationDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Warning'),
content: const Text(
'You will lose all game progress. Are you sure you want to exit?'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).popUntil((route) => route.isFirst);
},
child: const Text('Yes'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('No'),
),
],
);
},
);
}
2024-08-13 17:50:11 +03:00
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: FutureBuilder<List<Sprite>>(
future: _spritesFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (!snapshot.hasData || snapshot.data == null) {
return const Center(child: Text('No sprites found'));
} else {
final sprites = snapshot.data!;
board ??= Board(
sprites: sprites, swapNotifier: context.read<SwapNotifier>());
return Stack(
children: [
Column(
children: [
const SizedBox(height: 50),
const ScoreDisplay(),
Expanded(
child: Center(
child: AspectRatio(
aspectRatio: 1,
child: GameWidget(game: board!),
),
),
2024-08-29 16:44:44 +03:00
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _restartGame,
child: const Text(
'Restart',
style: TextStyle(color: Colors.black),
),
),
const SizedBox(width: 20),
ElevatedButton(
onPressed: () {
board?.showHint();
},
child: const Text(
'Hint',
style: TextStyle(color: Colors.black),
),
),
],
2024-08-29 16:44:44 +03:00
),
const SizedBox(height: 50),
],
),
Positioned(
top: 16,
left: 16,
child: IconButton(
icon: const Icon(Icons.settings, color: Colors.white),
onPressed: _showSettingsDialog,
2024-08-29 16:44:44 +03:00
),
),
],
);
}
} else {
return const Center(child: CircularProgressIndicator());
2024-08-13 17:50:11 +03:00
}
},
),
2024-08-13 17:50:11 +03:00
);
}
}
class ScoreDisplay extends StatelessWidget {
const ScoreDisplay({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Consumer<SwapNotifier>(
builder: (context, notifier, child) {
return Card(
color: Colors.blueGrey[900],
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Score: ${notifier.score}',
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
);
},
),
Consumer<SwapNotifier>(
builder: (context, notifier, child) {
return Card(
color: Colors.blueGrey[900],
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Moves: ${notifier.moveCount}',
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
);
},
),
],
2024-08-05 11:04:54 +03:00
),
);
2024-08-02 13:53:40 +03:00
}
}