-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamescreen.cpp
1203 lines (1034 loc) · 42.5 KB
/
gamescreen.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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gamescreen.h"
#include <QDebug>
#include "mainwindow.h"
#include <QSpacerItem>
#include <QGridLayout>
GameScreen::GameScreen(QWidget *parent) :
QGraphicsView(parent), sunSpawnInterval(10000),
mouseCursor(0), lastZombieSpawned(false)
{
//Makes a graphics view of following size
this->setFixedHeight(760);
this->setFixedWidth(1032);
//Enables mouse tracking
this->setMouseTracking(true);
//Creates a new scene the same size as the view
scene = new QGraphicsScene(this);
this->setSceneRect(0,0,geometry().width(),geometry().height());
this->setScene(scene);
//Creates the background of the view
QPixmap background(":/Images/pvzBackground");
scene->addPixmap(background);
//Sets a custom cursor
mouseCursor = new QCursor(QPixmap(":/Images/mainCursor"),0,0);
this->setCursor(*mouseCursor);
//Creates background for updating hud elements
QPixmap score_Background_note(":/Images/scoreBackgroundNote");
QPixmap score_Background(":/Images/scoreBackground");
scene->addPixmap(score_Background_note);
scene->addPixmap(score_Background)->setPos(score_Background.width(),0);
scene->addPixmap(score_Background)->setPos(score_Background.width()*2,0);
/*Adding clickable plant icons*/
//Aligning to y-point and x-point
int y_alignment = 50;
int x_adj = 50;
//Loading pixmap of each card
peashooterCard = new QGraphicsPixmapItem(QPixmap(":/Images/peashooterCard"));
sunflowerCard = new QGraphicsPixmapItem(QPixmap(":/Images/sunflowerCard"));
walnutCard = new QGraphicsPixmapItem(QPixmap(":/Images/walnutCard"));
cherrybombCard = new QGraphicsPixmapItem(QPixmap(":/Images/cherrybombCard"));
chomperCard = new QGraphicsPixmapItem(QPixmap(":/Images/chomperCard"));
snowpeashooterCard = new QGraphicsPixmapItem(QPixmap(":/Images/snowpeaCard"));
repeaterCard = new QGraphicsPixmapItem(QPixmap(":/Images/repeaterCard"));
potatomineCard = new QGraphicsPixmapItem(QPixmap(":/Images/potatomineCard"));
//Setting their positions, making them selectable and adding them to the scene
//peashooter
peashooterCard->setPos(score_Background.width()+40,y_alignment);
peashooterCard->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(peashooterCard);
//sunflower Card
sunflowerCard->setPos(peashooterCard->x()+x_adj,y_alignment);
sunflowerCard->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(sunflowerCard);
//walnut Card
walnutCard->setPos(sunflowerCard->x()+x_adj,y_alignment);
walnutCard->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(walnutCard);
//cherrybomb Card
cherrybombCard->setPos(walnutCard->x()+x_adj,y_alignment);
cherrybombCard->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(cherrybombCard);
//chomper card
chomperCard->setPos(score_Background.width()*2 + 40, y_alignment);
chomperCard->setFlag((QGraphicsItem::ItemIsSelectable));
scene->addItem(chomperCard);
//snowpea card
snowpeashooterCard->setPos(chomperCard->x()+x_adj,y_alignment);
snowpeashooterCard->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(snowpeashooterCard);
//repeater card
repeaterCard->setPos(snowpeashooterCard->x()+x_adj,y_alignment);
repeaterCard->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(repeaterCard);
//potatomine card
potatomineCard->setPos(repeaterCard->x()+x_adj,y_alignment);
potatomineCard->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(potatomineCard);
/*End of adding selectable cards to scene*/
//Adding all plants' seeding timers
peashooterTimer = new QTimer(this);
sunflowerTimer = new QTimer(this);
cherrybombTimer = new QTimer(this);
walnutTimer = new QTimer(this);
repeaterTimer = new QTimer(this);
chomperTimer = new QTimer(this);
snowpeashooterTimer = new QTimer(this);
potatomineTimer = new QTimer(this);
//Connecting all the timers to coressponding countdown slots
connect(peashooterTimer,SIGNAL(timeout()),this,SLOT(peashooterCountdown()));
connect(sunflowerTimer,SIGNAL(timeout()),this,SLOT(sunflowerCountdown()));
connect(cherrybombTimer,SIGNAL(timeout()),this,SLOT(cherrybombCountdown()));
connect(walnutTimer,SIGNAL(timeout()),this,SLOT(walnutCountdown()));
connect(repeaterTimer,SIGNAL(timeout()),this,SLOT(repeaterCountdown()));
connect(chomperTimer,SIGNAL(timeout()),this,SLOT(chomperCountdown()));
connect(snowpeashooterTimer,SIGNAL(timeout()),this,SLOT(snowpeashooterCountdown()));
connect(potatomineTimer,SIGNAL(timeout()),this,SLOT(potatomineCountdown()));
//Adds dynamic hud elements
Hud = new GameHud;
scene->addItem(Hud);
//Creates Rect item used to cover cards during countdown so user knows when countdown finishes
peashooterRect = new QGraphicsRectItem(peashooterCard->boundingRect());
sunflowerRect = new QGraphicsRectItem(peashooterCard->boundingRect());
cherrybombRect = new QGraphicsRectItem(peashooterCard->boundingRect());
walnutRect = new QGraphicsRectItem(peashooterCard->boundingRect());
chomperRect = new QGraphicsRectItem(peashooterCard->boundingRect());
repeaterRect = new QGraphicsRectItem(peashooterCard->boundingRect());
potatomineRect = new QGraphicsRectItem(peashooterCard->boundingRect());
snowpeashooterRect = new QGraphicsRectItem(peashooterCard->boundingRect());
/*Adding rectangles to the scene - Start */
peashooterRect->setOpacity(0.3);
peashooterRect->setBrush(QBrush(Qt::black));
peashooterRect->setPos(peashooterCard->pos());
scene->addItem(peashooterRect);
sunflowerRect->setOpacity(0.3);
sunflowerRect->setBrush(QBrush(Qt::black));
sunflowerRect->setPos(sunflowerCard->pos());
scene->addItem(sunflowerRect);
cherrybombRect->setOpacity(0.3);
cherrybombRect->setBrush(QBrush(Qt::black));
cherrybombRect->setPos(cherrybombCard->pos());
scene->addItem(cherrybombRect);
walnutRect->setOpacity(0.3);
walnutRect->setBrush(QBrush(Qt::black));
walnutRect->setPos(walnutCard->pos());
scene->addItem(walnutRect);
chomperRect->setOpacity(0.3);
chomperRect->setBrush(QBrush(Qt::black));
chomperRect->setPos(chomperCard->pos());
scene->addItem(chomperRect);
repeaterRect->setOpacity(0.3);
repeaterRect->setBrush(QBrush(Qt::black));
repeaterRect->setPos(repeaterCard->pos());
scene->addItem(repeaterRect);
potatomineRect->setOpacity(0.3);
potatomineRect->setBrush(QBrush(Qt::black));
potatomineRect->setPos(potatomineCard->pos());
scene->addItem(potatomineRect);
snowpeashooterRect->setOpacity(0.3);
snowpeashooterRect->setBrush(QBrush(Qt::black));
snowpeashooterRect->setPos(snowpeashooterCard->pos());
scene->addItem(snowpeashooterRect);
/*End of adding graphics rect*/
//Refresh rate of 20 ms
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),scene,SLOT(advance()));
connect(timer,SIGNAL(timeout()),this,SLOT(checkSunPoints()));
timer->start(20);
//Spawns new sun every 10 seconds
sunSpawnTimer = new QTimer(this);
connect(sunSpawnTimer,SIGNAL(timeout()),this,SLOT(spawnSun()));
sunSpawnTimer->start(sunSpawnInterval);
//Load level elements
loadLevel();
//Setting up zombie spawn timers
spawnFirstZombie = new QTimer(this);
spawnNextZombie = new QTimer(this);
zombieSquenceTime = startingInterval;
//Connecting zombie spawn timer
connect(spawnFirstZombie, SIGNAL(timeout()),this,SLOT(startZombieSpawning()));
connect(spawnNextZombie,SIGNAL(timeout()),this,SLOT(spawnZombie()));
//Trigger to spawn first zombie
spawnFirstZombie->start(startTime);
//Adds restart button
resetButton = new QGraphicsPixmapItem(QPixmap(":/Images/restart.png"));
resetButton->setPos(potatomineRect->x()+100,potatomineRect->y());
resetButton->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(resetButton);
}
GameScreen::~GameScreen()
{
delete mouseCursor;
delete scene;
qDebug() << "dest called";
}
void GameScreen::setPlayerInfo(QString name, QString level)
{
//Sets player in for gamescreen and hud instance
Hud->user = name;
Hud->level = level;
}
void GameScreen::closeEvent(QCloseEvent *event)
{
timer->stop(); // pauses the scene's advance calls
int remember_sun_spawn_timer = sunSpawnTimer->remainingTime();
sunSpawnTimer->stop(); //Stops new suns from spawning
Sun::isPaused = true; //Changes state for all suns in the scene
scene->advance(); //Advances to allow suns to activate Sun::pause() func
//Asks user if they want to exit the level
QMessageBox exit_message;
QSpacerItem* horizontalSpacer = new QSpacerItem(560,288, QSizePolicy::Minimum, QSizePolicy::Maximum);
exit_message.setText( "Are you sure you want to quit this level?" );
exit_message.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
QGridLayout* layout = (QGridLayout*)exit_message.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
int result = exit_message.exec();
if(result == exit_message.Yes) //If yes is selected
{
deleteGameWindow(); //Signals to delete gamewindow
event->accept(); //closes game window
showMainWindow(); //signals to display main window
}
else if(result == exit_message.No) //If cancel is selected or window is closed
{
event->ignore(); //resumes game
Sun::isPaused = false; //Changes back the state for all suns in the scene
timer->start(20); //reactivates the advance and timeout() connection
//Sun spawn timer interval is the remaining time from pause
sunSpawnTimer->start(remember_sun_spawn_timer);
}
}
void GameScreen::mousePressEvent(QMouseEvent *e)
{
QGraphicsView::mousePressEvent(e);
//Checks to see which card(if any) is selected when mouse is pressed
if(peashooterCard->isSelected() && Sun::getSunPoints() >= 100)
{
mouseState = 1; //Gives permission to plant peashooter
//Changes mouse cursor to reflect clicked plant
delete mouseCursor;
mouseCursor = new QCursor(QPixmap(":/Images/peashooterGray"));
this->setCursor(*mouseCursor);
//Decreases opacity of other cards to indicate selected one is active
peashooterCard->setOpacity(1);
sunflowerCard->setOpacity(0.5);
walnutCard->setOpacity(0.5);
cherrybombCard->setOpacity(0.5);
chomperCard->setOpacity(0.5);
snowpeashooterCard->setOpacity(0.5);
repeaterCard->setOpacity(0.5);
potatomineCard->setOpacity(0.5);
//Leaves function
return;
}
else if(sunflowerCard->isSelected() && Sun::getSunPoints() >= 50)
{
mouseState = 2; //Gives permission to plant sunflower
//Changes mouse cursor to reflect clicked plant
delete mouseCursor;
mouseCursor = new QCursor(QPixmap(":/Images/sunflowerGray"));
this->setCursor(*mouseCursor);
//Decreases opacity of other cards to indicate selected one is active
sunflowerCard->setOpacity(1);
peashooterCard->setOpacity(0.5);
walnutCard->setOpacity(0.5);
cherrybombCard->setOpacity(0.5);
chomperCard->setOpacity(0.5);
snowpeashooterCard->setOpacity(0.5);
repeaterCard->setOpacity(0.5);
potatomineCard->setOpacity(0.5);
//Leaves function
return;
}
else if(walnutCard->isSelected() && Sun::getSunPoints() >= 50)
{
mouseState = 3; //Gives permission to plant walnut
//Changes mouse cursor to reflect clicked plant
delete mouseCursor;
mouseCursor = new QCursor(QPixmap(":/Images/walnutGray"));
this->setCursor(*mouseCursor);
//Decreases opacity of other cards to indicate selected one is active
walnutCard->setOpacity(1);
peashooterCard->setOpacity(0.5);
sunflowerCard->setOpacity(0.5);
cherrybombCard->setOpacity(0.5);
chomperCard->setOpacity(0.5);
snowpeashooterCard->setOpacity(0.5);
repeaterCard->setOpacity(0.5);
potatomineCard->setOpacity(0.5);
//Leaves function
return;
}
else if(cherrybombCard->isSelected() && Sun::getSunPoints() >= 150)
{
mouseState = 4; //Gives permission to plant cherrybomb
//Changes mouse cursor to reflect clicked plant
delete mouseCursor;
mouseCursor = new QCursor(QPixmap(":/Images/cherrybombGray"));
this->setCursor(*mouseCursor);
//Decreases opacity of other cards to indicate selected one is active
cherrybombCard->setOpacity(1);
peashooterCard->setOpacity(0.5);
sunflowerCard->setOpacity(0.5);
walnutCard->setOpacity(0.5);
chomperCard->setOpacity(0.5);
snowpeashooterCard->setOpacity(0.5);
repeaterCard->setOpacity(0.5);
potatomineCard->setOpacity(0.5);
//Leaves function
return;
}
else if(chomperCard->isSelected() && Sun::getSunPoints() >= 150)
{
mouseState = 5; //Gives permission to plant chomper
//Changes mouse cursor to reflect clicked plant
delete mouseCursor;
mouseCursor = new QCursor(QPixmap(":/Images/chomperGray"));
this->setCursor(*mouseCursor);
//Decreases opacity of other cards to indicate selected one is active
chomperCard->setOpacity(1);
cherrybombCard->setOpacity(0.5);
peashooterCard->setOpacity(0.5);
sunflowerCard->setOpacity(0.5);
walnutCard->setOpacity(0.5);
snowpeashooterCard->setOpacity(0.5);
repeaterCard->setOpacity(0.5);
potatomineCard->setOpacity(0.5);
//Leaves function
return;
}
else if(snowpeashooterCard->isSelected() && Sun::getSunPoints() >= 175)
{
mouseState = 6; //Gives permission to plant snowpea
//Changes mouse cursor to reflect clicked plant
delete mouseCursor;
mouseCursor = new QCursor(QPixmap(":/Images/snowpeaGray"));
this->setCursor(*mouseCursor);
//Decreases opacity of other cards to indicate selected one is active
snowpeashooterCard->setOpacity(1);
cherrybombCard->setOpacity(0.5);
peashooterCard->setOpacity(0.5);
sunflowerCard->setOpacity(0.5);
walnutCard->setOpacity(0.5);
chomperCard->setOpacity(0.5);
repeaterCard->setOpacity(0.5);
potatomineCard->setOpacity(0.5);
//Leaves function
return;
}
else if(repeaterCard->isSelected() && Sun::getSunPoints() >= 200)
{
mouseState = 7; //Gives permission to plant repeater
//Changes mouse cursor to reflect clicked plant
delete mouseCursor;
mouseCursor = new QCursor(QPixmap(":/Images/repeaterGray"));
this->setCursor(*mouseCursor);
//Decreases opacity of other cards to indicate selected one is active
repeaterCard->setOpacity(1);
cherrybombCard->setOpacity(0.5);
peashooterCard->setOpacity(0.5);
sunflowerCard->setOpacity(0.5);
walnutCard->setOpacity(0.5);
chomperCard->setOpacity(0.5);
snowpeashooterCard->setOpacity(0.5);
potatomineCard->setOpacity(0.5);
//Leaves function
return;
}
else if(potatomineCard->isSelected() && Sun::getSunPoints() >= 25)
{
mouseState = 8; //Gives permission to potatomine
//Changes mouse cursor to reflect clicked plant
delete mouseCursor;
mouseCursor = new QCursor(QPixmap(":/Images/potatomineGray"));
this->setCursor(*mouseCursor);
//Decreases opacity of other cards to indicate selected one is active
potatomineCard->setOpacity(1);
cherrybombCard->setOpacity(0.5);
peashooterCard->setOpacity(0.5);
sunflowerCard->setOpacity(0.5);
walnutCard->setOpacity(0.5);
chomperCard->setOpacity(0.5);
snowpeashooterCard->setOpacity(0.5);
repeaterCard->setOpacity(0.5);
//Leaves function
return;
}
else if(resetButton->isSelected())
{
timer->stop(); // pauses the scene's advance calls
int remember_sun_spawn_timer = sunSpawnTimer->remainingTime();
sunSpawnTimer->stop(); //Stops new suns from spawning
Sun::isPaused = true; //Changes state for all suns in the scene
scene->advance(); //Advances to allow suns to activate Sun::pause() func
//Asks user if they want to exit the level
QMessageBox exit_message;
QSpacerItem* horizontalSpacer = new QSpacerItem(560,288, QSizePolicy::Minimum, QSizePolicy::Maximum);
exit_message.setText( "Are you sure you want to restart?" );
exit_message.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
QGridLayout* layout = (QGridLayout*)exit_message.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
int result = exit_message.exec();
if(result == exit_message.Yes) //If yes is selected
{
levelWin(false);
}
else if(result == exit_message.No) //If cancel is selected or window is closed
{
Sun::isPaused = false; //Changes back the state for all suns in the scene
timer->start(20); //reactivates the advance and timeout() connection
//Sun spawn timer interval is the remaining time from pause
sunSpawnTimer->start(remember_sun_spawn_timer);
}
}
else if(mouseState != 0)
{
//Checks if user clicked to plant
addPlant(e->x(),e->y());
}
}
void GameScreen::mouseMoveEvent(QMouseEvent *e)
{
QGraphicsView::mouseMoveEvent(e);
//Displays tooltip information for each plant selection card
if(peashooterCard->isUnderMouse())
{
this->setToolTip("Pea Shooter\n Cost: 100");
}
else if(sunflowerCard->isUnderMouse())
this->setToolTip("Sun Flower\n Cost: 50");
else if(walnutCard->isUnderMouse())
this->setToolTip("Wall Nut\n Cost: 50");
else if(cherrybombCard->isUnderMouse())
this->setToolTip("Cherry Bomb\n Cost: 150");
else if(chomperCard->isUnderMouse())
this->setToolTip("Chomper\n Cost: 150");
else if(snowpeashooterCard->isUnderMouse())
this->setToolTip("Snowpea Shooter\n Cost: 175");
else if(repeaterCard->isUnderMouse())
this->setToolTip("Repeater\n Cost: 200");
else if(potatomineCard->isUnderMouse())
this->setToolTip("Potato Mine\n Cost: 25");
else
this->setToolTip("");
}
void GameScreen::addPlant(int m_x, int m_y)
{
//Uses activeRows to only allow planting on active rows
int start_row(0), end_row;
if(activeRows == 1) // middle row active
start_row = 2;
else if(activeRows == 3) //middle 3 rows active
start_row = 1;
else if(activeRows == 5) //all rows active
start_row = 0;
end_row = start_row + activeRows;
for(int i = start_row; i < end_row; i++)
{
for(int j = 0; j < (int)lawnVector.at(i).size(); j++)
{
lawnPiece *temp = &lawnVector[i][j];
if(((m_x >= temp->topX && m_x <= temp->botX) &&
(m_y >= temp->topY && m_y <= temp->botY)))
{
//Checks for plant item in current tile
QGraphicsRectItem *tile = new QGraphicsRectItem(temp->topX,temp->topY,
temp->botX-temp->topX,
temp->botY-temp->topY);
tile->setPen(QPen(Qt::transparent));
scene->addItem(tile);
//Creates a list of items currently colliding with the mask
QList<QGraphicsItem *> collision_list = scene->collidingItems(tile);
if(mouseState != 7)
{
//Checks for zombies colliding with mask and fires if there is atleast one zombie in row
for(int i = 0; i < (int)collision_list.size(); i++)
{
Plant * item = dynamic_cast<Plant *>(collision_list.at(i));
if (item)
{
delete tile;
tile = NULL;
goto reset;
}
}
//deletes the tile object created for collision test
delete tile;
tile = NULL;
}
//Rect that holds information of the row it is planted on
QRect temp_rect;
temp_rect.setX(temp->topX);
temp_rect.setY(temp->topY);
temp_rect.setWidth(scene->width()-temp->topX);
temp_rect.setHeight(temp->botY - temp->topY);
if(mouseState == 1)
{
Peashooter *peashooter = new Peashooter(&temp_rect);
scene->addItem(peashooter);
Sun::updateSunPoints(-peashooter->getCost());
peashooterCard->setFlag(QGraphicsItem::ItemIsSelectable,false);
peashooterTimer->start(Peashooter::seedingTime);
goto reset;
}
else if(mouseState == 2)
{
Sunflower *sunflower = new Sunflower(&temp_rect);
scene->addItem(sunflower);
Sun::updateSunPoints(-sunflower->getCost());
sunflowerCard->setFlag(QGraphicsItem::ItemIsSelectable,false);
sunflowerTimer->start(Sunflower::seedingTime);
goto reset;
}
else if(mouseState == 3)
{
Walnut *walnut = new Walnut(&temp_rect);
scene->addItem(walnut);
Sun::updateSunPoints(-walnut->getCost());
walnutCard->setFlag(QGraphicsItem::ItemIsSelectable,false);
walnutTimer->start(Walnut::seedingTime);
goto reset;
}
else if(mouseState == 4)
{
QRect tile(temp->topX,temp->topY,
temp->botX-temp->topX,
temp->botY-temp->topY);
Cherrybomb *cherrybomb = new Cherrybomb(&tile);
scene->addItem(cherrybomb);
Sun::updateSunPoints(-cherrybomb->getCost());
cherrybombCard->setFlag(QGraphicsItem::ItemIsSelectable,false);
cherrybombTimer->start(Cherrybomb::seedingTime);
goto reset;
}
else if(mouseState == 5)
{
QRect tile(temp->topX,temp->topY,
temp->botX-temp->topX,
temp->botY-temp->topY);
Chomper *chomper = new Chomper(&tile);
scene->addItem(chomper);
Sun::updateSunPoints(-chomper->getCost());
chomperCard->setFlag(QGraphicsItem::ItemIsSelectable,false);
chomperTimer->start(Chomper::seedingTime);
goto reset;
}
else if(mouseState == 6)
{
Peashooter *peashooter = new Peashooter(&temp_rect,true);
scene->addItem(peashooter);
Sun::updateSunPoints(-peashooter->getCost());
snowpeashooterCard->setFlag(QGraphicsItem::ItemIsSelectable,false);
snowpeashooterTimer->start(Peashooter::seedingTime);
goto reset;
}
else if(mouseState == 7)
{
//Checks for zombies colliding with mask and fires if there is atleast one zombie in row
for(int i = 0; i < (int)collision_list.size(); i++)
{
Peashooter * item = dynamic_cast<Peashooter *>(collision_list.at(i));
if (item)
{
delete item;
Repeater *repeater = new Repeater(&temp_rect);
scene->addItem(repeater);
Sun::updateSunPoints(-repeater->getCost());
repeaterCard->setFlag(QGraphicsItem::ItemIsSelectable,false);
repeaterTimer->start(Repeater::seedingTime);
goto reset;
}
}
goto reset; //if no peashooter is found on tile
}
else if(mouseState == 8)
{
QRect tile(temp->topX,temp->topY,
temp->botX-temp->topX,
temp->botY-temp->topY);
Potatomine *potatomine = new Potatomine(&tile);
scene->addItem(potatomine);
Sun::updateSunPoints(-potatomine->getCost());
potatomineCard->setFlag(QGraphicsItem::ItemIsSelectable,false);
potatomineTimer->start(Potatomine::seedingTime);
goto reset;
}
}
}
}
reset:
//Returns the mouse to default state and cursor
mouseState = 0;
setDefaultCursor();
//Resets the opacities after plant is placed
peashooterCard->setOpacity(1);
sunflowerCard->setOpacity(1);
walnutCard->setOpacity(1);
cherrybombCard->setOpacity(1);
chomperCard->setOpacity(1);
snowpeashooterCard->setOpacity(1);
repeaterCard->setOpacity(1);
potatomineCard->setOpacity(1);
}
void GameScreen::setDefaultCursor()
{
//Sets a default custom cursor
delete mouseCursor;
mouseCursor = new QCursor(QPixmap(":/Images/mainCursor"),0,0);
this->setCursor(*mouseCursor);
}
void GameScreen::save()
{
//Finds the level the user is now going to be on
QString new_level = QString::number(MainWindow::userLevel.toInt() + 1);
QFile username_file("/Users/Parth/Documents/QT/RvZ/rvz_players.csv");
QString final_text;
//Displays warning if file is not writable
if(!username_file.open(QIODevice::ReadWrite|QIODevice::Text))
QMessageBox::warning(this,tr("Error!"),tr("Error with rvz_players.csv! No new user created"),
QMessageBox::Ok);
QTextStream modifier(&username_file);
while(!modifier.atEnd())
{
QString temp = modifier.readLine().split(':').at(1);
if(temp != MainWindow::userName)
final_text.append(modifier.readLine() + '\n');
else
final_text.append("");
}
modifier << final_text << QDateTime::currentDateTime().toString("yyyyMMddhhmmss:")
<< MainWindow::userName << ':' << new_level << '\n';
username_file.close();
}
void GameScreen::loadLevel()
{
QString file_name("/Users/Parth/Documents/QT/RvZ/rvz_levels.csv");
QFile level_file(file_name);
//Displays warning if file is not readable and exits program
if(!level_file.open(QIODevice::ReadOnly|QIODevice::Text))
{
QMessageBox::warning(0,"Cannot execute RvZ","rvz_levels.csv does not exist or is unreadable!",
QMessageBox::Ok);
return;
}
QTextStream read_level(&level_file);
while(!read_level.atEnd())
{
QString temp_level;
QStringList temp_list;
temp_list = read_level.readLine().split(':');
temp_level = temp_list.at(0);
if(temp_level == MainWindow::userLevel) //searches for current level
{
//Extracts level information from file
zombieSequence = temp_list.at(1).split(',');
activeRows = temp_list.at(2).toInt();
startTime = temp_list.at(3).toDouble()*1000; //changing to ms
startingInterval = temp_list.at(4).toDouble()*1000;
intervalDecrement = temp_list.at(5).toDouble()*1000;
goto close;
}
}
close:
level_file.close(); //closes file
//Loading lawn from activeRows
//Lawn plot properties
const int lawn_x = 240;//location of where row 1 and column 1 plot is
const int lawn_y = 245;
const int lawn_plot_width = 80; //width of each plot
const int lawn_plot_height = 96; //height of each plot
lawnVector.resize(5); //max five rows
//Generating 2D lawn vector (5x9)
for(int i = 0; i < (int)lawnVector.size(); i++)
{
lawnVector.at(i).resize(9); //max number of columns
for(int j = 0; j < (int)lawnVector.at(i).size(); j++)
{
//Sets the top and bottom cordinates for each plot
lawnPiece temp;
temp.topX = lawn_x + (j*lawn_plot_width);
temp.topY = lawn_y + (i*lawn_plot_height-1);
temp.botX = (temp.topX-1) + lawn_plot_width;
temp.botY = (temp.topY-1) + lawn_plot_height;
lawnVector[i][j] = temp;
}
}
//Adding dead zones to the lawn to show inactive lanes
//Creating inactive rects to use for drawing
QRect inactive_area_1;
QRect inactive_area_2;
//Used to create lawnmowers
LawnMower *lawnmower;
if(activeRows == 5)
{
for(int i = 0; i < 5; i++)
{
//Adding lawn mowers to active rows
QRect tile; //tile where lawnmower spawns
tile.setX(lawnVector[i][0].topX);
tile.setY(lawnVector[i][0].topY);
tile.setWidth(lawn_plot_width);
tile.setHeight(lawn_plot_height);
lawnmower = new LawnMower(&tile,scene->width());
scene->addItem(lawnmower);
}
return;
}
if(activeRows == 1)
{
//Top 2 rows
inactive_area_1.setX(lawnVector[0][0].topX);
inactive_area_1.setY(lawnVector[0][0].topY);
inactive_area_1.setWidth(lawn_plot_width*9);
inactive_area_1.setHeight(lawn_plot_height*2);
//Bottom 2 rows
inactive_area_2.setX(lawnVector[3][0].topX);
inactive_area_2.setY(lawnVector[3][0].topY);
inactive_area_2.setWidth(lawn_plot_width*9);
inactive_area_2.setHeight(lawn_plot_height*2);
//Adding lawn mowers to active rows
QRect tile; //tile where lawnmower spawns
tile.setX(lawnVector[2][0].topX);
tile.setY(lawnVector[2][0].topY);
tile.setWidth(lawn_plot_width);
tile.setHeight(lawn_plot_height);
lawnmower = new LawnMower(&tile,scene->width());
scene->addItem(lawnmower);
}
else if(activeRows == 3)
{
//Top row
inactive_area_1.setX(lawnVector[0][0].topX);
inactive_area_1.setY(lawnVector[0][0].topY);
inactive_area_1.setWidth(lawn_plot_width*9);
inactive_area_1.setHeight(lawn_plot_height);
//Bottom row
inactive_area_2.setX(lawnVector[4][0].topX);
inactive_area_2.setY(lawnVector[4][0].topY);
inactive_area_2.setWidth(lawn_plot_width*9);
inactive_area_2.setHeight(lawn_plot_height);
for(int i = 1; i < 4; i++)
{
//Adding lawn mowers to active rows
QRect tile; //tile where lawnmower spawns
tile.setX(lawnVector[i][0].topX);
tile.setY(lawnVector[i][0].topY);
tile.setWidth(lawn_plot_width);
tile.setHeight(lawn_plot_width);
lawnmower = new LawnMower(&tile,scene->width());
scene->addItem(lawnmower);
}
}
//Creates visual representation on scene of dead zones
QGraphicsRectItem *deadArea = new QGraphicsRectItem(inactive_area_1);
deadArea->setPen(QPen(Qt::transparent));
deadArea->setBrush(QBrush(QColor(139,69,19)));
deadArea->setOpacity(0.4);
scene->addItem(deadArea);
deadArea = new QGraphicsRectItem(inactive_area_2);
deadArea->setPen(QPen(Qt::transparent));
deadArea->setBrush(QBrush(QColor(139,69,19)));
deadArea->setOpacity(0.4);
scene->addItem(deadArea);
}
void GameScreen::spawnSun()
{
/*Accounting for a interval that isn't the desired spawn rate
this when the game is paused the interval is the remaining time
from last countdown*/
if(sunSpawnTimer->interval() != sunSpawnInterval)
{
//Restarts the timer with default sunSpawnInterval
sunSpawnTimer->stop();
sunSpawnTimer->start(sunSpawnInterval);
}
//Spawns a new type 1 sun
sun = new Sun;
scene->addItem(sun);
}
void GameScreen::checkSunPoints()
{
if(Sun::getSunPoints() < 25)
{
peashooterCard->setPixmap(QPixmap(":/Images/peashooterCardGray"));
sunflowerCard->setPixmap(QPixmap(":/Images/sunflowerCardGray"));
cherrybombCard->setPixmap(QPixmap(":/Images/cherrybombCardGray"));
walnutCard->setPixmap(QPixmap(":/Images/walnutCardGray"));
repeaterCard->setPixmap(QPixmap(":/Images/repeaterCardGray"));
chomperCard->setPixmap(QPixmap(":/Images/chomperCardGray"));
potatomineCard->setPixmap(QPixmap(":/Images/potatomineCardGray"));
snowpeashooterCard->setPixmap(QPixmap(":Images/snowpeaCardGray"));
}
else if(Sun::getSunPoints() < 50)
{
peashooterCard->setPixmap(QPixmap(":/Images/peashooterCardGray"));
sunflowerCard->setPixmap(QPixmap(":/Images/sunflowerCardGray"));
cherrybombCard->setPixmap(QPixmap(":/Images/cherrybombCardGray"));
walnutCard->setPixmap(QPixmap(":/Images/walnutCardGray"));
repeaterCard->setPixmap(QPixmap(":/Images/repeaterCardGray"));
chomperCard->setPixmap(QPixmap(":/Images/chomperCardGray"));
potatomineCard->setPixmap(QPixmap(":/Images/potatomineCard"));
snowpeashooterCard->setPixmap(QPixmap(":Images/snowpeaCardGray"));
}
else if(Sun::getSunPoints() < 100)
{
peashooterCard->setPixmap(QPixmap(":/Images/peashooterCardGray"));
sunflowerCard->setPixmap(QPixmap(":/Images/sunflowerCard"));
cherrybombCard->setPixmap(QPixmap(":/Images/cherrybombCardGray"));
walnutCard->setPixmap(QPixmap(":/Images/walnutCard"));
repeaterCard->setPixmap(QPixmap(":/Images/repeaterCardGray"));
chomperCard->setPixmap(QPixmap(":/Images/chomperCardGray"));
potatomineCard->setPixmap(QPixmap(":/Images/potatomineCard"));
snowpeashooterCard->setPixmap(QPixmap(":Images/snowpeaCardGray"));
}
else if(Sun::getSunPoints() < 150)
{
peashooterCard->setPixmap(QPixmap(":/Images/peashooterCard"));
sunflowerCard->setPixmap(QPixmap(":/Images/sunflowerCard"));
cherrybombCard->setPixmap(QPixmap(":/Images/cherrybombCardGray"));
walnutCard->setPixmap(QPixmap(":/Images/walnutCard"));
repeaterCard->setPixmap(QPixmap(":/Images/repeaterCardGray"));
chomperCard->setPixmap(QPixmap(":/Images/chomperCardGray"));
potatomineCard->setPixmap(QPixmap(":/Images/potatomineCard"));
snowpeashooterCard->setPixmap(QPixmap(":Images/snowpeaCardGray"));
}
else if(Sun::getSunPoints() < 175)
{
peashooterCard->setPixmap(QPixmap(":/Images/peashooterCard"));
sunflowerCard->setPixmap(QPixmap(":/Images/sunflowerCard"));
cherrybombCard->setPixmap(QPixmap(":/Images/cherrybombCard"));
walnutCard->setPixmap(QPixmap(":/Images/walnutCard"));
repeaterCard->setPixmap(QPixmap(":/Images/repeaterCardGray"));
chomperCard->setPixmap(QPixmap(":/Images/chomperCard"));
potatomineCard->setPixmap(QPixmap(":/Images/potatomineCard"));
snowpeashooterCard->setPixmap(QPixmap(":Images/snowpeaCardGray"));
}
else if(Sun::getSunPoints() < 200)
{
peashooterCard->setPixmap(QPixmap(":/Images/peashooterCard"));
sunflowerCard->setPixmap(QPixmap(":/Images/sunflowerCard"));
cherrybombCard->setPixmap(QPixmap(":/Images/cherrybombCard"));
walnutCard->setPixmap(QPixmap(":/Images/walnutCard"));
repeaterCard->setPixmap(QPixmap(":/Images/repeaterCardGray"));
chomperCard->setPixmap(QPixmap(":/Images/chomperCard"));
potatomineCard->setPixmap(QPixmap(":/Images/potatomineCard"));
snowpeashooterCard->setPixmap(QPixmap(":Images/snowpeaCard"));
}
else
{
peashooterCard->setPixmap(QPixmap(":/Images/peashooterCard"));
sunflowerCard->setPixmap(QPixmap(":/Images/sunflowerCard"));
cherrybombCard->setPixmap(QPixmap(":/Images/cherrybombCard"));
walnutCard->setPixmap(QPixmap(":/Images/walnutCard"));
repeaterCard->setPixmap(QPixmap(":/Images/repeaterCard"));
chomperCard->setPixmap(QPixmap(":/Images/chomperCard"));
potatomineCard->setPixmap(QPixmap(":/Images/potatomineCard"));
snowpeashooterCard->setPixmap(QPixmap(":/Images/snowpeaCard"));
}
//Peashooter countdown
if(peashooterTimer->remainingTime() == -1) //timer stopped
peashooterRect->hide();
else
peashooterRect->show();
//Sunflower countdown
if(sunflowerTimer->remainingTime() == -1) //timer stopped
sunflowerRect->hide();
else
sunflowerRect->show();
//Cherrybomb countdown
if(cherrybombTimer->remainingTime() == -1) //timer stopped
cherrybombRect->hide();
else
cherrybombRect->show();
//Walnut countdown
if(walnutTimer->remainingTime() == -1) //timer stopped
walnutRect->hide();
else
walnutRect->show();
//Repeater countdown
if(repeaterTimer->remainingTime() == -1) //timer stopped
repeaterRect->hide();