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