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