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