/
tank.cpp
296 lines (271 loc) · 8.1 KB
/
tank.cpp
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
#include <cassert>
#include <cstdio>
#include "tank.h"
#include "collision.h"
#include "rect.h"
#include "message.h"
#include "effects.h"
#ifndef M_PI
#define M_PI 3.1415926
#endif
extern std::vector<Tank> tanks;
extern std::vector<Bullet> bullets;
extern std::vector<Message> messages;
extern std::vector<Effect> effects;
extern int wall[numberHeight][numberWidth];
extern Rect boundary;
extern bool going;
// This file contains the definitions of the functions in Tank.h
// Create functions
Tank createTank(int owner, float x, float y, float angle)
{
Tank tank = {{{x,y},angle,tankLength,tankWidth},0,fullHP,0,0,
{angle,fullHP,fullBullet,0},owner};
return tank;
}
// Add a new tank to the array
bool addTank(int owner, float x, float y, float angle)
{
Tank tank = createTank(owner,x,y,angle);
if(!tankCollision(tank.rect))
{
tanks.push_back(tank);
return true;
}
return false;
}
bool getFreeTank(Tank & tank)
{
for(int i = 0; i < 256; i++)
{
float x = (float)rand()/RAND_MAX*scaleWidth;
float y = (float)rand()/RAND_MAX*scaleHeight;
float a = (float)rand()/RAND_MAX*2*M_PI;
tank = createTank(-1,x,y,a);
if(!tankCollision(tank.rect)) return true;
}
return false;
}
bool addFreeTank(int owner)
{
Tank tank;
if(getFreeTank(tank))
{
tank.commander = owner;
tanks.push_back(tank);
return true;
}
return false;
}
bool reviveTank(int idx)
{
if(idx < 0 || idx >= tanks.size() || tanks.at(idx).HP > 0) return false;
Tank tank;
if(getFreeTank(tank))
{
tank.commander = tanks.at(idx).commander;
tanks.at(idx) = tank;
}
return false;
}
// Move functions
void moveon(int dt)
{
// Move on all the bullets
if(going)
{
for(int i = 0; i < bullets.size(); i++)
moveonBullet(i, dt);
// Move on all the tanks
for(int i = 0; i < tanks.size(); i++)
moveonTank(i, dt);
// Deal with messages
dealWithMessages();
}
// Deal with effects
for(int i = 0; i < effects.size(); i++)
{
if(!moveOnEffect(effects.at(i),dt))
{
effects.at(i) = effects.back();
effects.pop_back();
i--;
}
}
}
// Check if rectangle rc is valid on the field
// if idx is valid tank number, then this tank is omitted, this is useful when
// moving a tank
bool tankCollision(const Rect & rc, int idx)
{
// Collision detection with other tanks
for(int i = 0; i < tanks.size(); i++)
{
if(tanks.at(i).HP <= 0) continue; // Pass dead tank
if(i == idx) continue; // If this is the tank assigned to omit, pass
// cld is true only if the two do not collide or contain each other
if(solidCollide(rc, tanks.at(i).rect)) return true;
}
// Collision detection with the walls
for(int i = 0; i < numberHeight; i++)
{
for(int j = 0; j < numberWidth; j++)
{
if(wall[i][j]==0) continue; // If there is no wall at (i,j), pass
Rect wRc = getWallRect(i,j); // Get the rect of the wall
if(solidCollide(rc, wRc)) return true; // Detect collision
}
}
// Collisiont detection with the boundary
if(!containRect(boundary,rc)) return true;
return false;
}
void moveonTank(int idx, int dt)
{
// Make sure that this function is properly invoked
assert(idx >=0 && idx < tanks.size());
if(tanks.at(idx).HP <= 0) return;
// Deal with the tank body first //////////////////////////////////////////
// Generate a new rectangle, move the new rectangle, then detect collision
// if collide, just cancel and return. If no collision, move the tank to
// the new rectangle
float ang = tanks.at(idx).rect.angle;
float spd = tanks.at(idx).speed;
float dx = spd * dt * cos(ang); // The displacement of the rectangle
float dy = spd * dt * sin(ang); // The displacement of the rectangle
Rect nRc = tanks.at(idx).rect; // create new rectangle
nRc.pos[0] += dx; // Move the new rectangle
nRc.pos[1] += dy;
if(tanks.at(idx).turning == 1)
{
nRc.angle += tankTurnSpeed * dt; // Rotate the new rectangle
if(nRc.angle > 2*M_PI) nRc.angle -= 2*M_PI;
tanks.at(idx).tower.angle += tankTurnSpeed * dt;
if(tanks.at(idx).tower.angle > 2*M_PI)
tanks.at(idx).tower.angle -= 2*M_PI;
}
else if(tanks.at(idx).turning == 2)
{
nRc.angle -= tankTurnSpeed * dt; // Rotate the new rectangle
if(nRc.angle < 0) nRc.angle += 2*M_PI;
tanks.at(idx).tower.angle -= tankTurnSpeed * dt;
if(tanks.at(idx).tower.angle < 0)
tanks.at(idx).tower.angle += 2*M_PI;
}
// Collision detection
bool cld = tankCollision(nRc, idx);
if(cld) // If there is collision
{
tanks.at(idx).speed = -tanks.at(idx).speed * rebRatio; // Rebounce
tanks.at(idx).turning = 0;
emitFeedbackMessage(fbkCollide, idx);
}
else // If there is no collision
{
tanks.at(idx).rect = nRc; // Move the tank to the new rectangle
// If accelerating
if(tanks.at(idx).accel == 1)
{
tanks.at(idx).speed += tankAccel * dt;
}
else if(tanks.at(idx).accel == 2)
{
tanks.at(idx).speed -= tankAccel * dt;
}
// Slow down by friction
if(tanks.at(idx).speed > 0)
{
tanks.at(idx).speed -= friction * dt * tanks.at(idx).speed;
if(tanks.at(idx).speed < 0) tanks.at(idx).speed = 0;
}
else if(tanks.at(idx).speed < 0)
{
tanks.at(idx).speed -= friction * dt * tanks.at(idx).speed;
if(tanks.at(idx).speed > 0) tanks.at(idx).speed = 0;
}
}
////////////////Done with the body movement////////////////////////////////
// Then deal with the tower (there is no need for detection) //////////////
switch(tanks.at(idx).tower.turning)
{
case 1: // Turning left until maximum angle
tanks.at(idx).tower.angle += towerTurnSpeed * dt;
if(tanks.at(idx).tower.angle > 2*M_PI)
tanks.at(idx).tower.angle -= 2*M_PI;
break;
case 2: // Turning right until maximum angle
tanks.at(idx).tower.angle -= towerTurnSpeed * dt;
if(tanks.at(idx).tower.angle < 0)
tanks.at(idx).tower.angle += 2*M_PI;
break;
}
}
void moveonBullet(int idx, int dt)
{
// Make sure that this function is invoked properly
assert(idx >=0 && idx < bullets.size());
float *p = bullets.at(idx).pos;
float a = bullets.at(idx).angle;
float v = bullets.at(idx).speed;
// Detect Collsion first
bool hit = false;
// Collision detection with the wall
for(int i = 0; i < numberHeight; i++)
{
for(int j = 0; j < numberWidth; j++)
{
if(wall[i][j]==0) continue; // If there is no wall, pass
Rect wRc = getWallRect(i,j); // Else, get the rectangle of this wall
if(inRect(wRc, p)) // If the bullet is in the wall
{
hit = true; // Hit
// Calculate the point where the bullet entered the wall
float trace[][2] = {
{p[0],p[1]}, {p[0]-10*v*dt*cos(a),p[1]-10*v*dt*sin(a)}
};
float wallV[4][2];
float inter[2];
getVertices(wRc,wallV); // Get the vertices of the wall
bool c =
lineCollide(wallV[0],wallV[1],trace[0],trace[1],inter) ||
lineCollide(wallV[1],wallV[2],trace[0],trace[1],inter) ||
lineCollide(wallV[2],wallV[3],trace[0],trace[1],inter) ||
lineCollide(wallV[3],wallV[0],trace[0],trace[1],inter);
// Emit the hit message
if(c)
emitHitWallMessage(idx, i, j, inter[0], inter[1]);
else
emitHitWallMessage(idx, i, j, p[0], p[1]);
}
if(hit) break; // If already hit, break the loop
}
if(hit) break;
}
// Collision with tanks
for(int i = 0; i < tanks.size(); i++)
{
if(tanks.at(i).HP <= 0) continue;
Rect tRc = tanks.at(i).rect;
if(inRect(tRc, p))
{
hit = true;
emitHitTankMessage(idx, i, p[0], p[1]); // Emit the hit message
break; // Break the loop
}
}
// Collision with boundary, treated as hitting wall
if(!inRect(boundary, p)) emitHitWallMessage(idx, -1, -1, p[0], p[1]);
// Move the bullet
p[0] += v * dt * cos(a);
p[1] += v * dt * sin(a);
}
// Get the rectangle of the wall at position (i,j)
Rect getWallRect(int i, int j)
{
float x = j * wallSize + 0.5 * wallSize;
float y = i * wallSize + 0.5 * wallSize;
Rect rc = {{x,y},0,wallSize,wallSize};
return rc;
}
//////////////////////////////Tank.cpp/////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////