-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullet.cpp
More file actions
98 lines (75 loc) · 2.69 KB
/
bullet.cpp
File metadata and controls
98 lines (75 loc) · 2.69 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
#include "Bullet.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "Enemy.h"
#include "Game.h"
#include "healthpack.h"
#include "healthpack_blue.h"
#include "healthpack_yellow.h"
#include <typeinfo>
extern Game * game;
Bullet::Bullet(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){
setPixmap(QPixmap(":/images/bullet.png"));
QTimer * timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
timer->start(50);
}
void Bullet::move(){
QList<QGraphicsItem *> colliding_items = collidingItems();
for (int i = 0, n = colliding_items.size(); i < n; ++i){
if (typeid(*(colliding_items[i])) == typeid(Enemy)){
if (1 + rand() % 100 > 95)
{
// 25 - это половина healthpack_blue по X
// 75 - это половина colliding_items[i] aka enemy по X
// 50 - это половина colliding_items[i] aka enemy по Y
HealthPack_Blue * healthpack_blue = new HealthPack_Blue(colliding_items[i]->x() + 75 - 25, colliding_items[i]->y() + 50 - 25);
scene()->addItem(healthpack_blue);
}
else if (1 + rand() % 100 > 98)
{
HealthPack_Yellow * healthpack_yellow = new HealthPack_Yellow(colliding_items[i]->x() + 75 - 25, colliding_items[i]->y() + 50 - 25);
scene()->addItem(healthpack_yellow);
}
game->score->increase();
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
delete colliding_items[i];
delete this;
return;
}
else if (typeid(*(colliding_items[i])) == typeid(HealthPack))
{
game->health->increase();
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
delete colliding_items[i];
delete this;
return;
}
else if (typeid(*(colliding_items[i])) == typeid(HealthPack_Blue))
{
game->health->increase(3);
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
delete colliding_items[i];
delete this;
return;
}
else if (typeid(*(colliding_items[i])) == typeid(HealthPack_Yellow))
{
game->health->increase(5);
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
delete colliding_items[i];
delete this;
return;
}
}
setPos(x(),y()-10);
if (pos().y() < 0){
scene()->removeItem(this);
delete this;
}
}