1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export control
laws.
This software is made available AS IS, and Xerox Corporation makes no warranty
about the software, its performance or its conformity to any specification.
|<--- this code is formatted to fit into 80 columns --->|
|<--- this code is formatted to fit into 80 columns --->|
|<--- this code is formatted to fit into 80 columns --->|
*/
package spacewar;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
class Player extends Pilot implements KeyListener {
private KeyMapping keyMapping;
/** current rotation key */
private int rotation_direction = Ship.STOP; // current rotation key
/** current thrust */
private boolean thrust_on = false;
Player(Game theGame, int number) {
super(theGame,number);
if (getNumber() == 1)
keyMapping = KeyMapping.keyMapping1;
else if (getNumber() == 2)
keyMapping = KeyMapping.keyMapping2;
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
boolean consumed = true;
if (keyCode == keyMapping.fire) {
ship.fire();
}
else if (keyCode == keyMapping.thrust && !thrust_on) {
ship.thrust(true);
thrust_on = true;
}
else if (keyCode == keyMapping.right &&
rotation_direction != Ship.COUNTERCLOCKWISE) {
//start rotating clockwise unless already rotating in the
//opposite direction
rotation_direction = Ship.CLOCKWISE;
ship.rotate(Ship.CLOCKWISE);
}
else if (keyCode == keyMapping.left &&
rotation_direction != Ship.CLOCKWISE) {
//start rotating counterclockwise unless already rotating in the
//opposite direction
rotation_direction = Ship.COUNTERCLOCKWISE;
ship.rotate(Ship.COUNTERCLOCKWISE);
}
else {
consumed = false;
}
if (consumed) e.consume();
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == keyMapping.thrust) {
ship.thrust(false); //engine off
thrust_on = false;
}
else if (keyCode == keyMapping.right &&
rotation_direction == Ship.CLOCKWISE
||
keyCode == keyMapping.left &&
rotation_direction == Ship.COUNTERCLOCKWISE) {
ship.rotate(Ship.STOP); //stop rotation
rotation_direction = Ship.STOP;
}
}
public void keyTyped(KeyEvent e) {
// have to implement this because it's in KeyListener
}
}
class KeyMapping {
static final KeyMapping keyMapping1 =
new KeyMapping(KeyEvent.VK_LEFT,
KeyEvent.VK_RIGHT,
KeyEvent.VK_UP,
KeyEvent.VK_SPACE);
static final KeyMapping keyMapping2 =
new KeyMapping(KeyEvent.VK_X,
KeyEvent.VK_V,
KeyEvent.VK_D,
KeyEvent.VK_ALT);
int left, right, thrust, fire;
KeyMapping(int k_left, int k_right, int k_thrust, int k_fire) {
left = k_left;
right = k_right;
thrust = k_thrust;
fire = k_fire;
}
}
|