1 Star 1 Fork 22

zhjun5337 / air_plane

forked from zhouxiang / air_plane 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bullet.cpp 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
zhouxiang 提交于 2013-09-16 18:58 . 添加注释
#include "bullet.h"
Bullet::Bullet(const QPainterPath &path, const QColor &color, qreal angle, uint speed, QGraphicsScene *scene,
QGraphicsItem *parent):Flyer(speed, scene, parent),
m_path(path),
m_color(color),
m_angle(angle),
xSpeed(::cos(m_angle / ROTATE) * m_speed),
ySpeed(::sin(m_angle / ROTATE) * m_speed)
{
}
Bullet* Bullet::clone() const
{
return NULL;
}
QRectF Bullet::boundingRect() const
{
return m_path.boundingRect();
}
void Bullet::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
{
painter->setPen(Qt::NoPen);
painter->setBrush(m_color);
painter->drawPath(m_path);
}
/**
* @brief Bullet::autofly 初定一秒更新一次,那么速度为每秒速度 50ms更新一次
*/
void Bullet::autofly()
{
if (!checkPos(Front)) {
posLost();
return;
}
//需要度数转化为弧度
// qreal xSpeed = ::cos(m_angle / ROTATE) * m_speed;
// qreal ySpeed = ::sin(m_angle / ROTATE) * m_speed;
QPointF t = scenePos();
t.rx() -= xSpeed;
t.ry() -= ySpeed;
setPos(t);
doCollide();
}
void Bullet::advance(int)
{
if (!checkPos(Front)) {
posLost();
return;
}
//需要度数转化为弧度,提前计算可以可以提高性能
// qreal xSpeed = ::cos(m_angle / ROTATE) * m_speed;
// qreal ySpeed = ::sin(m_angle / ROTATE) * m_speed;
QPointF t = scenePos();
t.rx() -= xSpeed;
t.ry() -= ySpeed;
setPos(t);
doCollide();
}
int Bullet::name() const
{
return GlobalParameter::instance()->bulletClassFlag;
}
void Bullet::doCollide()
{
foreach (QGraphicsItem *t, collidingItems()) {
if (t->type() == CustomItem::Type) {
} else {
Flyer *fy = static_cast<Flyer*>(t);
if (fy->name() == ENEMYPLANE) {
FlightVehicle* fv = static_cast<FlightVehicle*>(fy);
this->setVisible(false);
fv->strike();
if (fv->destroy()) {
fv->fall();
//缓存起来,未加入
}
}
}
}
}
1
https://gitee.com/zhjun5337/air_plane.git
git@gitee.com:zhjun5337/air_plane.git
zhjun5337
air_plane
air_plane
master

搜索帮助