-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullet.cpp
More file actions
58 lines (50 loc) · 1.39 KB
/
Copy pathbullet.cpp
File metadata and controls
58 lines (50 loc) · 1.39 KB
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
#include "bullet.h"
#include "enemy.h"
#include "mainwindow.h"
#include <QPainter>
#include <QPropertyAnimation>
const QSize Bullet::ms_fixedSize(8, 8);
Bullet::Bullet(QPoint startPos, QPoint targetPoint, int damage, Enemy *target,
MainWindow *game, const QPixmap &sprite/* = QPixmap(":/image/bullet.png")*/)
: m_startPos(startPos)
, m_targetPos(targetPoint)
, m_sprite(sprite)
, m_currentPos(startPos)
, m_target(target)
, m_game(game)
, m_damage(damage)
{
}
void Bullet::draw(QPainter *painter) const
{
painter->drawPixmap(m_currentPos, m_sprite);
}
void Bullet::move()
{
// 100毫秒内击中敌人
static const int duration = 100;
QPropertyAnimation *animation = new QPropertyAnimation(this, "m_currentPos");
animation->setDuration(duration);
animation->setStartValue(m_startPos);
animation->setEndValue(m_targetPos);
connect(animation, SIGNAL(finished()), this, SLOT(hitTarget()));
animation->start();
}
void Bullet::hitTarget()
{
// 这样处理的原因是:
// 可能多个炮弹击中敌人,而其中一个将其消灭,导致敌人delete
// 后续炮弹再攻击到的敌人就是无效内存区域
// 因此先判断下敌人是否还有效
if (m_game->enemyList().indexOf(m_target) != -1)
m_target->getDamage(m_damage);
m_game->removedBullet(this);
}
void Bullet::setCurrentPos(QPoint pos)
{
m_currentPos = pos;
}
QPoint Bullet::currentPos() const
{
return m_currentPos;
}