match_magic/lib/game/swap_notifier.dart

30 lines
756 B
Dart

import 'package:flutter/material.dart';
import 'board.dart';
import 'tile.dart';
class SwapNotifier extends ChangeNotifier {
Tile? selectedTile;
void selectTile(Tile tile, Board board) {
if (selectedTile == null) {
selectedTile = tile;
tile.select();
} else {
if (_isNeighbor(selectedTile!, tile)) {
board.swapTiles(selectedTile!, tile, true);
selectedTile = null;
} else {
selectedTile?.deselect();
selectedTile = tile;
tile.select();
}
}
notifyListeners();
}
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);
}
}