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