| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 | |
| 5 /** A local player (API known to the main isolate when creating players). */ | |
| 6 interface Player default PlayerImpl { | |
| 7 | |
| 8 Player(); | |
| 9 | |
| 10 final Future<SendPort> portToPlayer; | |
| 11 | |
| 12 void setup(Window window, int player); | |
| 13 | |
| 14 void set enemy(SendPort portToEnemy); | |
| 15 | |
| 16 void set _portForTest(SendPort testPort); | |
| 17 } | |
| 18 | |
| 19 /** A remote enemy (API visible to a player to communicate with the enemy). */ | |
| 20 interface Enemy default EnemyImpl { | |
| 21 Enemy(SendPort port); | |
| 22 | |
| 23 /** tell the enemy that we are ready, receive confirmation asynchronously. */ | |
| 24 Future<int> ready(); | |
| 25 | |
| 26 /** shoot asynchronously. */ | |
| 27 Future<int> shoot(int x, int y); | |
| 28 } | |
| 29 | |
| 30 | |
| 31 /** | |
| 32 * A default implementation for player that sends messages to an isolate, which | |
| 33 * contains the actual player state. | |
| 34 */ | |
| 35 class PlayerImpl implements Player { | |
| 36 final Future<SendPort> portToPlayer; | |
| 37 | |
| 38 PlayerImpl() : portToPlayer = new PlayerState().spawn(); | |
| 39 | |
| 40 void setup(Window window, int player) { | |
| 41 portToPlayer.then((SendPort port) => port.call( | |
| 42 { "action" : MessageIds.SETUP, | |
| 43 "args" : [player] })); | |
| 44 } | |
| 45 | |
| 46 void set enemy(SendPort portToEnemy) { | |
| 47 portToPlayer.then((port) => port.call( | |
| 48 { "action" : MessageIds.SET_ENEMY, | |
| 49 "args" : [portToEnemy]})); | |
| 50 } | |
| 51 | |
| 52 void set _portForTest(SendPort testPort) { | |
| 53 portToPlayer.then((port) => port.call( | |
| 54 { "action" : MessageIds.SET_PORT_FOR_TEST, | |
| 55 "args" : [testPort]})); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 /** | |
| 60 * A default implementation for an enemy that sends messages to an isolate, | |
| 61 * which contains the actual enemy state. | |
| 62 */ | |
| 63 class EnemyImpl implements Enemy { | |
| 64 SendPort portToEnemy; | |
| 65 | |
| 66 EnemyImpl(this.portToEnemy) {} | |
| 67 | |
| 68 Future<int> ready() { | |
| 69 Completer<int> res = new Completer<int>(); | |
| 70 ReceivePort port = portToEnemy.call( | |
| 71 { "action" : MessageIds.ENEMY_IS_READY }); | |
| 72 port.receive((var message, SendPort replyTo) { | |
| 73 bool success = message[0]; | |
| 74 if (success) { | |
| 75 res.complete(0); | |
| 76 } else { | |
| 77 res.completeException(message[1]); | |
| 78 } | |
| 79 }); | |
| 80 return res.future; | |
| 81 } | |
| 82 | |
| 83 Future<int> shoot(int x, int y) { | |
| 84 Completer<int> res = new Completer<int>(); | |
| 85 ReceivePort port = portToEnemy.call( | |
| 86 { "action" : MessageIds.SHOOT, "args" : [x, y] }); | |
| 87 port.receive((var message, SendPort replyTo) { | |
| 88 bool success = message[0]; | |
| 89 if (success) { | |
| 90 res.complete(message[1][0]); | |
| 91 } else { | |
| 92 res.completeException(message[1]); | |
| 93 } | |
| 94 }); | |
| 95 return res.future; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 /** Collection of message IDs used to communicate with player isolates. */ | |
| 100 class MessageIds { | |
| 101 /** message to set up a new player. */ | |
| 102 static final SETUP = 1; | |
| 103 | |
| 104 /** message to initialize the enemy of a player. */ | |
| 105 static final SET_ENEMY = 2; | |
| 106 | |
| 107 /** message indicating that the enemy is ready to play. */ | |
| 108 static final ENEMY_IS_READY = 3; | |
| 109 | |
| 110 /** message describing a shoot action. */ | |
| 111 static final SHOOT = 4; | |
| 112 | |
| 113 /** message to set up a test port, used to make tests non-flaky. */ | |
| 114 static final SET_PORT_FOR_TEST = 5; | |
| 115 } | |
| OLD | NEW |