-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullet.cpp
More file actions
66 lines (54 loc) · 2.4 KB
/
bullet.cpp
File metadata and controls
66 lines (54 loc) · 2.4 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
#include "bullet.h"
#include <QTimer>
#include <QDebug>
#include <QGraphicsScene>
#include <QList>
#include "enemy.h"
#include "game.h"
extern Game* game;
Bullet::Bullet(QGraphicsItem* parent): QObject(), QGraphicsPixmapItem(parent){
// draw the rect
setPixmap(QPixmap(":/images/bullet1.gif"));//0,0,10,50
// connect
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
timer->start(50);
}
void Bullet::move(){
// if bullet collides witm enemy, destroy both
/* How do I check what the bullet is colliding with?
* Every item has member function which calls colliding items-
* which returns a list of all the items that are colliding witm calling object
*/
QList<QGraphicsItem*> colliding_items = collidingItems();
// this member function will return
// a list of pointers to all the QGraphics Items that the calling object is actually colliding with
/* This basically houses all the items that this bullet is collidng wtm right now
* The plan is to treverse this list and find out if the bullet is colliding with an enemy. */
for (int i=0, n=colliding_items.size(); i<n; ++i){
//if (typeid()) // if the type of (this object that we're corrently traversing) and the way we get that object is we have to
// dereferance the pointer (which pointer? - the current pointer.)
// => so *(colliding_items(i))
// and I want to check if that type ID is equal to an enemy
if (typeid(*(colliding_items[i])) == typeid (Enemy)){
game->score->increase();
//remove them both
scene()->removeItem(colliding_items[i]); // remove the enemy first
scene()->removeItem(this);
// and then remove also the bullet
/* once I remove it from the scene, these objects are still occupying memory on the heap in order to free that memory-
* I have to actually delete that object on the heap */
// delete them both
delete colliding_items[i];
delete this;
return;
}
}
// move bullet up
setPos(x(),y()-10);
// checking if the bullet is off the screen and deleting( if it is)
if (pos().y()+pixmap().height() < 0){
scene()->removeItem(this); delete this;
qDebug() << "bullet deleted";
}
}