work towards new pause and menu systems
[ghostsoverboard] / seascene.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 #include "seascene.h"
24 #include "octopus.h"
25 #include "ship.h"
26 #include <QGraphicsPixmapItem>
27 #include <QDebug>
28 #include <QMessageBox>
29 #include <QTime>
30
31 const QString ghostImageFilename_ = ":/pix/aave.png";
32 const QString rockImageFilename_ =":/pix/kari.png";
33 const QString octopusImageFilename_= ":/pix/tursas.png";
34
35
36 SeaScene::SeaScene(QObject *parent) :
37     QGraphicsScene(parent)
38 {
39     paused_ = false;
40     screenLitKeeper_.keepScreenLit(true);
41
42     //set background
43
44     QPixmap waves (":/pix/meri.png");
45     waves.scaled(20,20);
46     setBackgroundBrush(QBrush(waves));
47
48     //set random seed
49
50     qsrand(QTime::currentTime().msec()+2);  //+2 to avoid setting it to 1
51
52     //connect selecting to menu handling (only menu items selectable)
53
54     connect(this,SIGNAL(selectionChanged()),this,SLOT(menuClicked()));
55
56
57
58 }
59
60 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
61 {
62     //empty the map
63
64     clear();
65
66     //empty the list of moving items
67
68     movingItems_.clear();
69
70     //empty the list of free slots
71     freeTiles_.clear();
72
73     //fill the list of free slots
74
75     int numberOfXTiles  = width() / 40;
76     int numberOfYTiles = height() /40;
77
78 //    qDebug() << numberOfXTiles << " slots in x direction";
79 //    qDebug() << numberOfYTiles << " slots in y rirection";
80
81     for (int i = 0; i < numberOfXTiles; i++ )
82     {
83         for (int j = 0; j < numberOfYTiles; j++)
84         {
85             freeTiles_.append(QPointF(i*40,j*40));
86         }
87     }
88
89
90     //spread the rocks
91
92     for (int i = 0; i < rocks; i++)
93     {
94         QPointF * pPosition = findRandomFreeSlot();
95
96         //If there was no room no point to continue
97         if (pPosition == NULL)
98             break;
99
100         QPixmap rockPixmap (":/pix/kari.png");
101         QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
102         pRock->setData(0,"rock");
103         pRock->setPos(*pPosition);
104         delete pPosition;
105
106     }
107
108     //spread the ghosts
109
110     ghostsLeft_ = ghosts;
111     spreadGhosts(ghosts);
112
113
114
115     //spread the octopuses
116
117     QList <Octopus*> octopusList;
118
119     for (int i=0; i < octopuses; i++)
120     {
121         QPointF * pPosition = findRandomFreeSlot();
122
123         //If there was no room no point to continue
124         if (pPosition == NULL)
125             break;
126
127     QPixmap octopusPixmap (":/pix/tursas.png");
128     Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
129     pOctopus->setData(0,"octopus");
130     pOctopus->setPos(*pPosition);
131     addItem(pOctopus);
132     pOctopus->startMoving();
133     movingItems_.append(pOctopus);
134     connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
135     connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
136     octopusList.append(pOctopus);
137     delete pPosition;
138
139     }
140
141
142     //place the ship
143
144     QPointF * pPosition = findRandomFreeSlot();
145     if (pPosition == NULL)
146     {
147         // Game cannot begin without a free position for ship, so give an error message and return
148
149         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
150         return;
151     }
152
153     QList<QPixmap> shipImages;
154     shipImages.append(QPixmap(":/pix/laiva.png"));
155     shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
156     shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
157     shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
158     shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
159     shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
160     shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
161     shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
162     shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
163     shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
164     shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
165
166     Ship * pShip = new Ship (shipImages);
167     pShip->setData(0,"ship");
168     pShip->setPos(*pPosition);
169     addItem(pShip);
170     connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
171     connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
172     connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
173     pShip->startMoving();
174     movingItems_.append(pShip);
175     connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
176     connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
177     foreach (Octopus* pOctopus, octopusList)
178     {
179         connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
180     }
181     delete pPosition;
182 }
183
184 void SeaScene::setupMap(Level level)
185 {
186     setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
187 }
188
189 void SeaScene::spreadGhosts(int ghosts)
190 {
191
192
193     //the octopuses and the ship may have moved from their original positions,
194     //so the list of free slots must be adjusted to exclude their current positions
195
196     QList<QPointF> temporarilyReservedSlots;
197
198     foreach (QGraphicsItem* pItem, movingItems_)
199     {
200         if (pItem == NULL)
201         {
202  //           qDebug() << "NULL item in movingItems_";
203             continue;
204         }
205
206         //round x and y down to fit the slot size
207         int x = pItem->x();
208         x = x/40;
209         x = x*40;
210
211         int y = pItem->y();
212         y = y/40;
213         y=y*40;
214
215
216         QPointF position (x,y);
217
218         //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
219
220         if (freeTiles_.removeOne(position))
221             temporarilyReservedSlots.append(position);
222
223
224         position.setX(x+40);
225
226         if (freeTiles_.removeOne(position))
227             temporarilyReservedSlots.append(position);
228
229         position.setY(y+40);
230
231         if (freeTiles_.removeOne(position))
232             temporarilyReservedSlots.append(position);
233
234         position.setX(x);
235
236         if (freeTiles_.removeOne(position))
237             temporarilyReservedSlots.append(position);
238
239     }
240
241
242     //spread ghosts in random free slots
243
244     for (int i=0; i < ghosts; i++)
245     {
246         QPointF * pPosition = findRandomFreeSlot();
247
248         //If there was no room no point to continue
249         if (pPosition == NULL)
250             return;
251
252         QPixmap ghostPixmap(":/pix/aave.png");
253         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
254         pGhost->setData(0,"ghost");
255         pGhost->setPos(*pPosition);
256         delete pPosition;
257     }
258
259     //return the slots occupied by moving items to free slots
260     freeTiles_.append(temporarilyReservedSlots);
261
262     //clear temp for the next round
263     temporarilyReservedSlots.clear();
264 }
265
266 QPointF* SeaScene::findRandomFreeSlot()
267 {
268     if (freeTiles_.isEmpty())
269         return NULL;
270
271     int index = qrand()%freeTiles_.size();
272
273 //    qDebug()  << index << " index";
274     return new QPointF (freeTiles_.takeAt(index));
275
276 }
277
278 void SeaScene::removeGhost(QGraphicsItem *pGhost)
279 {
280     removeItem(pGhost);  //remove the item from scene
281     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
282     delete pGhost;
283     ghostsLeft_--;
284     if (ghostsLeft_ == 0)
285     {
286         emit allGhostsPicked();
287  //       qDebug() << "All ghosts picked!";
288     }
289 }
290
291 void SeaScene::ghostsDropped(int ghosts)
292 {
293     ghostsLeft_ += ghosts;
294
295     spreadGhosts(ghosts);
296 }
297
298 void SeaScene::pause(bool paused)
299 {
300     //    qDebug() << "pause pressed " << paused;
301         if (paused_ == paused)
302                 return;
303
304         paused_ = paused;
305
306         if (paused == false)
307         {
308      //       qDebug() << "starting to move again";
309             emit pauseOff();
310             screenLitKeeper_.keepScreenLit(true);
311         }
312
313         else
314         {
315      //       qDebug("about to stop movement");
316             emit pauseOn();
317             screenLitKeeper_.keepScreenLit(false);
318         }
319 }
320
321 void SeaScene::vibrationActivate(bool on)
322 {
323     emit vibrationActivated(on);
324 }
325
326 void SeaScene::menuClicked()
327 {
328     QList<QGraphicsItem *> items = selectedItems();
329
330     //if nothing selected (selection was removed) do nothing
331
332     if (items.isEmpty())
333         return;
334
335     //Menu functions
336
337     QString menuitem = items.at(0)->data(0).toString();
338
339     if (menuitem == "restart game")
340     {
341
342     }
343
344     else if (menuitem == "restart level")
345     {
346
347     }
348
349     else if (menuitem == "vibration effects")
350     {
351
352     }
353
354     else if (menuitem == "about")
355     {
356
357     }
358
359
360     //Selection is just used to get notice of being clicked, removed after use
361
362     clearSelection();
363
364 }
365
366 void SeaScene::showMenu()
367 {
368     menuItems_.show();
369 }
370
371 void::SeaScene::hideMenu()
372 {
373     menuItems_.hide();
374 }