-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamePanel.java
1475 lines (1412 loc) · 48.2 KB
/
GamePanel.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel implements KeyListener{
private BoxHead BH;
private boolean[] keys;
//Audio
private AudioClip barrelExplodeAudio = Applet.newAudioClip(getClass().getResource("barrelExplosionAudio.wav"));
private AudioClip[] audio = new AudioClip[8];
private AudioClip explodeGrenade =Applet.newAudioClip(getClass().getResource("grenadeExplode.wav"));
//sprites
private Image[][] charSprites = new Image[8][3];
private Image[][] zombieSprites = new Image[8][8];
private Image[][] devilSprites = new Image[8][8];
private Image boxSprite = new ImageIcon("sprites/box.png").getImage();
private Image[] fireballSprites = new Image[8];
private Image[] bulletSprites = new Image[8];
private Image grenadeSprite = new ImageIcon("sprites/grenade.png").getImage();
private Image grenadeExploded = new ImageIcon("sprites/grenadeExplode.png").getImage();
private Image barrelSprite = new ImageIcon("sprites/barrel.png").getImage();
private Image barrelExplosion = new ImageIcon("sprites/barrelExplosion.png").getImage();
private Image barricadeSprite = new ImageIcon("sprites/barricade.png").getImage();
//backgrounds
private BufferedImage mask_background;
private Image background = new ImageIcon("sprites/forestmap2.jpg").getImage();
//constants for the weapons/health
private final int HEALTH=0,PISTOL=1,UZI=2,SHOTGUN=3,BARREL = 4,GRENADE= 5, BARRICADE=6,SENTRY=7;
//sizes of different objects/screens
private int rectsx = 34, rectsy = 47; //size of all characters on screen (zombies/devils/MC)
private int barricadesx = 60, barricadesy = 48;
private int barrelsx = 27, barrelsy = 43;
private int sentrysx = 20, sentrysy = 20;
private int screensx = 800, screensy = 640; //screensize
private int mapx=0, mapy=0, mapsx = 2000, mapsy = 2000; //the coordinates and size of the original map
private int bx1 = 100, bx2 = 670, by1 = 100, by2 = 510; //the map boundary on the screen before the map is shifted
private int shiftx = 0, shifty = 0; //The distance the map shifted
//all objects on the map at any given time
private ArrayList<Zombie> allZombies = new ArrayList<Zombie>();
private ArrayList<Devil> allDevils = new ArrayList<Devil>();
public ArrayList<PosPair> fireballs = new ArrayList<PosPair>(); //all fireballs in air
private ArrayList<PosPair> activeBullets = new ArrayList<PosPair>(); //all bullets in air
private ArrayList<MagicalBox> allBoxes = new ArrayList<MagicalBox>();
private ArrayList<Barricade> allBarricades = new ArrayList<Barricade>();
private ArrayList<Barrel> allBarrels = new ArrayList<Barrel>();
private ArrayList<Explosion> allExplosions = new ArrayList<Explosion>(); //all explosions taking place at the time (to blit the sprites)
private ArrayList<SentryGun> allSentries = new ArrayList<SentryGun>();
private ArrayList<Grenade> allGrenades = new ArrayList<Grenade>();
private ArrayList<Grenade> explodedGrenade = new ArrayList<Grenade>();
//consecutive kills + upgrades (consecutive kills lead to the upgrades)
private int consecutiveKills=0; //the number of consecutive kills at any point of time
private int consecutiveCountDown=0; //the counter in which after the countdown reaches 0, the consecutiveKills is reduced by 1
//and countdown at full again so it can continue to reduce consecutiveKills
private int nextUpgrade=0;//the position in the BH.ug.allUpgradesNum in which the next upgrade for the character will be
private String printUpgradeString=""; //the name of the upgrade to display
private int UpgradeStringCountDown=0; //countdown to display the string. when countdown is at 0, string is set at ""
//box item name blitting
private String boxString = ""; //the string to blit when the character obtains a box
private int boxCountDown=0; //count down to blit the box
//regulated shooting
private int shootCountDown=0; //countdown to shoot bullets to ensure that the bullets are not shot consecutively like a machine gun
//the character can only shoot when the countdown is at 0. once a bullet is shot, the counter is set back to full
//font loading
private Font SMALLfont = new Font("Impact",Font.PLAIN,15);
private Font font = new Font("Impact", Font.PLAIN, 40);
private Font LARGEfont = new Font("Impact", Font.PLAIN, 100);
//counters
private int spriteCounter = 0; //sprite counter for the MC so movement of MC is blitted properly
private int displayLevelCounter=0; //this is the counter used to display the "+-+-+-+ Level 2 +-+-+-+"
//other variables
private boolean lastSpaceStat=false; //the status of space being pressed in the gampanel before the current one
private String[] weaponNames = new String[8]; //stores the names of all the weapons
private int ZombiesDead=0,DevilsDead=0; //the number of zombies dead at any point, accumulated for the current level
private int ZombiesThisLevel, DevilsThisLevel; //the number of zombies/devils remaining in the level (alive, does not have to be spawned)
private int currentLevel;
private int spawnsp = 2000; //spawn speed
public GamePanel(BoxHead bh){
BH=bh;
keys = new boolean[65535];
addKeyListener(this);
getWeaponNames();
loadAudio();
loadSprites();
loadMask();
currentLevel=1;
ZombiesThisLevel = getZombiesThisLevel();
DevilsThisLevel = getDevilsThisLevel();
generateEnemy();
}
////////////////SETUP STUFF BEGIN///////////////
public void loadMask(){
//Load the mask needed for collision for the map
try{
File file= new File("mask_map3.jpg");
mask_background = ImageIO.read(file);
}
catch (IOException ex){}
}
public void loadSprites(){
for (int i=0;i<8;i++){
for (int k=0;k<3;k++){
charSprites[i][k]=new ImageIcon("sprites/guy0"+i+k+".png").getImage();
}
}
for (int i=0;i<8;i++){
for (int k=0;k<8;k++){
zombieSprites[i][k]=new ImageIcon("sprites/zombie"+i+k+".png").getImage();
devilSprites[i][k]=new ImageIcon("sprites/devil"+i+k+".png").getImage();
}
}
for (int i=0;i<8;i++){
bulletSprites[i]=new ImageIcon("sprites/pistol"+i+".png").getImage();
fireballSprites[i]=new ImageIcon("sprites/fireball"+i+".png").getImage();
}
}
public void loadAudio(){
//load the audio sound effects
audio[PISTOL]=Applet.newAudioClip(getClass().getResource("pistol.wav"));
audio[UZI]=Applet.newAudioClip(getClass().getResource("uzi.wav"));
audio[SHOTGUN]=Applet.newAudioClip(getClass().getResource("shotgun.wav"));
audio[SENTRY]=Applet.newAudioClip(getClass().getResource("sentrygun_qq.wav"));
audio[GRENADE] = Applet.newAudioClip(getClass().getResource("grenadethrow.wav"));
}
public void getWeaponNames(){
//load the weapon names
weaponNames[PISTOL]="PISTOL";
weaponNames[UZI]="UZI";
weaponNames[SHOTGUN]="SHOTGUN";
weaponNames[BARREL]="BARREL";
weaponNames[GRENADE]="GRENADE";
weaponNames[BARRICADE]="BARRICADE";
weaponNames[SENTRY]="SENTRY";
}
public void fullUpgradeCountDown(){
//set the upgrade string count down at full so it can count down while printing the string to screen
UpgradeStringCountDown=100;
}
public void UpgradeCountDown(){
//count down on the counter if it is not zero
//if it is zero then set printUpgradeString to "" (empty string) so nothing will blit on screeen
if (UpgradeStringCountDown!=0){
UpgradeStringCountDown--;
}
else{
printUpgradeString="";
}
}
public void setUpgradeString(String n){
//set the printUpgradeString to blit on screen
printUpgradeString=n;
}
public void setBoxString(String n){
//set the boxString to blit on screen
//set the boxCountDown on full
//when count down is at 0, the boxString stops appearing on the screen
boxString=n;
boxCountDown=100;
}
//////////////methods for key/////////////
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){keys[e.getKeyCode()]=true;}
public void keyReleased(KeyEvent e){keys[e.getKeyCode()]=false;}
////////////////////////////////////
public void addNotify(){
super.addNotify();
requestFocus();
BH.start();
}
public void activateAudio(int weapon){
//play the audio of the weapon given
audio[weapon].play();
}
public int getNextUpgrade(){
//getter for nextUpgrade
return nextUpgrade;
}
public void checkPause(){
//display another pause screen if the player presses P/p
if (keys[KeyEvent.VK_P]){
BH.state=BH.PAUSE;
BH.sm.requestFocus();
keys[KeyEvent.VK_P]=false; //set to false for future use
}
}
/////////////SETUP STUFF END////////////////
//MAIN CHARACTER BEGIN
public void moveMC(){
///move the character based on its speed and direction (angle)
//yay for trigonometry
//add on to the sprite counter so the right sprite can blit on the screen
int speed = BH.mc.getspeed();
double nx=BH.mc.getX(), ny=BH.mc.getY();
//new x position, new y position
///moving diagonally
if (keys[KeyEvent.VK_LEFT] && keys[KeyEvent.VK_UP]){
BH.mc.setAngle(225);
nx = Math.max(BH.mc.getX()-speed/2*Math.sqrt(2),0);
ny = Math.max(BH.mc.getY()-speed/2*Math.sqrt(2),0);
//Doesn't go off screen
spriteCounter++;
}
else if (keys[KeyEvent.VK_LEFT] && keys[KeyEvent.VK_DOWN]){
BH.mc.setAngle(135);
nx = Math.max(BH.mc.getX()-speed/2*Math.sqrt(2),0);
ny = Math.min(BH.mc.getY()+speed/2*Math.sqrt(2),640);
//Doesn't go off screen
spriteCounter++;
}
else if (keys[KeyEvent.VK_RIGHT] && keys[KeyEvent.VK_DOWN]){
BH.mc.setAngle(45);
nx = Math.min(BH.mc.getX()+speed/2*Math.sqrt(2),800);
ny = Math.min(BH.mc.getY()+speed/2*Math.sqrt(2),640);
//Doesn't go off screen
spriteCounter++;
}
else if (keys[KeyEvent.VK_RIGHT] && keys[KeyEvent.VK_UP]){
BH.mc.setAngle(315);
nx = Math.min(BH.mc.getX()+speed/2*Math.sqrt(2),800);
ny = Math.max(BH.mc.getY()-speed/2*Math.sqrt(2),0);
//Doesn't go off screen
spriteCounter++;
}
//moving up down left right (not diagonally)
else if (keys[KeyEvent.VK_LEFT]){
BH.mc.setAngle(180);
nx = Math.max(0, BH.mc.getX()-speed);
spriteCounter++;
}
else if (keys[KeyEvent.VK_RIGHT]){
nx = Math.min(800-BH.mc.getWidth(), BH.mc.getX()+speed);
//Doesn't go off screen
BH.mc.setAngle(0);
spriteCounter++;
}
else if (keys[KeyEvent.VK_UP]){
ny = Math.max(0, BH.mc.getY()-speed);
//Doesn't go off screen
BH.mc.setAngle(270);
spriteCounter++;
}
else if (keys[KeyEvent.VK_DOWN]){
ny = Math.min(640-BH.mc.getLength(),BH.mc.getY()+speed);
//Doesn't go off screen
BH.mc.setAngle(90);
spriteCounter++;
}
int inx = (int)nx, iny = (int)ny;
// If position is valid in the mask
if (validMove(inx,BH.mc.getY()) && numbercollisions(inx,BH.mc.getY()) <= 1){
BH.mc.setX(nx);
}//note : co-ordinates are stored as doubles, allows mini movements
if (validMove(BH.mc.getX(),iny) && numbercollisions(BH.mc.getX(),iny) <= 1){
BH.mc.setY(ny);
}
}
public void MCshoot(){ //Main character doing action
if (keys[KeyEvent.VK_SPACE]){
//shoot the weapons based on the angle and how far they are supposed to be shot to (at)
//yay for trigs again
int mx = BH.mc.getcx(), my = BH.mc.getcy();//get center position
//if the user shoots, add a bullet into the arraylist keeping track of flying bullets
//check if the weapon can be consecutively dropped:
//if so, check if the counter is at 0 so the weapon can be fired, if not, it must wait until the counter is at 0
//if it shoots, it sets the shoot countdown to full
//if not, check if the last status of space bar was at false
///consecutively shooting/dropping something:
//holding space bar down and the bullet will shoot at a regular interval
//non consecutive shooting/dropping
//must press space bar multiple times to drop/shoot multiple times
//reduce the ammo everytime a weapon is used
//add the weapon to its arraylist so it can be blit and moved (if needed) properly
//ex. add the barricade to allBarricades
if (BH.mc.getWeapon()==BARRICADE){
//barricade cannot be consecutively dropped
if (!lastSpaceStat){
int nx = (int) (mx + 45*Math.cos(Math.toRadians(BH.mc.getANGLE())));
int ny = (int) (my + 45*Math.sin(Math.toRadians(BH.mc.getANGLE())));
if(!objectPlacementCollision(nx-barricadesx/2,ny-barricadesy/2,barricadesx,barricadesy)){
//If the spot is empty
allBarricades.add(new Barricade(nx-barricadesx/2,ny-barricadesy/2));
BH.mc.useAmmo(BARRICADE);
}
}
}
else if (BH.mc.getWeapon()==BARREL){
//barrel cannot be consecutively dropped
if (!lastSpaceStat){
int nx = (int) (mx + 30*Math.cos(Math.toRadians(BH.mc.getANGLE())));
int ny = (int) (my + 55*Math.sin(Math.toRadians(BH.mc.getANGLE())));
if(!objectPlacementCollision(nx-barrelsx/2,ny-barrelsy/2,barrelsx,barrelsy)){
//If the spot is empty
allBarrels.add(new Barrel(nx - barrelsx/2, ny - barrelsy/2));//If the place is empty
BH.mc.useAmmo(BARREL);
}
}
}
else if (BH.mc.getWeapon()==GRENADE){
//grenade cannot be consecutively dropped
if (!lastSpaceStat){
int nx = (int) (mx + 60*Math.cos(Math.toRadians(BH.mc.getANGLE())));
int ny = (int) (my + 60*Math.sin(Math.toRadians(BH.mc.getANGLE())));
allGrenades.add(new Grenade(nx,ny,BH));
BH.mc.useAmmo(GRENADE);
activateAudio(GRENADE); //play the sound to drop a grenade
}
}
else if (BH.mc.getWeapon()==SENTRY){
//sentry cannot be consecutively dropped
if (!lastSpaceStat){
int nx = (int) (mx + 37*Math.cos(Math.toRadians(BH.mc.getANGLE())));
int ny = (int) (my + 37*Math.sin(Math.toRadians(BH.mc.getANGLE())));
if(!objectPlacementCollision(nx-sentrysx/2,ny-sentrysy/2,sentrysx,sentrysy)){
//If the spot is empty
allSentries.add(new SentryGun(nx-sentrysx/2,ny-sentrysy/2));//Man's best friend
BH.mc.useAmmo(SENTRY);
}
}
}
else if (BH.mc.getWeapon()==SHOTGUN){
if ((BH.mc.getConsecutiveShoot(SHOTGUN) &&shootCountDown==0)|| lastSpaceStat==false){
//if it can shoot consecutively. if not, it cannot shoot consecutively and the last space stat must be false
//check how wide the shotgun is to determine how far the bullets are apart
if (BH.mc.getSGW()==0){
activeBullets.add(new PosPair(mx,my,BH.mc.getANGLE(),BH.mc.getWeapon()));
activeBullets.add(new PosPair(mx,my,(BH.mc.getANGLE()+5)%360,BH.mc.getWeapon()));
activeBullets.add(new PosPair(mx,my,(BH.mc.getANGLE()-5+360)%360,BH.mc.getWeapon()));
}
else if (BH.mc.getSGW()==1){
//wide shot
activeBullets.add(new PosPair(mx,my,BH.mc.getANGLE(),BH.mc.getWeapon()));
activeBullets.add(new PosPair(mx,my,(BH.mc.getANGLE()+10)%360,BH.mc.getWeapon()));
activeBullets.add(new PosPair(mx,my,(BH.mc.getANGLE()-10+360)%360,BH.mc.getWeapon()));
}
else{
//wider shot
activeBullets.add(new PosPair(mx,my,BH.mc.getANGLE(),BH.mc.getWeapon()));
activeBullets.add(new PosPair(mx,my,(BH.mc.getANGLE()+20)%360,BH.mc.getWeapon()));
activeBullets.add(new PosPair(mx,my,(BH.mc.getANGLE()-20+360)%360,BH.mc.getWeapon()));
}
shootCountDown=15;
activateAudio(BH.mc.getWeapon()); //play the sound effect
BH.mc.useAmmo(BH.mc.getWeapon());
}
}
else if (BH.mc.getWeapon()==PISTOL){
//If it can be shot consecutively otherwise last space stat must be false
if ((BH.mc.getConsecutiveShoot(PISTOL)&&shootCountDown==0)|| lastSpaceStat==false){
activeBullets.add(new PosPair(mx,my,BH.mc.getANGLE(),BH.mc.getWeapon()));
BH.mc.useAmmo(BH.mc.getWeapon());
activateAudio(BH.mc.getWeapon());//play the sound effect
shootCountDown=15;
}
}
else{
if (shootCountDown==0){
shootCountDown=15;
activeBullets.add(new PosPair(mx,my,BH.mc.getANGLE(),BH.mc.getWeapon()));
BH.mc.useAmmo(BH.mc.getWeapon());
activateAudio(BH.mc.getWeapon());//play the sound effect
}
}
if (shootCountDown>0){
//if shoot count down is not at 0, then reduce it
shootCountDown--;
}
}
}
public void checkMC(){
//Check if MC is getting beat up
//check if it dies
for (Zombie a:allZombies){
if (a.collideMC()){
BH.mc.setHealth(Math.max(BH.mc.getHealth()-a.getdmg(),0));
}
}
for (Devil a:allDevils){
if (a.collideMC()){
BH.mc.setHealth(Math.max(BH.mc.getHealth()-a.getdmg(),0));
}
}
checkDeath();
}
public void fullCountDown(){consecutiveCountDown=225;} //set counter to full
public void CountDown(){
//this counts down on the consecutive kills counter
if (consecutiveCountDown>0){
//reduce counter if it is greater than 0
consecutiveCountDown--;
if (consecutiveCountDown==0 && consecutiveKills>0){
//reduce consecutive kills if there are still any after counter is at 0
consecutiveKills--;
if (consecutiveKills>0){
// if there are still consecutive killss left, set couner to full
fullCountDown();
}
}
}
}
public void addConsecutive(){
//this adds on to the consecutive kill
//this checks if it is time to upgrade (based on the number of consecutive kills and the next upgrade)
consecutiveKills++;
if (nextUpgrade==26){return;}
//make sure it doesn't get past 25 so index won't be out of bounds
if (consecutiveKills==BH.ug.allUpgradesNum[nextUpgrade]){
BH.ug.getUpgrade(BH.ug.allUpgradesNum[nextUpgrade++]);
}
}
public void switchWeapon(){
//switches the weapon based on the number key pressed
if (keys[KeyEvent.VK_1]){
BH.mc.setWeapon(1);
}
else if (keys[KeyEvent.VK_2]){
if (BH.mc.getAmmo(2)>0){
BH.mc.setWeapon(2);
}
}
else if (keys[KeyEvent.VK_3]){
if (BH.mc.getAmmo(3)>0){
BH.mc.setWeapon(3);
}
}
else if (keys[KeyEvent.VK_4]){
if (BH.mc.getAmmo(4)>0){
BH.mc.setWeapon(4);
}
}
else if (keys[KeyEvent.VK_5]){
if (BH.mc.getAmmo(5)>0){
BH.mc.setWeapon(5);
}
}
else if (keys[KeyEvent.VK_6]){
if (BH.mc.getAmmo(6)>0){
BH.mc.setWeapon(6);
}
}
else if (keys[KeyEvent.VK_7]){
if (BH.mc.getAmmo(7)>0){
BH.mc.setWeapon(7);
}
}
}
//MAIN CHARACTER END
//GENERAL FUNCTIONS START
public double dist(int x1, int y1,int x2,int y2){
//returns the distance between two points
return Math.pow(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2),0.5);
}
public boolean validMove(int x, int y){
//check if the pixel is valid in the mask
if (mapx+x >=2000 || mapy+y >= 2000){
return false;
}
if (mapx+x>=0&& mapy+y>=0){
//only return true if pixel is on the right colour
int clr= mask_background.getRGB(mapx+x,mapy+y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
if (red == 255 && green == 255 && blue == 255){
return true;
}
}
return false;
}
public int numbercollisions(int x, int y){
//If pixel does not collide with any rects on screen
//collisions should max 1 (itself)
int ncollision = 0;
if (rectcollision(x,y,rectsx,rectsy,BH.mc.getX(),BH.mc.getY(),rectsx,rectsy)){
ncollision++;
}
for (Zombie z : allZombies){
if (rectcollision(x,y,rectsx,rectsy,z.getX(),z.getY(),rectsx,rectsy)){
ncollision++;
}
}
for (Devil d : allDevils){
if (rectcollision(x,y,rectsx,rectsy,d.getX(),d.getY(),rectsx,rectsy)){
ncollision++;
}
}
for (Barricade b : allBarricades){
if (b.rectcollision(x,y)){
ncollision++;
}
}
for (SentryGun b : allSentries){
if (b.rectcollision(x,y)){
ncollision++;
}
}
for (Barrel b : allBarrels){
if (b.rectcollision(x,y)){
ncollision++;
}
}
return ncollision;
}
public boolean rectcollision(int x1, int y1,int rectsx1, int rectsy1, int x2, int y2,int rectsx2, int rectsy2){
//this method checks the collision of two rectangles
if (x1+rectsx1 < x2 || x1 > x2 + rectsx2 || y1 + rectsy1 < y2 || y1 > y2 + rectsy2){
//proof by contradiction
return false;
}
return true;
}
public boolean circleRectangleCollision(int cx,int cy, int cr, int rx,int ry,int rl,int rw){
//this method checks the collision of a circle against a rectangle
if(rx<=cx && cx<=rx+rw && ry<=cy && cy<=ry+rl){
return true;
}
else if (dist(rx,ry,cx,cy)<=cr){
return true;
}
else if (dist(rx,ry+rl,cx,cy)<=cr){
return true;
}
else if (dist(rx+rw,ry,cx,cy)<=cr){
return true;
}
else if (dist(rx+rw,ry+rl,cx,cy)<=cr){
return true;
}
return false;
}
public boolean checkFireballCollision(int fx,int fy,int mcx,int mcy){
//check if the fireball is on the character
return (fx+10>=mcx && fx+10 <=mcx+BH.mc.getWidth() && fy+10>=mcy && fy+10<=mcy+BH.mc.getLength());
}
public boolean objectPlacementCollision(int objx,int objy,int objsx,int objsy){
//this method is called to check if the user can place an object on the map
//check if it would collide with any other object on the map
for (Zombie zombie: allZombies){
if (rectcollision(objx,objy,objsx,objsy,zombie.getX(),zombie.getY(),zombie.getsx(),zombie.getsy())){
return true;
}
}
for(Devil devil:allDevils){
if (rectcollision(objx,objy,objsx,objsy,devil.getX(),devil.getY(),devil.getsx(),devil.getsy())){
return true;
}
}
for(Barrel barrel:allBarrels){
if (rectcollision(objx,objy,objsx,objsy,barrel.getX(),barrel.getY(),barrel.getsx(),barrel.getsy())){
return true;
}
}
for (Barricade barricade: allBarricades){
if (rectcollision(objx,objy,objsx,objsy,barricade.getX(),barricade.getY(),barricade.getsx(),barricade.getsy())){
return true;
}
}
for (SentryGun sentry: allSentries){
if (rectcollision(objx,objy,objsx,objsy,sentry.getX(),sentry.getY(),sentrysx,sentrysy)){
return true;
}
}
return false;
}
//GENERAL FUNCTIONS END
//LEVEL STUFF BEGIN
public void checkLevelOver(){
//TBH If you can beat level 1 you're already beast
if (ZombiesDead >= getZombiesThisLevel() && DevilsDead >= getDevilsThisLevel()){
//if the number of zombies/devils dead in the level is the same as the number of them that is supposed to exist in the level
//this level is over
levelUp();
}
}
public void levelUp(){
//preparing variables for a new level
BH.enemyGenerationTimer = new Timer(spawnsp-currentLevel*300, BH);
BH.enemyGenerationTimer.start();
currentLevel++;
ZombiesDead=0;
DevilsDead=0;
ZombiesThisLevel=getZombiesThisLevel();
DevilsThisLevel=getDevilsThisLevel();
displayLevelCounter=200;
}
public int getZombiesThisLevel(){
//# of zombies for the level
return (currentLevel+2)*20;
}
public int getDevilsThisLevel(){
//# of devil for the level
return (currentLevel+2)*15;
}
public void checkDeath(){
//check MC's death
//go to GameOver screen
if(BH.mc.getHealth()<=0){
BH.state=BH.OVER;
BH.go.activateMouse();
BH.go.requestFocus();
}
}
public void restart(){
//restart the game
//set everything back to default
//clear everything
spawnsp = 2000;
BH.enemyGenerationTimer = new Timer(spawnsp, BH);
BH.enemyGenerationTimer.start();
BH.mc.unloadCAmmo();
printUpgradeString="";
consecutiveCountDown=0;
BH.mc.setWeapon(1);
BH.magicalBoxAllowance=1;
currentLevel=1;
shootCountDown=0;
mapx=0;
mapy=0;
BH.mc.loadMaxAmmo();
BH.mc.loadWeaponSpeed();
BH.mc.loadConsecutiveShoot();
BH.mc.setHealth(BH.mc.full_health);
//enemies reset
ZombiesThisLevel=getZombiesThisLevel();
DevilsThisLevel=getDevilsThisLevel();
BH.score=0;
ZombiesDead=0;
DevilsDead=0;
//ListArrays reset
allZombies.clear();
allDevils.clear();
allGrenades.clear();
explodedGrenade.clear();
fireballs.clear();
activeBullets.clear();
allBarricades.clear();
allBarrels.clear();
allExplosions.clear();
allSentries.clear();
allBoxes.clear();
//Mc position reset
BH.mc.setX(100);
BH.mc.setY(400);
//keys reset
keys[KeyEvent.VK_SPACE]=false;
keys[KeyEvent.VK_UP]=false;
keys[KeyEvent.VK_DOWN]=false;
keys[KeyEvent.VK_LEFT]=false;
keys[KeyEvent.VK_RIGHT]=false;
keys[KeyEvent.VK_1]=false;
keys[KeyEvent.VK_2]=false;
keys[KeyEvent.VK_3]=false;
keys[KeyEvent.VK_4]=false;
keys[KeyEvent.VK_5]=false;
keys[KeyEvent.VK_6]=false;
keys[KeyEvent.VK_7]=false;
keys[KeyEvent.VK_8]=false;
//shotgun reset
BH.mc.setSGW(0);
BH.mc.loadConsecutiveShoot();
consecutiveKills=0;
nextUpgrade=0;
}
//LEVEL STUFF END
//CHECKING OBJECTS START
public void checkObjects(){
//General Check function
checkSentry();
checkBarricades();
checkBarrels();
checkExplosions();
checkDead();
}
private void checkExplosions(){
//counts how long the explosions lasts
ArrayList<Explosion> toRemove = new ArrayList<Explosion>();
for (Explosion b : allExplosions){
b.incrementtime(); //add to the counter
if (b.getctime() >= 10){
//if the counter is at max, remove the explosion
toRemove.add(b);
}
}
for (Explosion b : toRemove){
allExplosions.remove(b);
}
}
public void checkDead(){
//this method is called to see if the character's bullets damage the enemies
ArrayList<Zombie> toRemoveZ = new ArrayList<Zombie>();
for (Zombie z : allZombies){
//check if a zombie dies
if (z.getHealth() <= 0){
toRemoveZ.add(z);
}
}
for (Zombie zzh8829:toRemoveZ){
//remove the dead zombies
if ((int)(Math.random()*3)==1){
//Every once in a while a magical unicorn drops by after a zombie dies
//And lays a magicalbox
allBoxes.add(new MagicalBox(zzh8829.getX(),zzh8829.getY(),BH));
}
BH.score+=200+consecutiveKills*100; //add the score and bonus for consecutive kills
//add on to the consecutive kills and set the consecutive countdown to full
//add on to the dead count and remove the zombie
ZombiesDead++;
allZombies.remove(zzh8829);
fullCountDown();
addConsecutive();
}
ArrayList<Devil> toRemoveD = new ArrayList<Devil>();
//see comments for the zombies above
for (Devil d : allDevils){
if (d.getHealth() <= 0){
toRemoveD.add(d);
}
}
for (Devil zzh8829:toRemoveD){
BH.score+=200+consecutiveKills*100;
allDevils.remove(zzh8829);
//All Devils lay boxes
allBoxes.add(new MagicalBox(zzh8829.getX(),zzh8829.getY(),BH));
fullCountDown();
DevilsDead++;
addConsecutive();
}
}
private void checkBarricades(){
//if barricade has been destroyed
ArrayList<Barricade> toRemove = new ArrayList<Barricade>();
for (Barricade b : allBarricades){
if (b.getHealth() <= 0){
toRemove.add(b);
}
}
for (Barricade b : toRemove){
allBarricades.remove(b);
}
}
//CHECKING OBJECTS END
//Checking position BEGIN
public boolean checkOutside(int x,int y){
//check if it is outside the screen
return x<0||x>800||y<0||y>640;
}
public boolean checkOutsideMap(int x, int y){
//check if it is outside the map
if (x >= 2000 || y >= 2000 || x < 0 || y < 0)
return true;
return false;
}
//CHECK POSITION END
//BULLET AND FIREBALLS MOVING BEGIN
public void moveFireballs()
{
//The OP devil weapon
ArrayList<PosPair> toRemove = new ArrayList<PosPair>();
for (PosPair temp : fireballs)
{
//go through every single fireball in the air
final double ANG = Math.toRadians(temp.getANGLE());
double xx = temp.getDX(), yy = temp.getDY();
//move fireball based on the angle and position...TRIGS!
temp.setPos(xx+20*Math.cos(ANG),yy+20*Math.sin(ANG));
//remove the fireball if it hits anythign
if (checkFireballCollision(temp.getX(),temp.getY(),BH.mc.getX(),BH.mc.getY())){
//checks if MC gets hit
BH.mc.setHealth(BH.mc.getHealth()-temp.getdmg());
toRemove.add(temp);
}
else if (checkOutsideMap(temp.getX(),temp.getY())|| !validMove(temp.getX(),temp.getY())){
toRemove.add(temp);
}
//check if the fireball hits a barrel or barricade
//if so, reduce the health/remove it
for (Barrel bar : allBarrels){
int x = bar.getX(), y = bar.getY(), sx = bar.getsx(), sy = bar.getsy(), px = temp.getX(), py = temp.getY();
if (px >= x && px <= x + sx && py >= y && py <= y + sy){//barrels get blown up by one hit
bar.setHealth(0);
toRemove.add(temp);
}
}
for (Barricade bar : allBarricades){
int x = bar.getX(), y = bar.getY(), sx = bar.getsx(), sy = bar.getsy(), px = temp.getX(), py = temp.getY();
if (px >= x && px <= x + sx && py >= y && py <= y + sy){//barricades get destroyed
bar.setHealth(bar.getHealth()-temp.getdmg());
toRemove.add(temp);
}
}
}
for (PosPair pair:toRemove){
fireballs.remove(pair);
}
}
public void moveBullets(){
//move all the active bullets
ArrayList<PosPair> toRemove = new ArrayList<PosPair>();
for (PosPair temp : activeBullets)
{
final double ANG = Math.toRadians(temp.getANGLE());
double xx = temp.getDX(), yy = temp.getDY();
final int sp =BH.mc.getAtWeaponSpeed(temp.getType());
//calculate how far to move based on the angle, speed and position of the bullet
double nx = xx+sp*Math.cos(ANG), ny = yy+sp*Math.sin(ANG);
//if it is a valid move then move it, if not then remove it
if (validMove((int)nx,(int)ny)){
temp.setPos(nx,ny);
}
else{
toRemove.add(temp);
}
}
for (PosPair temp : toRemove){
activeBullets.remove(temp);
}
}
public void checkBulletCollision(){
//check if the bullets hit any enemies
ArrayList<PosPair> toRemove = new ArrayList<PosPair>();
for (PosPair temp : activeBullets){
if (enemyBulletCollision(temp.getX(),temp.getY())){
//REMOVE THE bullets
toRemove.add(temp);
}
else if (checkOutside(temp.getX(),temp.getY())){
toRemove.add(temp);
}
else if (!validMove(temp.getX(),temp.getY())){
//If it hits a wall
toRemove.add(temp);
}
for (Barrel bar : allBarrels){
//if it hits a barrel, an explosion occurs
int x = bar.getX(), y = bar.getY(), sx = bar.getsx(), sy = bar.getsy(), px = temp.getX(), py = temp.getY();
if (px >= x && px <= x + sx && py >= y && py <= y + sy){
bar.setHealth(0);
toRemove.add(temp);
}
}
}
for (PosPair pair:toRemove){
activeBullets.remove(pair);
}
}
public boolean enemyBulletCollision(int x, int y){
//this method is called to see if the character's bullets damage the enemies
for (Zombie z : allZombies){
if (z.getCollide(x,y)){
z.setHealth(z.getHealth() - BH.mc.getdmg(BH.mc.getWeapon()));
return true;
}
}
for (Devil d : allDevils){
if (d.getCollide(x,y)){
d.setHealth(d.getHealth() - BH.mc.getdmg(BH.mc.getWeapon()));
return true;
}
}
return false;
}
public void checkBulletDistance(){
//checks how far the bullet travels and if it needs to be removed
ArrayList<PosPair> toRemove = new ArrayList<PosPair>();
for (PosPair a: activeBullets){
if (dist(a.getorigX(),a.getorigY(),a.getX(),a.getY()) > BH.mc.getMaxDist(a.getTYPE())){
toRemove.add(a);
}
}
for (PosPair pair:toRemove){
activeBullets.remove(pair);
}
}
//BULLET AND FIREBALLS MOVING END
//ENEMY MOVEMENT BEGIN
public void moveZombie(){
//move zombie AI
for (Zombie temp : allZombies){
//go through all zombies
//Manhat = manhatten distance (Straight line distance)
double ManhatX = Math.abs(temp.getDX() - BH.mc.getDX()), ManhatY = Math.abs(temp.getDY() - BH.mc.getDY()), speed = temp.getspeed();
//manhatx, x difference manhaty, y difference
double moveX = ManhatX/(ManhatX+ManhatY)*speed, moveY = ManhatY/(ManhatX+ManhatY)*speed, nx, ny;
//amount the zombie will move
//move the zombie toward the MC
temp.setAngle(Math.toDegrees(3.14159265358+Math.atan2(temp.getDY() - BH.mc.getDY(),temp.getDX() - BH.mc.getDX())));
if (temp.getDX() <= BH.mc.getDX()){
nx = temp.getDX()+moveX;
}
else{
nx = temp.getDX()-moveX;
}
if (temp.getDY() <= BH.mc.getDY()){
ny = temp.getDY()+moveY;
}
else{
ny = temp.getDY()-moveY;
}
for (Barricade b : allBarricades){//zombies destroy barricades
if (b.rectcollision((int)nx,(int)ny)){
b.setHealth(b.getHealth()-temp.getdmg());
}
}
for (SentryGun b : allSentries){//also destroy sentryguns
if (b.rectcollision((int)nx,(int)ny)){
b.setHealth(b.getHealth()-temp.getdmg());
}
}
//If movement is valid
if (numbercollisions((int)nx, temp.getY()) <= 1 && validMove((int)nx,temp.getY())){
temp.setX(nx);
}
if (numbercollisions(temp.getX(), (int)ny) <= 1 && validMove(temp.getX(),(int)ny)){
temp.setY(ny);
}//note: co-ordinates are stored as doubles to allow micro movements
}
}
public void moveDevil(){
//Refer to comments above :)
for (Devil temp : allDevils){
double ManhatX = Math.abs(temp.getDX() - BH.mc.getDX()), ManhatY = Math.abs(temp.getDY() - BH.mc.getDY()), speed = temp.getspeed();
double moveX = ManhatX/(ManhatX+ManhatY)*speed, moveY = ManhatY/(ManhatX+ManhatY)*speed, nx, ny;
temp.setAngle(Math.toDegrees(Math.PI+Math.atan2(temp.getDY() - BH.mc.getDY(),temp.getDX() - BH.mc.getDX())));
temp.addTime(BH);
if (temp.getDX() <= BH.mc.getDX()){
nx = temp.getDX()+moveX;
}
else{
nx = temp.getDX()-moveX;
}
if (temp.getDY() <= BH.mc.getDY()){
ny = temp.getY()+moveY;
}
else{
ny = temp.getY()-moveY;
}
for (Barricade b : allBarricades){
if (b.rectcollision((int)nx,(int)ny)){
b.setHealth(b.getHealth()-temp.getdmg());
}
}
for (SentryGun b : allSentries){
if (b.rectcollision((int)nx,(int)ny)){
b.setHealth(b.getHealth()-temp.getdmg());
}
}
if (numbercollisions((int)nx, temp.getY()) <= 1 && validMove((int)nx,temp.getY())){
temp.setX(nx);