-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hero.java
719 lines (702 loc) · 16.1 KB
/
Hero.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
import javax.swing.*;
import java.awt.*;
class Hero extends Item{
public static final int NONE = 0, BORDER = 1, BUMPER = 2, FIGHT = 3;
public double dy,dx;
private double SPEED = 3;
private GWar world;
int jumps=0;
private int sparkindex=0;
private int sparkcounter=0;
private int tiedcount=0;
public double mass=1;
private boolean lacceleration;
private boolean racceleration;
private boolean onbumper = false;
private final static boolean DEBUG=false;
private double[] xspark = new double[5];
private double[] yspark = new double[5];
private int xflash;
private int yflash;
private boolean flashleft;
private int flashcount = 0;
private int lastPositionValue=-500, actionType, actionValue=-1;
public Hero(double x, double y, int r,GWar tp, java.awt.Color c,double mass){
super(x,y,r,c);
dy=0;
dx=0;
world=tp;
this.mass =mass;
SPEED=(3/mass);
actionType = NONE;
actionValue = -1;
}
private void addsparkle(){
for (int k = xspark.length-1;k>0;k--){
xspark[k]=xspark[k-1];
yspark[k]=yspark[k-1];
}
xspark[0]=x;
yspark[0]=y;
}
public void reset(){
actionType = NONE;
actionValue = -1;
lastPositionValue = -500;
}
public void update(){
flashcount--;
onbumper=onBumper();
if (tiedcount>0)
tiedcount --;
sparkcounter++;
if (sparkcounter>=3){
sparkcounter=0;
addsparkle();
}
if (onbumper){
//System.out.println("onbumper");
if (racceleration==lacceleration){
if(tiedcount==0)
dx=dx*.975;//friction?
}
else{
}
if(dy>=0)
jumps = 0;
if (Math.abs(dx) < .05)
dx=0;
}
if(tiedcount==0){
if(lacceleration&&dx>-SPEED)
dx -= 1;
if(racceleration&&dx<SPEED)
dx += 1;
}
if (checkVBumper()){
dy = Math.abs(dy);
}
else if (checkHBumper()){
dx = -dx;
}
lX = x;
lY = y;
y += dy;
x += dx;
onbumper=onBumper();
}
public double nearestBorder(Hero h){
double wL = h.x,wR = GWar.WIDTH-h.x;
double hT = h.y,hB = GWar.HEIGHT-h.y;
double min=wL;
if(wR < min)
{
min = wR;
}
if(hT < min)
{
min = hT;
}
if(hB < min)
{
min = hB;
}
return min;
}
public int getBorderValue(Hero h){
double minDist = nearestBorder(h);
if(minDist<40)
{
return -6;
}
else if(minDist<80)
{
return -3;
}
else if(minDist < 120)
{
return 3;
}
else
{
return 6;
}
}
public int getBumperValue(Hero h){
if(h.onBumper())
{
return 6;
}
else if(h.overBumper())
{
return 3;
}
else
{
return -6;
}
}
public int getPositionValue(Hero h){
int val = 0;
val+=getBorderValue(h);
val+=getBumperValue(h);
return val;
}
public int getProblem(Hero h){
int bord = getBorderValue(h);
int bump = getBumperValue(h);
if(bump<0)
{
return BUMPER;
}
if(bord<0)
{
return BORDER;
}
return NONE;
}
public void fixBorder(){
actionType = BORDER;
actionValue = -1;
}
public void fixBumper(){
actionType = BUMPER;
actionValue = -1;
}
public void setTarget(){
actionType = FIGHT;
actionValue = findNickTarget(this);
}
public void fixBumperProblem(){
if(actionValue == -1)
{
actionValue = nearestTargetBumper(0,0,this,false);
}
else
{
try
{
Bumper tar = world.bump[actionValue];
goToTarget(tar);
Bumper on = onWhatBumper();
if(on!=null && on.x == tar.x && on.y == tar.y)
{
stop();
actionType = NONE;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
}
}
}
public void fixBorderProblem(){
if(actionValue == -1)
{
actionValue = nearestTargetBumper(GWar.WIDTH/2,GWar.HEIGHT/2,this,true);
}
else
{
try
{
Bumper tar = world.bump[actionValue];
goToTarget(tar);
Bumper on = onWhatBumper();
if(on!=null && on.x == tar.x && on.y == tar.y)
{
stop();
actionType = NONE;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Wrong index!");
}
}
}
public void goToTarget(Bumper b)
{
double cX = b.x + b.width/2, dif = cX-x;
if(dif > 3)
{
racceleration = true;
lacceleration = false;
}
else if(dif < -3)
{
lacceleration = true;
racceleration = false;
}
if(b.y < y && dy >= 0)
{
jump();
}
}
public void checkVelocityProblem()
{
if(dx>9)
{
if(onWhatBumper() instanceof SpeedBumper)
{
jump();
}
attackLeft();
}
else if(dx>6&&GWar.WIDTH-x<50)
{
attackLeft();
}
if(dx<-9)
{
if(onWhatBumper() instanceof SpeedBumper)
{
jump();
}
attackRight();
}
else if(dx<-6&&x<50)
{
attackRight();
}
if(dy<-8&&overBumper()&&y<50)
{
fastFall();
}
}
public void nickAI(){
int position = getPositionValue(this);
int problem = NONE;
checkVelocityProblem();
if(actionType == NONE)
{
problem = getProblem(this);
}
else if(actionType == BORDER)
{
fixBorderProblem();
}
else if(actionType == BUMPER)
{
fixBumperProblem();
}
if(problem == BORDER)
{
fixBorder();
}
else if(problem == BUMPER)
{
fixBumper();
}
else if(problem == NONE)
{
// setTarget();
}
/*if(position < lastPositionValue)
{
reverseLastAction();
}*/
lastPositionValue = position;
}
public int findNickTarget(Hero h){
int mVal = 9999,value,distValue=0, lifeValue, totValue;
int min=-1;
Hero t;
for(int k =0; k < world.p.length;k++)
{
t = world.p[k];
distValue = (int)(distance(h,t.x,t.y)/50);
value = getPositionValue(t);
lifeValue = world.TOTALLIVES-world.lives[k];
totValue = distValue+value+lifeValue;
/* System.out.println("---- " + k+" ----");
System.out.println("Dif: "+(b.y-h.y));
System.out.println(dist1);
System.out.println(dist2);
System.out.println(distTot);*/
if(totValue<mVal)
{
mVal = totValue;
min = k;
}
}
return min;
}
public double getMaxXDist(double tx, double ty, Hero h){
int lJumps = 2-jumps;
if(lJumps==2)
{
return 170;
}
else if(lJumps==1)
{
return 90;
}
else if(lJumps==0)
{
return 50;
}
return 0;
}
public int nearestTargetBumper(double tx,double ty, Hero h, boolean both){
double mDist = 999999999,dist1,dist2=0,distTot;
int min=-1;
Bumper b;
for(int k =0; k < world.bump.length;k++)
{
b = world.bump[k];
dist1 = distance(h,b.x+b.width/2,b.y);
if(b.y - h.y < -(2-jumps)*55)
{
dist1*=5;
}
if(Math.abs(b.x+b.width/2-h.x)>getMaxXDist(b.x,b.y,h))
{
dist1*=5;
}
if(both)
{
dist2 = distance(tx,ty,b.x+b.width/2,b.y)*1.5;
}
distTot = dist1+dist2;
/* System.out.println("---- " + k+" ----");
System.out.println("Dif: "+(b.y-h.y));
System.out.println(dist1);
System.out.println(dist2);
System.out.println(distTot);*/
if(distTot<mDist)
{
mDist = distTot;
min = k;
}
}
return min;
}
public void ai(){
if(!overBumper()&&!onBumper()){
if(dy>=0)
jump();
Bumper goal = world.bump[0];
double tdistance=distance(this, goal.getX(),goal.getY());
for (int k = 0; k <world.bump.length;k++){
if(distance(this, world.bump[k].getX(),world.bump[k].getY())<tdistance){
goal=world.bump[k];
tdistance=distance(this, world.bump[k].getX(),world.bump[k].getY());
}
if(distance(this, world.bump[k].getX() + world.bump[k].getWidth(),world.bump[k].getY())<tdistance){
goal=world.bump[k];
tdistance=distance(this, world.bump[k].getX()+world.bump[k].getWidth(),world.bump[k].getY());
}
}
if(goal.getX()>x){
racceleration=true;
lacceleration=false;
if(goal.getY()>y)
attackRight();
}
else{
racceleration=false;
lacceleration=true;
if(goal.getY()>y)
attackLeft();
}
}
else{
Hero target = findTarget();
if (target ==null)
return;
if((Math.abs(target.x-x)<r)&&(y<target.y)){
fastFall();
}
if (target.x<x){
lacceleration=true;
racceleration=false;
}
else{
lacceleration=false;
racceleration=true;
}
if(target.y<y&&dy>=0)
jump();
if(Math.abs(target.y-y)<5)
if (target.x<x)
attackLeft();
else
attackRight();
}
}
public void stop(){
dx=0;
racceleration = false;
lacceleration = false;
}
private Hero findTarget(){
Hero target = null;
double distance = -1;
for (int k = 0; k< world.p.length; k++){
if (target==null){
target=world.p[k];
if(target==null)
continue;
distance = distance(this,target);
if (target==this)
target=null;
}
Hero ptarget = world.p[k];
if(ptarget!=null){
double pdist = distance(this,ptarget);
if(ptarget!=this&&pdist<distance){
distance=pdist;
target=ptarget;
}
}
}
return target;
}
public void moveLeft(){
lacceleration=true;
}
public void attackLeft(){
if(tiedcount==0){
dx-=2*SPEED;
tiedcount = 15;
xflash=(int)x;
yflash=(int)y;
flashcount=5;
flashleft=true;
}
}
public void unMoveRight(){
racceleration=false;
}
public void unMoveLeft(){
lacceleration=false;
}
public void moveRight(){
racceleration=true;
}
public void attackRight(){
if(tiedcount==0){
dx+=2*SPEED;
tiedcount = 15;
flashleft=false;
xflash=(int)x;
yflash=(int)y;
flashcount=5;
}
}
public void jump(){
if(tiedcount==0)
if (jumps < 2){
dy = -2.5*SPEED;
jumps++;
if(!onbumper)
dx=0;
}
}
public void fastFall(){
if(tiedcount==0){
if (onBumper()){
if (onWhatBumper().passthrough){
y++;
dy=10;
}
}
else if ((dy <10)&&(jumps<2)){
dy=10;
jumps++;
dx=0;
}
}
}
public void gravity(){
if(!onBumper())
dy+=.5;
}
public boolean overBumper(){
boolean temp = false;
for(int k = 0;k<world.bump.length;k++){
if(world.bump[k].overBumper(this)){
temp = true;
break;
}
}
return temp;
}
public boolean checkHBumper()
{
if (onBumper())
return false;
else
return checkBumper(dx,0)!=-1;
}
public boolean checkVBumper()
{
int k = checkBumper(0,dy);
if (k!=-1){
if (y<world.bump[k].y){
y = world.bump[k].y-r;
dy = 0;
return false;
}
return true;
}
return false;
}
public boolean onBumper(){
for(int k=0;k<world.bump.length;k++)
if (world.bump[k].onBumper(this)){
return true;
}
return false;
}
public Bumper onWhatBumper(){
for(int k=0;k<world.bump.length;k++)
if (world.bump[k].onBumper(this)){
return world.bump[k];
}
return null;
}
public int checkBumper(double pdx, double pdy){
for(int k=0;k<world.bump.length;k++)
if (checkBumper(world.bump[k],pdx,pdy))
return k;
return -1;
}
public boolean checkBumper(Bumper b,double pdx, double pdy){
if ((b.overBumper(x+pdx,y,r)||b.onBumper(this))&&(y+r+pdy>b.y)){
y= b.y-r;
dy=0;
return false;
}
else if (b.check(x,y,pdx,pdy,r)){
return true;
}
return false;
}
public boolean onScreen(int width, int height)
{
if(x-r>0&&x+r<width&&y-r>0&&y+r<height)
return true;
return false;
}
public void checkPlayer(Hero p){
if (intersects(p)){
/*if(y<p.y){
dy = Math.min(dy-3,-3);
}
else if(p.y<y){
dy = Math.max(dy+3,3);
}
if (p.x<x){
dx +=5;
dy -=1;
}
else if (x<p.x){
dx -=5;
dy -=1;
}
*/
double angle = Math.atan2(p.y-y,p.x-x)+Math.PI;
//dx+=((1/mass)*8*(p.mass)/distance(this,p))*Math.cos(angle);
//dy+=((1/mass)*8*(p.mass)/distance(this,p))*Math.sin(angle);
//double dxAdd
double phi = angle;
//double term = Math.PI/180;
double v1i = Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
double v2i = Math.sqrt(Math.pow(p.dx,2)+Math.pow(p.dy,2));
double ang1 =Math.atan2(dy,dx);
double ang2 = Math.atan2(p.dy,p.dx);
double v1xr = v1i*Math.cos((ang1-phi));
double v1yr = v1i*Math.sin((ang1-phi));
double v2xr = v2i*Math.cos((ang2-phi));
double v2yr = v2i*Math.sin((ang2-phi));
double v1fxr = ((mass-p.mass)*v1xr+(2*p.mass)*v2xr)/(mass+p.mass);
double v2fxr = ((2*mass)*v1xr+(p.mass-mass)*v2xr)/(mass+p.mass);
double v1fyr = v1yr;
double v2fyr = v2yr;
double v1fx = Math.cos(phi)*v1fxr+Math.cos(phi+Math.PI/2)*v1fyr;
double v1fy = Math.sin(phi)*v1fxr+Math.sin(phi+Math.PI/2)*v1fyr;
double v2fx = Math.cos(phi)*v2fxr+Math.cos(phi+Math.PI/2)*v2fyr;
double v2fy = Math.sin(phi)*v2fxr+Math.sin(phi+Math.PI/2)*v2fyr;
dx = v1fx;
dy = v1fy;
p.dx = v2fx;
p.dy = v2fy;
//System.out.println("DX: "+dx);
//System.out.println("DY: "+dy);
//System.out.println("P DX: "+p.dx);
//System.out.println("P DY: "+p.dy);
double newx = (p.x+x)/2+Math.cos(angle)*(r+1);
double newy = (p.y+y)/2+Math.sin(angle)*(r+1);
p.x = (p.x+x)/2+Math.cos(angle+Math.PI)*(p.r+1);
p.y = (p.y+y)/2+Math.sin(angle+Math.PI)*(p.r+1);
x=newx;
y=newy;
tiedcount+=2;
}
}
public static void main(String[] aahrghs){
GWar.main(aahrghs);
}
/*public void goOnScreen(int n)
{
if (x-r<=0)
{
x+=SPEED;
}
if (y-r<=0)
{
y+=SPEED;
}
if (y+r>=n)
{
dy = -.75*Math.abs(dy);
}
if (x+r>=n)
{
x-=SPEED;
}
}*/
public static double distance(Item i1, Item i2){
return Math.sqrt(Math.pow(i1.x-i2.x,2)+Math.pow(i1.y-i2.y,2));
}
public static double distance(Item i1, double x2, double y2){
return Math.sqrt(Math.pow(i1.x-x2,2)+Math.pow(i1.y-y2,2));
}
public static double distance(double x1, double y1, double x2, double y2){
return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
}
public void draw(java.awt.Graphics g){
super.draw(g,world.xScale,world.yScale);
if (flashcount>0){
if(world.sparkles){
Color[] colors = {Color.white,Color.blue,Color.yellow};
int ranColor,ranX,ranY;
for(int a=0;a<10;a++){
ranColor = (int)(Math.random()*colors.length);
ranX = (int)(Math.random()*(this.r*2+10))-5;
ranY = (int)(Math.random()*(this.r*2+10))-5;
g.setColor(colors[ranColor]);
g.fillOval((int)((this.lX-this.r+ranX-1)*world.xScale),(int)((this.lY-this.r+ranY-1)*world.yScale),(int)(2*world.xScale),(int)(2*world.yScale));
}
}
else{
g.setColor(new java.awt.Color(255,75,75));
if(!flashleft)
g.fillPolygon(new int[]{(int)(xflash*world.xScale),(int)((xflash-r)*world.xScale),(int)((xflash-(r/2))*world.xScale),(int)((xflash-r)*world.xScale)},new int[]{(int)(yflash*world.yScale),(int)((yflash+r)*world.yScale),(int)(yflash*world.yScale),(int)((yflash-r)*world.yScale)},4);
else
g.fillPolygon(new int[]{(int)(xflash*world.xScale),(int)((xflash+r)*world.xScale),(int)((xflash+(r/2))*world.xScale),(int)((xflash+r)*world.xScale)},new int[]{(int)(yflash*world.yScale),(int)((yflash+r)*world.yScale),(int)(yflash*world.yScale),(int)((yflash-r)*world.yScale)},4);
}
}
if(DEBUG==true){
g.setColor(new java.awt.Color(255,255,0));
g.drawLine((int)(x*world.xScale),(int)(y*world.yScale),(int)((x+(dx*3))*world.xScale),(int)((y+(dy*3))*world.yScale));
}
}
/*public void draw(java.awt.Graphics g){
super.draw(g);
g.setColor(java.awt.Color.WHITE);
for (int k = 0; k < xspark.length;k++)
g.fillRect((int)xspark[k]-3,(int)yspark[k]-3,6,6);
}*/
}