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