OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 void spawnPlayer() { | |
6 PlayerState state = new PlayerState(); | |
7 port.receive((message, SendPort replyTo) { | |
kasperl
2012/08/03 05:20:27
port.receive(state.dispatch)?
| |
8 state.dispatch(message, replyTo); | |
9 }); | |
10 } | |
11 | |
5 /** | 12 /** |
6 * Stores the actual data on a player's boat grid, the UI representation for its | 13 * Stores the actual data on a player's boat grid, the UI representation for its |
7 * grid and the status of each shot. Acts as a controller handling isolate | 14 * grid and the status of each shot. Acts as a controller handling isolate |
8 * messages (from the main isolate message and shots from the enemy), and UI | 15 * messages (from the main isolate message and shots from the enemy), and UI |
9 * events. | 16 * events. |
10 */ | 17 */ |
11 // TODO(sigmund): move UI setup out of here, e.g. into a controller class. | 18 // TODO(sigmund): move UI setup out of here, e.g. into a controller class. |
12 class PlayerState extends Isolate { | 19 class PlayerState { |
13 | 20 |
14 /** internal id for this player. */ | 21 /** internal id for this player. */ |
15 int _id; | 22 int _id; |
16 | 23 |
17 /** Set up of boats on the board. */ | 24 /** Set up of boats on the board. */ |
18 BoatGrid boats; | 25 BoatGrid boats; |
19 | 26 |
20 /** State of shots taken by the enemy on this player's board. */ | 27 /** State of shots taken by the enemy on this player's board. */ |
21 GridState localGrid; | 28 GridState localGrid; |
22 | 29 |
(...skipping 17 matching lines...) Expand all Loading... | |
40 | 47 |
41 /** UI representation of this player's grid. */ | 48 /** UI representation of this player's grid. */ |
42 PlayerGridView _ownView; | 49 PlayerGridView _ownView; |
43 | 50 |
44 /** UI representation of the enemy's grid. */ | 51 /** UI representation of the enemy's grid. */ |
45 EnemyGridView _enemyView; | 52 EnemyGridView _enemyView; |
46 | 53 |
47 /** Port used for testing purposes. */ | 54 /** Port used for testing purposes. */ |
48 SendPort _portForTest; | 55 SendPort _portForTest; |
49 | 56 |
50 // This can take no arguments for now (wait for isolate redesign). | |
51 PlayerState() : super.light() {} | |
52 | |
53 void main() { | |
54 this.port.receive((message, SendPort replyTo) { | |
55 dispatch(message, replyTo); | |
56 }); | |
57 } | |
58 | |
59 /** dispatches all messages that are expected in this isolate. */ | 57 /** dispatches all messages that are expected in this isolate. */ |
60 void dispatch(var message, SendPort replyTo) { | 58 void dispatch(var message, SendPort replyTo) { |
61 int action = message['action']; | 59 int action = message['action']; |
62 List args = message['args']; | 60 List args = message['args']; |
63 switch (action) { | 61 switch (action) { |
64 // message from the main program, setting this as player 1 or 2 | 62 // message from the main program, setting this as player 1 or 2 |
65 case MessageIds.SETUP: | 63 case MessageIds.SETUP: |
66 handleSetup(args[0]); | 64 handleSetup(args[0]); |
67 break; | 65 break; |
68 // message from the main program, giving port to talk with other player | 66 // message from the main program, giving port to talk with other player |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 case Constants.SUNK: | 296 case Constants.SUNK: |
299 totalHits++; | 297 totalHits++; |
300 boatsSunk++; | 298 boatsSunk++; |
301 _enemyView.addHit(x, y); | 299 _enemyView.addHit(x, y); |
302 enemyGrid.hit(x, y); | 300 enemyGrid.hit(x, y); |
303 break; | 301 break; |
304 } | 302 } |
305 _enemyView.statusBar.updateStatus(); | 303 _enemyView.statusBar.updateStatus(); |
306 } | 304 } |
307 } | 305 } |
OLD | NEW |