Changed the section to user/hidden as required for Nokia Store
[ghostsoverboard] / ship.cpp
1 /**************************************************************************
2         Ghosts Overboard - a game for 'Meego 1.2 Harmattan'
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
31
32 QTM_USE_NAMESPACE
33
34
35 Ship::Ship(QList<QPixmap> pixmapList, QGraphicsItem *parent) :
36     OrientationControlledGraphicsPixmapObject(pixmapList.at(0),parent)
37 {
38     shipImages_ = pixmapList;
39     ghostsAboard_ = 0;
40     vibrationActive_ = false;
41     vibrationAllowed_ = false;
42
43     pVibrationEffect_ = new QFeedbackHapticsEffect();
44     pVibrationEffect_->setDuration(500);
45     pVibrationEffect_->setIntensity(1.0);
46
47 }
48
49 Ship::~Ship()
50 {
51     if (pVibrationEffect_)
52       delete pVibrationEffect_;
53 }
54
55
56 bool Ship::handleCollisions()
57 {
58     QList<QGraphicsItem*>  collidesList = collidingItems();
59
60     if (collidesList.isEmpty())
61         return true;
62
63     else
64     {
65         //since the game logic does not leave items to collide with each other we can take just the topmost one
66         //and trust it is the only one
67         QString type = collidesList.at(0)->data(0).toString();
68 //        qDebug() << type;
69
70         if (type == "rock" || type == "octopus")
71         {
72             // drop all ghosts when hitting an obstacle
73
74             dropAllGhosts();
75
76             //go back to old position
77
78             return false;
79         }
80
81         else if (type == "ghost")
82         {
83             ghostsAboard_++;
84             updateShipImage();
85
86 //            qDebug() << ghostsAboard_ << " ghosts aboard";
87
88             emit pickingGhost(collidesList.at(0));
89
90             return true;
91         }
92
93     }
94
95
96     return true; //execution can never reach here, this is just to stop the compiler from complaining
97 }
98
99 void Ship::updateShipImage()
100 {
101     int index = qBound(0,ghostsAboard_,shipImages_.length()-1);
102     setPixmap(shipImages_.at(index));
103 }
104
105 void Ship::dropAllGhosts()
106 {
107
108     emit droppingGhosts(ghostsAboard_);
109     ghostsAboard_ = 0;
110     updateShipImage();
111
112     //vibrate
113
114     if (vibrationActive_ && vibrationAllowed_)
115     {
116
117  //       This is for fremantle
118 //        QDBusMessage message = QDBusMessage::createMethodCall("com.nokia.mce","/com/nokia/mce/request","com.nokia.mce.request","req_vibrator_pattern_activate");
119
120 //        QList<QVariant> arguments;
121
122 //        arguments.append("PatternChatAndEmail");
123
124 //        message.setArguments(arguments);
125
126 //        message = QDBusConnection::systemBus().call(message);
127
128     //qDebug() << message;
129
130
131         //This is for Harmattan
132
133         pVibrationEffect_->start();
134
135
136     }
137 }
138
139 void Ship::setVibrationActivate(bool on)
140 {
141     vibrationActive_ = on;
142 }
143
144 void Ship::allowVibration()
145 {
146     vibrationAllowed_ = true;
147 }
148
149 void Ship::disallowVibration()
150 {
151     vibrationAllowed_ = false;
152 }