Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: samples/third_party/pop-pop-win/lib/src/html/game_manager.dart

Issue 200723008: samples/poppopwin: upgrade to first-class sample (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixed pkgbuild.status for rename Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 part of ppw_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 ||
76 _game.state == GameState.started;
77 }
78
79 void _gameStateChanged(GameState newState) {
80 _gameStorage.recordState(newState);
81 if(newState == GameState.won) {
82 _gameStorage.updateBestTime(_game)
83 .then((bool newBestTime) {
84 if(newBestTime) {
85 bestTimeMilliseconds.then((int val) {
86 onNewBestTime(val);
87 });
88 }
89 });
90 }
91 updateClock();
92 onGameStateChanged(newState);
93 }
94 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698