-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTankBasic.java
100 lines (84 loc) · 3.86 KB
/
TankBasic.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
public class TankBasic extends Tank {
private boolean seesMainTank;
private int clockCyclesAfterLastShot = 0;
private int cannonrotationsign = 1;
public void setSeesMainTank(boolean seesMainTank) {
this.seesMainTank = seesMainTank;
}
public boolean getSeesMainTank() {
return this.seesMainTank;
}
public TankBasic(int initialx, int initialy, int initialAngle) {
super(initialx, initialy, initialAngle, "TankBasic", 1);
loadImage("Resources/base_blue.png", "Resources/cannon_blue.png");
this.setSeesMainTank(false);
}
public void fire(double shootAngle) {
double dx = Math.cos(Math.toRadians(360 - shootAngle));
double dy = -Math.sin(Math.toRadians(360 - shootAngle));
Missile missile = new Missile(this.getPosx(), this.getPosy(), dx, dy, shootAngle, "enemy", false);
Board.missiles.add(missile);
}
public void update(Tank mainTank) {
if (this.getLives() < 1) {
this.setVisible(false);
return;
}
int angle2Main = (int) Math.toDegrees(
Math.atan2(
(mainTank.getPosy() + TankGame.getImgSizeTank() / 2)
- (this.getPosy() + TankGame.getImgSizeTank() / 2),
(mainTank.getPosx() + TankGame.getImgSizeTank() / 2)
- (this.getPosx() + TankGame.getImgSizeTank() / 2)));
if (angle2Main < 0) {
angle2Main += 360;
}
// CHECK LINE OF SIGHT WITH MAIN TANK:
double posxCheck = this.getPosx() + TankGame.getImgSizeTank() / 2;
double posyCheck = this.getPosy() + TankGame.getImgSizeTank() / 2;
double dx = Math.cos(Math.toRadians(360 - angle2Main));
double dy = -Math.sin(Math.toRadians(360 - angle2Main));
boolean lastSeen = getSeesMainTank();
if (!Board.MainTank.getGhost()) {
setSeesMainTank(true);
while (!(posxCheck > mainTank.getPosx() && posxCheck < (mainTank.getPosx() + TankGame.getImgSizeWall())
&& posyCheck > mainTank.getPosy()
&& posyCheck < (mainTank.getPosy() + TankGame.getImgSizeWall()))) {
for (Wall wall : Board.walls) {
if (posxCheck > wall.getPosx() && posxCheck < (wall.getPosx() + TankGame.getImgSizeWall())
&& posyCheck > wall.getPosy() && posyCheck < (wall.getPosy() + TankGame.getImgSizeWall())) {
setSeesMainTank(false);
break;
}
}
if (!getSeesMainTank())
break;
// posxCheck += dx * (TankGame.getImgSizeWall() - 20);
// posyCheck += dy * (TankGame.getImgSizeWall() - 20);
posxCheck += dx;
posyCheck += dy;
if (posxCheck < 0 || posxCheck > TankGame.getGameWidth() || posyCheck < 0
|| posyCheck > TankGame.getGameHeight()) {
// setSeesMainTank(false);
break;
}
}
} else {
setSeesMainTank(false);
}
if (this.getSeesMainTank()) {
this.setShootAngle(angle2Main);
if (!lastSeen || (lastSeen && clockCyclesAfterLastShot == 100)) {
this.fire(this.getshootAngle());
clockCyclesAfterLastShot = 0;
} else {
clockCyclesAfterLastShot++;
}
} else {
double newangle = this.getshootAngle() + 0.2;
if (Math.abs(newangle - angle2Main) > 45)
cannonrotationsign *= -1;
this.setShootAngle(this.getshootAngle() + cannonrotationsign * 0.2);
}
}
}