-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucketheadzombie.cpp
More file actions
128 lines (104 loc) · 3.18 KB
/
bucketheadzombie.cpp
File metadata and controls
128 lines (104 loc) · 3.18 KB
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
#include "bucketheadzombie.h"
#include "lawnmower.h"
BucketHeadZombie::BucketHeadZombie(QRect *spawn_row)
{
xCordinate = spawn_row->x() + spawn_row->width();
yCordinate = spawn_row->y();
spawnRowX = spawn_row->x();
this->setPos(xCordinate,yCordinate);
zombieImage = new QPixmap(":/Images/buckethead");
equipmentLife = 5;
zombieLife = 10;
damage = 1;
attackRate = 500;
xVelocity = 0.30;
attackCounter = new QTime;
collisionLine = new QGraphicsLineItem;
}
BucketHeadZombie::~BucketHeadZombie()
{
delete zombieImage;
delete attackCounter;
delete collisionLine;
}
QRectF BucketHeadZombie::boundingRect() const
{
return QRectF(0,0,zombieImage->width(),zombieImage->height());
}
void BucketHeadZombie::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
//Paints zombie pixmap representation to screen with boundingRect as source and target rect
painter->drawPixmap(boundingRect(),*zombieImage,boundingRect());
}
void BucketHeadZombie::advance(int phase)
{
if(!phase) return;
//Deletes zombie if health is 0 or less
if(zombieLife <= 0)
{
delete this;
return;
}
move(); //moves zombie based on velocity
//change zombie image if zombie is attacking
if(isAttacking)
{
delete zombieImage;
zombieImage = new QPixmap(":/Images/bucketheadAttack");
update();
}
else
{
delete zombieImage;
zombieImage = new QPixmap(":/Images/buckethead");
update();
}
this->setPos(xCordinate,yCordinate); //updates pos
//Checks if zombies are past homeblock
if(this->x() < spawnRowX - zombieImage->width())
zombiesWin = true;
}
void BucketHeadZombie::move()
{
int y_adj = 40;
collisionLine->setLine(xCordinate,yCordinate+y_adj,xCordinate+y_adj,yCordinate+y_adj);
//Creates a list of items currently colliding with the mask
QList<QGraphicsItem *> collision_list = scene()->collidingItems(collisionLine);
//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)
{
if(item->isTargetable)
{
if(attackCounter->isNull()) //Attacks and starts counter
{
attackCounter->start();
}
else if(attackCounter->elapsed() >= attackRate) //attacks every 500 ms
{
attack(item);
attackCounter->restart(); //restarts counter
isAttacking = true;
}
}
else if(!item->isTargetable)
{
xCordinate -= xVelocity;
isAttacking = false;
}
return;
}
LawnMower *item2 = dynamic_cast<LawnMower *>(collision_list.at(i));
if(item2)
{
item2->activateMovement();
zombieLife -= 9999;
return;
}
}
//Updates the x cordinate based on velocity
xCordinate -= xVelocity;
isAttacking = false;
}