forked from phu004/JavaRTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgunTurret.java
More file actions
796 lines (590 loc) · 23.3 KB
/
Copy pathgunTurret.java
File metadata and controls
796 lines (590 loc) · 23.3 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
package entity;
import java.awt.Rectangle;
import core.*;
import enemyAI.enemyCommander;
//the power plant model
public class gunTurret extends solidObject{
public static int maxHP = 250;
public int countDownToDeath = 16;
public vector tempVector = new vector(0,0,0);
public vector tempVector0 = new vector(0,0,0);
public vector tempVector1 = new vector(0,0,0);
public vector tempVector2 = new vector(0,0,0);
public vector tempVector3 = new vector(0,0,0);
public int [] tileIndex = new int[1];
public int[] tempInt;
public float[] tempFloat;
public vector shadowvertex0, tempshadowvertex0,shadowvertex1, tempshadowvertex1,shadowvertex2, tempshadowvertex2,shadowvertex3, tempshadowvertex3;
// a screen space boundary which is used to test if the harvester object is
// visible from camera point of view
public final static Rectangle visibleBoundary = new Rectangle(-70, -25,screen_width+140, screen_height+85);
// a screen space boundary which is used to test if the entire harvester
// object is within the screen
public final static Rectangle screenBoundary = new Rectangle(40, 40, screen_width-90,screen_height-80);
//screen space boundary which is used to test if the shadow of the object is within the screen
public final static Rectangle shadowBoundary = new Rectangle(0,0,screen_width, screen_height);
// a screen space boundary which is used to test if the vision polygon of
// the object is visible.
public final static Rectangle visionBoundary = new Rectangle(0, 0, 1400+(screen_width-768),1300+(screen_height-512));
public final static int visionW = 500 + (screen_width-768);
public final static int visionH = 650 + (screen_height-512);
//a bitmap representation of the vision of the power plant for enemy commander
public static boolean[] bitmapVisionForEnemy;
public static boolean[] bitmapVisionGainFromAttackingUnit;
//gunTurret never moves
public final static vector movenment = new vector(0,0,0);
//the oreintation of the turret
public int turretAngle;
//attack range
public final static float attackRange = 2f;
//the angle that the turret have rotated between current frame and previous frame
public int turretAngleDelta, accumulatedDelta;
public int turretTurnRate = 8;
public int myAttackCooldown= 24;
public int attackCoolDown;
public vector firingPosition;
//index of the tiles to check when the turret is in standby mode
public static int[] tileCheckList;
//once the turret starts attacking, it exposed itself to the enemy
public int exposedCountDown;
public baseInfo theBaseInfo;
public int attackAngle;
public int randomInt;
public gunTurret(float x, float y, float z, int teamNo){
//uncontrollable unit, but act as a big sized static collidable agent
type = 200;
myDamage = 15;
ID = globalUniqID++;
randomInt = gameData.getRandom();
if(teamNo == 0){
isRevealed = true;
theBaseInfo = mainThread.pc.theBaseInfo;
}else{
theBaseInfo = mainThread.ec.theBaseInfo;
}
theBaseInfo.numberOfGunTurret++;
currentHP = maxHP;
this.teamNo = teamNo;
currentCommand = StandBy;
if(teamNo == 0){
isRevealed = true;
}
if(tileCheckList == null){
tileCheckList = generateTileCheckList(8f);
}
if(bitmapVisionForEnemy == null){
bitmapVisionForEnemy = createBitmapVision(6);
bitmapVisionGainFromAttackingUnit = createBitmapVision(2);
}
//create 2D boundary
boundary2D = new Rect((int)(x*64) - 8, (int)(z*64) + 8, 16, 16);
boundary2D.owner = this;
int centerX = (int)(x*64);
int centerY = (int)(z*64);
tileIndex[0] = centerX/16 + (127 - centerY/16)*128;
mainThread.gridMap.tiles[tileIndex[0]][0] = this;
mainThread.gridMap.tiles[tileIndex[0]][1] = this;
mainThread.gridMap.tiles[tileIndex[0]][2] = this;
mainThread.gridMap.tiles[tileIndex[0]][3] = this;
mainThread.gridMap.tiles[tileIndex[0]][4] = this;
//init model
start = new vector(x,y,z);
iDirection = new vector(1f,0,0);
jDirection = new vector(0,1f,0);
kDirection = new vector(0,0,1f);
firingPosition = new vector(0,0,0);
//define centre of the model in world coordinate
start = new vector(x,y,z);
centre = start.myClone();
tempCentre = start.myClone();
shadowvertex0 =start.myClone();
shadowvertex0.add(-0.45f,-0.2f, -0.15f);
tempshadowvertex0 = new vector(0,0,0);
shadowvertex1 =start.myClone();
shadowvertex1.add(-0.45f,-0.2f, 0.2f);
tempshadowvertex1 = new vector(0,0,0);
shadowvertex2 =start.myClone();
shadowvertex2.add(0.2f,-0.2f, -0.15f);
tempshadowvertex2 = new vector(0,0,0);
shadowvertex3 =start.myClone();
shadowvertex3.add(0.2f,-0.2f, 0.2f);
tempshadowvertex3 = new vector(0,0,0);
turretAngle = (int)(360*Math.random());
makePolygons();
}
//create polygons
public void makePolygons(){
polygons = new polygon3D[46];
vector[] v;
//turret base
float l =0.07f;
float h = 0.07f;
v = new vector[]{put(-l,h, l), put(l,h, l), put(l,h, -l), put(-l,h, -l)};
polygons[0] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[12], 0.5f, 0.5f, 1);
v = new vector[]{put(-l,h, -l), put(l,h, -l), put(l+0.04,h - 0.15f, -l-0.04), put(-l-0.04,h - 0.15f, -l-0.04)};
polygons[1] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[12], 0.5f, 0.5f, 1);
v = new vector[]{put(-l-0.04,h - 0.15f, l+0.04), put(l+0.04,h - 0.15f, l+0.04), put(l,h, l), put(-l,h, l)};
polygons[2] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[12], 0.5f, 0.5f, 1);
v = new vector[]{put(l,h, -l), put(l,h, l), put(l+0.04,h - 0.15f, l+0.04), put(l+0.04,h - 0.15f, -l-0.04)};
polygons[3] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[12], 0.5f, 0.5f, 1);
v = new vector[]{put(-l-0.04,h - 0.15f, -l-0.04), put(-l-0.04,h - 0.15f, l+0.04), put(-l,h, l), put(-l,h, -l)};
polygons[4] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[12], 0.5f, 0.5f, 1);
for(int i = 0; i < 5; i++){
polygons[i].Ambient_I-=5;
polygons[i].findDiffuse();
polygons[i].parentObject = this;
}
//turret tower
iDirection.scale(0.82f);
kDirection.scale(0.75f);
iDirection.rotate_XZ(360-turretAngle);
jDirection.rotate_XZ(360-turretAngle);
kDirection.rotate_XZ(360-turretAngle);
h = 0.11f;
vector a1 = put(-0.035, h, 0.08);
vector a2 = put(0.035, h, 0.08);
vector a3 = put(0.06, h, 0.03);
vector a4 = put(0.06, h, -0.05);
vector a5 = put(0.04, h, -0.07);
vector a6 = put(-0.04, h, -0.07);
vector a7 = put(-0.06, h, -0.05);
vector a8 = put(-0.06, h, 0.03);
int textureIndex = 66;
if(teamNo != 0)
textureIndex = 67;
v = new vector[]{a1, a2, a3, a4,a5, a6, a7, a8};
polygons[5] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[5].myClone(), mainThread.textures[textureIndex], 0.7f, 1f, 1);
iDirection.scale(1.2f);
kDirection.scale(1.2f);
vector b1 = put(-0.035, 0.07, 0.08);
vector b2 = put(0.035, 0.07, 0.08);
vector b3 = put(0.06, 0.07, 0.03);
vector b4 = put(0.06, 0.07, -0.05);
vector b5 = put(0.04, 0.07, -0.07);
vector b6 = put(-0.04, 0.07, -0.07);
vector b7 = put(-0.06, 0.07, -0.05);
vector b8 = put(-0.06, 0.07, 0.03);
v = new vector[]{a2.myClone(), a1.myClone(), b1.myClone(), b2.myClone()};
polygons[6] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[textureIndex], 0.5f, 0.1f, 1);
polygons[6].shadowBias = 20000;
v = new vector[]{a1.myClone(), a8.myClone(), b8.myClone(), b1.myClone()};
polygons[7] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[2].myClone(), mainThread.textures[textureIndex], 0.5f, 0.1f, 1);
v = new vector[]{a3.myClone(), a2.myClone(), b2.myClone(), b3.myClone()};
polygons[8] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[textureIndex], 0.5f, 0.1f, 1);
v = new vector[]{a4.myClone(), a3.myClone(), b3.myClone(), b4.myClone()};
polygons[9] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[textureIndex], 0.5f, 0.1f, 1);
v = new vector[]{a5.myClone(), a4.myClone(), b4.myClone(), b5.myClone()};
polygons[10] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[textureIndex], 0.5f, 0.1f, 1);
v = new vector[]{a6.myClone(), a5.myClone(), b5.myClone(), b6.myClone()};
polygons[11] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[textureIndex], 0.5f, 0.1f, 1);
v = new vector[]{a7.myClone(), a6.myClone(), b6.myClone(), b7.myClone()};
polygons[12] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[textureIndex], 0.5f, 0.1f, 1);
v = new vector[]{a8.myClone(), a7.myClone(), b7.myClone(), b8.myClone()};
polygons[13] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[textureIndex], 0.5f, 0.1f, 1);
double r1 = 0.007;
double r2 = 0.01;
double theta = Math.PI/16;
for(int i = 0; i < 32; i++){
v = new vector[]{put(r2*Math.cos(i*theta), r2*Math.sin(i*theta)+0.093, 0.05),
put(r2*Math.cos((i+1)*theta), r2*Math.sin((i+1)*theta)+0.093, 0.05),
put(r1*Math.cos((i+1)*theta), r1*Math.sin((i+1)*theta)+0.093, 0.17),
put(r1*Math.cos(i*theta), r1*Math.sin(i*theta)+0.093, 0.17)
};
polygons[14 +i] = new polygon3D(v, v[0].myClone(), v[1].myClone(), v[3].myClone(), mainThread.textures[33], 10,10,1);
polygons[14 +i].Ambient_I -=15;
polygons[14 +i].reflectance -=30;
polygons[14 +i].findDiffuse();
}
}
//update the model
public void update(){
//process emerging from ground animation
if(centre.y < -0.5f){
centre.y+=0.01f;
if(centre.y > -0.5){
for(int i = 0; i < polygons.length; i++){
polygons[i].origin.y+=0.0000005;
polygons[i].rightEnd.y+=0.0000005;
polygons[i].bottomEnd.y+=0.0000005;
for(int j = 0; j < polygons[i].vertex3D.length; j++){
polygons[i].vertex3D[j].y+=0.0000005;
}
}
shadowvertex0.y+=0.0000005;
shadowvertex1.y+=0.0000005;
shadowvertex2.y+=0.0000005;
shadowvertex3.y+=0.0000005;
centre.y = -0.5f;
}else{
for(int i = 0; i < polygons.length; i++){
polygons[i].origin.y+=0.01;
polygons[i].rightEnd.y+=0.01;
polygons[i].bottomEnd.y+=0.01;
for(int j = 0; j < polygons[i].vertex3D.length; j++){
polygons[i].vertex3D[j].y+=0.01;
}
}
shadowvertex0.y+=0.01;
shadowvertex1.y+=0.01;
shadowvertex2.y+=0.01;
shadowvertex3.y+=0.01;
}
//the building is invulnerable during emerging stage
currentHP = maxHP;
}
if(underAttackCountDown > 0)
underAttackCountDown--;
//check if power plant has been destroyed
if(currentHP <= 0){
countDownToDeath--;
if(countDownToDeath == 0){
//spawn an explosion when the tank is destroyed
float[] tempFloat = theAssetManager.explosionInfo[theAssetManager.explosionCount];
tempFloat[0] = centre.x;
tempFloat[1] = centre.y + 0.15f;
tempFloat[2] = centre.z;
tempFloat[3] = 2.5f;
tempFloat[4] = 1;
tempFloat[5] = 0;
tempFloat[6] = 7;
tempFloat[7] = this.height;
theAssetManager.explosionCount++;
theAssetManager.removeObject(this);
if(teamNo == 0)
mainThread.pc.theBaseInfo.numberOfGunTurret--;
else
mainThread.ec.theBaseInfo.numberOfGunTurret--;
//removeFromGridMap();
mainThread.gridMap.tiles[tileIndex[0]][0] = null;
mainThread.gridMap.tiles[tileIndex[0]][1] = null;
mainThread.gridMap.tiles[tileIndex[0]][2] = null;
mainThread.gridMap.tiles[tileIndex[0]][3] = null;
mainThread.gridMap.tiles[tileIndex[0]][4] = null;
if(attacker.teamNo != teamNo)
attacker.experience+=20;
return;
}else{
if(mainThread.gameFrame%2==0){
float[] tempFloat = theAssetManager.explosionInfo[theAssetManager.explosionCount];
tempFloat[0] = centre.x + (float)Math.random()/4f - 0.125f;
tempFloat[1] = centre.y + 0.15f;
tempFloat[2] = centre.z + (float)Math.random()/4f - 0.125f;
tempFloat[3] = 1.5f;
tempFloat[4] = 1;
tempFloat[5] = 0;
tempFloat[6] = 6 + (gameData.getRandom()%4);
tempFloat[7] = this.height;
theAssetManager.explosionCount++;
}
return;
}
}
if(isRepairing && currentHP >0){
if(mainThread.gameFrame%5==0 && theBaseInfo.currentCredit > 0 && currentHP <maxHP){
currentHP+=1;
theBaseInfo.currentCredit--;
}
}
//process turret AI
carryOutCommands();
if(attackCoolDown > 0)
attackCoolDown--;
if(exposedCountDown > 0)
exposedCountDown --;
//mark itself on obstacle map
mainThread.gridMap.currentObstacleMap[tileIndex[0]] = false;
//update center in camera coordinate
tempCentre.set(centre);
tempCentre.subtract(camera.position);
tempCentre.rotate_XZ(camera.XZ_angle);
tempCentre.rotate_YZ(camera.YZ_angle);
tempCentre.updateLocation();
theAssetManager = mainThread.theAssetManager;
//test if the gun turret is visible in camera point of view
if(visibleBoundary.contains(tempCentre.screenX, tempCentre.screenY) && isRevealed){
visible = true;
if(screenBoundary.contains(tempCentre.screenX, tempCentre.screenY))
withinViewScreen = true;
else
withinViewScreen = false;
tempshadowvertex0.set(shadowvertex0);
tempshadowvertex0.subtract(camera.position);
tempshadowvertex0.rotate_XZ(camera.XZ_angle);
tempshadowvertex0.rotate_YZ(camera.YZ_angle);
tempshadowvertex0.updateLocation();
tempshadowvertex1.set(shadowvertex1);
tempshadowvertex1.subtract(camera.position);
tempshadowvertex1.rotate_XZ(camera.XZ_angle);
tempshadowvertex1.rotate_YZ(camera.YZ_angle);
tempshadowvertex1.updateLocation();
tempshadowvertex2.set(shadowvertex2);
tempshadowvertex2.subtract(camera.position);
tempshadowvertex2.rotate_XZ(camera.XZ_angle);
tempshadowvertex2.rotate_YZ(camera.YZ_angle);
tempshadowvertex2.updateLocation();
tempshadowvertex3.set(shadowvertex3);
tempshadowvertex3.subtract(camera.position);
tempshadowvertex3.rotate_XZ(camera.XZ_angle);
tempshadowvertex3.rotate_YZ(camera.YZ_angle);
tempshadowvertex3.updateLocation();
//if the object is visible then draw it on the shadow buffer from light point of view
if(shadowBoundary.contains(tempshadowvertex0.screenX, tempshadowvertex0.screenY) ||
shadowBoundary.contains(tempshadowvertex1.screenX, tempshadowvertex1.screenY) ||
shadowBoundary.contains(tempshadowvertex2.screenX, tempshadowvertex2.screenY) ||
shadowBoundary.contains(tempshadowvertex3.screenX, tempshadowvertex3.screenY)
){
for(int i = 0; i < polygons.length; i++){
polygons[i].update_lightspace();
}
}
//add this object to visible unit list
theAssetManager.visibleUnit[theAssetManager.visibleUnitCount] = this;
theAssetManager.visibleUnitCount++;
}else{
visible = false;
}
//create vision for enemy commander
if(teamNo == 1){
int xPos = boundary2D.x1/16 - 6 + 10;
int yPos = 127 - boundary2D.y1/16 - 6 + 10;
for(int y = 0; y < 13; y++){
for(int x = 0; x < 13; x++){
if(bitmapVisionForEnemy[x+ y*13])
enemyCommander.tempBitmap[xPos + x + (yPos+y)*148] =true;
}
}
}else if(exposedCountDown > 0){
xPos = boundary2D.x1/16 - 2 + 10;
yPos = 127 - boundary2D.y1/16 - 2 + 10;
for(int y = 0; y < 5; y++){
for(int x = 0; x < 5; x++){
if(bitmapVisionGainFromAttackingUnit[x+ y*5])
enemyCommander.tempBitmap[xPos + x + (yPos+y)*148] =true;
}
}
}
visionBoundary.x = (int) (tempCentre.screenX - visionW);
visionBoundary.y = (int) (tempCentre.screenY - visionH);
visionInsideScreen = camera.screen.intersects(visionBoundary);
if(visionInsideScreen){
if(teamNo != 0){
if(exposedCountDown > 0){
tempFloat = theAssetManager.visionPolygonInfo[theAssetManager.visionPolygonCount];
tempFloat[0] = teamNo;
tempFloat[1] = centre.x;
tempFloat[2] = -0.4f;
tempFloat[3] = centre.z;
tempFloat[4] = 1;
theAssetManager.visionPolygonCount++;
}
}else{
tempFloat = theAssetManager.visionPolygonInfo[theAssetManager.visionPolygonCount];
tempFloat[0] = teamNo;
tempFloat[1] = centre.x;
tempFloat[2] = -0.4f;
tempFloat[3] = centre.z;
tempFloat[4] = 0;
theAssetManager.visionPolygonCount++;
}
}
if(theAssetManager.minimapBitmap[tileIndex[0]]){
isRevealed = true;
}
visible_minimap = isRevealed;
if(teamNo == 0 || attackStatus == isAttacking || exposedCountDown > 0 || visible_minimap){
tempInt = theAssetManager.unitsForMiniMap[theAssetManager.unitsForMiniMapCount];
tempInt[0] = teamNo;
tempInt[1] = boundary2D.x1/16;
tempInt[2] = 127 - boundary2D.y1/16;
tempInt[3] = 2;
if(teamNo == 0 && underAttackCountDown > 0)
tempInt[4] = 10001;
else{
if(exposedCountDown > 0)
tempInt[4] = exposedCountDown;
else
tempInt[4] = 10000;
}
theAssetManager.unitsForMiniMapCount++;
}
accumulatedDelta+=turretAngleDelta;
accumulatedDelta= accumulatedDelta%360;
if(visible){
//update turret polygons
for(int i = 5; i < 46; i++){
polygons[i].origin.subtract(centre);
polygons[i].origin.rotate_XZ(accumulatedDelta);
polygons[i].origin.add(centre);
polygons[i].bottomEnd.subtract(centre);
polygons[i].bottomEnd.rotate_XZ(accumulatedDelta);
polygons[i].bottomEnd.add(centre);
polygons[i].rightEnd.subtract(centre);
polygons[i].rightEnd.rotate_XZ(accumulatedDelta);
polygons[i].rightEnd.add(centre);
for(int j = 0; j < polygons[i].vertex3D.length; j++){
polygons[i].vertex3D[j].subtract(centre);
polygons[i].vertex3D[j].rotate_XZ(accumulatedDelta);
polygons[i].vertex3D[j].add(centre);
}
polygons[i].normal.rotate_XZ(accumulatedDelta);
polygons[i].findDiffuse();
}
accumulatedDelta = 0;
}
}
//process turret AI
public void carryOutCommands(){
if(targetObject != null){
destinationX = targetObject.getRealCentre().x;
destinationY = targetObject.getRealCentre().z;
distanceToDesination = (float)Math.sqrt((destinationX - centre.x) * (destinationX - centre.x) + (destinationY - centre.z) * (destinationY - centre.z));
//check if light tank has the line of sight to its target
boolean hasLineOfSightToTarget = true;
if(distanceToDesination <= attackRange){
int numberOfIterations = (int)(distanceToDesination * 8);
float dx = (destinationX - centre.x)/numberOfIterations;
float dy = (destinationY - centre.z)/numberOfIterations;
float xStart = centre.x;
float yStart = centre.z;
for(int i = 0; i < numberOfIterations; i++){
xStart+=dx;
yStart+=dy;
solidObject s = mainThread.gridMap.tiles[(int)(xStart*4) + (127 - (int)(yStart*4))*128][0];
if(s != null){
if(s.type > 100 && s .type < 200 && s != targetObject){
hasLineOfSightToTarget = false;
break;
}
}
}
}else{
hasLineOfSightToTarget = false;
}
if(targetObject.currentHP <=0 || (targetObject.isCloaked && teamNo != targetObject.teamNo)){
targetObject = null;
turretAngleDelta = 0;
return;
}
if(hasLineOfSightToTarget){
attackAngle = geometry.findAngle(centre.x, centre.z, destinationX, destinationY);
if(turretAngle != attackAngle){
turretAngleDelta = 360 - (geometry.findAngleDelta(turretAngle, attackAngle, turretTurnRate) + 360)%360;
turretAngle= (turretAngle - turretAngleDelta + 360)%360;
if(Math.abs(turretAngle - attackAngle) < 10)
fireBullet(attackAngle);
}else{
fireBullet(attackAngle);
turretAngleDelta = 0;
}
}else{
turretAngleDelta = 0;
targetObject = null;
}
}else{
//if there is no target, perform standby logic
//scan for hostile unit
if((randomInt + mainThread.gameFrame)%240 == 0){
attackAngle = (int)(Math.random()*360);
}
if(turretAngle != attackAngle){
turretAngleDelta = 360 - (geometry.findAngleDelta(turretAngle, attackAngle, 2) + 360)%360;
turretAngle= (turretAngle - turretAngleDelta + 360)%360;
}else{
turretAngleDelta = 0;
}
if((ID + mainThread.gameFrame)%4 == 0){
currentOccupiedTile = (int)(centre.x*64)/16 + (127 - (int)(centre.z*64)/16)*128;
for(int i = 0; i < tileCheckList.length; i++){
if(tileCheckList[i] != Integer.MAX_VALUE){
int index = currentOccupiedTile + tileCheckList[i];
if(index < 0 || index >= 16384)
continue;
tile = mainThread.gridMap.tiles[index];
for(int j = 0; j < 4; j++){
if(tile[j] != null){
if(tile[j].teamNo != teamNo && tile[j].teamNo != -1 && tile[j].currentHP > 0 && !tile[j].isCloaked){
targetObject = tile[j];
return;
}
}
}
}
}
}
}
}
//draw the model
public void draw(){
if(!visible)
return;
for(int i = 0; i < polygons.length; i++){
polygons[i].update();
polygons[i].draw();
}
}
public void attack(solidObject o){
if(targetObject != o){
distanceToDesination = (float)Math.sqrt((o.centre.x - centre.x) * (o.centre.x - centre.x) + (o.centre.z - centre.z) * (o.centre.z - centre.z));
//check if target is within range
if(distanceToDesination <= attackRange){
//check if there is any obstacles between target and the turret
//check if light tank has the line of sight to its target
boolean hasLineOfSightToTarget = true;
int numberOfIterations = (int)(distanceToDesination * 8);
float dx = (o.centre.x - centre.x)/numberOfIterations;
float dy = (o.centre.z - centre.z)/numberOfIterations;
float xStart = centre.x;
float yStart = centre.z;
for(int i = 0; i < numberOfIterations; i++){
xStart+=dx;
yStart+=dy;
solidObject s = mainThread.gridMap.tiles[(int)(xStart*4) + (127 - (int)(yStart*4))*128][0];
if(s != null){
if(s.type > 100 && s .type < 200 && s != o){
hasLineOfSightToTarget = false;
break;
}
}
}
if(hasLineOfSightToTarget){
targetObject = o;
}
}
}
}
public void fireBullet(int attackAngle){
if(targetObject != null && targetObject.teamNo != teamNo){
exposedCountDown = 64;
isRevealed = true;
}
int theDamage = myDamage;
if(targetObject.type == 0 || targetObject.type == 6)
theDamage = 20;
if(attackCoolDown == 0 ){
//if there is nothing between the tank and its target
firingPosition.set(0, -0.4f, 0.18f);
firingPosition.rotate_XZ(360 - attackAngle);
firingPosition.add(centre.x, 0, centre.z);
theAssetManager.spawnBullet(attackAngle, theDamage, targetObject, firingPosition, this);
attackCoolDown = myAttackCooldown;
//spawn a mini explosion
float[] tempFloat = theAssetManager.explosionInfo[theAssetManager.explosionCount];
tempFloat[0] = firingPosition.x;
tempFloat[1] = firingPosition.y;
tempFloat[2] = firingPosition.z;
tempFloat[3] = 0.4f;
tempFloat[4] = 3;
tempFloat[5] = 0;
tempFloat[6] = 6 + (gameData.getRandom()%4);
tempFloat[7] = centre.y;
theAssetManager.explosionCount++;
}
}
public void hold(){
targetObject = null;
turretAngleDelta = 0;
}
public vector getMovement(){
return movenment;
}
}