/
shot.cpp
92 lines (84 loc) · 2.52 KB
/
shot.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
#include "shot.h"
Shot::Shot(Common::Person person, const QPoint& pos, int height, const QVector<QPixmap>& vecPix, QGraphicsItem* parent) : AbstractSprite(parent)
, m_rectSprite(QRect(pos.x(), pos.y(), vecPix.front().width(), vecPix.front().width()))
, m_person(person)
{
if(person == Common::Person::Player)
{
m_posBoundingSprite = QPoint(pos.x(), pos.y() - vecPix.front().height() + 4);
QSound::play(":sound/resource/sound/playerShot.wav");
}
else if(person == Common::Person::Enemy)
{
m_posBoundingSprite = pos;
QSound::play(":sound/resource/sound/enemyShot.wav");
}
m_pixSprite_ = vecPix;
m_heightBounding = height;
setSpeed(5);
}
QRectF Shot::boundingRect() const
{
if(m_person == Common::Person::Player)
return QRectF(m_posBoundingSprite.x(), 0, m_pixSprite_.front().width(), m_heightBounding);
else if(m_person == Common::Person::Enemy)
return QRectF(m_posBoundingSprite.x(), m_posBoundingSprite.y(), m_pixSprite_.front().width(), m_heightBounding);
return QRectF();
}
void Shot::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
{
painter->drawPixmap(m_rectSprite, m_pixSprite_[m_indexAnimPix == m_pixSprite_.size()
? m_indexAnimPix - 1 : m_indexAnimPix]);
}
void Shot::setSpeed(int msec)
{
if(msec > 0)
{
if(m_timerId != -1)
this->killTimer(m_timerId);
m_timerId = -1;
m_indexAnimPix = 0;
this->startTimer(msec);
}
}
void Shot::timerEvent(QTimerEvent* event)
{
if(outputAbroad())
{
m_timerId = -1;
this->killTimer(event->timerId());
emit deleteShot(this);
return;
}
m_timerId = event->timerId();
this->update(boundingRect());
emit pathShot(this);
}
QPainterPath Shot::shape() const
{
QPainterPath path;
path.addRect(m_rectSprite.x(), m_rectSprite.y(), m_rectSprite.width(), m_rectSprite.height());
return path;
}
void Shot::stopGame()
{
emit deleteShot(this);
}
bool Shot::outputAbroad()
{
if(m_person == Common::Person::Player)
{
if(m_rectSprite.top() <= 0)
return true;
m_rectSprite.moveTo(m_rectSprite.left(), m_rectSprite.top() - 2);
return false;
}
else if(m_person == Common::Person::Enemy)
{
if(m_rectSprite.top() >= boundingRect().bottom())
return true;
m_rectSprite.moveTo(m_rectSprite.left(), m_rectSprite.top() + 2);
return false;
}
return false;
}