-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalien.cpp
More file actions
149 lines (128 loc) · 3.08 KB
/
alien.cpp
File metadata and controls
149 lines (128 loc) · 3.08 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "alien.h"
#include <QTimer>
#include "cannon.h"
#include "settings.h"
int CAlien::alienCount = 0;
CAlien::CAlien(CAlienType type, int stage, QGraphicsItem *pParent)
{
this->type = type;
this->stage = stage;
QPixmap pixmap;
switch (type)
{
case CAlienType::BOSS:
pixmap.load(":/Resources/ship.png");
case CAlienType::UPPER:
pixmap.load(":/Resources/RedAlien.png");
break;
case CAlienType::MIDDLE:
pixmap.load(":/Resources/PinkAlien.png");
break;
case CAlienType::LOWER:
default:
pixmap.load(":/Resources/BlueAlien.png");
break;
}
setPixmap(pixmap.scaled(QSize(100, 100), Qt::KeepAspectRatio));
}
CAlien::~CAlien()
{
if (below != nullptr)
{
below->setAbove(above);
}
if (above != nullptr)
{
above->setBelow(below);
}
}
void CAlien::advance(int step)
{
int speed = 4 + stage;
if (step)
{
// the alien position is changed
// we track formation position too, so we can change direction
if (goingRight)
{
setPos(x() + speed, y());
formationPosition += speed;
if (formationPosition > scene()->width() / 10)
{
goingRight = false;
setPos(x(), y() + speed * 2);
}
}
else
{
setPos(x() - speed, y());
formationPosition -= speed;
if (formationPosition < -scene()->width() / 10 + 100)
{
goingRight = true;
setPos(x(), y() + speed * 2);
}
}
if (y() > scene()->height() - gCannonSize.height())
{
scene()->removeItem(this);
emit sigGameOver();
delete this;
}
if (below == nullptr && rand() % 150 == 0 && type != CAlienType::BOSS)
{
shoot();
}
}
// for all items that collides with the alien
// QList<QGraphicsItem *> listCollidingItem = collidingItems();
// for (auto const pItem : listCollidingItem)
// {
// // if the alien touches the player, end game
// if (dynamic_cast<CCannon *>(pItem))
// {
// scene()->removeItem(this);
// emit sigGameOver();
// }
// }
}
void CAlien::shoot()
{
int speed;
switch (type)
{
case CAlienType::UPPER:
speed = 50;
break;
case CAlienType::MIDDLE:
speed = 40;
break;
case CAlienType::LOWER:
speed = 20;
break;
default:
speed = 20;
}
CBullet *pBullet = new CBullet(speed);
connect(pBullet, &CBullet::sigDecreaseHealth, this, &CAlien::sigDecreaseHealth);
// orient below alien
pBullet->setPos(x() + 25, y() + 60);
// we add the bullet to the scene
scene()->addItem(pBullet);
}
inline void CAlien::setBelow(CAlien *below)
{
this->below = below;
}
inline void CAlien::setAbove(CAlien *above)
{
this->above = above;
}
CAlien *CAlien::getBelow()
{
return below;
}
CAlien *CAlien::getAbove()
{
return above;
}