-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchomper.cpp
101 lines (86 loc) · 3.35 KB
/
chomper.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
#include "chomper.h"
#include "zombie.h"
#include <QDebug>
int Chomper::seedingTime = 7500;
Chomper::Chomper(QRect *lawn_tile)
{
//chomper properties
life = 4;
cost = 150;
plantDamage = 150;
fireRate = 42000;
isEating = false; //intially chomper is not eating
//Sets up position of chomper based on the tile it is planted on
this->setPos(lawn_tile->x(),lawn_tile->y()+10 /*+10 used for better center to tile*/);
//Sets ups pixmap for both states
chomperImage = new QPixmap(":/Images/chomper");
chomperEatingImage = new QPixmap(":/Images/chomperEating");
//Creates and starts counter
resetTimer = new QTime;
resetTimer->start();
int y_adj = 15; //This value is used to create a rect that is more centered in the lawn plot
//and thus zombie collision is more accurate since zombies in different lanes have a smaller
//chance of trigger collision events in another lane
//Making collision mask to detect zombie one tile infront of chomper's current tile
collisionRect = new QGraphicsRectItem(lawn_tile->x()+lawn_tile->width(),
lawn_tile->y() + y_adj,
lawn_tile->width()/2, //make the range of detection smaller in front of plant
//allow the zombie to be closer to center of tile before
//destroying it
lawn_tile->height() - y_adj);
}
Chomper::~Chomper()
{
delete chomperImage;
delete chomperEatingImage;
delete resetTimer;
}
QRectF Chomper::boundingRect() const
{
return QRectF(0,0,chomperImage->width(),chomperImage->height());
}
void Chomper::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
//Draws chomper pixmap based on what state it is in
if(!isEating)
painter->drawPixmap(boundingRect(),*chomperImage,boundingRect());
else
painter->drawPixmap(boundingRect(),*chomperEatingImage,boundingRect());
}
void Chomper::advance(int phase)
{
if(!phase) return;
//Deletes chomper if health is 0 or less
if(life <= 0)
{
delete this;
return;
}
//Checks to see if counter has reached the fireRate and chomper is not eating
if(resetTimer->elapsed() >= fireRate && !isEating)
{
//Creates a list of items currently colliding with the mask
QList<QGraphicsItem *> collision_list = scene()->collidingItems(collisionRect);
//Checks for zombies colliding with mask
for(int i = 0; i < (int)collision_list.size(); i++)
{
Zombie * item = dynamic_cast<Zombie *>(collision_list.at(i));
if (item)
{
//Damages zombie, restarts timer and changes state
item->decreaseLife(plantDamage);
resetTimer->restart();
isEating = true;
update(); //updates chomper image since state just changed
return;
}
}
}
//Checks to see if counter has reached the fireRate and chomper is eating
else if(isEating && resetTimer->elapsed() >= fireRate)
{
//Used to revert chomper's active state and not eating zombie pixmap
isEating = false;
update();
}
}