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 /** 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 default PlayerImpl { | 6 interface Player default PlayerImpl { |
7 | 7 |
8 Player(); | 8 Player(); |
9 | 9 |
10 final Future<SendPort> portToPlayer; | 10 final Future<SendPort> portToPlayer; |
11 | 11 |
(...skipping 14 matching lines...) Expand all Loading... |
26 /** shoot asynchronously. */ | 26 /** shoot asynchronously. */ |
27 Future<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 Future<SendPort> portToPlayer; | 36 final SendPort portToPlayer; |
37 | 37 |
38 PlayerImpl() : portToPlayer = new PlayerState().spawn(); | 38 // TODO(ager): This will break with dart2js once spawnFunction runs |
| 39 // isolates in web workers. The player isolates need access to the |
| 40 // DOM and should be spawned with something like spawnFunctionInDOM |
| 41 // that will run on the renderer thread. |
| 42 PlayerImpl() : portToPlayer = spawnFunction(spawnPlayer); |
39 | 43 |
40 void setup(Window window, int player) { | 44 void setup(Window window, int player) { |
41 portToPlayer.then((SendPort port) => port.call( | 45 portToPlayer.call( |
42 { "action" : MessageIds.SETUP, | 46 { "action" : MessageIds.SETUP, |
43 "args" : [player] })); | 47 "args" : [player] }); |
44 } | 48 } |
45 | 49 |
46 void set enemy(SendPort portToEnemy) { | 50 void set enemy(SendPort portToEnemy) { |
47 portToPlayer.then((port) => port.call( | 51 portToPlayer.call( |
48 { "action" : MessageIds.SET_ENEMY, | 52 { "action" : MessageIds.SET_ENEMY, |
49 "args" : [portToEnemy]})); | 53 "args" : [portToEnemy]}); |
50 } | 54 } |
51 | 55 |
52 void set _portForTest(SendPort testPort) { | 56 void set _portForTest(SendPort testPort) { |
53 portToPlayer.then((port) => port.call( | 57 portToPlayer.call( |
54 { "action" : MessageIds.SET_PORT_FOR_TEST, | 58 { "action" : MessageIds.SET_PORT_FOR_TEST, |
55 "args" : [testPort]})); | 59 "args" : [testPort]}); |
56 } | 60 } |
57 } | 61 } |
58 | 62 |
59 /** | 63 /** |
60 * A default implementation for an enemy that sends messages to an isolate, | 64 * A default implementation for an enemy that sends messages to an isolate, |
61 * which contains the actual enemy state. | 65 * which contains the actual enemy state. |
62 */ | 66 */ |
63 class EnemyImpl implements Enemy { | 67 class EnemyImpl implements Enemy { |
64 SendPort portToEnemy; | 68 SendPort portToEnemy; |
65 | 69 |
(...skipping 28 matching lines...) Expand all Loading... |
94 | 98 |
95 /** message indicating that the enemy is ready to play. */ | 99 /** message indicating that the enemy is ready to play. */ |
96 static final ENEMY_IS_READY = 3; | 100 static final ENEMY_IS_READY = 3; |
97 | 101 |
98 /** message describing a shoot action. */ | 102 /** message describing a shoot action. */ |
99 static final SHOOT = 4; | 103 static final SHOOT = 4; |
100 | 104 |
101 /** message to set up a test port, used to make tests non-flaky. */ | 105 /** message to set up a test port, used to make tests non-flaky. */ |
102 static final SET_PORT_FOR_TEST = 5; | 106 static final SET_PORT_FOR_TEST = 5; |
103 } | 107 } |
OLD | NEW |