| 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(state.dispatch); |
| 8 } |
| 9 |
| 5 /** | 10 /** |
| 6 * Stores the actual data on a player's boat grid, the UI representation for its | 11 * 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 | 12 * 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 | 13 * messages (from the main isolate message and shots from the enemy), and UI |
| 9 * events. | 14 * events. |
| 10 */ | 15 */ |
| 11 // TODO(sigmund): move UI setup out of here, e.g. into a controller class. | 16 // TODO(sigmund): move UI setup out of here, e.g. into a controller class. |
| 12 class PlayerState extends Isolate { | 17 class PlayerState { |
| 13 | 18 |
| 14 /** internal id for this player. */ | 19 /** internal id for this player. */ |
| 15 int _id; | 20 int _id; |
| 16 | 21 |
| 17 /** Set up of boats on the board. */ | 22 /** Set up of boats on the board. */ |
| 18 BoatGrid boats; | 23 BoatGrid boats; |
| 19 | 24 |
| 20 /** State of shots taken by the enemy on this player's board. */ | 25 /** State of shots taken by the enemy on this player's board. */ |
| 21 GridState localGrid; | 26 GridState localGrid; |
| 22 | 27 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 40 | 45 |
| 41 /** UI representation of this player's grid. */ | 46 /** UI representation of this player's grid. */ |
| 42 PlayerGridView _ownView; | 47 PlayerGridView _ownView; |
| 43 | 48 |
| 44 /** UI representation of the enemy's grid. */ | 49 /** UI representation of the enemy's grid. */ |
| 45 EnemyGridView _enemyView; | 50 EnemyGridView _enemyView; |
| 46 | 51 |
| 47 /** Port used for testing purposes. */ | 52 /** Port used for testing purposes. */ |
| 48 SendPort _portForTest; | 53 SendPort _portForTest; |
| 49 | 54 |
| 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. */ | 55 /** dispatches all messages that are expected in this isolate. */ |
| 60 void dispatch(var message, SendPort replyTo) { | 56 void dispatch(var message, SendPort replyTo) { |
| 61 int action = message['action']; | 57 int action = message['action']; |
| 62 List args = message['args']; | 58 List args = message['args']; |
| 63 switch (action) { | 59 switch (action) { |
| 64 // message from the main program, setting this as player 1 or 2 | 60 // message from the main program, setting this as player 1 or 2 |
| 65 case MessageIds.SETUP: | 61 case MessageIds.SETUP: |
| 66 handleSetup(args[0]); | 62 handleSetup(args[0]); |
| 67 break; | 63 break; |
| 68 // message from the main program, giving port to talk with other player | 64 // 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: | 294 case Constants.SUNK: |
| 299 totalHits++; | 295 totalHits++; |
| 300 boatsSunk++; | 296 boatsSunk++; |
| 301 _enemyView.addHit(x, y); | 297 _enemyView.addHit(x, y); |
| 302 enemyGrid.hit(x, y); | 298 enemyGrid.hit(x, y); |
| 303 break; | 299 break; |
| 304 } | 300 } |
| 305 _enemyView.statusBar.updateStatus(); | 301 _enemyView.statusBar.updateStatus(); |
| 306 } | 302 } |
| 307 } | 303 } |
| OLD | NEW |