-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsPanel.java
700 lines (630 loc) · 28.2 KB
/
GraphicsPanel.java
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.*;
public class GraphicsPanel extends JPanel {
// players
private Player player1 = new Player(new Point(80, 280), new Point(-1, 0), Color.blue);
private Player player2 = new Player(new Point(1080, 280), new Point(1, 0), Color.red);
private Rectangle playerBound = new Rectangle(88, 88, 1024, 424);
private Rectangle bulletBound = new Rectangle(60, 60, 1080, 480);
// walls
private boolean[][] walls;
private ArrayList<Rectangle> wallHitBoxes;
private Map map = new Map();
// frames
private Clock clock = new Clock();
private FrameRate fr = new FrameRate();
private MovementClock mc = new MovementClock();
private JFrame gameFrame;
// initialization
public GraphicsPanel(JFrame gameFrame) {
MyKeyListener keyListener = new MyKeyListener();
addKeyListener(keyListener);
setFocusable(true);
Bullet.init();
Pickup.init();
this.gameFrame = gameFrame;
walls = map.getMap();
wallHitBoxes = map.getWallHitBoxes();
Thread t = new Thread(new Runnable() {
public void run() {
animate();
}
});
t.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// painting playing field
paintBackground(g);
paintPickups(g);
paintBullets(g);
paintWallObstacles(g);
// painting players
paintPlayer(g, player1);
paintPlayer(g, player2);
// painting HUD
paintHealthBars(g);
paintAmmoLeft(g);
if (player1.getIsReloading()) {
paintReloadTime(g, player1);
}
if (player2.getIsReloading()) {
paintReloadTime(g, player2);
}
paintLives(g);
drawFrameRate(g);
}
private void paintBackground(Graphics g) {
super.paintComponent(g);
////////////////// Background Graphics //////////////////
// playing field graphics
g.setColor(Color.BLACK);
g.fillRect(0, 0, 1200, 600);
g.setColor(Color.WHITE);
g.fillRect(50, 50, 1100, 500);
// colours
Color paleBlue = new Color(172, 162, 255);
Color paleRed = new Color(255, 162, 162);
Color paleGreen = new Color(165, 255, 162);
Color paleYellow = new Color(255, 255, 162);
Color paleCyan = new Color(164, 255, 255);
// lower display pale background graphics
g.setColor(paleBlue);
g.fillRect(0, 600, 550, 200);
g.setColor(paleRed);
g.fillRect(650, 600, 550, 200);
g.setColor(paleGreen);
g.fillRect(0, 625, 1200, 28);
g.setColor(paleYellow);
g.fillRect(0, 675, 1200, 28);
g.setColor(paleCyan);
g.fillRect(0, 725, 1200, 28);
// text box colours
g.setColor(Color.black);
g.fillRect(550, 600, 100, 200);
g.setColor(Color.green);
g.fillRect(550, 625, 100, 28);
g.setColor(Color.yellow);
g.fillRect(550, 675, 100, 28);
g.setColor(Color.cyan);
g.fillRect(550, 725, 100, 28);
// border
g.setColor(Color.lightGray);
for (int i = 0; i < 5; i++) {
g.draw3DRect(550 - i, 604 - i, 100 + 2 * i - 1, 163 + 2 * i - 1, true);
}
// drawing strings
g.setColor(Color.black);
Font standardFont = new Font("SansSerif", Font.BOLD, 16);
g.setFont(standardFont);
int healthWidth = g.getFontMetrics().stringWidth("Health");
g.drawString("Health", Math.round(50 - healthWidth / 2) + 550, 645);
int ammoWidth = g.getFontMetrics().stringWidth("Ammo");
g.drawString("Ammo", Math.round(50 - ammoWidth / 2) + 550, 695);
int livesWidth = g.getFontMetrics().stringWidth("Lives");
g.drawString("Lives", Math.round(50 - livesWidth / 2) + 550, 745);
}
private void paintPickups(Graphics g) {
////////////////// Paint and generate pickups //////////////////
Pickup pickup;
for (int i = 0; i < Pickup.getInstances().size(); i++) {
pickup = Pickup.getInstances().get(i);
if (pickup.getDurationLeft() < 7000) {
g.setColor(pickup.getColor());
} else {
g.setColor(pickup.getFadedColor());
}
g.fillRect((int) pickup.getPos().x, (int) pickup.getPos().y, 20, 20);
}
}
private void paintWallObstacles(Graphics g) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 22; j++) {
if (walls[i][j]) {
g.setColor(Color.black);
g.fillRect(50 + (j * 50), 50 + (i * 50), 50, 50);
}
}
}
}
/*
paintPlayer
paints the player and their cannon
*/
private void paintPlayer(Graphics g, Player player) {
//base
if (player.getIsRespawning()) {
if (System.currentTimeMillis() - player.getTimeRespawnStart() < 2000) {
g.setColor(new Color(255, 255, 0, 175));
} else {
g.setColor(new Color(255, 255, 0, 100));
}
g.fillOval((int) player.getPos().x - 15, (int) player.getPos().y - 15, 70, 70);
} else if (player.getSpeedTime() != 0 || player.getDamageTime() != 0) {
if (player.getSpeedTime() != 0) {
if (System.currentTimeMillis() - player.getSpeedTime() < 27000) {
g.setColor(new Color(0, 255, 255, 175));
} else {
g.setColor(new Color(0, 255, 255, 100));
}
g.fillOval((int) player.getPos().x - 15, (int) player.getPos().y - 15, 70, 70);
}
if (player.getDamageTime() != 0) {
if (System.currentTimeMillis() - player.getDamageTime() < 27000) {
g.setColor(new Color(255, 0, 255, 175));
} else {
g.setColor(new Color(255, 0, 255, 100));
}
g.fillOval((int) player.getPos().x - 15, (int) player.getPos().y - 15, 70, 70);
}
}
g.setColor(player.getColor());
g.fillRect((int) player.getPos().x, (int) player.getPos().y, player.getSize(), player.getSize());
// cannon
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(4));
g2.drawLine((int) player.getPos().x + player.getSize() / 2, (int) player.getPos().y + player.getSize() / 2, // draw a line from center of player to player's gun direction
(int) player.getPos().x + player.getSize() / 2 + (int) player.getGunDir().x * player.getSize() / 2,
(int) player.getPos().y + player.getSize() / 2 + (int) player.getGunDir().y * player.getSize() / 2);
}
private void paintBullets(Graphics g) {
Bullet bullet; // to access instances arraylist
for (int i = 0; i < Bullet.getInstances().size(); ++i) { // cycle through every bullet shot
bullet = Bullet.getInstances().get(i);
g.setColor(bullet.getColor());
g.fillRect((int) bullet.getPos().x - bullet.getSize() / 2, (int) bullet.getPos().y - bullet.getSize() / 2, bullet.getSize(), bullet.getSize());
}
}
private void paintHealthBars(Graphics g) {
g.setColor(Color.green);
for (int i = 0; i < 5; i++) {
g.draw3DRect(25 - i, 631 - i, 500 + 2 * i - 1, 15 + 2 * i - 1, true);
}
g.fillRect(25, 631, player1.getHealth() * 5 * 100 / player1.getMaxHealth(), 15);
for (int i = 0; i < 5; i++) {
g.draw3DRect(675 - i, 631 - i, 500 + 2 * i - 1, 15 + 2 * i - 1, true);
}
g.fillRect(1175 - player2.getHealth() * 5 * 100 / player2.getMaxHealth(), 631,
player2.getHealth() * 5 * 100 / player2.getMaxHealth(), 15);
}
private void paintAmmoLeft(Graphics g) {
g.setColor(Color.orange);
for (int i = 0; i < player1.getAmmo(); i++) {
g.fillRect(30 + i * 15, 679, 10, 18);
}
for (int i = 0; i < 5; i++) {
g.draw3DRect(25 - i, 681 - i, 125 + 2 * i - 1, 15 + 2 * i - 1, true);
}
for (int i = player2.getAmmo(); i > 0; i--) {
g.fillRect(1175 - i * 15, 679, 10, 18);
}
for (int i = 0; i < 5; i++) {
g.draw3DRect(1050 - i, 681 - i, 125 + 2 * i - 1, 15 + 2 * i - 1, true);
}
}
private void paintReloadTime(Graphics g, Player player) {
g.setColor(Color.orange);
int currentReloadTime = (int) System.currentTimeMillis() - (int) player.getTimeReloadStart();
if (player == player1 && currentReloadTime <= 1500) {
for (int i = 0; i < 5; i++) {
g.draw3DRect(375 - i, 683 - i, 150 + 2 * i - 1, 10 + 2 * i - 1, true);
}
g.fillRect(375, 683, currentReloadTime / 10, 10);
} else if (currentReloadTime <= 1500) {
for (int i = 0; i < 5; i++) {
g.draw3DRect(675 - i, 683 - i, 150 + 2 * i - 1, 10 + 2 * i - 1, true);
}
g.fillRect(825 - currentReloadTime / 10, 683, currentReloadTime / 10, 10);
}
}
private void paintLives(Graphics g) {
g.setColor(Color.red);
for (int i = 0; i < player1.getLives(); i++) {
drawHeart(g, 25 + i * 50, 729, 20, 20);
}
for (int i = 0; i < player2.getLives(); i++) {
drawHeart(g, 1155 - i * 50, 729, 20, 20);
}
}
private void drawHeart(Graphics g, int x, int y, int width, int height) {
int[] triangleX = {x - 2 * width / 18, x + width + 2 * width / 18,
(x - 2 * width / 18 + x + width + 2 * width / 18) / 2};
int[] triangleY = {y + height - 2 * height / 3, y + height - 2 * height / 3, y + height};
g.fillOval(x - width / 12, y, width / 2 + width / 6, height / 2);
g.fillOval(x + width / 2 - width / 12, y, width / 2 + width / 6, height / 2);
g.fillPolygon(triangleX, triangleY, triangleX.length);
}
/*
Draws frame rate to screen
*/
private void drawFrameRate(Graphics g) {
fr.update();
g.setColor(Color.white);
fr.draw(g, 20, 20);
}
/*
Main game loop
*/
public void animate() {
while (true) {
// clock for movement
mc.update();
// player movement when not reloading, player and collisions with
// walls and obstacles (considered "out of bounds")
if (!player1.getIsReloading()) {
//System.out.println("Player 1 is not reloading");
double player1ElapsedTime = mc.getElapsedTime();
player1.move(player1ElapsedTime); // pos changes, lastPos does not change
keyPlayerInBounds(player1, player1ElapsedTime); // check if
// player is out
// of bounds,
// then change pos or lastPos
}
checkPickupCollisions(player1);
checkBuffDuration(player1);
if (!player2.getIsReloading()) {
// System.out.println("Player 2 is not reloading");
double player2ElapsedTime = mc.getElapsedTime();
player2.move(player2ElapsedTime);
keyPlayerInBounds(player2, player2ElapsedTime);
}
checkPickupCollisions(player2);
checkBuffDuration(player2);
Bullet bullet;
bulletCollision:
for (int i = 0; i < Bullet.getInstances().size(); ++i) {
bullet = Bullet.getInstances().get(i);
bullet.move(mc.getElapsedTime());
// Bullets and collisions
if (bullet.getHitBox().intersects(player1.getHitBox()) && bullet.getPlayerShot() == 2) {
if (!player1.getIsRespawning()) {
player1.reduceHealth(bullet.getDamage());
}
Bullet.getInstances().remove(i);
i--;
continue;
} else if (bullet.getHitBox().intersects(player2.getHitBox()) && bullet.getPlayerShot() == 1
&& !player2.getIsRespawning()) {
if (!player2.getIsRespawning()) {
player2.reduceHealth(bullet.getDamage());
}
Bullet.getInstances().remove(i);
i--;
continue;
}
if (!bullet.getHitBox().intersects(bulletBound)) {
Bullet.getInstances().remove(i);
i--;
continue;
}
for (int j = 0; j < wallHitBoxes.size(); j++) {
if (bullet.getHitBox().intersects(wallHitBoxes.get(j))) {
Bullet.getInstances().remove(i);
i--;
continue bulletCollision;
}
}
}
spawnDespawnPickups();
if (player1.getHealth() <= 0 || player1.getIsRespawning()) {
if (!player1.getIsRespawning()) {
respawn(player1);
} else if (System.currentTimeMillis() - player1.getTimeRespawnStart() > 3000) {
player1.setIsRespawning(false);
}
}
if (player2.getHealth() <= 0 || player2.getIsRespawning()) {
if (!player2.getIsRespawning()) {
respawn(player2);
} else if (System.currentTimeMillis() - player2.getTimeRespawnStart() > 3000) {
player2.setIsRespawning(false);
}
}
if (player1.getLives() == 0) {
// print player 1 wins on the game over screen
gameFrame.dispose();
Sound.playClip(new File("playerDeath.wav"));
new GameOver(2);
break;
} else if (player2.getLives() == 0) {
// print player 2 wins on the game over screen
gameFrame.dispose();
Sound.playClip(new File("playerDeath.wav"));
new GameOver(1);
break;
}
this.repaint();
}
}
// for collisions
/*
Checks if player is colliding with an obstacle.
If the player is moving diagonally into the obstacle, it will move the player in the direction that it is able to move in.
*/
private void keyPlayerInBounds(Player player, double elapsedTime) {
boolean intersectingObstacle = false;
int indexCount = 0;
while (!intersectingObstacle && indexCount < wallHitBoxes.size()) {
if (player.getHitBox().intersects(wallHitBoxes.get(indexCount))) {
intersectingObstacle = true;
}
indexCount++;
}
if (player == player1) {
if (!player.getHitBox().intersects(playerBound) || intersectingObstacle || player.getHitBox().intersects(player2.getHitBox())) { //player is out of bounds
Point dir = player.getDir();
double x = dir.x;
double y = dir.y;
boolean inObstacle = false;
if (x != 0 && y != 0) {
double dirOrigX = x;
player.setDirX(0);
player.setPos(player.getLastPos());
player.move(elapsedTime);
indexCount = 0;
while (!inObstacle && indexCount < wallHitBoxes.size()) {
if (player.getHitBox().intersects(wallHitBoxes.get(indexCount))) {
inObstacle = true;
}
indexCount++;
}
if (!player.getHitBox().intersects(playerBound) || inObstacle || player.getHitBox().intersects(player2.getHitBox())) {
player.setDirX(dirOrigX);
player.setDirY(0);
player.setPos(player.getLastPos());
player.move(elapsedTime);
inObstacle = false;
indexCount = 0;
while (!inObstacle && indexCount < wallHitBoxes.size()) {
if (player.getHitBox().intersects(wallHitBoxes.get(indexCount))) {
inObstacle = true;
}
indexCount++;
}
if (!player.getHitBox().intersects(playerBound) || inObstacle || player.getHitBox().intersects(player2.getHitBox())) {
player.setPos(player.getLastPos());
player.setHitBox(new Rectangle((int) player.getPos().x, (int) player.getPos().y, player.getSize(), player.getSize()));
} else {
player.setPos(player.getLastPos());
player.move(elapsedTime);
}
} else {
player.setPos(player.getLastPos());
player.move(elapsedTime);
}
} else {
player.setPos(player.getLastPos());
player.setHitBox(new Rectangle((int) player.getPos().x, (int) player.getPos().y, player.getSize(), player.getSize()));
}
} else { //player is in bounds
player.setLastPos(player.getPos());
}
} else {
if (!player.getHitBox().intersects(playerBound) || intersectingObstacle || player.getHitBox().intersects(player1.getHitBox())) { //player is out of bounds
Point dir = player.getDir();
double x = dir.x;
double y = dir.y;
boolean inObstacle = false;
if (x != 0 && y != 0) {
double dirOrigX = x;
player.setDirX(0);
player.setPos(player.getLastPos());
player.move(elapsedTime);
indexCount = 0;
while (!inObstacle && indexCount < wallHitBoxes.size()) {
if (player.getHitBox().intersects(wallHitBoxes.get(indexCount))) {
inObstacle = true;
}
indexCount++;
}
if (!player.getHitBox().intersects(playerBound) || inObstacle || player.getHitBox().intersects(player1.getHitBox())) {
player.setDirX(dirOrigX);
player.setDirY(0);
player.setPos(player.getLastPos());
player.move(elapsedTime);
inObstacle = false;
indexCount = 0;
while (!inObstacle && indexCount < wallHitBoxes.size()) {
if (player.getHitBox().intersects(wallHitBoxes.get(indexCount))) {
inObstacle = true;
}
indexCount++;
}
if (!player.getHitBox().intersects(playerBound) || inObstacle || player.getHitBox().intersects(player1.getHitBox())) {
player.setPos(player.getLastPos());
player.setHitBox(new Rectangle((int) player.getPos().x, (int) player.getPos().y, player.getSize(), player.getSize()));
} else {
player.setPos(player.getLastPos());
player.move(elapsedTime);
}
} else {
player.setPos(player.getLastPos());
player.move(elapsedTime);
}
} else {
player.setPos(player.getLastPos());
player.setHitBox(new Rectangle((int) player.getPos().x, (int) player.getPos().y, player.getSize(), player.getSize()));
}
} else { //player is in bounds
player.setLastPos(player.getPos());
}
}
}
public void checkPickupCollisions(Player player) {
Pickup pickup;
for (int i = 0; i < Pickup.getInstances().size(); i++) {
pickup = Pickup.getInstances().get(i);
if (player.getHitBox().intersects(pickup.getHitBox())) {
if (pickup instanceof Speed) {
// boost player speed
player.startSpeedPickup();
} else if (pickup instanceof Health) {
// boost player health
player.useHealthPickup();
Sound.playClip(new File("healthPickup.wav"));
} else {
// boost player damage
player.startDamagePickup();
}
Pickup.getInstances().remove(i);
}
}
}
public void spawnDespawnPickups() {
if (clock.update()) {
map.generatePickups();
}
Pickup pickup;
for (int i = 0; i < Pickup.getInstances().size(); i++) {
pickup = Pickup.getInstances().get(i);
if (pickup.getDurationLeft() > 10000) {
Pickup.getInstances().remove(i);
}
}
}
public void checkBuffDuration(Player player) {
if (player.getSpeedTime() != 0 && System.currentTimeMillis() - player.getSpeedTime() > 30000) {
player.endSpeedPickup();
}
if (player.getDamageTime() != 0 && System.currentTimeMillis() - player.getDamageTime() > 30000) {
player.endDamagePickup();
}
}
// for respawning
private void respawn(Player player) {
if (player == player1) {
player.setPos(new Point(80, 280));
player.setGunDir(new Point(-1, 0));
} else {
player.setPos(new Point(1080, 280));
player.setGunDir(new Point(1, 0));
}
player.setHealth(player.getMaxHealth());
player.refillAmmo();
player.loseLife();
Pickup.clearPickups();
player1.clearBuffs();
player2.clearBuffs();
player.setIsRespawning(true);
player.setTimeRespawnStart(System.currentTimeMillis());
}
/**
* LISTENER SUBCLASS
**/
// listeners
private class MyKeyListener implements KeyListener {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
movePlayer(e, player1, KeyEvent.VK_W, KeyEvent.VK_A, KeyEvent.VK_S, KeyEvent.VK_D);
movePlayer(e, player2, KeyEvent.VK_I, KeyEvent.VK_J, KeyEvent.VK_K, KeyEvent.VK_L);
try {
shoot(e, player1, KeyEvent.VK_C);
shoot(e, player2, KeyEvent.VK_N);
} catch (Exception ex) {
}
reload(e, player1, KeyEvent.VK_X);
reload(e, player2, KeyEvent.VK_M);
}
public void keyReleased(KeyEvent e) {
// stops player movement if key is released
stopPlayer(e, player1, KeyEvent.VK_W, KeyEvent.VK_A, KeyEvent.VK_S, KeyEvent.VK_D);
stopPlayer(e, player2, KeyEvent.VK_I, KeyEvent.VK_J, KeyEvent.VK_K, KeyEvent.VK_L);
// refills ammo if reload was held long enough
finishReload(e, player1, KeyEvent.VK_X);
finishReload(e, player2, KeyEvent.VK_M);
}
/*
movePlayer
updates player direction based on keyboard input
*/
private void movePlayer(KeyEvent e, Player player, int up, int left, int down, int right) {
// direction is checked every frame to move player
if (e.getKeyCode() == up)
player.setDirY(-1);
if (e.getKeyCode() == left)
player.setDirX(-1);
if (e.getKeyCode() == down)
player.setDirY(1);
if (e.getKeyCode() == right)
player.setDirX(1);
}
/*
movePlayer
updates player direction based on keyboard input
*/
private void stopPlayer(KeyEvent e, Player player, int up, int left, int down, int right) {
if (e.getKeyCode() == up)
player.setDirY(0);
if (e.getKeyCode() == left)
player.setDirX(0);
if (e.getKeyCode() == down)
player.setDirY(0);
if (e.getKeyCode() == right)
player.setDirX(0);
}
/*
shoot
initializes bullets based on keyboard input, player position and player gun direction
plays a sound each time a bullet is fired
shooting is restricted by fire rate and ammo
*/
private void shoot(KeyEvent e, Player player, int shoot) {
if (e.getKeyCode() == shoot && player.getAmmo() > 0 && !player.getIsReloading()) { // if player has ammo and isn't reloading
if (player == player1 && System.currentTimeMillis() - player.getLastShotTime() >= 500) {
// spawn a bullet at player's position with the direction of the player's gun direction
Bullet bullet = new Bullet(new Point(player.getPos().x + player.getSize() / 2, player.getPos().y + player.getSize() / 2),
player.getGunDir().clone(), player.getDamage(), 1, Color.blue);
player.setLastShotTime();
player.reduceAmmo();
Sound.playClip(new File("playerShooting.wav"));
} else if (player == player2 && System.currentTimeMillis() - player.getLastShotTime() >= 500) {
Bullet bullet = new Bullet(new Point(player.getPos().x + player.getSize() / 2, player.getPos().y + player.getSize() / 2),
player.getGunDir().clone(), player.getDamage(), 2, Color.red);
player.setLastShotTime();
player.reduceAmmo();
Sound.playClip(new File("playerShooting.wav"));
}
}
}
/*
reload
starts counting how much time has passed since the reload key is pressed
*/
private void reload(KeyEvent e, Player player, int reload) {
if (e.getKeyCode() == reload && !player.getIsReloading()) {
// reload works by calculating the time between when the reload starts and when it ends; if the time is long enough, ammo is refilled
player.setIsReloading(true);
player.setTimeReloadStart(System.currentTimeMillis());
System.out.println(player + " starts a reload.");
}
}
/*
finishReload
checks how much time has passed since the reload key was pressed and refills player ammo if time is long enough
*/
private void finishReload(KeyEvent e, Player player, int reload) {
if (e.getKeyCode() == reload) {
player.setIsReloading(false);
System.out.println(
player + " time reload held: " + (System.currentTimeMillis() - player.getTimeReloadStart()));
System.out.println(player + " reload time: " + player.getReloadTime());
if (System.currentTimeMillis() - player.getTimeReloadStart() >= player.getReloadTime()) { // checks reload time
player.refillAmmo();
Sound.playClip(new File("playerReload.wav"));
System.out.println(player + " reload succeeds. Ammo: " + player.getAmmo());
}
}
}
}
}