| OLD | NEW |
| (Empty) |
| 1 part of pop_pop_win.html; | |
| 2 | |
| 3 abstract class GameManager { | |
| 4 final int _width, _height, _bombCount; | |
| 5 final GameStorage _gameStorage = new GameStorage(); | |
| 6 | |
| 7 Game _game; | |
| 8 StreamSubscription _updatedEventId; | |
| 9 StreamSubscription _gameStateChangedId; | |
| 10 Timer _clockTimer; | |
| 11 | |
| 12 GameManager(this._width, this._height, this._bombCount) { | |
| 13 newGame(); | |
| 14 } | |
| 15 | |
| 16 Game get game => _game; | |
| 17 | |
| 18 Stream<EventArgs> get bestTimeUpdated => _gameStorage.bestTimeUpdated; | |
| 19 | |
| 20 Future<int> get bestTimeMilliseconds => | |
| 21 _gameStorage.getBestTimeMilliseconds(_width, _height, _bombCount); | |
| 22 | |
| 23 void newGame() { | |
| 24 if (_updatedEventId != null) { | |
| 25 assert(_game != null); | |
| 26 assert(_gameStateChangedId != null); | |
| 27 _updatedEventId.cancel(); | |
| 28 _gameStateChangedId.cancel(); | |
| 29 _gameStateChanged(GameState.reset); | |
| 30 } | |
| 31 final f = new Field(_bombCount, _width, _height); | |
| 32 _game = new Game(f); | |
| 33 _updatedEventId = _game.updated.listen(gameUpdated); | |
| 34 _gameStateChangedId = _game.stateChanged.listen(_gameStateChanged); | |
| 35 } | |
| 36 | |
| 37 void gameUpdated(args); | |
| 38 | |
| 39 void resetScores() { | |
| 40 _gameStorage.reset(); | |
| 41 } | |
| 42 | |
| 43 void _click(int x, int y, bool alt) { | |
| 44 final ss = _game.getSquareState(x, y); | |
| 45 | |
| 46 if (alt) { | |
| 47 if (ss == SquareState.hidden) { | |
| 48 _game.setFlag(x, y, true); | |
| 49 } else if (ss == SquareState.flagged) { | |
| 50 _game.setFlag(x, y, false); | |
| 51 } else if (ss == SquareState.revealed) { | |
| 52 _game.reveal(x, y); | |
| 53 } | |
| 54 } else { | |
| 55 if (ss == SquareState.hidden) { | |
| 56 _game.reveal(x, y); | |
| 57 } | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 void updateClock() { | |
| 62 if (_clockTimer == null && _game.state == GameState.started) { | |
| 63 _clockTimer = new Timer(const Duration(seconds: 1), updateClock); | |
| 64 } else if (_clockTimer != null && _game.state != GameState.started) { | |
| 65 _clockTimer.cancel(); | |
| 66 _clockTimer = null; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 void onNewBestTime(int value) {} | |
| 71 | |
| 72 void onGameStateChanged(GameState value) {} | |
| 73 | |
| 74 bool get _canClick { | |
| 75 return _game.state == GameState.reset || _game.state == GameState.started; | |
| 76 } | |
| 77 | |
| 78 void _gameStateChanged(GameState newState) { | |
| 79 _gameStorage.recordState(newState); | |
| 80 if (newState == GameState.won) { | |
| 81 _gameStorage.updateBestTime(_game).then((bool newBestTime) { | |
| 82 if (newBestTime) { | |
| 83 bestTimeMilliseconds.then((int val) { | |
| 84 onNewBestTime(val); | |
| 85 }); | |
| 86 } | |
| 87 }); | |
| 88 } | |
| 89 updateClock(); | |
| 90 onGameStateChanged(newState); | |
| 91 } | |
| 92 } | |
| OLD | NEW |