-
Notifications
You must be signed in to change notification settings - Fork 2
/
zombie.cpp
170 lines (155 loc) · 3.74 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <QLabel>
#include <QTimer>
#include <QPainter>
#include <QStyleOption>
#include <QPixmap>
#include <QTimer>
#include <QDebug>
#include "zombie.h"
#include "Constants.h"
Zombie::Zombie(Field* ff, QString name, QObject *parent) :
zombieName(name),typeName("zombie"),f(ff),healthPoint(10),isFrozen(0),whichStep(0)
{
xShift = 0;
yShift = -10;
eating = true;
plant = NULL;
movie = new QMovie(":/images/"+name+".gif");
movie->start();
dead = false;
/*
QLabel *zombie = new QLabel(this);
QMovie *movie = new QMovie(":/images/"+name+".gif");
zombie->setMovie(movie);
movie->start();
zombie->show();
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(zombie);
this->setLayout(layout);
*/
setZValue(5+qrand()%5);
timer = new Timer();
connect(timer,SIGNAL(timeout()), this, SLOT(tryEating()));
timer->start(1);
// qDebug()<<"Zombie at location "<<(long)this;
}
Zombie::~Zombie()
{
delete(movie);
delete(timer);
}
int Zombie::type() const
{
return Type;
}
QRectF Zombie::boundingRect() const
{
qreal adjust = 5;
return QRectF( xShift - adjust, yShift - adjust,
90 + adjust, 130 + adjust);
}
QPainterPath Zombie::shape() const
{
QPainterPath path;
qreal adjust = -5;
path.addRect(xShift - adjust, yShift - adjust, 90 + adjust, 130 + adjust);
return path;
}
void Zombie::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
// painter->drawPixmap(0,0,QPixmap(":/images/"+zombieName+".gif"));
painter->drawImage(xShift,yShift,movie->currentImage());
// painter->drawPath(shape());
// qreal adjust = 0.5;
// painter->drawRect(0,0,182,173);
}
void Zombie::advance(int step)
{
if (!step)
return;
// list = collidingItems();
QPoint point(targetPoint(this->pos()));
Plant* pp=f->getPlant(point.y(),point.x());
if (pp && QString(pp->metaObject()->className())!="Spikeweed"){
eating = true;
plant = pp;
update();
return;
}
else{
plant = NULL;
eating = false;
}
/*
for (int i=0; i< list.size(); ++i){
pp = qgraphicsitem_cast<Plant*>(list.at(i));
if (pp){
// qDebug()<<"find plant";
eating = true;
plant = pp;
// emit sendEat(pp, 10);
update();
return;
}
else{
plant = NULL;
eating = false;
}
}
*/
if (((whichStep % 6==0) && !isFrozen )|| (whichStep % 20==0)){
setPos(mapToParent(-1,0));
}
whichStep = whichStep > 0? whichStep-1: 100;
update();
if (pos().x()<-GRID_X)
emit youLose();
}
void Zombie::pause()
{
movie->setPaused(true);
timer->pause();
}
void Zombie::restore()
{
movie->start();
timer->restore();
}
void Zombie::hitten(Zombie* z, int hitNumber, int property)
{
if (dead)
return;
// qDebug()<<"come to Zombie::hitten";
if (z != this)
return;
// qDebug()<<"come to Zombie::hitten "<<hitNumber;
healthPoint -= hitNumber;
// qDebug()<<"current healthPoint "<<healthPoint;
if (property == 3)
freeze();
if (healthPoint <= 0){
emit die(this);
dead = true;
}
}
void Zombie::freeze()// to be implemented
{
isFrozen += 1;
QTimer::singleShot(FROZEN_TIME*1000,this,SLOT(unfreeze()));
}
// void loseMetal(); this may be implemeted in individual zombies
void Zombie::unfreeze()
{
isFrozen -= 1;
}
void Zombie::tryEating()
{
if (eating){
// qDebug()<<(plant != NULL);
emit sendEat(plant,10);
}
}
const QPoint Zombie::targetPoint(const QPointF &position) const
{
return QPoint((position.x()+GRID_X/2)/GRID_X, (position.y())/GRID_Y);
}