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