426cd63f8e8f89e9dce9dcb251ad914e54edbda9
[ghostsoverboard] / ship.cpp
1 /**************************************************************************
2         Ghosts Overboard - a game for Maemo 5
3
4         Copyright (C) 2011  Heli Hyvättinen
5
6         This file is part of Ghosts Overboard
7
8         Ghosts Overboard is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 **************************************************************************/
22
23
24 #include "ship.h"
25 #include <QDebug>
26
27 Ship::Ship(QList<QPixmap> pixmapList, QGraphicsItem *parent) :
28     OrientationControlledGraphicsPixmapObject(pixmapList.at(0),parent)
29 {
30     shipImages_ = pixmapList;
31     ghostsAboard_ = 0;
32
33 }
34
35 bool Ship::handleCollisions()
36 {
37     QList<QGraphicsItem*>  collidesList = collidingItems();
38
39     if (collidesList.isEmpty())
40         return true;
41
42     else
43     {
44         //since the game logic does not leave items to collide with each other we can take just the topmost one
45         //and trust it is the only one
46         QString type = collidesList.at(0)->data(0).toString();
47 //        qDebug() << type;
48
49         if (type == "rock" || type == "octopus")
50         {
51             // drop all ghosts when hitting an obstacle
52
53             dropAllGhosts();
54
55             //go back to old position
56
57             return false;
58         }
59
60         else if (type == "ghost")
61         {
62             ghostsAboard_++;
63             updateShipImage();
64
65 //            qDebug() << ghostsAboard_ << " ghosts aboard";
66
67             emit pickingGhost(collidesList.at(0));
68
69             return true;
70         }
71
72     }
73
74
75     return true; //execution can never reach here, this is just to stop the compiler from complaining
76 }
77
78 void Ship::updateShipImage()
79 {
80     int index = qBound(0,ghostsAboard_,shipImages_.length()-1);
81     setPixmap(shipImages_.at(index));
82 }
83
84 void Ship::dropAllGhosts()
85 {
86
87     emit droppingGhosts(ghostsAboard_);
88     ghostsAboard_ = 0;
89     updateShipImage();
90 }
91