-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregularzombie.cpp
More file actions
139 lines (114 loc) · 3.46 KB
/
regularzombie.cpp
File metadata and controls
139 lines (114 loc) · 3.46 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
128
129
130
131
132
133
134
135
136
137
138
139
#include "regularzombie.h"
#include <QDebug>
#include "lawnmower.h"
RegularZombie::RegularZombie(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/regularZombie");
equipmentLife = 0;
zombieLife = 10;
damage = 1;
attackRate = 500;
xVelocity = 0.30;
attackCounter = new QTime;
collisionLine = new QGraphicsLineItem;
}
RegularZombie::~RegularZombie()
{
delete zombieImage;
delete attackCounter;
delete collisionLine;
}
QRectF RegularZombie::boundingRect() const
{
return QRectF(0,0,zombieImage->width(),zombieImage->height());
}
void RegularZombie::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
//Paints zombie pixmap representation to screen with boundingRect as source and target rect
if(!isSlowed)
painter->drawPixmap(boundingRect(),*zombieImage,boundingRect());
else
{
//Deletes current zombie image
delete zombieImage;
//Adjusted image if zombie is slowed
zombieImage = new QPixmap(":/Images/regularZombieSlowed");
painter->drawPixmap(boundingRect(),*zombieImage,boundingRect());
}
}
void RegularZombie::advance(int phase)
{
if(!phase) return;
//Deletes when health is below 0
if(zombieLife <= 0)
{
delete this;
return;
}
//change zombie image if zombie is attacking
if(isAttacking)
{
delete zombieImage;
zombieImage = new QPixmap(":/Images/regularzombieAttack");
update();
}
else
{
delete zombieImage;
zombieImage = new QPixmap(":/Images/regularZombie");
update();
}
move(); //moves zombie based on velocity
this->setPos(xCordinate,yCordinate); //updates pos
//Checks if zombies are past homeblock
if(this->x() < spawnRowX - zombieImage->width())
zombiesWin = true;
}
void RegularZombie::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;
}