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