match_magic/lib/game/swap_notifier.dart

47 lines
975 B
Dart
Raw Normal View History

2024-08-05 08:04:54 +00:00
import 'package:flutter/material.dart';
import 'tile.dart';
class SwapNotifier extends ChangeNotifier {
Tile? selectedTile;
2024-08-13 14:50:11 +00:00
int _score = 0;
2024-08-05 08:04:54 +00:00
2024-08-13 14:50:11 +00:00
int get score => _score;
void resetScore() {
_score = 0;
selectedTile = null;
notifyListeners();
}
void incrementScore(int value) {
_score += value;
notifyListeners();
}
void selectTile(Tile tile) {
2024-08-05 08:04:54 +00:00
if (selectedTile == null) {
selectedTile = tile;
tile.select();
2024-08-05 08:04:54 +00:00
} else {
if (_isNeighbor(selectedTile!, tile)) {
2024-08-13 14:50:11 +00:00
notifyListeners();
2024-08-05 08:04:54 +00:00
} else {
selectedTile?.deselect();
2024-08-05 08:04:54 +00:00
selectedTile = tile;
tile.select();
2024-08-13 14:50:11 +00:00
notifyListeners();
2024-08-05 08:04:54 +00:00
}
}
}
bool _isNeighbor(Tile tile1, Tile tile2) {
return (tile1.row == tile2.row && (tile1.col - tile2.col).abs() == 1) ||
(tile1.col == tile2.col && (tile1.row - tile2.row).abs() == 1);
}
2024-08-13 14:50:11 +00:00
void clearSelectedTile() {
selectedTile = null;
notifyListeners();
}
2024-08-05 08:04:54 +00:00
}