Qctopuses now drop the ghosts when hitting the ship
[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
30             dropAllGhosts();
31
32             //go back to old position
33
34             return false;
35         }
36
37         else if (type == "ghost")
38         {
39             ghostsAboard_++;
40             updateShipImage();
41
42 //            qDebug() << ghostsAboard_ << " ghosts aboard";
43
44             emit pickingGhost(collidesList.at(0));
45
46             return true;
47         }
48
49     }
50
51
52     return true; //execution can never reach here, this is just to stop the compiler from complaining
53 }
54
55 void Ship::updateShipImage()
56 {
57     int index = qBound(0,ghostsAboard_,shipImages_.length()-1);
58     setPixmap(shipImages_.at(index));
59 }
60
61 void Ship::dropAllGhosts()
62 {
63
64     emit droppingGhosts(ghostsAboard_);
65     ghostsAboard_ = 0;
66     updateShipImage();
67 }
68