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