Updated the web pages
[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 #include <QDBusMessage>
27 #include <QDBusConnection>
28
29
30 Ship::Ship(QList<QPixmap> pixmapList, QGraphicsItem *parent) :
31     OrientationControlledGraphicsPixmapObject(pixmapList.at(0),parent)
32 {
33     shipImages_ = pixmapList;
34     ghostsAboard_ = 0;
35     vibrationActive_ = false;
36 }
37
38 bool Ship::handleCollisions()
39 {
40     QList<QGraphicsItem*>  collidesList = collidingItems();
41
42     if (collidesList.isEmpty())
43         return true;
44
45     else
46     {
47         //since the game logic does not leave items to collide with each other we can take just the topmost one
48         //and trust it is the only one
49         QString type = collidesList.at(0)->data(0).toString();
50 //        qDebug() << type;
51
52         if (type == "rock" || type == "octopus")
53         {
54             // drop all ghosts when hitting an obstacle
55
56             dropAllGhosts();
57
58             //go back to old position
59
60             return false;
61         }
62
63         else if (type == "ghost")
64         {
65             ghostsAboard_++;
66             updateShipImage();
67
68 //            qDebug() << ghostsAboard_ << " ghosts aboard";
69
70             emit pickingGhost(collidesList.at(0));
71
72             return true;
73         }
74
75     }
76
77
78     return true; //execution can never reach here, this is just to stop the compiler from complaining
79 }
80
81 void Ship::updateShipImage()
82 {
83     int index = qBound(0,ghostsAboard_,shipImages_.length()-1);
84     setPixmap(shipImages_.at(index));
85 }
86
87 void Ship::dropAllGhosts()
88 {
89
90     emit droppingGhosts(ghostsAboard_);
91     ghostsAboard_ = 0;
92     updateShipImage();
93
94     //vibrate
95
96     if (vibrationActive_)
97     {
98         QDBusMessage message = QDBusMessage::createMethodCall("com.nokia.mce","/com/nokia/mce/request","com.nokia.mce.request","req_vibrator_pattern_activate");
99
100         QList<QVariant> arguments;
101
102         arguments.append("PatternChatAndEmail");
103
104         message.setArguments(arguments);
105
106         message = QDBusConnection::systemBus().call(message);
107
108     //qDebug() << message;
109     }
110 }
111
112 void Ship::setVibrationActivate(bool on)
113 {
114     vibrationActive_ = on;
115 }