OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 library pop_pop_win.game_manager; | |
5 | |
6 import 'dart:async'; | |
7 | |
8 import 'game_storage.dart'; | |
9 import 'game.dart'; | |
10 | |
11 abstract class GameManager { | |
12 final int _width, _height, _bombCount; | |
13 final GameStorage _gameStorage = new GameStorage(); | |
14 | |
15 Game _game; | |
16 StreamSubscription _updatedEventId; | |
17 StreamSubscription _gameStateChangedId; | |
18 Timer _clockTimer; | |
19 | |
20 GameManager(this._width, this._height, this._bombCount) { | |
21 newGame(); | |
22 } | |
23 | |
24 Game get game => _game; | |
25 | |
26 Stream get bestTimeUpdated => _gameStorage.bestTimeUpdated; | |
27 | |
28 Future<int> get bestTimeMilliseconds => | |
29 _gameStorage.getBestTimeMilliseconds(_width, _height, _bombCount); | |
30 | |
31 void newGame() { | |
32 if (_updatedEventId != null) { | |
33 assert(_game != null); | |
34 assert(_gameStateChangedId != null); | |
35 _updatedEventId.cancel(); | |
36 _gameStateChangedId.cancel(); | |
37 _gameStateChanged(GameState.reset); | |
38 } | |
39 final f = new Field(_bombCount, _width, _height); | |
40 _game = new Game(f); | |
41 _updatedEventId = _game.updated.listen((_) => gameUpdated()); | |
42 _gameStateChangedId = _game.stateChanged.listen(_gameStateChanged); | |
43 } | |
44 | |
45 void gameUpdated() {} | |
46 | |
47 void resetScores() { | |
48 _gameStorage.reset(); | |
49 } | |
50 | |
51 void _click(int x, int y, bool alt) { | |
52 final ss = _game.getSquareState(x, y); | |
53 | |
54 if (alt) { | |
55 if (ss == SquareState.hidden) { | |
56 _game.setFlag(x, y, true); | |
57 } else if (ss == SquareState.flagged) { | |
58 _game.setFlag(x, y, false); | |
59 } else if (ss == SquareState.revealed) { | |
60 _game.reveal(x, y); | |
61 } | |
62 } else { | |
63 if (ss == SquareState.hidden) { | |
64 _game.reveal(x, y); | |
65 } | |
66 } | |
67 } | |
68 | |
69 void updateClock() { | |
70 if (_clockTimer == null && _game.state == GameState.started) { | |
71 _clockTimer = new Timer(const Duration(seconds: 1), updateClock); | |
72 } else if (_clockTimer != null && _game.state != GameState.started) { | |
73 _clockTimer.cancel(); | |
74 _clockTimer = null; | |
75 } | |
76 } | |
77 | |
78 void onNewBestTime(int value) {} | |
79 | |
80 void onGameStateChanged(GameState value) {} | |
81 | |
82 bool get _canClick { | |
83 return _game.state == GameState.reset || _game.state == GameState.started; | |
84 } | |
85 | |
86 void _gameStateChanged(GameState newState) { | |
87 _gameStorage.recordState(newState); | |
88 if (newState == GameState.won) { | |
89 _gameStorage.updateBestTime(_game).then((bool newBestTime) { | |
90 if (newBestTime) { | |
91 bestTimeMilliseconds.then((int val) { | |
92 onNewBestTime(val); | |
93 }); | |
94 } | |
95 }); | |
96 } | |
97 updateClock(); | |
98 onGameStateChanged(newState); | |
99 } | |
100 } | |
OLD | NEW |