forked from GeeeekExplorer/PVZ
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zombie.cpp
85 lines (77 loc) · 1.81 KB
/
zombie.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
#include "zombie.h"
#include "plant.h"
#include <QDebug>
Zombie::Zombie()
{
movie = head = nullptr;
hp = atk = state = 0;
speed = 0.0;
}
Zombie::~Zombie()
{
delete movie;
delete head;
}
QRectF Zombie::boundingRect() const
{
return QRectF(-80, -100, 200, 140);
}
void Zombie::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
QImage image = movie->currentImage();
if (speed < 0.55 && state != 3)
{
if (state != 2)
movie->setSpeed(50);
int w = image.width();
int h = image.height();
for (int i = 0; i < h; ++i)
{
uchar *line = image.scanLine(i);
for (int j = 5; j < w - 5; ++j)
line[j << 2] = 200;
}
}
painter->drawImage(QRectF(-70, -100, 140, 140), image);
if (head)
{
image = head->currentImage();
if (speed < 0.55)
{
int w = image.width();
int h = image.height();
for (int i = 0; i < h; ++i)
{
uchar *line = image.scanLine(i);
for (int j = 5; j < w - 5; ++j)
line[j << 2] = 200;
}
}
painter->drawImage(QRectF(0, -100, 140, 140), image);
}
}
bool Zombie::collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode) const
{
Q_UNUSED(mode)
return other->type() == Plant::Type && qFuzzyCompare(other->y(), y()) && qAbs(other->x() - x()) < 30;
}
int Zombie::type() const
{
return Type;
}
void Zombie::setMovie(QString path)
{
if (movie)
delete movie;
movie = new QMovie(path);
movie->start();
}
void Zombie::setHead(QString path)
{
if (head)
delete head;
head = new QMovie(path);
head->start();
}