Vibration setting is now saved
[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 #include <QApplication>
31 #include <QAction>
32 #include <QPushButton>
33 #include <QLabel>
34 #include <QVBoxLayout>
35 #include <QSettings>
36
37 const QString ghostImageFilename_ = ":/pix/aave.png";
38 const QString rockImageFilename_ =":/pix/kari.png";
39 const QString octopusImageFilename_= ":/pix/tursas.png";
40
41
42 SeaScene::SeaScene(QObject *parent) :
43     QGraphicsScene(parent)
44 {
45     paused_ = false;
46     screenLitKeeper_.keepScreenLit(true);
47
48     //set background
49
50     QPixmap waves (":/pix/meri.png");
51     waves.scaled(20,20);
52     setBackgroundBrush(QBrush(waves));
53
54     //set random seed
55
56     qsrand(QTime::currentTime().msec()+2);  //+2 to avoid setting it to 1
57
58 //Setup the level list
59
60     Level level1(5,10);
61     levelList_.append(level1);
62     Level level2(5,10,2,50);
63     levelList_.append(level2);
64     Level level3(5,15,2,50);
65     levelList_.append(level3);
66     Level level4(5,15,4,50);
67     levelList_.append(level4);
68     Level level5(5,15,5,100);
69     levelList_.append(level5);
70
71     currentLevel_ = 0;
72
73     connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
74
75
76     pVibrateAction_ = new QAction(tr("Vibration effects"),this);
77     pVibrateAction_->setCheckable(true);
78     connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
79     QSettings settings;
80     pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
81
82
83     pPauseAction_ = new QAction(tr("Pause"),this);
84     pPauseAction_->setCheckable(true);
85     connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
86
87
88 }
89
90 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
91 {
92     //empty the map
93
94     clear();
95
96     createMenuItems();
97
98     //empty the list of moving items
99
100     movingItems_.clear();
101
102     //empty the list of free slots
103     freeTiles_.clear();
104
105     //fill the list of free slots
106
107     int numberOfXTiles  = width() / 40;
108     int numberOfYTiles = height() /40;
109
110 //    qDebug() << numberOfXTiles << " slots in x direction";
111 //    qDebug() << numberOfYTiles << " slots in y rirection";
112
113     for (int i = 0; i < numberOfXTiles; i++ )
114     {
115         for (int j = 0; j < numberOfYTiles; j++)
116         {
117             freeTiles_.append(QPointF(i*40,j*40));
118         }
119     }
120
121
122     //spread the rocks
123
124     for (int i = 0; i < rocks; i++)
125     {
126         QPointF * pPosition = findRandomFreeSlot();
127
128         //If there was no room no point to continue
129         if (pPosition == NULL)
130             break;
131
132         QPixmap rockPixmap (":/pix/kari.png");
133         QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
134         pRock->setData(0,"rock");
135         pRock->setPos(*pPosition);
136         delete pPosition;
137
138     }
139
140     //spread the ghosts
141
142     ghostsLeft_ = ghosts;
143     spreadGhosts(ghosts);
144
145
146
147     //spread the octopuses
148
149     QList <Octopus*> octopusList;
150
151     for (int i=0; i < octopuses; i++)
152     {
153         QPointF * pPosition = findRandomFreeSlot();
154
155         //If there was no room no point to continue
156         if (pPosition == NULL)
157             break;
158
159     QPixmap octopusPixmap (":/pix/tursas.png");
160     Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
161     pOctopus->setData(0,"octopus");
162     pOctopus->setPos(*pPosition);
163     addItem(pOctopus);
164     pOctopus->startMoving();
165     movingItems_.append(pOctopus);
166     connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
167     connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
168     octopusList.append(pOctopus);
169     delete pPosition;
170
171     }
172
173
174     //place the ship
175
176     QPointF * pPosition = findRandomFreeSlot();
177     if (pPosition == NULL)
178     {
179         // Game cannot begin without a free position for ship, so give an error message and return
180
181         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
182         return;
183     }
184
185     QList<QPixmap> shipImages;
186     shipImages.append(QPixmap(":/pix/laiva.png"));
187     shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
188     shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
189     shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
190     shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
191     shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
192     shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
193     shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
194     shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
195     shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
196     shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
197
198     Ship * pShip = new Ship (shipImages);
199     pShip->setData(0,"ship");
200     pShip->setPos(*pPosition);
201     addItem(pShip);
202     connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
203     connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
204     connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
205     pShip->startMoving();
206     movingItems_.append(pShip);
207     connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
208     connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
209     foreach (Octopus* pOctopus, octopusList)
210     {
211         connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
212     }
213     delete pPosition;
214 }
215
216 void SeaScene::setupMap(Level level)
217 {
218     setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
219 }
220
221 void SeaScene::spreadGhosts(int ghosts)
222 {
223
224
225     //the octopuses and the ship may have moved from their original positions,
226     //so the list of free slots must be adjusted to exclude their current positions
227
228     QList<QPointF> temporarilyReservedSlots;
229
230     foreach (QGraphicsItem* pItem, movingItems_)
231     {
232         if (pItem == NULL)
233         {
234  //           qDebug() << "NULL item in movingItems_";
235             continue;
236         }
237
238         //round x and y down to fit the slot size
239         int x = pItem->x();
240         x = x/40;
241         x = x*40;
242
243         int y = pItem->y();
244         y = y/40;
245         y=y*40;
246
247
248         QPointF position (x,y);
249
250         //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
251
252         if (freeTiles_.removeOne(position))
253             temporarilyReservedSlots.append(position);
254
255
256         position.setX(x+40);
257
258         if (freeTiles_.removeOne(position))
259             temporarilyReservedSlots.append(position);
260
261         position.setY(y+40);
262
263         if (freeTiles_.removeOne(position))
264             temporarilyReservedSlots.append(position);
265
266         position.setX(x);
267
268         if (freeTiles_.removeOne(position))
269             temporarilyReservedSlots.append(position);
270
271     }
272
273
274     //spread ghosts in random free slots
275
276     for (int i=0; i < ghosts; i++)
277     {
278         QPointF * pPosition = findRandomFreeSlot();
279
280         //If there was no room no point to continue
281         if (pPosition == NULL)
282             return;
283
284         QPixmap ghostPixmap(":/pix/aave.png");
285         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
286         pGhost->setData(0,"ghost");
287         pGhost->setPos(*pPosition);
288         delete pPosition;
289     }
290
291     //return the slots occupied by moving items to free slots
292     freeTiles_.append(temporarilyReservedSlots);
293
294     //clear temp for the next round
295     temporarilyReservedSlots.clear();
296 }
297
298 QPointF* SeaScene::findRandomFreeSlot()
299 {
300     if (freeTiles_.isEmpty())
301         return NULL;
302
303     int index = qrand()%freeTiles_.size();
304
305 //    qDebug()  << index << " index";
306     return new QPointF (freeTiles_.takeAt(index));
307
308 }
309
310 void SeaScene::removeGhost(QGraphicsItem *pGhost)
311 {
312     removeItem(pGhost);  //remove the item from scene
313     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
314     delete pGhost;
315     ghostsLeft_--;
316     if (ghostsLeft_ == 0)
317     {
318         emit allGhostsPicked();
319  //       qDebug() << "All ghosts picked!";
320     }
321 }
322
323 void SeaScene::ghostsDropped(int ghosts)
324 {
325     ghostsLeft_ += ghosts;
326
327     spreadGhosts(ghosts);
328 }
329
330 void SeaScene::pause(bool paused)
331 {
332     //    qDebug() << "pause pressed " << paused;
333         if (paused_ == paused)
334                 return;
335
336         paused_ = paused;
337
338         if (paused == false)
339         {
340      //       qDebug() << "starting to move again";
341             emit pauseOff();
342             screenLitKeeper_.keepScreenLit(true);
343             if (pPausetextItem_)
344                 pPausetextItem_->hide();
345         }
346
347         else
348         {
349          qDebug("about to stop movement");
350             emit pauseOn();
351             screenLitKeeper_.keepScreenLit(false);
352             if (pPausetextItem_ != NULL)
353             {
354                 qDebug() << "about to show the pause text";
355                 pPausetextItem_->show();
356                 qDebug() << "showing pause text";
357             }
358                 else qDebug() << "No pause text available";
359         }
360 }
361
362 void SeaScene::vibrationActivate(bool on)
363 {
364     emit vibrationActivated(on);
365 }
366
367 void SeaScene::handleScreenTapped()
368 {
369
370     //If the game is going just pause it
371     if (!paused_)
372     {
373         pPauseAction_->setChecked(true);
374         return;
375     }
376
377     //If the game is paused, chacl if menu item was selected
378
379     QList<QGraphicsItem *> items = selectedItems();
380
381     //if nothing selected resume play
382
383     if (items.isEmpty())
384     {
385         pPauseAction_->setChecked(false);
386         return;
387
388     }
389
390     //If something was selected check if it was one of the menu items and act on it
391     //(Nothing else should be made selectable anyway)
392
393     //Menu functions
394
395     QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
396                                        //... so we can just take the first one
397
398
399     if (pItem == pRestartGameItem_)
400     {
401         qDebug() << "game restart requested";
402         restartGame();
403     }
404
405     else if (pItem == pRestartLevelItem_)
406     {
407         qDebug() << "Level restart requested";
408         restartLevel();
409
410     }
411
412     else if (pItem == pSettingsItem_)
413     {
414     //Temporary code for settings, likely to be turned into a QML dialog
415           QSettings settings;
416
417           QMessageBox::StandardButton buttonpressed = QMessageBox::question(NULL,"Settings","Do you wish to have vibration effects enabled?", QMessageBox::Yes | QMessageBox::No);
418
419           if (buttonpressed == QMessageBox::Yes)
420           {
421               pVibrateAction_->setChecked(true);
422               settings.setValue("vibration",true);
423           }
424           if (buttonpressed == QMessageBox::No)
425           {
426               pVibrateAction_->setChecked(false);
427               settings.setValue("vibration",false);
428           }
429     }
430
431     else if (pItem == pAboutItem_)
432     {
433         about();
434     }
435
436     else if (pItem == pQuitItem_)
437     {
438         qApp->quit();
439     }
440
441
442     //Selection is just used to get notice of a menu item being clicked, removed after use
443
444     clearSelection();
445
446     //The user propably went to paused state just to access menu, so unpause
447
448     pPauseAction_->setChecked(false);
449
450 }
451
452
453
454 void SeaScene::createMenuItems()
455 {
456
457     QFont font;
458     font.setPixelSize(35);
459
460
461
462     pPausetextItem_ = new QGraphicsTextItem;
463     pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
464     pPausetextItem_->setZValue(1000);
465     pPausetextItem_->setPos(200,50);
466     addItem(pPausetextItem_);
467     pPausetextItem_->hide();
468
469     menuItemCount_ = 0;
470
471     QString menufonthtml = "<font size = \"4\" color = darkorange>";
472
473     pRestartGameItem_ = new QGraphicsTextItem;
474     pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
475     prepareForMenu(pRestartGameItem_);
476
477     pRestartLevelItem_ = new QGraphicsTextItem;
478     pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
479     prepareForMenu(pRestartLevelItem_);
480
481     pSettingsItem_ = new QGraphicsTextItem;
482     pSettingsItem_->setHtml(tr("Vibration <br> effects").prepend(menufonthtml));
483     prepareForMenu(pSettingsItem_);
484
485     pAboutItem_ = new QGraphicsTextItem;
486     pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
487     prepareForMenu(pAboutItem_);
488
489     pQuitItem_ = new QGraphicsTextItem;
490     pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
491     prepareForMenu(pQuitItem_);
492
493 }
494
495 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
496 {
497
498     //Menu items have pause text item as their parent and are thus added to scene automatically
499     //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
500     //Their coordinates are given relative to the parent.
501
502
503     pItem->setParentItem(pPausetextItem_);
504     pItem->setZValue(1000);
505     pItem->setFlag(QGraphicsItem::ItemIsSelectable);
506     pItem->setY(150);
507     pItem->setX(menuItemCount_++*160-150);
508  }
509
510
511 void SeaScene::about()
512 {
513     QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
514                        tr("Version %1"
515                           "<p>Copyright 2011 Heli Hyv&auml;ttinen"
516                           "<p>License: General Public License v2"
517                           "<p>Bug Reports: https://bugs.maemo.org/ "
518                           "enter_bug.cgi?product=Ghosts%20Overboard"
519                           ).arg(QApplication::applicationVersion()));
520
521
522
523 }
524
525
526 void SeaScene::restartLevel()
527 {
528     setupMap(levelList_.value(currentLevel_));  //value() returns default constructor Level if index is invalid, so no risk of crash
529     vibrationActivate(pVibrateAction_->isChecked());  //Vibration effects are lost without this
530    // qDebug() << pVibrateAction_->isChecked();
531 }
532
533
534
535 void SeaScene::nextLevel()
536 {
537
538     currentLevel_++;
539
540     if (levelList_.empty())
541         setupMap(Level());
542
543
544     if ( currentLevel_ < levelList_.size() )
545     {
546        restartLevel();
547     }
548
549     else //Victory!
550     {
551
552        QDialog* pVictoryDialog = new QDialog();
553        pVictoryDialog->setWindowTitle(tr("You won!"));
554
555
556        QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
557 //       QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
558
559        QPixmap victoryIcon (":/pix/aavesaari.png");
560        QLabel* pVictoryLabel = new QLabel();
561        pVictoryLabel->setPixmap(victoryIcon);
562
563        QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
564
565
566        QVBoxLayout* pMainLayout = new QVBoxLayout;
567
568        QHBoxLayout* pTopLayout = new QHBoxLayout;
569        pMainLayout->addLayout(pTopLayout);
570
571        pTopLayout->addWidget(pVictoryLabel);
572        pTopLayout->addWidget(pTextLabel);
573
574
575
576        QHBoxLayout* pButtonLayout = new QHBoxLayout();
577        pMainLayout->addLayout(pButtonLayout);
578
579  //      pButtonLayout->addWidget(pQuitButton);
580        pButtonLayout->addWidget(pPlayAgainButton);
581
582
583
584        pVictoryDialog->setLayout(pMainLayout);
585
586        connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
587
588        pVictoryDialog->exec();
589
590         //Never mind if the user cancels the dialog: restart the game anyway
591
592        restartGame();
593     }
594 }
595
596
597 void SeaScene::restartGame()
598 {
599     currentLevel_ = 0;
600     restartLevel();
601 }
602
603
604 void SeaScene::forcePause()
605 {
606     //Pause without setting the pause action state
607     pause(true);
608 }
609
610 void::SeaScene::softContinue()
611 {
612     //Continue if not being paused by the user
613     // Reverts forcePause()
614
615     pause(pPauseAction_->isChecked());
616 }