-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
527 lines (445 loc) · 10.4 KB
/
Controller.java
File metadata and controls
527 lines (445 loc) · 10.4 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import java.awt.event.KeyEvent;
import java.util.ArrayList;
/**
* This Class is the main driver class, it calls and runs the other classes
* @author elan
*
*/
public class Controller
{
/**
* these are all public and static as they are changed by many other classes and functions
*/
public static ArrayList<Bullet> bullets;
public static ArrayList<EnemyBullet> enemyBullets;
public static ArrayList<Enemy> enemies;
public static ArrayList<PowerUpBox> powerUpBoxes;
public static ArrayList<Bunker> bunkers;
public static Screen screen;
public static Shooter player;
public static SoundManager soundManager;
public static PowerUp powerUp;
/**
* Main Method
* @param args this is not used
*/
public static void main(String[] args)
{
initializeScreens();
Score.loadHighScores();
soundManager = new SoundManager();
while(true)
{
if(States.isWelcomeRunning)
{
welcomeScreen();
}
else if(States.isPlayRunning)
{
initializeGame();
playScreen();
}
else if(States.isUpgradeRunning)
{
upgradeScreen();
}
else if(States.isOptionRunning)
{
optionScreen();
}
else if(States.isInstructionsRunning)
{
instructionScreen();
}
else if(States.isHighScoreRunning)
{
highScoreScreen();
}
}
}
/**
* Sets the Size and scale of the screen
*/
private static void initializeScreens()
{
StdDraw.setCanvasSize((int)States.WINDOW_RESOLUTION, (int)States.WINDOW_RESOLUTION);
StdDraw.setXscale(0, States.WINDOW_RESOLUTION);
StdDraw.setYscale(0, States.WINDOW_RESOLUTION);
}
/**
* Initializes all the level variables and data structures
*/
private static void initializeGame()
{
bullets = new ArrayList<Bullet>();
enemyBullets = new ArrayList<EnemyBullet>();
enemies = new ArrayList<Enemy>();
powerUpBoxes = new ArrayList<PowerUpBox>();
bunkers = new ArrayList<Bunker>();
AI.initializeEnemies();
Bunker.initializeBunkers();
player = new Shooter();
GameTimer.enemyFireTimer.start();
GameTimer.powerUpSpawnTimer.start();
}
/**
* What should happen while the welcome screen is open
*/
private static void welcomeScreen()
{
screen = new WelcomeScreen();
GameTimer.initializeTimers();
while(States.isWelcomeRunning)
{
render();
update();
if(StdDraw.hasNextKeyTyped())
{
char keyTyped = StdDraw.nextKeyTyped();
if(keyTyped == KeyEvent.VK_ENTER)
{
States.isWelcomeRunning = false;
States.isPlayRunning = true;
}
else if(keyTyped == KeyEvent.VK_ESCAPE)
{
StdAudio.close();
System.exit(0);
}
}
}
clearKeyboard();
}
/**
* What should happen while the play screen is open
*/
private static void playScreen()
{
screen = new PlayScreen();
while(States.isPlayRunning)
{
if(StdDraw.hasNextKeyTyped())
{
if(!States.pause)
KeyHandler.processKey();
}
update();
render();
}
GameTimer.enemyFireTimer.stop();
GameTimer.powerUpSpawnTimer.stop();
clearKeyboard();
}
/**
* What should happen while the option screen is open
*/
private static void optionScreen()
{
screen = new OptionScreen();
while (States.isOptionRunning)
{
if(StdDraw.hasNextKeyTyped())
{
char keyTyped = StdDraw.nextKeyTyped();
if(keyTyped == KeyEvent.VK_ESCAPE)
{
States.isOptionRunning = false;
States.isWelcomeRunning = true;
}
}
update();
render();
}
clearKeyboard();
}
/**
* What should happen while the high score screen is open
*/
private static void highScoreScreen()
{
screen = new HighScoreScreen();
while(States.isHighScoreRunning)
{
if(StdDraw.hasNextKeyTyped())
{
char keyTyped = StdDraw.nextKeyTyped();
if(keyTyped == KeyEvent.VK_ESCAPE)
{
States.isHighScoreRunning = false;
States.isWelcomeRunning = true;
}
}
update();
render();
}
clearKeyboard();
}
/**
* What should happen while the upgrade screen is open
*/
private static void upgradeScreen()
{
screen = new UpgradeScreen();
while (States.isUpgradeRunning)
{
update();
render();
if(StdDraw.hasNextKeyTyped())
{
char keyTyped = StdDraw.nextKeyTyped();
if(keyTyped == KeyEvent.VK_ESCAPE)
{
States.isUpgradeRunning = false;
States.isPlayRunning = true;
}
}
}
clearKeyboard();
}
/**
* What should happen while the instructions screen is open
*/
private static void instructionScreen()
{
screen = new InstructionsScreen();
while (States.isInstructionsRunning)
{
if(StdDraw.hasNextKeyTyped())
{
char keyTyped = StdDraw.nextKeyTyped();
if(keyTyped == KeyEvent.VK_ESCAPE)
{
States.isInstructionsRunning = false;
States.isWelcomeRunning = true;
}
}
update();
render();
}
clearKeyboard();
}
/**
* Clears the keyboard so the inputs from one screen are not carried over to another
*/
private static void clearKeyboard()
{
while(StdDraw.hasNextKeyTyped())
StdDraw.nextKeyTyped();
}
/**
* pauses the screen for a few seconds
*/
public static void pause()
{
States.pause = true;
GameTimer.pauseTimer.start();
GameTimer.enemyFireTimer.stop();
}
/**
* calls all the relevant render methods for all other objects and classes
*/
private static void render()
{
if(!States.pause)
{
screen.render();
if(States.isPlayRunning)
{
for(int i = 0; i < bullets.size(); i++)
bullets.get(i).render();
for(int i = 0; i < enemyBullets.size(); i++)
enemyBullets.get(i).render();
for(int i = 0; i < enemies.size(); i++)
enemies.get(i).render();
for(int i = 0; i < powerUpBoxes.size(); i++)
powerUpBoxes.get(i).render();
for(int i = 0; i < bunkers.size(); i++)
bunkers.get(i).render();
if(powerUp != null)
powerUp.render();
Score.render();
Level.render();
player.render();
}
else if(States.isUpgradeRunning)
{
Store.render();
}
}
StdDraw.show(1000/(States.FPS*2));
}
/**
* calls all the relevant update methods for all other objects and classes
*/
private static void update()
{
if(!States.pause)
{
if(States.isPlayRunning)
{
checkWinLevel();
Index.updateBulletIndex();
Index.updateEnemyBulletIndex();
Index.updateEnemyIndex();
Index.updatePowerUpBoxesIndex();
Index.updateBunkerIndex();
player.update();
Index.updateEnemyBulletIndex();
for(int i = bullets.size() - 1; i >= 0; i--)
bullets.get(i).update();
Index.updateBulletIndex();
Index.updateEnemyIndex();
for(int i = enemyBullets.size() - 1; i >= 0 ; i--)
enemyBullets.get(i).update();
Index.updateEnemyBulletIndex();
Index.updateBunkerIndex();
for(int i = enemies.size() - 1; i >= 0 ; i--)
enemies.get(i).update();
Index.updateEnemyIndex();
for(int i = powerUpBoxes.size() - 1; i >= 0; i--)
powerUpBoxes.get(i).update();
Index.updatePowerUpBoxesIndex();
for(int i = bunkers.size() - 1; i >= 0; i--)
bunkers.get(i).update();
Index.updateBunkerIndex();
AI.enemyAI();
}
}
screen.checkMouseActivity();
checkGameOver();
}
/**
* What happens if you leave the play screen
*/
public static void exitConfirmation()
{
//will check if they really want to exit here
clearKeyboard();
States.isPlayRunning = false;
States.isWelcomeRunning = true;
resetLevel();
}
/**
* resets all the variables relevant to the level
*/
private static void resetLevel()
{
Enemy.resetHorizontalDisplacement();
Enemy.resetVerticalDisplacement();
if(!Enemy.isMovingRight())
Enemy.invertHorizontalMotion();
if(Enemy.isMovingDown())
Enemy.startOrStopVerticalMotion();
if(Level.getLevel() % 5 == 0)
{
Boss.resetRotationalDisplacement();
if(!Boss.isRotatingClock())
{
Boss.invertRotation();
}
}
PowerUp.clearPowerUps();
if(!States.gameOver)
{
initializeGame();
player = new Shooter();
}
}
/**
* resets all variables relevant to the beginning of the game
*/
public static void resetGame()
{
resetStateValues();
resetLevel();
Level.resetLevel();
Score.resetScore();
Store.resetStore();
initializeGame();
}
/**
* resets all variables relevant to the beginning of the game
*/
private static void resetStateValues()
{
States.fireRate = 500;
States.playerDamage = 10;
States.enemyDamage = 4;
States.enemyShootDelay = 1500;
States.enemyHealth = 10;
States.playerLives = new int[]{3, 3};
States.powerUpDuration = 5000;
States.numberBulletsPlayerShoots = 1;
States.playerHP = 10;
States.bulletPhaseChance = 1;
States.bunkerHP = 50;
States.enemySpeed = States.SHOOTER_MOVE_SPEED * 0.5;
States.enemyBulletSpeed = States.enemySpeed*1.5;
States.enemySize = States.SHOOTER_SIZE * 0.9;
States.bunkerWidth = States.WINDOW_CENTRE/(double)3;
}
/**
* checks if the level has been won by seeing of there are still enemies or not
*/
private static void checkWinLevel()
{
if(enemies != null && enemies.size() == 0)
winLevel();
}
/**
* what should happen when you beat a level
*/
private static void winLevel()
{
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.text(States.WINDOW_CENTRE, States.WINDOW_CENTRE + States.WINDOW_CENTRE/(double) 1.5, "LEVEL " + Level.getLevel() + " COMPLETED");
pause();
States.isUpgradeRunning = true;
States.isPlayRunning = false;
Store.addCredits(Score.getScore());
Score.consolidateScore();
Level.levelUp();
resetLevel();
}
/**
* set a boolean showing that you have lost the game
*/
public static void gameOver()
{
States.gameOver = true;
}
/**
* check if you have lost the game and if so resets it
*/
public static void checkGameOver()
{
if(States.gameOver)
{
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.text(States.WINDOW_CENTRE, States.WINDOW_CENTRE + States.WINDOW_CENTRE/(double) 1.5, "THE ALIENS HAVE DEFEATED YOU!");
StdDraw.text(States.WINDOW_CENTRE, States.WINDOW_CENTRE + States.WINDOW_CENTRE/(double) 1.9, "STARTING AT LEVEL 1 AGAIN!");
pause();
resetGame();
States.gameOver = false;
}
}
/**
* resets the level
*/
public static void loseLevel()
{
soundManager.playExplode();
if(States.playerLives[0] <= 0)
States.gameOver = true;
else
{
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.text(States.WINDOW_CENTRE, States.WINDOW_CENTRE + States.WINDOW_CENTRE/(double) 1.5, "YOU LOST THIS LEVEL! GET READY!");
pause();
}
Score.resetInterumScore();
resetLevel();
GameTimer.enemyFireTimer.stop();
States.isPlayRunning = true;
}
}