-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiveSystem.java
More file actions
436 lines (392 loc) · 13.8 KB
/
LiveSystem.java
File metadata and controls
436 lines (392 loc) · 13.8 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import java.util.*;
import java.util.List;
public class LiveSystem {
// Width of the current system. Objects that exceed the system bounds are
// moved to the opposite side of the system. (The system is a torus.)
int thisSystemWidth;
// Height of the current system. Objects that exceed the system bounds are
// moved to the opposite side of the system. (The system is a torus.)
int thisSystemHeight;
int viewWidth, viewHeight, starWidth, starHeight;
// Player's ship.
PlayerShip hero;
// Contains all ships.
List<Ship> ships;
// Contains all projectiles.
List<Projectile> projectiles;
// Contains positions, diameters, and colors of non-interactive background
// stars.
ArrayList<Star> stars;
// Contains positions, diameters, and color of non-interactive foreground
// dust.
ArrayList<Dust> dust;
// Used by the collision checker to prevent ships from shooting themselves.
int shipID;
// A reference to the SpaceObj on which to center the window when playing
// the game.
SpaceObj centerSpaceObj;
protected LiveSystem() {}
public LiveSystem(int level, int viewWidth, int viewHeight) {
shipID = 1;
thisSystemWidth = 10000;
thisSystemHeight = 10000;
this.viewWidth = viewWidth;
this.viewHeight = viewHeight;
starWidth = 2*viewWidth;
starHeight = 2*viewHeight;
hero = new PlayerShip(0, 0, shipID);
ships = new LinkedList<Ship>();
projectiles = new LinkedList<Projectile>();
stars = new ArrayList<Star>();
dust = new ArrayList<Dust>();
Weapon w = hero.weapons.get(0);
w.setReloadTime(w.getReloadTime() - 5*level);
w.setVelocity(w.getVelocity() + level/2);
w.setTTL(w.getTTL() + level/2);
spawnShips(level);
populateStars(stars);
populateDust(dust);
makeCenter(hero);
}
public LiveSystem(LiveSystem obj) {
thisSystemWidth = obj.thisSystemWidth;
thisSystemHeight = obj.thisSystemHeight;
viewWidth = obj.viewWidth;
viewHeight = obj.viewHeight;
starWidth = obj.starWidth;
starHeight = obj.starHeight;
hero = obj.hero;
// Shallow copy of all objects in list.
ships = new LinkedList<Ship>(obj.ships);
projectiles = new LinkedList<Projectile>(obj.projectiles);
stars = new ArrayList<Star>(obj.stars);
dust = new ArrayList<Dust>(obj.dust);
shipID = obj.shipID;
centerSpaceObj = obj.centerSpaceObj;
}
public void updatePhysics() {
fireWeapons();
expireProjectiles();
updatePositions();
checkCollisions();
kill();
ai();
}
public boolean enemiesRemain() {
return ships.size() > 0 ? true : false;
}
public void populateStars(ArrayList<Star> s) {
for (int i = 0; i < 50; i++) {
s.add(new Star(starWidth*2, starHeight*2));
}
}
public void populateDust(ArrayList<Dust> s) {
for (int i = 0; i < 20; i++) {
s.add(new Dust(starWidth*2, starHeight*2));
}
}
private void spawnShips(int level) {
int fighterCount = level*level + 1;
for (int i = 0; i < fighterCount; i++) {
ships.add(new Fighter(
(int)(Math.random() * 1000 * fighterCount)
- 500 * fighterCount,
(int)(Math.random() * 1000 * fighterCount)
- 500 * fighterCount,
getNextShipID(),
level/3.0));
}
}
public int getNextShipID() {
return ++shipID;
}
public void fireWeapons() {
for (Ship s : ships) {
fireShipWeapons(s);
}
fireShipWeapons(hero);
}
public void fireShipWeapons(Ship s) {
for (Weapon w : s.weapons) {
Projectile p = w.generate();
if (!(p instanceof ProjectileBlank)) {
projectiles.add(p);
}
}
}
private void expireProjectiles() {
Iterator<Projectile> it = projectiles.iterator();
while (it.hasNext()) {
Projectile p = it.next();
++p.cyclesLived;
if (p.cyclesLived >= p.ttl)
it.remove();
}
}
private void updatePositions() {
// Modify rotation.
if (hero.status == ShipStatus.ALIVE) {
// East is 0, west is -pi. Pi is outside the range of directions.
if (hero.turningLeft) {
hero.angle += hero.turnRate;
if (hero.angle >= Math.PI)
hero.angle -= 2*Math.PI;
}
if (hero.turningRight) {
hero.angle -= hero.turnRate;
if (hero.angle < -Math.PI)
hero.angle += 2*Math.PI;
}
hero.updateSprite();
}
for (Ship s : ships) {
if (s.status == ShipStatus.ALIVE) {
if (s.turningLeft) {
s.angle += s.turnRate;
if (s.angle >= Math.PI)
s.angle -= 2*Math.PI;
}
if (s.turningRight) {
s.angle -= s.turnRate;
if (s.angle < -Math.PI)
s.angle += 2*Math.PI;
}
}
}
// Modify velocity and position.
for (Ship s : ships) {
if (s.status == ShipStatus.ALIVE)
intervalAccel(s);
s.x += s.dx;
s.y += s.dy;
}
for (Projectile w : projectiles) {
intervalAccel(w);
w.x += w.dx;
w.y += w.dy;
}
if (hero.status == ShipStatus.ALIVE)
intervalAccel(hero);
// Bring speed back down from boosting. (Don't give extra speed for
// free.)
if (!hero.isBoosting) {
if (hero.dx > hero.maxVelocity)
--hero.dx;
else if (hero.dx < -hero.maxVelocity)
++hero.dx;
if (hero.dx > hero.maxVelocity)
--hero.dx;
else if (hero.dx < -hero.maxVelocity)
++hero.dx;
}
hero.x += hero.dx;
hero.y += hero.dy;
// Move everything based on movement of centerSpaceObj.
for (Ship s : ships) {
if (s != centerSpaceObj) {
s.x -= centerSpaceObj.dx;
s.y -= centerSpaceObj.dy;
if (s.x < -thisSystemWidth/2)
s.x += thisSystemWidth;
else if (s.x > thisSystemWidth/2)
s.x -= thisSystemWidth;
if (s.y < -thisSystemHeight/2)
s.y += thisSystemHeight;
else if (s.y > thisSystemHeight/2)
s.y -= thisSystemHeight;
}
}
for (Projectile w : projectiles) {
w.x -= centerSpaceObj.dx;
w.y -= centerSpaceObj.dy;
if (w.x < -thisSystemWidth/2)
w.x += thisSystemWidth;
else if (w.x > thisSystemWidth/2)
w.x -= thisSystemWidth;
if (w.y < -thisSystemHeight/2)
w.y += thisSystemHeight;
else if (w.y > thisSystemHeight/2)
w.y -= thisSystemHeight;
}
if (hero != centerSpaceObj) {
hero.x -= centerSpaceObj.dx;
hero.y -= centerSpaceObj.dy;
if (hero.x < -thisSystemWidth/2)
hero.x += thisSystemWidth;
else if (hero.x > thisSystemWidth/2)
hero.x -= thisSystemWidth;
if (hero.y < -thisSystemHeight/2)
hero.y += thisSystemHeight;
else if (hero.y > thisSystemHeight/2)
hero.y -= thisSystemHeight;
}
centerSpaceObj.x = 0;
centerSpaceObj.y = 0;
for (Star s : stars) {
// Change by movement divided by 3 so that stars move slowly and
// seem far away. (Parallaxing!)
s.x -= centerSpaceObj.dx / 3;
s.y -= centerSpaceObj.dy / 3;
if (s.x < -starWidth / 2)
s.x += starWidth;
else if (s.x > starWidth / 2)
s.x -= starWidth;
if (s.y < -starHeight / 2)
s.y += starHeight;
else if (s.y > starHeight / 2)
s.y -= starHeight;
}
for (Dust s : dust) {
// Have space dust move past at full speed.
s.x -= centerSpaceObj.dx;
s.y -= centerSpaceObj.dy;
if (s.x < -starWidth / 2)
s.x += starWidth;
else if (s.x > starWidth / 2)
s.x -= starWidth;
if (s.y < -starHeight / 2)
s.y += starHeight;
else if (s.y > starHeight / 2)
s.y -= starHeight;
}
}
public void intervalAccel(SpaceObj s) {
if (s.isAccel == 1)
s.accelerate();
if (s.isAccel > 0)
++s.isAccel;
if (s.isAccel > 4)
s.isAccel = 1;
}
public void checkCollisions() {
for (Ship s : ships) {
if (s.status == ShipStatus.ALIVE) {
Iterator<Projectile> it = projectiles.iterator();
while (it.hasNext()) {
Projectile w = it.next();
if (w.friendlyFire || (w.shipID != s.shipID)) {
boolean hit = s.y - s.diam / 2 <= w.y &&
s.y + s.diam / 2 >= w.y &&
s.x + s.diam / 2 >= w.x &&
s.x - s.diam / 2 <= w.x;
if (hit) {
s.structInteg -= w.damage;
if (s.structInteg <= 0) {
s.die();
}
w.applyInertia(s);
it.remove();
}
}
}
}
}
Ship s = hero;
if (hero.status == ShipStatus.ALIVE) {
Iterator<Projectile> it = projectiles.iterator();
while (it.hasNext()) {
Projectile w = it.next();
if (w.friendlyFire || (w.shipID != s.shipID)) {
boolean hit = s.y - s.diam / 2 <= w.y &&
s.y + s.diam / 2 >= w.y &&
s.x + s.diam / 2 >= w.x &&
s.x - s.diam / 2 <= w.x;
if (hit) {
s.structInteg -= w.damage;
if (s.structInteg <= 0) {
s.die();
}
w.applyInertia(s);
it.remove();
}
}
}
}
}
public void kill() {
Iterator<Ship> it = ships.iterator();
while (it.hasNext()) {
Ship s = it.next();
if (s.status == ShipStatus.DYING) {
--s.countDown;
if (s.countDown <= Ship.COUNTDOWN_END) {
s.status = ShipStatus.DEAD;
it.remove();
}
}
}
if (hero.status == ShipStatus.DYING) {
--hero.countDown;
if (hero.countDown <= Ship.COUNTDOWN_END) {
hero.status = ShipStatus.DEAD;
}
}
}
public void ai() {
for (Ship s : ships) { if (s.status == ShipStatus.ALIVE) {
// Follow the player's ship.
double dx = hero.x - s.x,
dy = hero.y - s.y;
double theta = Math.atan(dy/dx);
// East is 0, west is -pi. Pi is outside the range of directions.
if (dx < 0 && theta < 0) {
theta += Math.PI;
} else if (dx < 0 && theta >= 0) {
theta -= Math.PI;
}
if (s.angle < -Math.PI / 2 && theta > Math.PI / 2) {
s.turningRight = true;
s.turningLeft = false;
} else if (s.angle > Math.PI / 2 && theta < -Math.PI / 2) {
s.turningRight = false;
s.turningLeft = true;
} else if (theta - s.angle > 0.1) {
s.turningRight = false;
s.turningLeft = true;
} else if (theta - s.angle < -0.1) {
s.turningRight = true;
s.turningLeft = false;
} else {
s.turningRight = false;
s.turningLeft = false;
}
// Only shoot if the player is nearby.
double range = 0, nextRange;
for (Weapon w : s.weapons) {
nextRange = w.range();
if (nextRange > range)
range = nextRange;
}
double distance = Math.sqrt(dx*dx + dy*dy);
if (distance < range * 2)
s.firing = true;
else
s.firing = false;
}}
}
public void makeCenter(SpaceObj newCenter) {
centerSpaceObj = newCenter;
double x = newCenter.x, y = newCenter.y;
for (Ship s : ships) {
translateWithCenter(s, x, y);
}
for (Projectile w : projectiles) {
translateWithCenter(w, x, y);
}
translateWithCenter(hero, x, y);
// Should also translate dust and stars for a better effect. (Make it
// really feel like we are looking at a different part of space.)
}
public void translateWithCenter(SpaceObj s, double x, double y) {
s.x -= x;
s.y -= y;
if (s.x < -thisSystemWidth/2)
s.x += thisSystemWidth;
else if (s.x > thisSystemWidth/2)
s.x -= thisSystemWidth;
if (s.y < -thisSystemHeight/2)
s.y += thisSystemHeight;
else if (s.y > thisSystemHeight/2)
s.y -= thisSystemHeight;
}
}