-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathpea.cpp
47 lines (42 loc) · 1.12 KB
/
pea.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
#include "pea.h"
#include "zombie.h"
Pea::Pea(int attack, bool flag)
{
snow = flag;
atk = attack;
speed = 360.0 * 33 / 1000;
}
QRectF Pea::boundingRect() const
{
return QRectF(-12, -28, 24, 24);
}
void Pea::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->drawPixmap(QRect(-12, -28, 24, 24), QPixmap(snow ? ":/images/PeaSnow.png" : ":/images/Pea.png"));
}
bool Pea::collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode) const
{
Q_UNUSED(mode)
return other->type() == Zombie::Type && qFuzzyCompare(other->y(), y()) && qAbs(other->x() - x()) < 15;
}
void Pea::advance(int phase)
{
if (!phase)
return;
update();
QList<QGraphicsItem *> items = collidingItems();
if (!items.isEmpty())
{
Zombie *zombie = qgraphicsitem_cast<Zombie *>(items[qrand() % items.size()]);
zombie->hp -= atk;
if (snow && zombie->speed > 0.55)
zombie->speed /= 2;
delete this;
return;
}
setX(x() + speed);
if (x() > 1069)
delete this;
}