-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.java
More file actions
1392 lines (1344 loc) · 56.8 KB
/
Character.java
File metadata and controls
1392 lines (1344 loc) · 56.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
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
//Imported to use images
import java.awt.*;
//Imported to use ArrayLists
import java.util.ArrayList;
//Imported in order to get audio in our algorythm
import java.io.*;
import sun.audio.*;
public class Character{
//Name of the character
String name;
//The sprite
Sprite c;
//The character stats
int maxHP, hp, level, baseDamage, damage, speedX, speedY, speed;
private int baseSpeed;
//Used for movingdiagonally
int mmove;
//Boolean to speed up after the attack has stopped as the character slows down when attacking
boolean speedUpAfterAttack;
//Boolean to check if the attack may get out
boolean canAttack = false;
//Images used for character movements
Image upMove, downMove, rightMove, leftMove;
Image upLeftMove, upRightMove, downLeftMove, downRightMove;
//Images used for character attacks
Image upAttack, downAttack, leftAttack, rightAttack;
Image upLeftAttack, upRightAttack, downLeftAttack, downRightAttack;
//Stopping Images
Image upStill, downStill, rightStill, leftStill;
Image upLeftStill, upRightStill, downLeftStill, downRightStill;
//Ability Images
Image ability1Left, ability1Right, ability2Left, ability2Right;
//Image when knocked out as an ally
Image koImage;
//A full 3D image used to show the character
Image baseImage;
//Rectangles used for characters and weapon ranges
Rectangle boundsUpLeft = new Rectangle(), boundsUp = new Rectangle(), boundsUpRight = new Rectangle();
Rectangle boundsLeft = new Rectangle(), boundsMiddle = new Rectangle(), boundsRight = new Rectangle();
Rectangle boundsDownLeft = new Rectangle(),boundsDown = new Rectangle(), boundsDownRight = new Rectangle();
boolean isEnemy, isProtagonist = false;
//Variables to do with the bullet
ArrayList<Bullet> bullet = new ArrayList<Bullet>();
int bulletRadius;
int myBulletSpeed;
Image bulletImage;
//The time to hit the character
long maxToHitTime, toHitTime;
//Timers to display animations
long hittingTimer, hittingAnimation, ability1AnimationTimer, ability2AnimationTimer;
//The randomzer for the movement and its maximum
static int ranMax = 100;
int ranNo;
//Integer which determines which enemies need to have their health depleted and by hom much
static ArrayList<Integer> lstHit = new ArrayList<Integer>();
static ArrayList<Integer> lstAmount = new ArrayList<Integer>();
//Ability cooldown timers
long abilityTimer1, abilityTimer2;
long abilityCooldown1, abilityCooldown2;
//The direction the character is facing out of 8
int direction = 4;
//Timer and random number to be used for moving
int r;
long moveTimer;
boolean movingAroundChar = false, movingTowardsChar = false, movingAwayFromChar = false;
//Timer for how long the character has been touching the enemy
//and if the timer exceeds a certain amount he will be attacked
long touchingTimer;
//Timer for damaging animation to appear
long damagedTimer;
static Image damagedImage;
//The place of the rectangle bounds related to the image from top left
int boundsX, boundsY;
//Timers for stoppind at diagonal directions
long downTimer, upTimer, leftTimer, rightTimer;
//Boolean to stop the character movements after he attacks
boolean stopAfterAttacking = false;
//The booleans to indicate the program to stop the ability animations
boolean ability1Stop = false, ability2Stop = false;
//The duration that an ally remains dead for until he automatically revives
static long revivalTime;
long revivalTimer;
//The number of allies ocunter
static int noOfAllies = 0, noOfEnemies = 0;
//Images for the UI and their coordinates
Image healthyImage, hurtImage;
int healthyImagex, healthyImagey;
int hurtImagex, hurtImagey;
//Images for the ability icons
Image abilityImage1, abilityImage2;
//Sound items
static ArrayList<String> hitSounds = new ArrayList<String>();
static ArrayList<String> chooseSound = new ArrayList<String>();
static ArrayList<String> deathSound = new ArrayList<String>();
static ArrayList<String> specialSound = new ArrayList<String>();
//Booleans for staility in program
boolean isAddingDamage = false;
boolean isMovingBullet = false;
boolean canPerformAbility1 = false, canPerformAbility2 = false;
Sound s = new Sound();
//Default Constructor
//public Character(){}
public Character(//Movement images
Image pupMove, Image pdownMove, Image prightMove, Image pleftMove,
Image pupLeftMove, Image pupRightMove, Image pdownLeftMove, Image pdownRightMove,
//Attacking images
Image pupAttack, Image pdownAttack, Image pleftAttack, Image prightAttack,
Image pupLeftAttack, Image pupRightAttack, Image pdownLeftAttack, Image pdownRightAttack,
//Still images
Image pupStill, Image pdownStill, Image prightStill, Image pleftStill,
Image pupLeftStill, Image pupRightStill, Image pdownLeftStill, Image pdownRightStill,
//Ability Images
Image pability1Left, Image pability1Right, Image pability2Left, Image pability2Right,
//Knocked out image
Image pkoImage,
//A full 3D image used to show the character
Image pbaseImage,
//Length and height of rectangle bounds
int sideWidth, int midWidth,
int sideHeight, int midHeight,
//Enemy or ally
boolean pisEnemy,
//Protagonist or not
boolean pisProtagonist,
//bullet radius
int pbulletRadius, int pmyBulletSpeed,
//bullet Image
Image pbulletImage,
//Timers to display animations
long phittingAnimation, long pability1AnimationTimer, long pability2AnimationTimer,
//Ability cooldown timers
long pabilityCooldown1, long pabilityCooldown2,
//Character stats
int pmaxHP, int pbaseSpeed, int plevel, int pbaseDamage,
//Sprite postitions
int pcx, int pcy,
//The time to hit the character
int pmaxToHitTime,
//Images for the UI and their coordinates
Image phealthyImage, Image phurtImage,
int phealthyImagex, int phealthyImagey,
int phurtImagex, int phurtImagey,
//Images for the ability icons
Image pabilityImage1, Image pabilityImage2){
//Movement images
upMove = pupMove; downMove = pdownMove; rightMove = prightMove; leftMove = pleftMove;
upLeftMove = pupLeftMove; upRightMove = pupRightMove; downLeftMove = pdownLeftMove; downRightMove = pdownRightMove;
//Attacking images
upAttack = pupAttack; downAttack = pdownAttack; leftAttack = pleftAttack; rightAttack = prightAttack;
upLeftAttack = pupLeftAttack; upRightAttack = pupRightAttack; downLeftAttack = pdownLeftAttack; downRightAttack = pdownRightAttack;
//Still images
upStill = pupStill; downStill = pdownStill; rightStill = prightStill; leftStill = pleftStill;
upLeftStill = pupLeftStill; upRightStill = pupRightStill; downLeftStill = pdownLeftStill; downRightStill = pdownRightStill;
//Ability Images
ability1Left = pability1Left; ability1Right = pability1Right; ability2Left = pability2Left; ability2Right = pability2Right;
//Knocked out image
koImage = pkoImage;
//A full 3D image used to show the character
baseImage = pbaseImage;
//Length and height of rectangle bounds
boundsUpLeft.width = boundsUpRight.width = boundsLeft.width = boundsRight.width = boundsDownLeft.width = boundsDownRight.width = sideWidth;
boundsUp.width = boundsDown.width = boundsMiddle.width = midWidth;
boundsUpLeft.height = boundsUpRight.height = boundsUp.height = boundsDown.height = boundsDownLeft.height = boundsDownRight.height = sideHeight;
boundsLeft.height = boundsRight.height = boundsMiddle.height = midHeight;
//Enemy or ally
isEnemy = pisEnemy;
//Protagonist or not
isProtagonist = pisProtagonist;
//bullet radius
bulletRadius = pbulletRadius; myBulletSpeed = pmyBulletSpeed;
//bullet Image
bulletImage = pbulletImage;
//Timers to display animations
hittingAnimation = phittingAnimation; ability1AnimationTimer = pability1AnimationTimer; ability2AnimationTimer = pability2AnimationTimer;
//Ability cooldown timers
abilityCooldown1 = pabilityCooldown1; abilityCooldown2 = pabilityCooldown2;
//Rectangle positions relative to image
boundsX = sideWidth; boundsY = sideHeight;
//Character Stats
maxHP = hp = pmaxHP;
setBaseSpeed(pbaseSpeed);
level = plevel;
baseDamage = damage = pbaseDamage;
//Set the sprite
c = new Sprite(downStill, pcx, pcy, 2*sideWidth+midWidth, 2*sideHeight+midHeight);
//Positions
c.setX(pcx);
c.setY(pcy);
//The time to hit the character
maxToHitTime = toHitTime = pmaxToHitTime;
//Increase the number of allies counter
if(!isEnemy){
noOfAllies++;
}else{
noOfEnemies++;
}
//Images for the UI and their coordinates
healthyImage = phealthyImage; hurtImage = phurtImage;
healthyImagex = phealthyImagex; healthyImagey = phealthyImagey;
hurtImagex = phurtImagex; hurtImagey = phurtImagey;
//Images for the ability icons
abilityImage1 = pabilityImage1; abilityImage2 = pabilityImage2;
}
void resetBounds(){
boundsMiddle.setLocation(c.getX()+boundsX,c.getY()+boundsY);
boundsUpLeft.setLocation(boundsMiddle.x - boundsUpLeft.width, boundsMiddle.y - boundsUpLeft.height);
boundsUp.setLocation(boundsMiddle.x, boundsUpLeft.y);
boundsUpRight.setLocation(boundsMiddle.x + boundsMiddle.width, boundsUp.y);
boundsLeft.setLocation(boundsMiddle.x - boundsLeft.width, boundsMiddle.y);
boundsRight.setLocation(boundsMiddle.x + boundsMiddle.width, boundsMiddle.y);
boundsDownLeft.setLocation(boundsMiddle.x - boundsDownLeft.width,boundsMiddle.y + boundsMiddle.height);
boundsDown.setLocation(boundsMiddle.x, boundsMiddle.y + boundsMiddle.height);
boundsDownRight.setLocation(boundsMiddle.x + boundsMiddle.width, boundsMiddle.y + boundsMiddle.height);
}
//Changing the normal speed
void setSpeed(int newSpeed){
speed = newSpeed;
//Used for moving diagonally
double move = Math.round(Math.sqrt((Math.pow(speed,2)/2)));
if((int)move+0.5>move){
mmove = ((int)move);
}else{
mmove = ((int)move+1);
}
}
int getSpeed(){
return speed;
}
//Changing the speed
void setBaseSpeed(int newSpeed){
baseSpeed = speed = newSpeed;
//Used for moving diagonally
double move = Math.round(Math.sqrt((Math.pow(speed,2)/2)));
if((int)move+0.5>move){
mmove = ((int)move);
}else{
mmove = ((int)move+1);
}
}
//Getting the value of the speed
int getBaseSpeed(){
return baseSpeed;
}
//Set the health
void setHP(int health){
maxHP = hp = health;
}
void moveRandomly(ArrayList<Character> other, int widthLimit, int heightLimit){
if(Timer.time - moveTimer>2500){
movingAroundChar = false;
movingTowardsChar = false;
movingAwayFromChar = false;
moveTimer = Timer.time;
//The movement of the character correspeonds to how 'angry' he is from being hit and not being able to hit back
//and how much hit points he has
r = (int)(1+Math.random()*(100 * ((toHitTime * 1.00f)/(maxToHitTime * 1.00f) + (hp * 1.00f)/(maxHP * 1.00f))/2));
//Randomly
if(r>=1 && r<=20){
movingTowardsChar = true;
}else if(r>=21 && r<=40){
movingAwayFromChar = true;
}else if(r>=50 && r<=60){
movingAroundChar = true;
}else if(r>=61 && r<=84){
stop(widthLimit, heightLimit);
}else if(r>=85 && r<=86){
moveRight(other);
}else if(r>=87 && r<=88){
moveUpLeft();
}else if(r>=89 && r<=90){
moveUpRight(widthLimit);
}else if(r>=91 && r<=92){
moveDownLeft(heightLimit);
}else if(r>=93 && r<=94){
moveDownRight(widthLimit, heightLimit);
}else if(r>=95 && r<=96){
moveLeft(other);
}else if(r>=97 && r<=98){
moveDown(other);
}else{
moveUp(other);
}
}
}
//Setting the speeds to go towards the character
void moveTowardsChar(Character other){
//Get the midpoint of both sprites
int otherX = other.midpointX();
int otherY = other.midpointY();
int myX = boundsMiddle.x+boundsMiddle.width/2;
int myY = boundsMiddle.y+boundsMiddle.height/2;
//Find the hypotenuse
double hyp = Math.sqrt(Math.pow(otherX-myX,2)+Math.pow(otherY-myY,2));
//divide the hypotenuse by the speed
double dSpeed = hyp/speed;
//divide the horizontal and vertical distances by the dSpeed
double xx = Math.round(((Math.abs(otherX-myX))/dSpeed));
double yy = Math.round((Math.abs(otherY-myY)/dSpeed));
//round to the nearest integer
if((int)xx+0.5>xx){
speedX = (int)xx;
}else{
speedX = (int)xx++;
}
if((int)yy+0.5>yy){
speedY = (int)yy;
}else{
speedY = (int)yy++;
}
//Set the speeds and images to face the character
if(otherX<=myX && otherY<=myY){
speedX = -(speedX);
speedY = -(speedY);
}else if(otherX<=myX && otherY>=myY){
speedX = -(speedX);
}else if(otherX>=myX && otherY<=myY){
speedY = -(speedY);
}
//move down right
}
//Move towards a certain point
void moveTowardsPoint(int px, int py){
//Get the midpoint of the sprite and keep name the same as other method for readability purposes
int otherX = px;
int otherY = py;
int myX = midpointX();
int myY = midpointY();
//Find the hypotenuse
double hyp = Math.sqrt(Math.pow(otherX-myX,2)+Math.pow(otherY-myY,2));
//divide the hypotenuse by the speed
double dSpeed = hyp/speed;
//divide the horizontal and vertical distances by the dSpeed
double xx = Math.round(((Math.abs(otherX-myX))/dSpeed));
double yy = Math.round((Math.abs(otherY-myY)/dSpeed));
//round to the nearest integer
if((int)xx+0.5>xx){
speedX = (int)xx;
}else{
speedX = (int)xx++;
}
if((int)yy+0.5>yy){
speedY = (int)yy;
}else{
speedY = (int)yy++;
}
//Set the speeds and images to face the character
if(otherX<=myX){
speedX = -(speedX);
}
if(otherY<=myY){
speedY = -(speedY);
}
//move down right
}
//set the speeds to move directl away from the character
void moveAwayFromChar(Character other){
//Get the midpoint of both sprites
int otherX = other.boundsMiddle.x + other.boundsMiddle.width/2;
int otherY = other.boundsMiddle.y + other.boundsMiddle.height/2;
int myX = boundsMiddle.x+boundsMiddle.width/2;
int myY = boundsMiddle.y+boundsMiddle.height/2;
//Find the hypotenuse
double hyp = Math.sqrt(Math.pow(otherX-myX,2)+Math.pow(otherY-myY,2));
//divide the hypotenuse by the speed
double dSpeed = hyp/speed;
//divide the horizontal and vertical distances by the dSpeed
double xx = Math.round(((Math.abs(otherX-myX))/dSpeed));
double yy = Math.round((Math.abs(otherY-myY)/dSpeed));
//round to the nearest integer
if((int)xx+0.5>xx){
speedX = (int)xx;
}else{
speedX = (int)xx++;
}
if((int)yy+0.5>yy){
speedY = (int)yy;
}else{
speedY = (int)yy++;
}
//Set the speeds and images to face away from the character
if(otherX<=myX && otherY>=myY){
speedY = -(speedY);
}else if(otherX>=myX && otherY<=myY){
speedX = -(speedX);
}else if(otherX>=myX && otherY>=myY){
speedX = -(speedX);
speedY = -(speedY);
}
//move down right
}
void dodgeBulletChar(Character other){
//Double the speed
speed*=2;
//Get the midpoint of both sprites
int otherX = other.midpointX();
int otherY = other.midpointY();
int myX = boundsMiddle.x+boundsMiddle.width/2;
int myY = boundsMiddle.y+boundsMiddle.height/2;
//Find the hypotenuse
double hyp = Math.sqrt(Math.pow(otherX-myX,2)+Math.pow(otherY-myY,2));
//divide the hypotenuse by the speed
double dSpeed = hyp/speed;
//divide the horizontal and vertical distances by the dSpeed
double xx = Math.round(((Math.abs(otherX-myX))/dSpeed));
double yy = Math.round((Math.abs(otherY-myY)/dSpeed));
//round to the nearest integer
if((int)xx+0.5>xx){
speedX = (int)xx;
}else{
speedX = (int)xx++;
}
if((int)yy+0.5>yy){
speedY = (int)yy;
}else{
speedY = (int)yy++;
}
//Set the speeds and images to face the character
if(otherX<=myX && otherY<=myY){
speedY = -(speedY);
}else if(otherX>=myX && otherY<=myY){
speedX = -(speedX);
speedY = -(speedY);
}else if(otherX>=myX && otherY>=myY){
speedX = -(speedX);
}
//move down right
}
//Get the closest enemy character from current position
int getClosestChar(ArrayList<Character> other){
int selectedChar = -1;
double myHyp = Double.POSITIVE_INFINITY;
for(int i = 0; i<other.size();i++){
if((isEnemy && other.get(i).isEnemy == false || !isEnemy && other.get(i).isEnemy == true) && other.get(i).hp >0){
//Get the midpoint of both sprites
int otherX = other.get(i).midpointX();
int otherY = other.get(i).midpointY();
int myX = boundsMiddle.x+boundsMiddle.width/2;
int myY = boundsMiddle.y+boundsMiddle.height/2;
//Find the hypotenuse
double hyp = Math.sqrt(Math.pow(otherX-myX,2)+Math.pow(otherY-myY,2));
if(hyp < myHyp){
myHyp = hyp;
selectedChar = i;
}
}
}
return selectedChar;
}
//Get the furthest enemy character from current position
int getFurthestChar(ArrayList<Character> other){
int selectedChar = -1;
double myHyp = Double.NEGATIVE_INFINITY;
for(int i = 0; i<other.size();i++){
if((isEnemy && other.get(i).isEnemy == false || !isEnemy && other.get(i).isEnemy == true) && other.get(i).hp >0){
//Get the midpoint of both sprites
int otherX = other.get(i).midpointX();
int otherY = other.get(i).midpointY();
int myX = boundsMiddle.x+boundsMiddle.width/2;
int myY = boundsMiddle.y+boundsMiddle.height/2;
//Find the hypotenuse
double hyp = Math.sqrt(Math.pow(otherX-myX,2)+Math.pow(otherY-myY,2));
if(hyp > myHyp){
myHyp = hyp;
selectedChar = i;
}
}
}
return selectedChar;
}
//Return the array number of the enemy with the least hp percentage
int getEnemyWithLeastHP(ArrayList<Character> other){
int selectedChar = -1;
double myHP = Double.NEGATIVE_INFINITY;
for(int i = 0; i<other.size();i++){
if(isEnemy && other.get(i).isEnemy == false || !isEnemy && other.get(i).isEnemy == true){
if(other.get(i).hp == 0){
return i;
}else{
double charHP = (double)other.get(i).maxHP/(double)other.get(i).hp;
if(charHP > myHP){
myHP = charHP;
selectedChar = i;
}
}
}
}
return selectedChar;
}
//Return the array number of the enemy with the least hp percentage
int getAllyWithLeastHP(ArrayList<Character> other){
int selectedChar = -1;
double myHP = Double.NEGATIVE_INFINITY;
for(int i = 0; i<other.size();i++){
if(!(isEnemy && other.get(i).isEnemy == false || !isEnemy && other.get(i).isEnemy == true)){
if(other.get(i).hp == 0){
return i;
}else{
double charHP = (double)other.get(i).maxHP/(double)other.get(i).hp;
if(charHP > myHP){
myHP = charHP;
selectedChar = i;
}
}
}
}
return selectedChar;
}
//Return the array number of the enemy with the most hp percentage
int getEnemyWithMostHP(ArrayList<Character> other){
int selectedChar = -1;
double myHP = Double.POSITIVE_INFINITY;
for(int i = 0; i<other.size();i++){
if(isEnemy && other.get(i).isEnemy == false || !isEnemy && other.get(i).isEnemy == true){
if(other.get(i).hp == other.get(i).maxHP){
return i;
}else{
double charHP = (double)other.get(i).maxHP/(double)other.get(i).hp;
if(charHP < myHP){
myHP = charHP;
selectedChar = i;
}
}
}
}
return selectedChar;
}
//Return the array number of the enemy with the most hp percentage
int getAllyWithMostHP(ArrayList<Character> other){
int selectedChar = -1;
double myHP = Double.POSITIVE_INFINITY;
for(int i = 0; i<other.size();i++){
if(!(isEnemy && other.get(i).isEnemy == false || !isEnemy && other.get(i).isEnemy == true)){
if(other.get(i).hp == other.get(i).maxHP){
return i;
}else{
double charHP = (double)other.get(i).maxHP/(double)other.get(i).hp;
if(charHP < myHP){
myHP = charHP;
selectedChar = i;
}
}
}
}
return selectedChar;
}
//get the protagonist number
int getProtagonist(ArrayList<Character> other){
int selectedChar = -1;
for(int i = 0; i<other.size();i++){
if(other.get(i).isProtagonist){
selectedChar = i;
}
}
return selectedChar;
}
//Move horizontal and vertical
//In case there are monsters
void moveUp(ArrayList<Character> other){
if(!attacking()){
speedX = 0;
speedY = -speed;
}
}
//In case there are no monsters
void moveUp(){
if(!attacking()){
speedX = 0;
speedY = -speed;
}
}
//In case there are monsters
void moveDown(ArrayList<Character> other){
if(!attacking()){
speedX = 0;
speedY = speed;
}
}
//In case there are no monsters
void moveDown(){
if(!attacking()){
speedX = 0;
speedY = speed;
}
}
//In case there are monsters
void moveLeft(ArrayList<Character> other){
if(!attacking()){
speedX = -speed;
speedY = 0;
}
}
//In case there are no monsters
void moveLeft(){
if(!attacking()){
speedX = -speed;
speedY = 0;
}
}
//In case there are monsters
void moveRight(ArrayList<Character> other){
if(!attacking()){
speedX = speed;
speedY = 0;
}
}
//In case there are no monsters
void moveRight(){
if(!attacking()){
speedX = speed;
speedY = 0;
}
}
//MoveDiagonally
void moveUpLeft(){
if(!attacking()){
if(boundsMiddle.x-speedX > 0 && boundsMiddle.y-speedY > 25){
speedY = -mmove;
speedX = -mmove;
}else if(boundsMiddle.x-speedX > 0){
moveLeft();
}else if(boundsMiddle.y-speedY > 25){
moveUp();
}
}
}
void moveUpRight(int widthLimit){
if(!attacking()){
if(boundsMiddle.x+boundsMiddle.getWidth()+speed < widthLimit && boundsMiddle.y-speedY > 25){
speedY = -mmove;
speedX = mmove;
}else if(boundsMiddle.x+boundsMiddle.getWidth()+speed < widthLimit){
moveRight();
}else if(boundsMiddle.y-speedY > 25){
moveUp();
}
}
}
void moveDownLeft(int heightLimit){
if(!attacking()){
if(boundsMiddle.x-speed > 3 && boundsMiddle.y+boundsMiddle.getHeight()+speed < heightLimit){
speedY = mmove;
speedX = -mmove;
}else if(boundsMiddle.x-speed > 3){
moveLeft();
}else if(boundsMiddle.y+boundsMiddle.getHeight()+speed < heightLimit){
moveDown();
}
}
}
void moveDownRight(int widthLimit, int heightLimit){
if(!attacking()){
if(boundsMiddle.x+boundsMiddle.getWidth()+speed < widthLimit && boundsMiddle.y+boundsMiddle.getHeight()+speed < heightLimit){
speedY = mmove;
speedX = mmove;
}else if(boundsMiddle.x+boundsMiddle.getWidth()+speed < widthLimit){
moveRight();
}else if(boundsMiddle.y+boundsMiddle.getHeight()+speed < heightLimit){
moveDown();
}
}
}
//stop moving
void stop(int widthLimit, int heightLimit){
if((!attacking() && !performingAbility1() && !performingAbility2()) || (boundsMiddle.x+boundsMiddle.getWidth()+speed > widthLimit || boundsMiddle.y+boundsMiddle.getHeight()+speed > heightLimit || boundsMiddle.x-speedX < 3 || boundsMiddle.y-speedY < 25)){
if( (speedX > 0 && speedY > 0) || (Timer.time - downTimer < 50 && Timer.time - rightTimer < 50) ){
setImage(downRightStill);
}else if( (speedX > 0 && speedY < 0) || (Timer.time - upTimer < 50 && Timer.time - rightTimer < 50) ){
setImage(upRightStill);
}else if( (speedX < 0 && speedY < 0) || (Timer.time - upTimer < 50 && Timer.time - leftTimer < 50) ){
setImage(upLeftStill);
}else if( (speedX < 0 && speedY > 0) || (Timer.time - downTimer < 50 && Timer.time - leftTimer < 50) ){
setImage(downLeftStill);
}else if(speedX == 0 && speedY > 0){
setImage(downStill);
}else if(speedX == 0 && speedY < 0){
setImage(upStill);
}else if(speedX > 0 && speedY == 0){
setImage(rightStill);
}else if(speedX < 0 && speedY == 0){
setImage(leftStill);
}else{
setImage(downStill);
}
//Speeds become 0
speedX = speedY = 0;
}
}
//Move in a circle around the character, even when he is moving, constantly takes values
void moveAroundChar(Character other){
//Get midpoints of sprites
int x1 = (int)other.midpointX();
int y1 = (int)(other.midpointY());
int x2 = (int)(midpointX());
int y2 = (int)(midpointY());
//Get distance between points
double w = Math.abs(x1-x2);
double h = Math.abs(y1-y2);
//Get the speed squared divided by the distances squared and square root all
double t = Math.sqrt(Math.pow(speed,2)/(Math.pow(w,2) + Math.pow(h,2)));
//The result is the multiplicaton of previous results
double horizontal = t*h;
double diagonal = t*w;
//Round moveH
int moveH;
if((int)horizontal+0.5>horizontal){
moveH = ((int)horizontal);
}else{
moveH = ((int)horizontal+1);
}
//Round moveD
int moveD;
if((int)diagonal+0.5>diagonal){
moveD = ((int)diagonal);
}else{
moveD = ((int)diagonal+1);
}
if(moveH > speed){
moveH = speed;
}
if(moveD > speed){
moveD = speed;
}
//Depending on the direction the other is relative to the character
//set the directions of speedX and speedY
if(x1<=x2 && y1<=y2){
speedX = -moveH;
speedY = moveD;
}else if(x1<=x2 && y1>=y2){
speedX = moveH;
speedY = moveD;
}else if(x1>=x2 && y1<=y2){
speedX = -moveH;
speedY = -moveD;
}else if(x1>=x2 && y1>=y2){
speedX = moveH;
speedY = -moveD;
}
}
//Move the character IF it does not go out of the screen and change it's image
void move(int widthLimit, int heightLimit){
if(speedX > 0 && speedY > 0){
direction = 3;
if(boundsMiddle.x+boundsMiddle.getWidth()+speedX < widthLimit && boundsMiddle.y+boundsMiddle.getHeight()+speedY < heightLimit){
setImage(downRightMove);
}else{
stop(widthLimit, heightLimit);
}
}else if(speedX > 0 && speedY < 0){
direction = 1;
if(boundsMiddle.x+boundsMiddle.getWidth()+speedX < widthLimit && boundsMiddle.y-speedY > 25){
setImage(upRightMove);
}else{
stop(widthLimit, heightLimit);
}
}else if(speedX < 0 && speedY < 0){
direction = 7;
if(boundsMiddle.x-speedX > 3 && boundsMiddle.y-speedY > 25){
setImage(upLeftMove);
}else{
stop(widthLimit, heightLimit);
}
}else if(speedX < 0 && speedY > 0){
direction = 5;
if(boundsMiddle.x-speedX > 3 && boundsMiddle.y+boundsMiddle.getHeight()+speedY < heightLimit){
setImage(downLeftMove);
}else{
stop(widthLimit, heightLimit);
}
}else if(speedX == 0 && speedY > 0){
direction = 4;
if(boundsMiddle.y+boundsMiddle.getHeight()+speedY < heightLimit){
setImage(downMove);
}else{
stop(widthLimit, heightLimit);
}
}else if(speedX == 0 && speedY < 0){
direction = 0;
if(boundsMiddle.y-speedY > 25){
setImage(upMove);
}else{
stop(widthLimit, heightLimit);
}
}else if(speedX > 0 && speedY == 0){
direction = 2;
if(boundsMiddle.x+boundsMiddle.getWidth()+speedX < widthLimit){
setImage(rightMove);
}else{
stop(widthLimit, heightLimit);
}
}else if(speedX < 0 && speedY == 0){
direction = 6;
if(boundsMiddle.x-speedX > 3){
setImage(leftMove);
}else{
stop(widthLimit, heightLimit);
}
}
//Speeds are optimized to be +ve, -ve and 0 for this reason
c.moveRight(speedX);
c.moveDown(speedY);
}
//Method to check if the charactersis moving in a special way and move if he
void moveSpecial(ArrayList<Character> other, int widthLimit, int heightLimit){
checkForAttack(other);
if(!attacking() && speedUpAfterAttack == true){
setSpeed(getBaseSpeed());
speedUpAfterAttack = false;
}
if(getClosestChar(other) != -1){
//Move towars the closest character
if(movingTowardsChar == true ){
moveTowardsChar(other.get(getClosestChar(other)));
}
//Move away from the closest character
if(movingAwayFromChar == true){
moveAwayFromChar(other.get(getClosestChar(other)));
}
//Move around the closest character
if(movingAroundChar == true){
moveAroundChar(other.get(getClosestChar(other)));
}
}
}
//Shoot to 2 points
void shootBullet(int x, int y){
bullet.add(new Bullet(bulletImage, midpointX(), midpointY(), bulletRadius, myBulletSpeed, x, y));
}
void shootBullet(int x, int y, int radius){
bullet.add(new Bullet(bulletImage, midpointX(), midpointY(), radius, myBulletSpeed, x, y));
}
//Set a specific bullet's directions towards a particular point
void setBulletDirectionsToward(int x, int y, int bulletNo){
//The length between the points is calculated
double hyp = Math.sqrt(Math.pow(x-bullet.get(bulletNo).midpointX,2)+Math.pow(y-bullet.get(bulletNo).midpointY,2));
//The hyp is divided by the bullet speed and the answer is stored in dSpeed
double dSpeed = hyp/myBulletSpeed;
//This is then used to divide the length between vertical and horizontal distances by it
//to determine how many points the bullet must travel in each frame
double xx = (x-bullet.get(bulletNo).midpointX)/dSpeed;
double yy = (y-bullet.get(bulletNo).midpointY)/dSpeed;
//The speed is then rounded to the nearest integer. This is unfortunate as the angles may not be perfect
//and may miss if the target is very thin.
if(xx<0){
xx = -xx;
}
if(yy<0){
yy = -yy;
}
int x2;
int y2;
if((int)xx+0.5>xx){
x2 = (int)xx;
}else{
x2 = (int)xx++;
}
if((int)yy+0.5>yy){
y2 = (int)yy;
}else{
y2 = (int)yy++;
}
//The direction of the bullet is Down and Right, therefore we ened to set the variables to negative
//if they need to go Up or Left.
if(y<=bullet.get(bullet.size()-1).midpointY){
bullet.get(bulletNo).bulletSpeedY= -y2;
}else{
bullet.get(bulletNo).bulletSpeedY= y2;
}
if(x<=bullet.get(bullet.size()-1).midpointX){
bullet.get(bulletNo).bulletSpeedX= -x2;
}else{
bullet.get(bulletNo).bulletSpeedX= x2;
}
}
//Set a specific bullet's directions away from a particular point
void setBulletDirectionsAwayFrom(int x, int y, int bulletNo){
//The length between the points is calculated
double hyp = Math.sqrt(Math.pow(x-bullet.get(bulletNo).midpointX,2)+Math.pow(y-bullet.get(bulletNo).midpointY,2));
//The hyp is divided by the bullet speed and the answer is stored in dSpeed
double dSpeed = hyp/bullet.get(bulletNo).bulletSpeed;
//This is then used to divide the length between vertical and horizontal distances by it
//to determine how many points the bullet must travel in each frame
double xx = (x-bullet.get(bulletNo).midpointX)/dSpeed;
double yy = (y-bullet.get(bulletNo).midpointY)/dSpeed;
//The speed is then rounded to the nearest integer. This is unfortunate as the angles may not be perfect
//and may miss if the target is very thin.
if(xx<0){
xx = -xx;
}
if(yy<0){
yy = -yy;
}
int x2;
int y2;
if((int)xx+0.5>xx){
x2 = (int)xx;
}else{
x2 = (int)xx++;
}
if((int)yy+0.5>yy){
y2 = (int)yy;
}else{
y2 = (int)yy++;
}
//The direction of the bullet is Down and Right, therefore we ened to set the variables to negative
//if they need to go Up or Left.
if(y<=bullet.get(bullet.size()-1).midpointY){
bullet.get(bulletNo).bulletSpeedY= y2;
}else{
bullet.get(bulletNo).bulletSpeedY= -y2;
}
if(x<=bullet.get(bullet.size()-1).midpointX){
bullet.get(bulletNo).bulletSpeedX= x2;
}else{
bullet.get(bulletNo).bulletSpeedX= -x2;
}
}
//Add a new bullet and shoot it towards a specific character
void shootStraightTowardsChar(Character other){
//get the character midpoints
int otherX = other.midpointX();
int otherY = other.midpointY();
//Shoot the bullet at character midpoints
shootBullet(otherX, otherY, bulletRadius);
}
//Setting the speeds to go towards the character
void moveBulletTowardsChar(Character other2){
for(int i = 0; i < bullet.size(); i++){
//Get the midpoint of both sprites
int otherX = other2.boundsMiddle.x + other2.boundsMiddle.width/2;
int otherY = other2.boundsMiddle.y + other2.boundsMiddle.height/2;
//Find the hypotenuse
double hyp = Math.sqrt(Math.pow(otherX-bullet.get(i).midpointX,2)+Math.pow(otherY-bullet.get(i).midpointY,2));
//divide the hypotenuse by the speed
double dSpeed = hyp/myBulletSpeed;
//divide the horizontal and vertical distances by the dSpeed
double xx = Math.round(((Math.abs(otherX-bullet.get(i).midpointX))/dSpeed));
double yy = Math.round((Math.abs(otherY-bullet.get(i).midpointY)/dSpeed));
//round to the nearest integer
if((int)xx+0.5>xx){
bullet.get(i).bulletSpeedX = (int)xx;
}else{
bullet.get(i).bulletSpeedX = (int)xx;
}
if((int)yy+0.5>yy){
bullet.get(i).bulletSpeedY = (int)yy;
}else{
bullet.get(i).bulletSpeedY = (int)yy;
}
//Set the speeds and images to face the character
if(otherX<=bullet.get(i).midpointX && otherY<=bullet.get(i).midpointY){
bullet.get(i).bulletSpeedX = -bullet.get(i).bulletSpeedX;
bullet.get(i).bulletSpeedY = -bullet.get(i).bulletSpeedY;
}else if(otherX<=bullet.get(i).midpointX && otherY>=bullet.get(i).midpointY){
bullet.get(i).bulletSpeedX = -bullet.get(i).bulletSpeedX;
}else if(otherX>=bullet.get(i).midpointX && otherY<=bullet.get(i).midpointY){
bullet.get(i).bulletSpeedY = -bullet.get(i).bulletSpeedY;
}
//move down right
}
}
//Move the bullet and stop when it comes into contact with a wall
void moveBullet(int widthLimit, int heightLimit, ArrayList<Character> other){
for(int i = 0; i<bullet.size();i++){
bullet.get(i).moveRight(bullet.get(i).bulletSpeedX);
bullet.get(i).moveDown(bullet.get(i).bulletSpeedY);
for(int x = 0;x < other.size();x++){
if(bullet.get(i).hasCollidedWith(other.get(x).boundsMiddle) && (!isEnemy && other.get(x).isEnemy || isEnemy && other.get(x).isEnemy == false) && other.get(x).hp > 0){