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 final port = await portToPlayer; | |
42 port.call({ "action" : MessageIds.SETUP, "args" : [player] }); | |
43 } | |
44 | |
45 void set enemy(SendPort portToEnemy) { | |
46 final port = await portToPlayer; | |
47 port.call({ "action" : MessageIds.SET_ENEMY, "args" : [portToEnemy]}); | |
48 } | |
49 | |
50 void set _portForTest(SendPort testPort) { | |
51 final port = await portToPlayer; | |
52 port.call({ "action" : MessageIds.SET_PORT_FOR_TEST, "args" : [testPort]}); | |
53 } | |
54 } | |
55 | |
56 /** | |
57 * A default implementation for an enemy that sends messages to an isolate, | |
58 * which contains the actual enemy state. | |
59 */ | |
60 class EnemyImpl implements Enemy { | |
61 SendPort portToEnemy; | |
62 | |
63 EnemyImpl(this.portToEnemy) {} | |
64 | |
65 Future<int> ready() { | |
66 Completer<int> res = new Completer<int>(); | |
67 ReceivePort port = portToEnemy.call( | |
68 { "action" : MessageIds.ENEMY_IS_READY }); | |
69 port.receive((var message, SendPort replyTo) { | |
70 bool success = message[0]; | |
71 if (success) { | |
72 res.complete(0); | |
73 } else { | |
74 res.completeException(message[1]); | |
75 } | |
76 }); | |
77 return res.future; | |
78 } | |
79 | |
80 Future<int> shoot(int x, int y) { | |
81 Completer<int> res = new Completer<int>(); | |
82 ReceivePort port = portToEnemy.call( | |
83 { "action" : MessageIds.SHOOT, "args" : [x, y] }); | |
84 port.receive((var message, SendPort replyTo) { | |
85 bool success = message[0]; | |
86 if (success) { | |
87 res.complete(message[1][0]); | |
88 } else { | |
89 res.completeException(message[1]); | |
90 } | |
91 }); | |
92 return res.future; | |
93 } | |
94 } | |
95 | |
96 /** Collection of message IDs used to communicate with player isolates. */ | |
97 class MessageIds { | |
98 /** message to set up a new player. */ | |
99 static final SETUP = 1; | |
100 | |
101 /** message to initialize the enemy of a player. */ | |
102 static final SET_ENEMY = 2; | |
103 | |
104 /** message indicating that the enemy is ready to play. */ | |
105 static final ENEMY_IS_READY = 3; | |
106 | |
107 /** message describing a shoot action. */ | |
108 static final SHOOT = 4; | |
109 | |
110 /** message to set up a test port, used to make tests non-flaky. */ | |
111 static final SET_PORT_FOR_TEST = 5; | |
112 } | |
OLD | NEW |