246 lines
8.2 KiB
Dart
246 lines
8.2 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:match_magic/game/sprite_loader.dart';
|
|
import 'package:match_magic/utilities/audio_manager.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'board.dart';
|
|
import 'swap_notifier.dart';
|
|
|
|
class MatchMagicGameScreen extends StatefulWidget {
|
|
const MatchMagicGameScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_MatchMagicGameScreenState createState() => _MatchMagicGameScreenState();
|
|
}
|
|
|
|
class _MatchMagicGameScreenState extends State<MatchMagicGameScreen> {
|
|
late Future<List<Sprite>> _crystalsFuture;
|
|
late Future<Sprite> _magicCubeFuture;
|
|
Board? board;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_crystalsFuture = SpriteLoader.loadCrystalSprites();
|
|
_magicCubeFuture = SpriteLoader.loadMagicCubeSprite();
|
|
AudioManager.load();
|
|
}
|
|
|
|
void _restartGame() {
|
|
context.read<SwapNotifier>().resetScore();
|
|
setState(() {
|
|
board = null;
|
|
_crystalsFuture = SpriteLoader.loadCrystalSprites();
|
|
_magicCubeFuture = SpriteLoader.loadMagicCubeSprite();
|
|
});
|
|
}
|
|
|
|
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);
|
|
context.read<SwapNotifier>().resetScore();
|
|
},
|
|
child: const Text('Yes'),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: const Text('No'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.black,
|
|
body: FutureBuilder<List<Sprite>>(
|
|
future: _crystalsFuture,
|
|
builder: (context, crystalSnapshot) {
|
|
if (crystalSnapshot.connectionState == ConnectionState.done) {
|
|
if (crystalSnapshot.hasError) {
|
|
return Center(child: Text('Error: ${crystalSnapshot.error}'));
|
|
} else if (!crystalSnapshot.hasData ||
|
|
crystalSnapshot.data == null) {
|
|
return const Center(child: Text('No crystal sprites found'));
|
|
} else {
|
|
final crystals = crystalSnapshot.data!;
|
|
|
|
return FutureBuilder<Sprite>(
|
|
future: _magicCubeFuture,
|
|
builder: (context, cubeSnapshot) {
|
|
if (cubeSnapshot.connectionState == ConnectionState.done) {
|
|
if (cubeSnapshot.hasError) {
|
|
return Center(
|
|
child: Text('Error: ${cubeSnapshot.error}'));
|
|
} else if (!cubeSnapshot.hasData ||
|
|
cubeSnapshot.data == null) {
|
|
return const Center(
|
|
child: Text('No magic cube sprite found'));
|
|
} else {
|
|
final magicCube = cubeSnapshot.data!;
|
|
|
|
board ??= Board(
|
|
sprites: crystals,
|
|
magicCubeSprite: magicCube,
|
|
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!),
|
|
),
|
|
),
|
|
),
|
|
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),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 50),
|
|
],
|
|
),
|
|
Positioned(
|
|
top: 16,
|
|
left: 16,
|
|
child: IconButton(
|
|
icon: const Icon(Icons.settings,
|
|
color: Colors.white),
|
|
onPressed: _showSettingsDialog,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
} else {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
},
|
|
);
|
|
}
|
|
} else {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
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),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|