You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
889 B
34 lines
889 B
#include "bullet.h" |
|
#include <QTimer> |
|
#include <QGraphicsScene> |
|
#include <QList> |
|
#include "enemy.h" |
|
|
|
|
|
Bullet::Bullet() |
|
{ |
|
setRect(0,0,10,50); |
|
QTimer* timer = new QTimer(); |
|
connect(timer, SIGNAL(timeout()), this, SLOT(move())); |
|
timer->start(50); |
|
} |
|
|
|
void Bullet::move(){ |
|
// if bullet collides with enemy, destroy both |
|
QList<QGraphicsItem *> colliding_items = collidingItems(); |
|
for (int i = 0, n = colliding_items.size(); i < n; i++) { |
|
if (typeid(*(colliding_items[i])) == typeid(Enemy)) { |
|
scene()->removeItem(colliding_items[i]); |
|
scene()->removeItem(this); |
|
delete colliding_items[i]; |
|
delete this; |
|
return; |
|
} |
|
} |
|
this->setPos(x(), y()-10); |
|
if(pos().y() + rect().height() < 0) { |
|
scene()->removeItem(this); |
|
delete this; |
|
qDebug() << "bullet deleted"; |
|
} |
|
}
|
|
|