Ghosts now visible aboard
[ghostsoverboard] / ship.cpp
1 #include "ship.h"
2 #include <QDebug>
3
4 Ship::Ship(QList<QPixmap> pixmapList, QGraphicsItem *parent) :
5     OrientationControlledGraphicsPixmapObject(pixmapList.at(0),parent)
6 {
7     shipImages_ = pixmapList;
8     ghostsAboard_ = 0;
9
10 }
11
12 bool Ship::handleCollisions()
13 {
14     QList<QGraphicsItem*>  collidesList = collidingItems();
15
16     if (collidesList.isEmpty())
17         return true;
18
19     else
20     {
21         //since the game logic does not leave items to collide with each other we can take just the topmost one
22         //and trust it is the only one
23         QString type = collidesList.at(0)->data(0).toString();
24         qDebug() << type;
25
26         if (type == "rock" || type == "octopus")
27         {
28             // drop all ghosts when hitting an obstacle
29             emit droppingGhosts(ghostsAboard_);
30             ghostsAboard_ = 0;
31             updateShipImage();
32
33             return false;
34         }
35
36         else if (type == "ghost")
37         {
38             ghostsAboard_++;
39             updateShipImage();
40
41             qDebug() << ghostsAboard_ << " ghosts aboard";
42
43             emit pickingGhost(collidesList.at(0));
44
45             return true;
46         }
47
48     }
49 }
50
51 void Ship::updateShipImage()
52 {
53     int index = qBound(0,ghostsAboard_,shipImages_.length()-1);
54     setPixmap(shipImages_.at(index));
55 }