-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGameLevel2.cpp
697 lines (653 loc) · 18.9 KB
/
GameLevel2.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
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
#include "GameLevel2.h"
GameLevel2::GameLevel2()
{
}
GameLevel2::~GameLevel2()
{
}
void GameLevel2::Init()
{
srand((unsigned int)time(NULL));
levelName = L"关卡 1 - 2";
bg_img.LoadImageFile(L"res\\images\\interface\\background1unsodded2.jpg");
shadow_img.LoadImageFile(L"res\\images\\interface\\plantshadow32.png");
Sunlight_img.LoadImageFile(L"res\\images\\interface\\SunBack.png");
Sun.LoadImageFile(L"res\\images\\interface\\Sun.gif");
InitScene(0, 0, bg_img.GetImageWidth(), bg_img.GetImageHeight(), WIN_WIDTH, WIN_HEIGHT);
winImage.LoadImageFile(L"res\\images\\interface\\trophy.png");
loseImage.LoadImageFile(L"res\\images\\interface\\ZombiesWon.png");
gameFlag = 1; // 游戏运行中
sunlight = 300; //初始阳光值
//植物
PlantInit();
BullentInit();
PlantChang();
ZomArrayInit();
//过场动画
CutsceneInit();
//植物卡
CardInit();
//小推车
CarInit();
//僵尸
ZombiesInit();
//进度条
ProgressBarInit();
}
void GameLevel2::PlantChang()
{
CherryBomb = new T_Sprite(L"res\\images\\Plants\\Boom.png",213,196);
WallNutAte = new T_Sprite(L"res\\images\\Plants\\WallNut_crackd2.png",65,73);
}
void GameLevel2::AddZombies() {
SPRITEINFO info;
info.Active = true;
info.Dead = false;
info.Dir = DIR_LEFT;
info.Alpha = 255;
info.Score = 0;
info.Visible = true;
info.Level = 1;
info.Ratio = 1;
info.Rotation = TRANS_NONE;
info.Speed = 4;
ZOMBIES_INFO zombie_info;
/*判断当前帧是否需要新加入僵尸*/
for (int i = 0; i < zombiesArray.size(); i++)
{
if (frameCount == zombiesArray.at(i).frame) {
zombie_info.sprite = spriteZombie[zombiesArray.at(i).zombiesindex % 5];
zombie_info.row = INDEXROW + rand() % 3;
zombie_info.x = WIN_WIDTH; //僵尸横坐标
zombie_info.count = 0;
zombie_info.isChanged = false;
zombie_info.typeNum = zombiesArray.at(i).zombiesindex % 5;
info.X = zombie_info.x;
info.Y = (zombie_info.row) * PlantHeight + 10; //僵尸纵坐标
zombie_info.info = info;
zombie_info.frame = 110;
zombiesVector.push_back(zombie_info);
zombiesVector.back().sprite->Initiate(info);
zomEnter_buffer.Play(false);
}
}
}
void GameLevel2::PlantInit()
{
plant[0] = new T_Graph(L"res\\images\\Plants\\SunFlower.png");
spritePlant[0] = new T_Sprite(L"res\\images\\Plants\\SunFlower_18_1.png", 73, 74);
plant[1] = new T_Graph(L"res\\images\\Plants\\Peashooter.png");
spritePlant[1] = new T_Sprite(L"res\\images\\Plants\\Peashooter_13.png", 71, 71);
spritePlant[2] = new T_Sprite(L"res\\images\\Plants\\CherryBomb.png", 112, 81);
plant[2] = new T_Graph(L"res\\images\\Plants\\CherryBomb_1.png");
spritePlant[3] = new T_Sprite(L"res\\images\\Plants\\repeater.png", 73, 71);
plant[3] = new T_Graph(L"res\\images\\Plants\\repeater_1.png");
spritePlant[4] = new T_Sprite(L"res\\images\\Plants\\WallNut.png", 65, 73);
plant[4] = new T_Graph(L"res\\images\\Plants\\WallNut_1.png");
/*spritePlant[5] = new T_Sprite(L"res\\images\\Plants\\Peashooter_13.png", 71, 71);
plant[6] = new T_Graph(L"res\\images\\Plants\\SunFlower.png");
spritePlant[6] = new T_Sprite(L"res\\images\\Plants\\SunFlower_18_1.png", 73, 74);
plant[7] = new T_Graph(L"res\\images\\Plants\\Peashooter.png");
spritePlant[7] = new T_Sprite(L"res\\images\\Plants\\Peashooter_13.png", 71, 71);*/
}
void GameLevel2::ZomArrayInit()
{
ZOMBIES_ARRAY zombies_array;
zombies_array.zombiesindex = 0;
zombies_array.frame = 1000;
zombiesArray.push_back(zombies_array);
for (int i = 1; i < MAXZOMBIESNUM / 3; i++) {
zombies_array.zombiesindex = i % MAXZOMBIEARRAYSUN;
zombies_array.frame = 1000 + (2 * i + 1) * 300;
zombiesArray.push_back(zombies_array);
}
for (int i = MAXZOMBIESNUM / 3; i < MAXZOMBIESNUM * 2 / 3; i++)
{
zombies_array.zombiesindex = i % MAXZOMBIEARRAYSUN;
zombies_array.frame = 1000 + (2 * MAXZOMBIESNUM / 3 + 1) * 300 + (i - MAXZOMBIESNUM / 3) * 500;
zombiesArray.push_back(zombies_array);
}
for (int i = MAXZOMBIESNUM * 2 / 3; i < MAXZOMBIESNUM; i++)
{
zombies_array.zombiesindex = i % MAXZOMBIEARRAYSUN;
zombies_array.frame = 1000 + (2 * MAXZOMBIESNUM / 3 + 1) * 300 + (MAXZOMBIESNUM / 3) * 500 + 600 + i * 150;
zombiesArray.push_back(zombies_array);
}
ZOMENTER_MAXFRAME = zombiesArray.at(MAXZOMBIESNUM - 1).frame;
}
void GameLevel2::BullentInit()
{
bullet.LoadImageFile(L"res\\images\\Plants\\PeaBullet.png");
bulletHit.LoadImageFile(L"res\\images\\Plants\\PeaBulletHit.png");
}
void GameLevel2::CardInit()
{
int border = 10; //边距
int cardHeight;
plantCard[0].card = new T_Graph(L"res\\images\\interface\\menu\\handbook\\Card\\Plants\\SunFlower.png");
plantCard[1].card = new T_Graph(L"res\\images\\interface\\menu\\handbook\\Card\\Plants\\Peashooter.png");
plantCard[2].card = new T_Graph(L"res\\images\\interface\\menu\\handbook\\Card\\Plants\\CherryBomb.png");
plantCard[3].card = new T_Graph(L"res\\images\\interface\\menu\\handbook\\Card\\Plants\\Repeater.png");
plantCard[4].card = new T_Graph(L"res\\images\\interface\\menu\\handbook\\Card\\Plants\\WallNut.png");
cardHeight = plantCard[0].card->GetImageHeight() / 2 * 0.8;
CARD_INFO info;
info.time = 300;
info.x = border;
info.y = border;
//向日葵
info.sunlight = 50;
plantCard[0].info = info;
//豌豆射手
info.time = 500;
info.y = border * 2 + cardHeight;
info.sunlight = 100;
plantCard[1].info = info;
//CherryBomb
info.time = 700;
info.sunlight = 150;
info.y = border * 3 + cardHeight * 2;
plantCard[2].info = info;
//Reapter
info.time = 700;
info.sunlight = 150;
info.y = border * 4 + cardHeight * 3;
plantCard[3].info = info;
//WallNut
info.time = 300;
info.sunlight = 50;
info.y = border * 5 + cardHeight * 4;
plantCard[4].info = info;
plantCard[0].nowTime = 0;
plantCard[1].nowTime = 0;
plantCard[2].nowTime = 0;
plantCard[3].nowTime = 0;
plantCard[4].nowTime = 0;
plantCard[0].state = false;
plantCard[1].state = false;
plantCard[2].state = false;
plantCard[3].state = false;
plantCard[4].state = false;
}
void GameLevel2::DrawPlant(HDC hdc)
{
bool loop[MAXPLANTNUM];
for (int i = 0; i < MAXPLANTNUM; i++)
{
loop[i] = false;
}
for (int i = 0; i < plantVector.size(); i++)
{
plantVector.at(i).sprite->Initiate(plantVector.at(i).info);
plantVector.at(i).sprite->Draw(hdc);
if (trueFrame % 4 == 2)
{
if (loop[plantVector.at(i).pointNum] == false)
{
plantVector.at(i).sprite->LoopFrame();
loop[plantVector.at(i).pointNum] = true;
}
}
}
}
void GameLevel2::DrawCar(HDC hdc)
{
for (int i = 1; i < 4; i++)
{
if (car[i].car == NULL) {
continue;
}
if (car[i].state == true)
{
car[i].car->PaintImage(hdc, CarXSpace, (car[i].row)*PlantHeight + YSpace + 20, car[i].car->GetImageWidth(), car[i].car->GetImageHeight(), 255);
}
else
{
if (car[i].x <= WIN_WIDTH) {
car[i].x += CAR_LENGTH;
car_buffer.Play(false);
car[i].car->PaintImage(hdc, car[i].x, (car[i].row)*PlantHeight + YSpace + 20, car[i].car->GetImageWidth(), car[i].car->GetImageHeight(), 255);
}
else
{
car[i].car = NULL;
}
}
}
}
void GameLevel2::DrawCard(HDC hdc)
{
PAINTREGION_INFO info;
for (int i = 0; i < MAXPLANTNUM; i++)
{
info.destX = plantCard[i].info.x;
info.destY = plantCard[i].info.y;
info.srcX = 0;
info.srcY = plantCard[i].card->GetImageHeight() / 2;
info.regionWidth = plantCard[i].card->GetImageWidth();
info.regionHeight = plantCard[i].card->GetImageHeight() / 2;
plantCard[i].card->PaintRegion(plantCard[i].card->GetBmpHandle(), hdc, info.destX, info.destY, info.srcX, info.srcY, info.regionWidth, info.regionHeight, 0.8);
info.srcY = 0;
if (plantCard[i].state == false)
{
info.regionHeight = plantCard[i].card->GetImageHeight() / 2 * plantCard[i].nowTime / plantCard[i].info.time;
if (info.regionHeight < 2)
{
info.regionHeight = 2;
}
if (plantCard[i].nowTime <= plantCard[i].info.time)
{
plantCard[i].nowTime++;
plantCard[i].card->PaintRegion(plantCard[i].card->GetBmpHandle(), hdc, info.destX, info.destY, info.srcX, info.srcY, info.regionWidth, info.regionHeight, 0.8);
}
}
else
{
info.regionHeight = plantCard[i].card->GetImageHeight() / 2;
plantCard[i].card->PaintRegion(plantCard[i].card->GetBmpHandle(), hdc, info.destX, info.destY, info.srcX, info.srcY, info.regionWidth, info.regionHeight, 0.8);
}
RectF infoRec;
infoRec.X = (float)plantCard[i].info.x;
infoRec.Y = (float)plantCard[i].info.y + 30;
infoRec.Width = plantCard[i].card->GetImageWidth() - 20;
infoRec.Height = 20;
T_Graph::PaintText(hdc, infoRec, T_Util::int_to_wstring(plantCard[i].info.sunlight), 10, L"黑体", Color::Black, FontStyleBold, StringAlignmentFar);
}
}
void GameLevel2::DrawBullet(HDC hdc)
{
vector<BULLET_INFO>::iterator iter;
for (iter = bulletVector.begin(); iter != bulletVector.end(); iter++)
{
if (iter->hit == false)
{
if (iter->pointNum == 1) {
bullet.PaintImage(hdc, iter->position.X, iter->position.Y*PlantHeight + YSpace + 15, bullet.GetImageWidth(), bullet.GetImageHeight(), 255);
}
if (iter->pointNum == 3) {
bullet.PaintImage(hdc, iter->position.X, iter->position.Y*PlantHeight + YSpace + 15, bullet.GetImageWidth(), bullet.GetImageHeight(), 255);
bullet.PaintImage(hdc, iter->position.X + 10, iter->position.Y*PlantHeight + YSpace + 20, bullet.GetImageWidth(), bullet.GetImageHeight(), 255);
}
}
else
{
if (iter->pointNum == 1) {
bulletHit.PaintImage(hdc, iter->position.X, iter->position.Y*PlantHeight + YSpace + 15, bulletHit.GetImageWidth(), bulletHit.GetImageHeight(), 255);
}
if (iter->pointNum = 3) {
bulletHit.PaintImage(hdc, iter->position.X, iter->position.Y*PlantHeight + YSpace + 15, bulletHit.GetImageWidth(), bulletHit.GetImageHeight(), 255);
bulletHit.PaintImage(hdc, iter->position.X + 10, iter->position.Y*PlantHeight + YSpace + 20, bulletHit.GetImageWidth(), bulletHit.GetImageHeight(), 255);
}
}
}
}
void GameLevel2::Draw(HDC hdc)
{
trueFrame++;
if (frameCount < MaxFrameCount - 1)
{
frameCount++;
}
if (cutsceneFlag)
{
DrawCutscene(hdc);
}
else
{
//画背景
bg_img.PaintRegion(bg_img.GetBmpHandle(), hdc, 0, 0, 120, 0, WinWidth, WinHeight, 1);
//小推车
DrawCar(hdc);
//画植物
DrawPlant(hdc);
//画僵尸
DrawZombies(hdc);
//画子弹
DrawBullet(hdc);
//画植物卡
DrawCard(hdc);
if (pointState)
{
//画点击后的植物图
DrawClickPlant(hdc, mousex, mousey);
}
//画阳光面板
DrawSunLight(hdc);
//画进度条
DrawProgressBar(hdc);
if (gameFlag == 2) {
winImage.PaintImage(hdc, (WIN_WIDTH - winImage.GetImageWidth()) / 2, (WIN_HEIGHT - winImage.GetImageHeight()) / 2, winImage.GetImageWidth(), winImage.GetImageHeight(), 255);
}
if (gameFlag == 0)
{
loseImage.PaintImage(hdc, (WIN_WIDTH - loseImage.GetImageWidth()) / 2, (WIN_HEIGHT - loseImage.GetImageHeight()) / 2, loseImage.GetImageWidth(), loseImage.GetImageHeight(), 255);
}
}
}
void GameLevel2::CardLogic()
{
for (int i = 0; i < MAXPLANTNUM; i++)
{
if (plantCard[i].nowTime >= plantCard[i].info.time && sunlight >= plantCard[i].info.sunlight)
{
plantCard[i].state = true;
}
else
{
plantCard[i].state = false;
}
}
}
void GameLevel2::Logic()
{
AddZombies();
CardLogic();
bulletLogic();
attackPlantLogic();
attackZombieLogic();
carLogic();
GameWin();
GameLose();
}
void GameLevel2::attackZombieLogic()
{
bool zom[5];
for (int i = 0; i < 5; i++)
{
zom[i] = false; //先假设所有行都没有僵尸
}
for (int i = 0; i < zombiesVector.size(); i++)
{
zom[zombiesVector.at(i).row] = true; //有僵尸置为真
}
for (int i = 0; i < plantVector.size(); i++)
{
if (plantVector.at(i).pointNum == 1)//豌豆射手
{
if (zom[plantVector.at(i).position.Y] == true)//所在行有僵尸
{
if (trueFrame % 50 == 0) //子弹发射间隔
{
BULLET_INFO info;
info.pointNum = 1;
info.position.X = (plantVector.at(i).position.X + 1) * PlantWidth + CarXSpace + CarWidth;//x坐标
info.position.Y = plantVector.at(i).position.Y;//第几行
bulletVector.push_back(info);
}
}
}
if (plantVector.at(i).pointNum == 3)//另一个射手
{
if (zom[plantVector.at(i).position.Y] == true)//所在行有僵尸
{
if (trueFrame % 50 == 0) //子弹发射间隔
{
BULLET_INFO info;
info.pointNum = 3;
info.position.X = (plantVector.at(i).position.X + 1) * PlantWidth + CarXSpace + CarWidth;//x坐标
info.position.Y = plantVector.at(i).position.Y;//第几行
bulletVector.push_back(info);
}
}
}
}
}
int GameLevel2::CardMouseClick(int x, int y)
{
POINT pt;
pt.x = x;
pt.y = y;
RECT rec;
for (int i = 0; i < MAXPLANTNUM; i++)
{
rec.left = plantCard[i].info.x;
rec.top = plantCard[i].info.y;
rec.right = rec.left + plantCard[i].card->GetImageWidth()*0.8;
rec.bottom = rec.top + plantCard[i].card->GetImageHeight() / 2 * 0.8;
if (PtInRect(&rec, pt))
{
return i;
}
}
return -1;
}
void GameLevel2::MouseClick(int x, int y)
{
if (!pointState)
{
int card = CardMouseClick(x, y);
if (card != -1 && plantCard[card].state == true && sunlight >= plantCard[card].info.sunlight)
{
pointState = true;
pointPlant = card;
}
}
else
{
PlantMouseClick(x, y);
pointState = false;
}
sunlightMouseClick(x, y);
}
void GameLevel2::PlantMouseClick(int x, int y)
{
if (x < CarXSpace + CarWidth || y < YSpace + PlantHeight*INDEXROW || x > CarXSpace + CarWidth + PlantWidth*MAXCOLUMN || y>PlantHeight*MAXROW + YSpace + PlantHeight*(INDEXROW + 3))
{
return;
}
PLANT_INFO tempPlant;
x = x - CarXSpace - CarWidth;
y = y - YSpace;
tempPlant.position.X = x / PlantWidth;
tempPlant.position.Y = y / PlantHeight;
for (int i = 0; i < plantVector.size(); i++)
{
if (plantVector.at(i).position.X == tempPlant.position.X&&plantVector.at(i).position.Y == tempPlant.position.Y)
{
return;
}
}
click_buffer.Play(false);
tempPlant.sprite = spritePlant[pointPlant];
tempPlant.pointNum = pointPlant;
SPRITEINFO info;
info.Active = true;
info.Dead = false;
info.Dir = DIR_RIGHT;
info.Rotation = TRANS_NONE;
info.Ratio = 1.0f;
info.Level = 0;
info.Score = 0;
info.Speed = 0;
info.X = tempPlant.position.X*PlantWidth + CarXSpace + CarWidth + 5;
info.Y = tempPlant.position.Y*PlantHeight + YSpace + 10;
info.Alpha = 255;
info.Visible = true;
tempPlant.info = info;
tempPlant.life = 50;
tempPlant.attacked = false;
plantVector.push_back(tempPlant);
sunlight = sunlight - plantCard[pointPlant].info.sunlight;
plantCard[pointPlant].nowTime = 0;
}
void GameLevel2::attackPlantLogic()
{
int LineZoms[5][MAXZOMBIESNUM] = { WIN_WIDTH + 1 }; //每行的僵尸位置
int LinePlants[5][9] = { 0 }; //每行的植物
for (int m = 0; m < 5; m++)
{
int n = 0;
for (int i = 0; i < zombiesVector.size(); i++) {
if (zombiesVector.at(i).row == m) {
LineZoms[m][n] = zombiesVector.at(i).x;
n++;
}
}
}
for (int m = 0; m < 5; m++)
{
int n = 0;
for (int i = 0; i < plantVector.size(); i++) {
if (plantVector.at(i).position.Y == m) {
LinePlants[m][n] = plantVector.at(i).position.X * PlantWidth + CarWidth + CarXSpace + 5;
n++;
}
}
}
vector<ZOMBIES_INFO>::iterator it;
vector<PLANT_INFO>::iterator iter;
for (int m = 0; m < 5; m++)
{
for (int n = 0; n < 9; n++)
{
for (int i = 0; i < MAXZOMBIESNUM; i++)
{
if (abs(LineZoms[m][i] + 70 - LinePlants[m][n]) <= 30)
{
for (it = zombiesVector.begin(); it != zombiesVector.end(); it++)
{
if (abs(LineZoms[m][i] - it->x) <= 30)
{
it->sprite->SetSpeed(0);
for (iter = plantVector.begin(); iter != plantVector.end(); iter++)
{
if (LinePlants[m][n] == iter->info.X)
{
it->sprite->SetSpeed(0);
if (iter->pointNum == 2 && iter->attacked == false) {
iter->sprite = CherryBomb;
iter->sprite->Initiate(iter->info);
it->info.X = it->sprite->GetX();
it->info.Y = it->sprite->GetY();
it->sprite = attackedZombies[5]; //被炸死
it->sprite->Initiate(it->info);
it->sprite->SetFrame(0);
it->isChanged = true;
it->count = BULLETATTACKETIMES;
iter->attacked = true;
break;
}
if (iter->pointNum == 4 && iter->attacked == false) {
iter->info.X = iter->sprite->GetX();
iter->info.Y = iter->sprite->GetY();
iter->sprite = WallNutAte;
iter->sprite->Initiate(iter->info);
}
if (it->count >= BULLETATTACKETIMES && iter->attacked == true && it->isChanged == false)
{
eatPlant_buffer.Play(false);
it->info.X = it->sprite->GetX();
it->info.Y = it->sprite->GetY();
it->sprite = attackedZombies[2]; //掉头还不忘记吃的僵尸
it->sprite->Initiate(it->info);
iter->attacked = false;
}
if (iter->attacked == false)
{
eatPlant_buffer.Play(false);
if (it->count < BULLETATTACKETIMES)
{
it->info.Speed = 0;
it->info.X = it->sprite->GetX();
it->info.Y = it->sprite->GetY();
it->sprite = attackedZombies[4];
it->sprite->Initiate(it->info);
}
else
{
it->info.Speed = 0;
it->info.X = it->sprite->GetX();
it->info.Y = it->sprite->GetY();
it->sprite = attackedZombies[2];
it->sprite->Initiate(it->info);
}
iter->attacked = true;
}
iter->life--;
if (iter->life <= 0)
{
eatPlant_buffer.Stop();
iter = plantVector.erase(iter);
if (it->count < BULLETATTACKETIMES)
{
it->sprite->SetSpeed(3);
it->info.Speed = 3;
it->sprite->SetVisible(true);
it->sprite = spriteZombie[it->typeNum];
it->sprite->Initiate(it->info);
}
if (iter == plantVector.end()) {
break;
}
}
break;
}
}
break;
}
}
}
}
}
}
}
void GameLevel2::bulletLogic()
{
vector<BULLET_INFO>::iterator iter;
int firstzomX[5];
for (int i = 0; i < 5; i++)
{
firstzomX[i] = WIN_WIDTH + 1; //初始化
}
for (int i = 0; i < zombiesVector.size(); i++)
{
if (zombiesVector.at(i).x < firstzomX[zombiesVector.at(i).row])
{
firstzomX[zombiesVector.at(i).row] = zombiesVector.at(i).x;//遍历出每行第一个僵尸的地址
}
}
for (iter = bulletVector.begin(); iter != bulletVector.end(); iter++)
{
if (iter->position.X >= (firstzomX[iter->position.Y] + 60))
{
//击中僵尸
iter->hit = true;
bulletZom_buffer.Play(false);
}
if (iter->hit == false)
{
iter->position.X += BULLET_LENGTH;
}
else
{
iter->frame--;
if (iter->frame <= 0)
{
for (int i = 0; i < zombiesVector.size(); i++)
{
if (zombiesVector.at(i).x == firstzomX[iter->position.Y]) //找到该行第一个僵尸
{
if (iter->pointNum == 1) {
zombiesVector.at(i).count++; //僵尸减血次数加1
}
if (iter->pointNum == 3) {
zombiesVector.at(i).count = zombiesVector.at(i).count + 2; //僵尸减血次数加2
}
break;
}
}
iter = bulletVector.erase(iter);
}
/*判断是否越界*/
if (iter == bulletVector.end()) {
break;
}
}
if (iter->position.X >= WIN_WIDTH) {
iter = bulletVector.erase(iter);
/*判断是否越界*/
if (iter == bulletVector.end()) {
break;
}
}
}
}