-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget.cpp
86 lines (71 loc) · 1.71 KB
/
target.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 "target.h"
#include "widget.h"
static int randomBetween(int low, int high)
{
return (qrand() % ((high + 1) - low) + low);
}
Target::Target(QObject *parent) /*:
QObject(parent), QGraphicsItem()*/
{
health = randomBetween(1,15);
maxHealth = health;
shot = true;
bulletTimer = new QTimer();
connect(bulletTimer, &QTimer::timeout, this, &Target::slotBulletTimer);
bulletTimer->start(1000/1);
targetHSE = QPointF(qrand(), qrand());
}
Target::~Target()
{
qDebug() << "target destructor was called";
delete bulletTimer;
}
QRectF Target::boundingRect() const
{
return QRectF(-20,-20,40,40);
}
void Target::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setPen(Qt::black);
painter->setBrush(QColor(255, 0, 0, (17 * health) % 254));
painter->drawRect(-20,-10,40,30);
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::red);
painter->drawRect(-20,-20, (int) 40*health/maxHealth,3);
Q_UNUSED(option);
Q_UNUSED(widget);
}
void Target::hit(int damage)
{
health -= damage;
this->update(QRectF(-20,-20,40,40));
if(health <= 0){
if (this->getType() == TARGET){
emit signalScore();
}
this->deleteLater();
}
}
void Target::slotTarget(QPointF point)
{
Q_UNUSED(point);
}
void Target::slotBulletTimer()
{
if(shot) emit signalBullet(mapToScene(0,-25), QPointF(randomBetween(0, 600), randomBetween(0, 600)), this);
}
void Target::slotShot(bool shot)
{
Q_UNUSED(shot)
this->shot = true;
}
ObjectType Target::getType(){
return TARGET;
}
void Target::slotGameTimer(){
return;
}
void Target::stopSlot(){
bulletTimer->stop();
this->deleteLater();
}