-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTankMoves.java
209 lines (180 loc) · 9.2 KB
/
TankMoves.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
import java.util.Random;
public class TankMoves extends Tank {
private boolean movingRandomly;
private int ranDist2move;
private Wall wallMoving2;
private boolean seesMainTank;
private int clockCyclesAfterLastShot = 0;
private int cannonrotationsign = 1;
private int signofrotativemov = 1;
public void setSeesMainTank(boolean seesMainTank) {
this.seesMainTank = seesMainTank;
}
public boolean getSeesMainTank() {
return this.seesMainTank;
}
public TankMoves(int initialx, int initialy, int initialAngle) {
super(initialx, initialy, initialAngle, "TankMoves", 1);
loadImage("Resources/base_pink.png", "Resources/cannon_pink.png");
this.setSpeed(1);
this.setSeesMainTank(false);
this.movingRandomly = 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);
this.setMovementAngle(angle2Main);
this.setDx(dx);
this.setDy(dy);
// Check if distance to MainTank is prudencial in order to mode:
double newPosx = this.getPosx() + this.getDx() * this.getSpeed();
double newPosy = this.getPosy() + this.getDy() * this.getSpeed();
if (Math.sqrt(Math.pow(this.getPosx() - mainTank.getPosx(), 2)
+ Math.pow(this.getPosy() - mainTank.getPosy(), 2)) > (2 * TankGame.getImgSizeTank() * 1.5)
&& !this.collides(newPosx, newPosy)) {
this.setPosx(newPosx);
this.setPosy(newPosy);
}
else {
dx = Math.cos(Math.toRadians(360 - (angle2Main + signofrotativemov * 90)));
dy = -Math.sin(Math.toRadians(360 - (angle2Main + signofrotativemov * 90)));
newPosx = this.getPosx() + dx * this.getSpeed();
newPosy = this.getPosy() + dy * this.getSpeed();
if (!this.collides(newPosx, newPosy)) {
this.setMovementAngle(angle2Main + signofrotativemov * 90);
this.setPosx(newPosx);
this.setPosy(newPosy);
} else {
signofrotativemov = signofrotativemov * -1;
}
}
if (!lastSeen || (lastSeen && clockCyclesAfterLastShot == 100)) {
this.fire(this.getshootAngle());
clockCyclesAfterLastShot = 0;
} else {
clockCyclesAfterLastShot++;
}
} else {
// Move cannon to search for MAINTANK:
double newangle = this.getshootAngle() + 0.2;
if (Math.abs(newangle - angle2Main) > 45)
cannonrotationsign *= -1;
this.setShootAngle(this.getshootAngle() + cannonrotationsign * 0.2);
// Move position to search for MAINTANK:
if (!movingRandomly) {
int randDir2move = (new Random()).nextInt(8); // The tank has 8 angles to move with (0,45,90,135...)
this.ranDist2move = (new Random()).nextInt(3) + 1; // It will get as close as 1,2,3 block lengths to the
// wall
this.wallMoving2 = null;
dx = Math.cos(Math.toRadians(randDir2move * 45));
dy = -Math.sin(Math.toRadians(randDir2move * 45));
this.setDx(dx);
this.setDy(dy);
this.setMovementAngle(360 - randDir2move * 45);
// Detect towards which block I am moving, to be able to calculate distance:
posxCheck = this.getPosx() + TankGame.getImgSizeTank() / 2;
posyCheck = this.getPosy() + TankGame.getImgSizeTank() / 2;
while (!(posxCheck < 0 || posxCheck > TankGame.getGameWidth() || posyCheck < 0
|| posyCheck > TankGame.getGameHeight())) {
for (Wall wall : Board.walls) {
if (posxCheck > wall.getPosx()
&& posxCheck < (wall.getPosx() + TankGame.getImgSizeWall())
&& posyCheck > wall.getPosy()
&& posyCheck < (wall.getPosy() + TankGame.getImgSizeWall())) {
this.wallMoving2 = wall;
break;
}
}
if (this.wallMoving2 != null)
break;
posxCheck += dx * (TankGame.getImgSizeWall() - 5);
posyCheck += dy * (TankGame.getImgSizeWall() - 5);
}
double newPosx = this.getPosx() + this.getDx() * this.getSpeed() / 2;
double newPosy = this.getPosy() + this.getDy() * this.getSpeed() / 2;
if (Math.sqrt(Math
.pow(this.getPosx() + TankGame.getImgSizeTank() / 2
- (wallMoving2.getPosx() + TankGame.getImgSizeWall() / 2), 2)
+ Math.pow(
this.getPosy() + TankGame.getImgSizeTank() / 2
- (wallMoving2.getPosy() + TankGame.getImgSizeWall() / 2),
2)) > (this.ranDist2move * TankGame.getImgSizeWall())
&& !this.collides(newPosx, newPosy)) {
this.setPosx(newPosx);
this.setPosy(newPosy);
this.movingRandomly = true;
}
}
else {
double newPosx = this.getPosx() + this.getDx() * this.getSpeed() / 2;
double newPosy = this.getPosy() + this.getDy() * this.getSpeed() / 2;
if (Math.sqrt(Math
.pow(this.getPosx() + TankGame.getImgSizeTank() / 2
- (wallMoving2.getPosx() + TankGame.getImgSizeWall() / 2), 2)
+ Math.pow(
this.getPosy() + TankGame.getImgSizeTank() / 2
- (wallMoving2.getPosy() + TankGame.getImgSizeWall() / 2),
2)) > (this.ranDist2move * TankGame.getImgSizeWall())
&& !this.collides(newPosx, newPosy)) {
this.setPosx(newPosx);
this.setPosy(newPosy);
}
else {
this.movingRandomly = false;
}
}
}
}
}