1 /**************************************************************************
2 Ghosts Overboard - a game for 'Meego 1.2 Harmattan'
4 Copyright (C) 2011 Heli Hyvättinen
6 This file is part of Ghosts Overboard
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.
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.
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/>.
21 **************************************************************************/
26 #include <QGraphicsPixmapItem>
28 #include <QMessageBox>
30 #include <QApplication>
32 #include <QPushButton>
34 #include <QVBoxLayout>
38 const QString ghostImageFilename_ = ":/pix/aave.png";
39 const QString rockImageFilename_ =":/pix/kari.png";
40 const QString octopusImageFilename_= ":/pix/tursas.png";
43 SeaScene::SeaScene(QObject *parent) :
44 QGraphicsScene(parent)
47 setItemPointersNull();
50 screenLitKeeper_.keepScreenLit(true);
54 QPixmap waves (":/pix/meri.png");
56 setBackgroundBrush(QBrush(waves));
60 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
64 //Setup the level list
66 QList<Level> levelList;
68 levelList.append(level1);
69 Level level2(5,10,2,50);
70 levelList.append(level2);
71 Level level3(5,15,2,50);
72 levelList.append(level3);
73 Level level4(5,15,4,50);
74 levelList.append(level4);
75 Level level5(5,15,5,100);
76 levelList.append(level5);
78 Levelset set ("Original",levelList);
80 availableLevelsets_.append(set);
86 //Create another set of levels and place it in the available levelsets list
88 Level set2level1(8,15,4,50);
89 levelList.append(set2level1);
90 Level set2level2(8,20,4,50);
91 levelList.append(set2level2);
92 Level set2level3(8,15,5,100);
93 levelList.append(set2level3);
94 Level set2level4(8,20,6,100);
95 levelList.append(set2level4);
96 Level set2level5(8,20,6,150);
97 levelList.append(set2level5);
99 Levelset set2("Difficult",levelList);
100 availableLevelsets_.append(set2);
103 //This ensures that nextlevel will not be called until its safe to delete the Ship object.
104 //Leaving out Qt::QueuedConnection or calling nextlevel directly instead of emitting the signal will CRASH
105 connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()),Qt::QueuedConnection);
108 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
109 pVibrateAction_->setCheckable(true);
110 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
112 pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
115 pPauseAction_ = new QAction(tr("Pause"),this);
116 pPauseAction_->setCheckable(true);
117 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
120 autopauseTimer.setSingleShot(true);
121 autopauseTimer.setInterval(15*60*1000);
122 connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(turnPauseOn()));
127 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
133 setItemPointersNull();
137 createAboutBoxItems();
138 createVictoryItems();
140 createLevelCompletedItems();
143 //empty the list of moving items
145 movingItems_.clear();
147 //empty the list of free slots
150 //fill the list of free slots
152 int numberOfXTiles = width() / 40;
153 int numberOfYTiles = height() /40;
155 // qDebug() << numberOfXTiles << " slots in x direction";
156 // qDebug() << numberOfYTiles << " slots in y rirection";
158 for (int i = 0; i < numberOfXTiles; i++ )
160 for (int j = 0; j < numberOfYTiles; j++)
162 freeTiles_.append(QPointF(i*40,j*40));
169 for (int i = 0; i < rocks; i++)
171 QPointF * pPosition = findRandomFreeSlot();
173 //If there was no room no point to continue
174 if (pPosition == NULL)
177 QPixmap rockPixmap (":/pix/kari.png");
178 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
179 pRock->setData(0,"rock");
180 pRock->setPos(*pPosition);
187 ghostsLeft_ = ghosts;
188 spreadGhosts(ghosts);
192 //spread the octopuses
194 QList <Octopus*> octopusList;
196 for (int i=0; i < octopuses; i++)
198 QPointF * pPosition = findRandomFreeSlot();
200 //If there was no room no point to continue
201 if (pPosition == NULL)
204 QPixmap octopusPixmap (":/pix/tursas.png");
205 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
206 pOctopus->setData(0,"octopus");
207 pOctopus->setPos(*pPosition);
209 pOctopus->startMoving();
210 movingItems_.append(pOctopus);
211 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
212 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
213 octopusList.append(pOctopus);
221 QPointF * pPosition = findRandomFreeSlot();
222 if (pPosition == NULL)
224 // Game cannot begin without a free position for ship, so give an error message and return
226 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
230 QList<QPixmap> shipImages;
231 shipImages.append(QPixmap(":/pix/laiva.png"));
232 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
233 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
234 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
235 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
236 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
237 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
238 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
239 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
240 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
241 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
243 Ship * pShip = new Ship (shipImages);
244 pShip->setData(0,"ship");
245 pShip->setPos(*pPosition);
247 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
248 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
249 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
250 pShip->startMoving();
251 movingItems_.append(pShip);
252 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
253 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
254 foreach (Octopus* pOctopus, octopusList)
256 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
261 void SeaScene::setupMap(Level level)
263 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
266 void SeaScene::spreadGhosts(int ghosts)
270 //the octopuses and the ship may have moved from their original positions,
271 //so the list of free slots must be adjusted to exclude their current positions
273 QList<QPointF> temporarilyReservedSlots;
275 foreach (QGraphicsItem* pItem, movingItems_)
279 // qDebug() << "NULL item in movingItems_";
283 //round x and y down to fit the slot size
293 QPointF position (x,y);
295 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
297 if (freeTiles_.removeOne(position))
298 temporarilyReservedSlots.append(position);
303 if (freeTiles_.removeOne(position))
304 temporarilyReservedSlots.append(position);
308 if (freeTiles_.removeOne(position))
309 temporarilyReservedSlots.append(position);
313 if (freeTiles_.removeOne(position))
314 temporarilyReservedSlots.append(position);
319 //spread ghosts in random free slots
321 for (int i=0; i < ghosts; i++)
323 QPointF * pPosition = findRandomFreeSlot();
325 //If there was no room no point to continue
326 if (pPosition == NULL)
329 QPixmap ghostPixmap(":/pix/aave.png");
330 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
331 pGhost->setData(0,"ghost");
332 pGhost->setPos(*pPosition);
336 //return the slots occupied by moving items to free slots
337 freeTiles_.append(temporarilyReservedSlots);
339 //clear temp for the next round
340 temporarilyReservedSlots.clear();
343 QPointF* SeaScene::findRandomFreeSlot()
345 if (freeTiles_.isEmpty())
348 int index = qrand()%freeTiles_.size();
350 // qDebug() << index << " index";
351 return new QPointF (freeTiles_.takeAt(index));
355 void SeaScene::removeGhost(QGraphicsItem *pGhost)
357 removeItem(pGhost); //remove the item from scene
358 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
361 if (ghostsLeft_ == 0)
363 emit allGhostsPicked();
364 // qDebug() << "All ghosts picked!";
368 void SeaScene::ghostsDropped(int ghosts)
370 ghostsLeft_ += ghosts;
372 spreadGhosts(ghosts);
375 void SeaScene::pause(bool paused)
377 // qDebug() << "pause pressed " << paused;
378 if (paused_ == paused)
385 // qDebug() << "starting to move again";
386 // emit fullscreenRequested(); fremantle specific (since no "show statusbar" action in harmattan version)
388 screenLitKeeper_.keepScreenLit(true);
390 pPausetextItem_->hide();
392 scoreCounter_.start();
394 autopauseTimer.start(); //Start counting towards autopause
399 // qDebug("about to stop movement");
401 screenLitKeeper_.keepScreenLit(false);
402 if (pPausetextItem_ != NULL)
404 // qDebug() << "about to show the pause text";
405 pPausetextItem_->show();
406 // qDebug() << "showing pause text";
408 // else qDebug() << "No pause text available";
410 levelScore_ += scoreCounter_.elapsed();
412 autopauseTimer.stop(); //No need to count toward autopause when already paused
416 void SeaScene::vibrationActivate(bool on)
418 emit vibrationActivated(on);
421 void SeaScene::handleScreenTapped()
424 //If the game is going just pause it
427 pPauseAction_->setChecked(true);
431 //If the game is paused and about box is shown, close it and show the pause text and menu again
435 if(pAboutBoxItem_->isVisible())
437 pAboutBoxItem_->hide();
438 pPausetextItem_->show();
443 //If the game is paused, check if the level completed item is shown
445 if (pLevelCompletedItem_)
447 if (pLevelCompletedItem_->isVisible())
449 pLevelCompletedItem_->hide();
450 restartLevel(); //Current level has already been set to the next one before showing the level completed item
451 pPauseAction_->setChecked(false); //unpause
456 //If the game is paused, check if the victory item is being shown
457 if(pVictoryCongratulationsItem_)
459 if (pVictoryCongratulationsItem_->isVisibleTo(NULL)) //returns visibility to scene
461 pVictoryCongratulationsItem_->hide();
463 pPauseAction_->setChecked(false); // unpause
469 //If the game is paused and no victory or about box, check if menu item was selected
471 QList<QGraphicsItem *> items = selectedItems();
473 //if nothing selected resume play
477 pPauseAction_->setChecked(false);
482 //If something was selected check if it was one of the menu items and act on it
483 //(Nothing else should be made selectable anyway)
487 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
488 //... so we can just take the first one
490 //Selection is just used to get notice of a menu item being clicked, removed after use
495 //Act upon the selected item
498 if (pItem == pRestartGameItem_)
500 // qDebug() << "game restart requested";
502 pPauseAction_->setChecked(false); //unpause game
506 else if (pItem == pRestartLevelItem_)
508 // qDebug() << "Level restart requested";
510 pPauseAction_->setChecked(false); //unpause game
514 else if (pItem == pSettingsItem_)
516 pVibrateAction_->toggle();
519 settings.setValue("vibration",pVibrateAction_->isChecked());
521 QString text = pSettingsItem_->toHtml();
522 if (pVibrateAction_->isChecked())
523 text.replace(" on"," off"); //don't remove spaces or you get vibratioff...
525 text.replace(" off"," on");
526 pSettingsItem_->setHtml(text);
529 else if (pItem == pAboutItem_)
534 else if (pItem == pQuitItem_)
543 void SeaScene::createMenuItems()
547 font.setPixelSize(35);
551 pPausetextItem_ = new QGraphicsTextItem;
553 // This is for fremantle
554 // pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
556 //Harmattan needs bigger font
557 pPausetextItem_->setHtml("<font size = \"7\" color = darkorange> Game paused. Tap to continue.");
560 pPausetextItem_->setZValue(1000);
561 pPausetextItem_->setPos(200,50);
562 addItem(pPausetextItem_);
563 pPausetextItem_->hide();
567 // This is for fremantle
568 // QString menufonthtml = "<font size = \"4\" color = darkorange>";
571 //Harmattan needs bigger font
572 QString menufonthtml = "<font size = \"6\" color = darkorange>";
575 pRestartGameItem_ = new QGraphicsTextItem;
576 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
577 prepareForMenu(pRestartGameItem_);
579 pRestartLevelItem_ = new QGraphicsTextItem;
580 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
581 prepareForMenu(pRestartLevelItem_);
583 pSettingsItem_ = new QGraphicsTextItem;
584 QString vibraText(tr("Turn vibration <br> effects "));
586 if (pVibrateAction_->isChecked())
594 vibraText.append(statusText);
595 pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
596 prepareForMenu(pSettingsItem_);
598 pAboutItem_ = new QGraphicsTextItem;
599 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
600 prepareForMenu(pAboutItem_);
602 pQuitItem_ = new QGraphicsTextItem;
603 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
604 prepareForMenu(pQuitItem_);
608 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
611 //Menu items have pause text item as their parent and are thus added to scene automatically
612 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
613 //Their coordinates are given relative to the parent.
616 pItem->setParentItem(pPausetextItem_);
617 pItem->setZValue(1000);
618 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
620 pItem->setX(menuItemCount_++*160-150);
624 void SeaScene::about()
626 pPausetextItem_->hide();
627 pAboutBoxItem_->show();
631 void SeaScene::restartLevel()
636 setupMap(levelset_.getLevel(currentLevel_)); //getLevel() returns default constructor Level if index is invalid, so no risk of crash
638 scoreCounter_.start();
640 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
641 // qDebug() << pVibrateAction_->isChecked();
642 autopauseTimer.start(); //reset counting towards autopause
649 void SeaScene::nextLevel()
652 //get score for previous level
653 levelScore_ += scoreCounter_.elapsed();
654 totalScore_ += levelScore_;
655 int highscore = levelset_.getLevelHighScore(currentLevel_);
657 qDebug() << highscore;
661 if (levelScore_ >= highscore)
663 scoretext = tr("<font size=\"7\" color = darkorange>Your time: %1.%2 s<br>Best time: %3.%4 s").arg(levelScore_/1000).arg((levelScore_%1000)/100).arg(highscore/1000).arg((highscore%1000)/100);
666 else //New high score!
669 scoretext = tr("<font size=\"7\" color = darkorange>Your time %1.%2 s is<br>the new best time!").arg(levelScore_/1000).arg((levelScore_%1000)/100);
670 levelset_.setLevelHighScore(currentLevel_,levelScore_);
673 //pause to show the highscore or victory screen
676 pPausetextItem_->hide();
679 //Go to the next level if available
682 if ( currentLevel_ < levelset_.numberOfLevels() )
685 pLevelCompletedItem_->setHtml(scoretext);
686 pLevelCompletedItem_->show();
692 int totalHighsore = levelset_.getTotalHighScore();
693 if (totalScore_ >= totalHighsore)
695 scoretext.append(tr("<br>Your total time: %1.%2 s<br>Best total time: %3.%4 s").arg(totalScore_/1000).arg((totalScore_%1000)/100).arg(totalHighsore/1000).arg((totalHighsore%1000)/100));
697 else //new total high score
699 scoretext.append(tr("<br>Your total time %1.%2 s is<br>the new best time").arg(totalScore_/1000).arg((totalScore_%1000)/100));
700 levelset_.setTotalHighScore(totalScore_);
703 pVictoryScoreItem_->setHtml(scoretext);
704 pVictoryCongratulationsItem_->show();
709 void SeaScene::restartGame()
717 void SeaScene::forcePause()
719 //Pause without setting the pause action state
723 void SeaScene::softContinue()
725 //Continue if not being paused by the user
726 // Reverts forcePause()
728 pause(pPauseAction_->isChecked());
732 void SeaScene::createVictoryItems()
734 pVictoryCongratulationsItem_ = new QGraphicsTextItem;
735 pVictoryCongratulationsItem_->setHtml("<font size=\"7\" color = darkorange> <b> Victory!");
736 pVictoryCongratulationsItem_->hide();
737 pVictoryCongratulationsItem_->setPos(315,30);
738 pVictoryCongratulationsItem_->setZValue(1000);
739 addItem(pVictoryCongratulationsItem_);
742 //coordinates are relative to the parent
744 QGraphicsTextItem * pTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
745 pTextItem->setHtml("<font size=\"7\" color = darkorange> Congratulations!");
746 pTextItem->setPos(-50,80);
747 pTextItem->setZValue(1000);
749 QGraphicsTextItem * pMiddleTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
750 pMiddleTextItem->setHtml("<font size=\"7\" color = darkorange> You have saved all the ghosts.");
751 pMiddleTextItem->setPos(-145,120);
752 pMiddleTextItem->setZValue(1000);
755 pVictoryScoreItem_ = new QGraphicsTextItem(pVictoryCongratulationsItem_);
756 pVictoryScoreItem_->setPos(-50,180);
757 pMiddleTextItem->setZValue(1000);
758 //Text is set at usetime!
760 QGraphicsTextItem * pLowestTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
761 pLowestTextItem->setHtml("<font size=\"7\" color = darkorange> Tap to play again");
762 pLowestTextItem->setPos(-50,360);
763 pLowestTextItem->setZValue(1000);
767 void SeaScene::createAboutBoxItems()
769 pAboutBoxItem_ = new QGraphicsTextItem;
770 addItem(pAboutBoxItem_);
771 pAboutBoxItem_->setPos(25,50);
772 pAboutBoxItem_->setZValue(1000);
773 pAboutBoxItem_->hide();
775 pAboutBoxItem_->setHtml(tr("<font color = darkorange size = \"7\">"
776 "%1 <br> <font size = \"7\"> Version %2"
777 "<p><font size = \"6\"> Copyright 2011 Heli Hyvättinen"
778 "<p><font size = \"6\"> License: General Public License v2"
779 "<p><font size = \"5\"> Web: http://ghostsoverboard.garage.maemo.org/<br>"
780 "Bug Reports: <br> https://bugs.maemo.org/"
781 "enter_bug.cgi?product=Ghosts%20Overboard"
782 ).arg(QApplication::applicationName(),QApplication::applicationVersion()));
786 void SeaScene::setItemPointersNull()
788 pPausetextItem_ = NULL;
789 pRestartLevelItem_ = NULL;
790 pRestartGameItem_ = NULL;
791 pSettingsItem_ = NULL;
794 // pMinimizeItem_ = NULL; //Fremantle spesific
796 pAboutBoxItem_ = NULL;
797 pLevelCompletedItem_ = NULL;
798 pVictoryScoreItem_ = NULL;
802 void SeaScene::turnPauseOn()
804 pPauseAction_->setChecked(true);
809 void SeaScene::createLevelCompletedItems()
811 pLevelCompletedItem_ = new QGraphicsTextItem;
812 addItem(pLevelCompletedItem_);
813 pLevelCompletedItem_->setPos(240,100);
814 pLevelCompletedItem_->setZValue(1000);
815 pLevelCompletedItem_->hide();
816 //The text is set at usetime
818 QGraphicsTextItem * pTapForNextLevelItem = new QGraphicsTextItem(pLevelCompletedItem_);
819 pTapForNextLevelItem->setPos(-60,100);
820 pTapForNextLevelItem->setZValue(1000);
821 pTapForNextLevelItem->setHtml("<font size=\"7\" color = darkorange>Tap to start the next level");