match_magic/lib/game/swap_notifier.dart

55 lines
1.1 KiB
Dart
Raw Normal View History

2024-08-05 11:04:54 +03:00
import 'package:flutter/material.dart';
import 'tile.dart';
class SwapNotifier extends ChangeNotifier {
Tile? selectedTile;
2024-08-13 17:50:11 +03:00
int _score = 0;
int _moveCount = 0;
2024-08-05 11:04:54 +03:00
2024-08-13 17:50:11 +03:00
int get score => _score;
int get moveCount => _moveCount;
2024-08-13 17:50:11 +03:00
void resetScore() {
_score = 0;
_moveCount = 0;
2024-08-13 17:50:11 +03:00
selectedTile = null;
notifyListeners();
}
void incrementScore(int value) {
_score += value;
notifyListeners();
}
void incrementMoveCount() {
_moveCount += 1;
notifyListeners();
}
2024-08-13 17:50:11 +03:00
void selectTile(Tile tile) {
2024-08-05 11:04:54 +03:00
if (selectedTile == null) {
selectedTile = tile;
tile.select();
2024-08-05 11:04:54 +03:00
} else {
2024-09-09 15:09:43 +03:00
if (_isNeighbor(selectedTile!, tile) || selectedTile!.isMagicCube) {
2024-08-13 17:50:11 +03:00
notifyListeners();
2024-08-05 11:04:54 +03:00
} else {
selectedTile?.deselect();
2024-08-05 11:04:54 +03:00
selectedTile = tile;
tile.select();
2024-08-13 17:50:11 +03:00
notifyListeners();
2024-08-05 11:04:54 +03: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 17:50:11 +03:00
void clearSelectedTile() {
selectedTile = null;
notifyListeners();
}
2024-08-05 11:04:54 +03:00
}