-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceRangers.java
More file actions
288 lines (257 loc) · 10.5 KB
/
Copy pathSpaceRangers.java
File metadata and controls
288 lines (257 loc) · 10.5 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
/**
* The SpaceRangers class is the driver class for the entire game.
* It creates the JFrame that holds all the components as well
* as listens to the key inputs from the player.
*
* @author Herman Lin
* @author Devin Zhu
*/
public class SpaceRangers extends JFrame {
public static final int SCREEN_WIDTH = 800;
public static final int SCREEN_HEIGHT = 800;
public static final int DELAY = 10;
private Universe universe;
public static boolean keyHeld = false;
public static int heldKeyCode;
public static Client player;
/**
* Constructor for creating the JFrame and initializing the
* JPanel that is responsible for drawing components.
*/
SpaceRangers(Client player, Color color) {
super("Space Rangers");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.player = player;
universe = new Universe(color);
this.add(universe);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
keyHeld = true; heldKeyCode = e.getKeyCode();
universe.spaceship.rotateLeft();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
keyHeld = true; heldKeyCode = e.getKeyCode();
universe.spaceship.rotateRight();
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
keyHeld = true; heldKeyCode = e.getKeyCode();
universe.spaceship.increaseVelocity();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
keyHeld = true; heldKeyCode = e.getKeyCode();
universe.spaceship.decreaseVelocity();
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
universe.spaceship.computeTranslatedCentroid();
universe.ammunition.add(new Projectile(universe.spaceship.getColor(),
universe.spaceship.getFacing(),
universe.spaceship.getTranslatedCentroidX(),
universe.spaceship.getTranslatedCentroidY()));
}
}
@Override
public void keyReleased(KeyEvent e) { keyHeld = false; }
});
revalidate();
}
public static void main(String[] args) {
new StartUp();
}
}
class Universe extends JPanel {
// adjust this number to change how many starting asteroids
final int NUM_ASTEROIDS = 7;
Ship spaceship;
ArrayList<Projectile> ammunition;
ArrayList<Asteroid> asteroids;
// a queue that is continually updated with Data from other Players
SynchronousQueue<Data> updates;
// DataProcessor compresses/decompresses updates from the Server
DataProcessor dp;
String data = "";
Universe(Color color) {
super();
setBackground(Color.BLACK);
updates = new SynchronousQueue<Data>();
dp = new DataProcessor();
spaceship = new Ship(color);
ammunition = new ArrayList<Projectile>();
String update = "";
// System.out.println("Checking data");
if (SpaceRangers.player.sin.hasNextLine()) {
// System.out.println("Receiving new Data");
update = SpaceRangers.player.readFromServer();
}
// System.out.println("Finished Checking");
if (update.isEmpty()) {
// System.out.println("new data empty");
asteroids = new ArrayList<Asteroid>(NUM_ASTEROIDS);
for (int i = 0; i < NUM_ASTEROIDS; i++) {
Asteroid newAsteroid = new Asteroid(asteroids);
asteroids.add(newAsteroid);
}
} else {
// grab the current asteroids state
Data data = dp.decompressAsteroids(update);
asteroids = data.asteroids;
}
// thread that continually reads updates from the Server
new Thread() {
public void run() {
DataProcessor dp = new DataProcessor();
String update = "";
while(true) {
try {
sleep(SpaceRangers.DELAY);
} catch (InterruptedException e) {}
// check if the server has any data to read
if (SpaceRangers.player.sin.hasNextLine()) {
update = SpaceRangers.player.readFromServer();
}
if (!update.isEmpty()) {
// process the update and add to the Queue
Data data = dp.decompressUpdate(update);
try {
updates.put(data);
} catch (Exception e) {}
}
}
}
}.start();
// thread that continually writes updates to the Server
new Thread() {
public void run() {
DataProcessor dp = new DataProcessor();
while(true) {
try {
sleep(SpaceRangers.DELAY);
} catch (InterruptedException e) {}
// process and send update data
data = dp.compress(spaceship, ammunition, asteroids);
SpaceRangers.player.writeToServer(data);
}
}
}.start();
// thread that continually updates the Universe
new Thread() {
public void run() {
DataProcessor dp = new DataProcessor();
while(true) {
try {
sleep(SpaceRangers.DELAY); // delay for the whole universe, wow!
} catch (InterruptedException e) {}
repaint();
}
}
}.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Identity transformation matrix for resetting g2d
AffineTransform identity = new AffineTransform();
// poll the updates queue for another player's data
Data player2 = null;
try {
// System.out.println("Polling Queue");
player2 = updates.poll(SpaceRangers.DELAY, TimeUnit.MILLISECONDS);
// player2 = updates.poll();
} catch (Exception e) {}
// reset the graphics
g2d.setTransform(identity);
// set the color to the color of the spaceship
g2d.setColor(spaceship.getColor());
// check if the player is holding down a key for smoother movement
if (SpaceRangers.keyHeld && SpaceRangers.heldKeyCode == KeyEvent.VK_LEFT) {
spaceship.rotateLeft();
} else if (SpaceRangers.keyHeld && SpaceRangers.heldKeyCode == KeyEvent.VK_RIGHT) {
spaceship.rotateRight();
} else if (SpaceRangers.keyHeld && SpaceRangers.heldKeyCode == KeyEvent.VK_UP) {
spaceship.increaseVelocity();
} else if (SpaceRangers.keyHeld && SpaceRangers.heldKeyCode == KeyEvent.VK_DOWN) {
spaceship.decreaseVelocity();
}
// move the spaceship
spaceship.move();
// move the graphics to the ship's relative location
g2d.translate(spaceship.getPositionX(),
spaceship.getPositionY());
// update the ship's heading
g2d.rotate(Math.toRadians(spaceship.getFacing()),
spaceship.getCentroidX(),
spaceship.getCentroidY());
// draw the ship
g2d.drawPolygon(spaceship.getShip());
for (Projectile p : ammunition) {
p.move(); // first move the projectile
if (p.isAlive()) { // if the projectile is still alive/on-screen
g2d.setTransform(identity);
g2d.translate(p.getPositionX(), p.getPositionY());
g2d.drawPolygon(p);
}
}
// draw spaceship2 if present
if (player2 != null) {
g2d.setTransform(identity);
g2d.setColor(player2.spaceship.getColor());
g2d.translate(player2.spaceship.getPositionX(),
player2.spaceship.getPositionY());
g2d.rotate(Math.toRadians(player2.spaceship.getFacing()),
player2.spaceship.getCentroidX(),
player2.spaceship.getCentroidY());
g2d.drawPolygon(player2.spaceship.getShip());
for (Projectile p : player2.projectiles) {
p.move();
if (p.isAlive()) {
g2d.setTransform(identity);
g2d.translate(p.getPositionX(), p.getPositionY());
g2d.drawPolygon(p);
}
}
for (Asteroid a : asteroids) {
if (a.isAlive()) {
// check if any projectile intersects with Asteroid a
for (Projectile p : player2.projectiles) {
if (a.collidesWith(p)) {
a.destroy();
p.destroy();
}
}
}
}
} //else { System.out.println("No player2"); }
g2d.setColor(Color.WHITE);
// move and update asteroids within the universe
for (Asteroid a : asteroids) {
if (a.isAlive()) {
// check if any projectile intersects with Asteroid a
for (Projectile p : ammunition) {
if (a.collidesWith(p)) {
a.destroy();
p.destroy();
}
}
// move and draw the asteroid
a.move();
g2d.setTransform(identity);
g2d.translate(a.getPositionX(),a.getPositionY());
g2d.rotate(Math.toRadians(a.getDirection()),
a.getCentroidX(), a.getCentroidY());
g2d.drawPolygon(a);
}
}
}
}