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