-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlienBullet.cpp
51 lines (47 loc) · 1.25 KB
/
AlienBullet.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
#include "AlienBullet.h"
#include "SpaceShip.h"
#include "Game.h"
#include <QDebug>
#include <typeinfo>
extern Game* game;
AlienBullet::AlienBullet(QGraphicsItem* parent): Bullet(parent)
{
// set graphics.
setPic(QPixmap(":/images/AlienBullet.png"));
setPic(getPic().scaled(QSize(30,30), Qt::KeepAspectRatio));
setPixmap(getPic());
}
bool AlienBullet::hasCollided(){
QList<QGraphicsItem *> collided = collidingItems();
for(int i = 0; i < collided.size(); ++i){
if(typeid((*collided[i])) == typeid(SpaceShip)){
game->getBoard()->updateScore(game->getBoard()->score() - 10);
(dynamic_cast<SpaceShip*> (collided[i]))->updateHealth(
(dynamic_cast<SpaceShip*> (collided[i]))->health() - 10);
getTimer()->stop();
delete this;
return true;
}
}
return false;
}
void AlienBullet::move(){
if(game->getShip()->health() <= 0){
game->lose();
getTimer()->stop();
delete this;
return;
}
if(hasCollided()){
qDebug() << "Collision occured";
return;
}
else{
if(y() < game->scene->height()){
setY(y() + 10);
}
else{
delete this;
}
}
}