-
Notifications
You must be signed in to change notification settings - Fork 0
/
deal.cpp
419 lines (390 loc) · 15.5 KB
/
deal.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
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
#include "deal.h"
using namespace Game;
extern std::map<int, bool> keyboard;
extern Player player;
extern std::vector<Enemy> enemy;
extern std::vector<Bullet> bullet;
extern std::vector<Bonus> bonus;
extern bool gameOver;
extern int soulAttack;
extern int score, highestScore;
extern double endTime;
extern Image *imagePlayer, *imageBullet, *imageEnemy, *images[100], *imagesBonus[5];
const double PI = std::acos(-1);
double lastBulletTime = 0.0;
double lastSoulAttackTime = 0.0;
double ISA_Time = 0.0;
//Deal with Events
int dealWithPlayer(){
//Game Over
if (player.HP <= 0){
return -1;
}
//movement
bool move = false;
if(keyboard[KEY_UP] || keyboard['w']){ //move forward
player.vel = player.vel + PointD(0,-1) * player.speed;
move = true;
}
if(keyboard[KEY_DOWN] || keyboard['s']){ //move backward
player.vel = player.vel + PointD(0,+1) * player.speed;
move = true;
}
if(keyboard[KEY_LEFT] || keyboard['a']){ //move leftward
player.vel = player.vel + PointD(-1,0) * player.speed;
move = true;
}
if(keyboard[KEY_RIGHT] || keyboard['d']){ //move rightward
player.vel = player.vel + PointD(+1,0) * player.speed;
move = true;
}
double len = player.vel.length();
if(len > player.speed){ //move to fast!
player.vel = player.vel / len * player.speed;
}
player.pos = player.pos + player.vel;
maintainToScreen(player);
if(!move){ //not move, velocity gradually lows
player.vel = player.vel * 0.8; //speed of lowness
if(player.vel.length() < 0.1)
player.vel = PointD(); //stop
}
//no bonus!
if (ISA_Time > 10.0 && duration - ISA_Time >= 10.0){
ISA_Time = 0.0;
player.speedAttack *= 2;
}
//shoot
int wb, wp, hb, hp;
getImageSize(imageBullet, wb, hb);
if (keyboard[KEY_ENTER] || keyboard['z']){ //create a new bullet
if (duration - lastBulletTime >= player.speedAttack){
getImageSize(imagePlayer, wp, hp);
//PointD newBullet(posPlayer.x, posPlayer.y - hp / 2 - hb / 2);
bullet.emplace_back(imageBullet, player.pos.x, player.pos.y - (hp / 2 - hb) * player.hRate,
0, -20, player.wRate, player.hRate, 0, true);
// bullet[bullet.size() - 1].wRate = player.wRate;
//bullet[bullet.size() - 1].hRate = player.hRate;
lastBulletTime = duration;
}
}
if ((keyboard[KEY_SPACE] || keyboard['x']) && soulAttack > 0){ //soul attack
if (duration - lastSoulAttackTime >= 1.0){
bullet.clear();
for (auto &ene : enemy){
ene.HP -= 50;
}
--soulAttack;
lastSoulAttackTime = duration;
}
}
if (player.isCol){ //is collided
player.pos.x = PLAY_WIDTH / 2;
player.pos.y = PLAY_HEIGHT - player.imgh / 2 * player.hRate;
bullet.clear();
player.isCol = false;
}
return 0;
}
//double lastEnemyTime[10] = {0.0}; //the index represent the eType
double lastEnemyTime[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
const double gapEnemy[5] = {1.5, 3.3, 2.1, 2147483614, 2147483647}; //gap of two enemies' arrival
void dealWithEnemy(){
auto ite = enemy.begin();
int wb, hb;
getImageSize(imageBullet, wb, hb);
while (ite != enemy.end()){
//std::cout << "DEALWITHENEMY" << std::endl;
if (ite->HP <= 0){ //be destroyed
if (ite->eraseTime == 0){
if (ite->eType == 1){ //create bonus
int prob = rand() % 100;//probability
//4%: bonus HP
if (prob == 3) bonus.emplace_back(imagesBonus[4], ite->pos.x, ite->pos.y, 4);
//6%: bonus Bomb
else if (prob <= 9) bonus.emplace_back(imagesBonus[3], ite->pos.x, ite->pos.y, 3);
//10%: bonus speedAttack
else if (prob <= 19) bonus.emplace_back(imagesBonus[2], ite->pos.x, ite->pos.y, 2);
//20%: bonus 1000 points
else if (prob <= 39) bonus.emplace_back(imagesBonus[1], ite->pos.x, ite->pos.y, 1);
//40%: bonus 500 points
else if (prob <= 79) bonus.emplace_back(imagesBonus[0], ite->pos.x, ite->pos.y, 0);
}
ite->eraseTime = duration;
score += 100;
++ite;
}
else{
bool b = drawBurstAnime(3, *ite);
if (!b){
enemy.erase(ite);
//std::cout << "ERASE ENEMY" << std::endl;
}
else ++ite;
}
continue;
}
ite->pos = ite->pos + ite->vel;
if (outOfScreen(*ite))
enemy.erase(ite);
if (ite == enemy.end()) break;
if (duration - ite->bulTime >= ite->speedAttack){//Create new bullets
double wRate = ite->wRate;
double hRate = ite->hRate;
switch(ite->eType){
//each case is added an extra pair of brackets to avoid jumping labels and crossing initializations
case 0:{ //emit one bullet to the player
if (ite->pos.y < PLAY_HEIGHT * 0.8){ //A condition to decrease hardness
double bx = ite->pos.x, by = ite->pos.y + (ite->imgh / 2 - hb) * ite->hRate; //position in x and y
double ang = std::atan2(player.pos.y - by, player.pos.x - bx); //emitting angle in radian measure
double vx = 5 * std::cos(ang), vy = 5 * std::sin(ang); //velocity in x and y
ang = ang / PI * 180; //transform into angular measure
ang -= 90; //rotating angle
if (ang < 0) ang += 360;
bullet.emplace_back(imageBullet, bx, by, vx, vy, wRate, hRate, ang, false);
ite->bulTime = duration; //update the last emitting time
}
break;
}
case 1:{ //emit bullets as a semicircle
int AMOUNT = 25; //the number of bullets, may change with hardness(ToDo)
double radio = ite->colR; //initial radio of the semicircle
for (int k = 0; k <= AMOUNT; ++k){
double ang = (double)k / AMOUNT * PI; //emitting angle in radian measure
double bx = ite->pos.x - radio * std::cos(ang), by = ite->pos.y + radio * std::sin(ang); //position in x and y
double vx = -3 * std::cos(ang), vy = 3 * std::sin(ang); //velocity in x and y
ang = (k <= AMOUNT / 2) ? (90 - k * 180 / AMOUNT) : (450 - k * 180 / AMOUNT); //emitting angle in angular measure
bullet.emplace_back(imageBullet, bx, by, vx, vy, wRate, hRate, ang, false);
}
ite->bulTime = duration; //update the last emitting time
break;
}
case 2:{ //gradually change its direction to player
Enemy &ene2 = *ite;
double ang = std::atan2(ene2.vel.y, ene2.vel.x), angDir = std::atan2((player.pos.y - ene2.pos.y), (player.pos.x - ene2.pos.x));
//maintain the angle to [0, 2 * PI)
if (ang < 0) ang += 2 * PI;
if (angDir < 0) angDir += 2 * PI;
//rotate, always choose the direction that the differing angle is less than PI
//very annoying judgment, maybe some cases can be merged
if (angDir > ang){
if (angDir - ang <= PI){
ang += PI / 20;
if (ang > angDir) ang = angDir;
}
else{
//std::cout << "PREANG = " << ang / PI * 180 << std::endl;
ang -= PI / 20;
if (ang < 0){
ang += 2 * PI;
if (ang < angDir) ang = angDir;
}
//std::cout << "ANGDIR = " << angDir / PI * 180 << std::endl;
//std::cout << "NOWANG = " << ang / PI * 180 << std::endl;
std::cout << std::endl;
}
}
else{
if (ang - angDir <= PI){
ang -= PI / 20;
if (ang < angDir) ang = angDir;
}
else{
ang += PI / 20;
if (ang > 2 * PI){
ang -= 2 * PI;
if (ang > angDir) ang = angDir;
}
}
}
double vx = abs(15 * cos(ang)), vy = abs(15 * sin(ang)); //velocity in x and y
if (PI / 2 < ang && ang < PI / 2 * 3) vx = -vx;
if (PI < ang && ang < 2 * PI) vy = -vy;
ang = abs(ang / PI * 180); //transform into angular measure
ang -= 90.0;
if (ang < 0) ang += 360; //rotating angle
ene2.vel.x = vx;
ene2.vel.y = vy; //position in x and y
ene2.imgAngle = ang;
ite->bulTime = duration;
//std::cout << "CHANGE ETYPE2" << std::endl;
break;
}
default:
break;
}
}
++ite;
}
//create a new enemy
for (int i = 0; i <= 2; ++i){
if (duration - lastEnemyTime[i] >= gapEnemy[i]){
int we, he;
getImageSize(imageEnemy, we, he);
int r = rand() % (PLAY_WIDTH - we - we / 2) + we / 2;
switch(i){
case 0:{
enemy.emplace_back(imageEnemy, r, he / 2 * 0.5, 0);
Enemy &ene0 = enemy[enemy.size() - 1];
ene0.wRate = 0.5;
ene0.hRate = 0.5;
ene0.colR = hypot(ene0.imgw / 2 * ene0.wRate, ene0.imgh / 2 * ene0.hRate);
if (outOfScreen(ene0))
maintainToScreen(ene0);
lastEnemyTime[0] = duration;
break;
}
case 1:{
if (duration < 20) break;
enemy.emplace_back(imageEnemy, r, he / 2, 1);
Enemy &ene1 = enemy[enemy.size() - 1];
ene1.wRate = 1.0;
ene1.hRate = 1.0;
ene1.colR = hypot(ene1.imgw / 3 * ene1.wRate, ene1.imgh / 3 * ene1.hRate);
if (outOfScreen(ene1))
maintainToScreen(ene1);
lastEnemyTime[1] = duration;
break;
}
case 2:{
if (duration < 10) break;
enemy.emplace_back(imageEnemy, r, he / 2 * 0.3, 2);
Enemy &ene2 = enemy[enemy.size() - 1];
double ang = std::atan2(player.pos.y - ene2.pos.y, player.pos.x - ene2.pos.x);
ene2.vel.x = 15 * std::cos(ang);
ene2.vel.y = 15 * std::sin(ang); //initial velocity
ene2.wRate = 0.3;
ene2.hRate = 0.3;
ene2.colR = hypot(ene2.imgw * ene2.wRate, ene2.imgh * ene2.hRate);
if (outOfScreen(ene2))
maintainToScreen(ene2);
lastEnemyTime[2] = duration;
break;
}
default:
break;
}
}
}
}
void dealWithBullet(){
auto itb = bullet.begin();
while (itb != bullet.end()){
itb->pos = itb->pos + itb->vel;
if (outOfScreen(*itb))
bullet.erase(itb); //out of screen
if (itb == bullet.end()) break;
else ++itb;
}
//if (keyboard['z']) std::cout << "Z" << std::endl;
}
void dealWithBonus(){
auto itn = bonus.begin();
while (itn != bonus.end()){
itn->pos.y += itn->speed;
itn->speed += itn->deltaSpeed;
if (itn->speed > itn->maxSpeed) itn->speed = itn->maxSpeed;
if (outOfScreen(*itn))
bonus.erase(itn); //out of screen
if (itn == bonus.end()) break;
else ++itn;
}
}
void dealWithCollision(){
//bullets with planes
auto itb = bullet.begin();
while (itb != bullet.end()){
//std::cout << itb->isPlayer << std::endl;
//system("PAUSE");
if (itb->isPlayer){
auto ite = enemy.begin();
while (ite != enemy.end()){
if (ite->eraseTime == 0){
if (isCollide(*itb, *ite)){
--ite->HP;
bullet.erase(itb);
break;
}
}
++ite;
}
if (itb != bullet.end()) ++itb;
}
else{
if (isCollide(*itb, player)){
#ifndef DEBUG_MODE
--player.HP;
if (soulAttack < 3) ++soulAttack;
player.isCol = true;
#endif // DEBUG_MODE
bullet.erase(itb);
continue;
}
else ++itb;
}
}
//player with enemies
auto ite = enemy.begin();
while (ite != enemy.end()){
if (ite->HP > 0){
if (isCollide(*ite, player)){
ite->HP = 0;
#ifndef DEBUG_MODE
--player.HP;
if (soulAttack < 3) ++soulAttack;
player.isCol = true;
#endif // DEBUG_MODE
}
}
++ite;
}
//player with bonus
auto itn = bonus.begin();
while (itn != bonus.end()){
if (isCollide(*itn, player)){
switch(itn->bType){
case 0: //add 500 scores
score += 500;
break;
case 1: //add 1000 scores
score += 1000;
break;
case 2: //add speedAttack
if (ISA_Time < 1e-6) player.speedAttack /= 2;
ISA_Time = duration;
break;
case 3: //add a bomb
++soulAttack;
break;
case 4: //add a HP
++player.HP;
break;
default:
break;
}
bonus.erase(itn);
continue;
}
else ++itn;
}
}
bool dealWithEnd(){
enemy.clear();
bullet.clear();
bonus.clear();
gameOver = true;
if (!drawBurstAnime(2, player)){ //show score
static int ctrl = 0; //control delay time
if (player.eraseTime > 0){
drawEndInfo();
if (ctrl % 3 == 0) player.eraseTime -= 1; //do per 5 frames
if (ctrl % 3 == 0) ++score; //do per 5 frames
++ctrl;
return false;
}
else{
return true;
}
}
return false;
}