-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlienShip.cpp
110 lines (95 loc) · 2.47 KB
/
AlienShip.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "AlienShip.h"
#include <QDebug>
#include <QPixmap>
#include "Game.h"
#include <typeinfo>
#include "SpaceBullet.h"
#include "AlienBullet.h"
extern Game* game;
AlienShip::AlienShip(QGraphicsItem* parent): Alien(parent)
{
// set graphics.
setPic(QPixmap(":/images/AlienShip.png"));
setHeight(40);
setWidth(40);
setPic(getPic().scaled(QSize(width(),height()), Qt::KeepAspectRatio));
setPixmap(getPic());
setX(0);
setY(60);
health_ = 100;
timer_ = new QTimer(this);
timer_->setSingleShot(true);
rate = new QTimer(this);
rate->setSingleShot(true);
startTimer(15000);
}
AlienShip::~AlienShip(){
delete timer_;
delete rate;
}
void AlienShip::startTimer(int t){
timer_->start(t);
getTimer()->start(100);
}
int AlienShip::health(){
return health_;
}
void AlienShip::updateHealth(int h){
health_ = h;
}
bool AlienShip::checkCollisions()
{
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);
delete this;
return true;
}
else if(typeid((*collided[i])) == typeid(SpaceBullet)){
(dynamic_cast<SpaceBullet*> (collided[i]))->getTimer()->stop();
delete (dynamic_cast<SpaceBullet*> (collided[i]));
game->getBoard()->updateScore(game->getBoard()->score() + 1);
if(health() < 10){
delete this;
return true;
}
else{
health_ -= 10;
}
}
}
return false;
}
void AlienShip::move(){
if(game->getShip()->health() <= 0){
getTimer()->stop();
}
if(checkCollisions()) return;
if(timer_->isActive()){
if(game->getShip()->x() < x()){
setX(x() - 5);
}
else if(game->getShip()->x() > x()){
setX(x() + 5);
}
else{
if(!rate->isActive() && game->state() == game->State::ongoing){
// create bullet and shoot.
AlienBullet* b = new AlienBullet();
b->setPos(x() + 5,y() + 13);
scene()->addItem(b);
rate->start(1000);
}
}
}
else{
if(x() > -40){
setX(x() - 10);
}
else{
qDebug() << "object hidden.";
getTimer()->stop();
}
}
}