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
62 //Setup the level list
64 QList<Level> levelList;
66 levelList.append(level1);
67 Level level2(5,10,2,50);
68 levelList.append(level2);
69 Level level3(5,15,2,50);
70 levelList.append(level3);
71 Level level4(5,15,4,50);
72 levelList.append(level4);
73 Level level5(5,15,5,100);
74 levelList.append(level5);
76 Levelset set ("Original",levelList);
81 //This ensures that nextlevel will not be called until its safe to delete the Ship object.
82 //Leaving out Qt::QueuedConnection or calling nextlevel directly instead of emitting the signal will CRASH
83 connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()),Qt::QueuedConnection);
86 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
87 pVibrateAction_->setCheckable(true);
88 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
90 pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
93 pPauseAction_ = new QAction(tr("Pause"),this);
94 pPauseAction_->setCheckable(true);
95 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
98 autopauseTimer.setSingleShot(true);
99 autopauseTimer.setInterval(15*60*1000);
100 connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(turnPauseOn()));
105 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
111 setItemPointersNull();
115 createAboutBoxItems();
116 createVictoryItems();
119 //empty the list of moving items
121 movingItems_.clear();
123 //empty the list of free slots
126 //fill the list of free slots
128 int numberOfXTiles = width() / 40;
129 int numberOfYTiles = height() /40;
131 // qDebug() << numberOfXTiles << " slots in x direction";
132 // qDebug() << numberOfYTiles << " slots in y rirection";
134 for (int i = 0; i < numberOfXTiles; i++ )
136 for (int j = 0; j < numberOfYTiles; j++)
138 freeTiles_.append(QPointF(i*40,j*40));
145 for (int i = 0; i < rocks; i++)
147 QPointF * pPosition = findRandomFreeSlot();
149 //If there was no room no point to continue
150 if (pPosition == NULL)
153 QPixmap rockPixmap (":/pix/kari.png");
154 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
155 pRock->setData(0,"rock");
156 pRock->setPos(*pPosition);
163 ghostsLeft_ = ghosts;
164 spreadGhosts(ghosts);
168 //spread the octopuses
170 QList <Octopus*> octopusList;
172 for (int i=0; i < octopuses; i++)
174 QPointF * pPosition = findRandomFreeSlot();
176 //If there was no room no point to continue
177 if (pPosition == NULL)
180 QPixmap octopusPixmap (":/pix/tursas.png");
181 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
182 pOctopus->setData(0,"octopus");
183 pOctopus->setPos(*pPosition);
185 pOctopus->startMoving();
186 movingItems_.append(pOctopus);
187 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
188 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
189 octopusList.append(pOctopus);
197 QPointF * pPosition = findRandomFreeSlot();
198 if (pPosition == NULL)
200 // Game cannot begin without a free position for ship, so give an error message and return
202 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
206 QList<QPixmap> shipImages;
207 shipImages.append(QPixmap(":/pix/laiva.png"));
208 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
209 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
210 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
211 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
212 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
213 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
214 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
215 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
216 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
217 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
219 Ship * pShip = new Ship (shipImages);
220 pShip->setData(0,"ship");
221 pShip->setPos(*pPosition);
223 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
224 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
225 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
226 pShip->startMoving();
227 movingItems_.append(pShip);
228 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
229 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
230 foreach (Octopus* pOctopus, octopusList)
232 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
239 void SeaScene::setupMap(Level level)
241 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
244 void SeaScene::spreadGhosts(int ghosts)
248 //the octopuses and the ship may have moved from their original positions,
249 //so the list of free slots must be adjusted to exclude their current positions
251 QList<QPointF> temporarilyReservedSlots;
253 foreach (QGraphicsItem* pItem, movingItems_)
257 // qDebug() << "NULL item in movingItems_";
261 //round x and y down to fit the slot size
271 QPointF position (x,y);
273 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
275 if (freeTiles_.removeOne(position))
276 temporarilyReservedSlots.append(position);
281 if (freeTiles_.removeOne(position))
282 temporarilyReservedSlots.append(position);
286 if (freeTiles_.removeOne(position))
287 temporarilyReservedSlots.append(position);
291 if (freeTiles_.removeOne(position))
292 temporarilyReservedSlots.append(position);
297 //spread ghosts in random free slots
299 for (int i=0; i < ghosts; i++)
301 QPointF * pPosition = findRandomFreeSlot();
303 //If there was no room no point to continue
304 if (pPosition == NULL)
307 QPixmap ghostPixmap(":/pix/aave.png");
308 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
309 pGhost->setData(0,"ghost");
310 pGhost->setPos(*pPosition);
314 //return the slots occupied by moving items to free slots
315 freeTiles_.append(temporarilyReservedSlots);
317 //clear temp for the next round
318 temporarilyReservedSlots.clear();
321 QPointF* SeaScene::findRandomFreeSlot()
323 if (freeTiles_.isEmpty())
326 int index = qrand()%freeTiles_.size();
328 // qDebug() << index << " index";
329 return new QPointF (freeTiles_.takeAt(index));
333 void SeaScene::removeGhost(QGraphicsItem *pGhost)
335 removeItem(pGhost); //remove the item from scene
336 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
339 if (ghostsLeft_ == 0)
341 emit allGhostsPicked();
342 // qDebug() << "All ghosts picked!";
346 void SeaScene::ghostsDropped(int ghosts)
348 ghostsLeft_ += ghosts;
350 spreadGhosts(ghosts);
353 void SeaScene::pause(bool paused)
355 // qDebug() << "pause pressed " << paused;
356 if (paused_ == paused)
363 // qDebug() << "starting to move again";
364 // emit fullscreenRequested(); fremantle specific (since no "show statusbar" action in harmattan version)
366 screenLitKeeper_.keepScreenLit(true);
368 pPausetextItem_->hide();
370 autopauseTimer.start(); //Start counting towards autopause
375 // qDebug("about to stop movement");
377 screenLitKeeper_.keepScreenLit(false);
378 if (pPausetextItem_ != NULL)
380 // qDebug() << "about to show the pause text";
381 pPausetextItem_->show();
382 // qDebug() << "showing pause text";
384 // else qDebug() << "No pause text available";
386 autopauseTimer.stop(); //No need to count toward autopause when already paused
390 void SeaScene::vibrationActivate(bool on)
392 emit vibrationActivated(on);
395 void SeaScene::handleScreenTapped()
398 //If the game is going just pause it
401 pPauseAction_->setChecked(true);
405 //If the game is paused and about box is shown, close it and show the pause text and menu again
409 if(pAboutBoxItem_->isVisible())
411 pAboutBoxItem_->hide();
412 pPausetextItem_->show();
417 //If the game is paused, check if the victory item is being shown
418 if(pVictoryCongratulationsItem_)
420 if (pVictoryCongratulationsItem_->isVisibleTo(NULL)) //returns visibility to scene
422 pVictoryCongratulationsItem_->hide();
424 pPauseAction_->setChecked(false); // unpause
429 //If the game is paused and no victory or about box, check if menu item was selected
431 QList<QGraphicsItem *> items = selectedItems();
433 //if nothing selected resume play
437 pPauseAction_->setChecked(false);
442 //If something was selected check if it was one of the menu items and act on it
443 //(Nothing else should be made selectable anyway)
447 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
448 //... so we can just take the first one
450 //Selection is just used to get notice of a menu item being clicked, removed after use
455 //Act upon the selected item
458 if (pItem == pRestartGameItem_)
460 // qDebug() << "game restart requested";
462 pPauseAction_->setChecked(false); //unpause game
466 else if (pItem == pRestartLevelItem_)
468 // qDebug() << "Level restart requested";
470 pPauseAction_->setChecked(false); //unpause game
474 else if (pItem == pSettingsItem_)
476 pVibrateAction_->toggle();
479 settings.setValue("vibration",pVibrateAction_->isChecked());
481 QString text = pSettingsItem_->toHtml();
482 if (pVibrateAction_->isChecked())
483 text.replace(" on"," off"); //don't remove spaces or you get vibratioff...
485 text.replace(" off"," on");
486 pSettingsItem_->setHtml(text);
489 else if (pItem == pAboutItem_)
494 else if (pItem == pQuitItem_)
503 void SeaScene::createMenuItems()
507 font.setPixelSize(35);
511 pPausetextItem_ = new QGraphicsTextItem;
513 // This is for fremantle
514 // pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
516 //Harmattan needs bigger font
517 pPausetextItem_->setHtml("<font size = \"7\" color = darkorange> Game paused. Tap to continue.");
520 pPausetextItem_->setZValue(1000);
521 pPausetextItem_->setPos(200,50);
522 addItem(pPausetextItem_);
523 pPausetextItem_->hide();
527 // This is for fremantle
528 // QString menufonthtml = "<font size = \"4\" color = darkorange>";
531 //Harmattan needs bigger font
532 QString menufonthtml = "<font size = \"6\" color = darkorange>";
535 pRestartGameItem_ = new QGraphicsTextItem;
536 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
537 prepareForMenu(pRestartGameItem_);
539 pRestartLevelItem_ = new QGraphicsTextItem;
540 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
541 prepareForMenu(pRestartLevelItem_);
543 pSettingsItem_ = new QGraphicsTextItem;
544 QString vibraText(tr("Turn vibration <br> effects "));
546 if (pVibrateAction_->isChecked())
554 vibraText.append(statusText);
555 pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
556 prepareForMenu(pSettingsItem_);
558 pAboutItem_ = new QGraphicsTextItem;
559 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
560 prepareForMenu(pAboutItem_);
562 pQuitItem_ = new QGraphicsTextItem;
563 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
564 prepareForMenu(pQuitItem_);
568 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
571 //Menu items have pause text item as their parent and are thus added to scene automatically
572 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
573 //Their coordinates are given relative to the parent.
576 pItem->setParentItem(pPausetextItem_);
577 pItem->setZValue(1000);
578 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
580 pItem->setX(menuItemCount_++*160-150);
584 void SeaScene::about()
586 pPausetextItem_->hide();
587 pAboutBoxItem_->show();
591 void SeaScene::restartLevel()
593 setupMap(levelset_.getLevel(currentLevel_)); //getLevel() returns default constructor Level if index is invalid, so no risk of crash
594 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
595 // qDebug() << pVibrateAction_->isChecked();
596 autopauseTimer.start(); //reset counting towards autopause
601 void SeaScene::nextLevel()
606 if (!levelset_.isValid())
610 if ( currentLevel_ < levelset_.numberOfLevels() )
618 pPauseAction_->setChecked(true); //Pause the game while showing the victory dialog
620 pPausetextItem_->hide();
622 pVictoryCongratulationsItem_->show();
628 void SeaScene::restartGame()
635 void SeaScene::forcePause()
637 //Pause without setting the pause action state
641 void SeaScene::softContinue()
643 //Continue if not being paused by the user
644 // Reverts forcePause()
646 pause(pPauseAction_->isChecked());
650 void SeaScene::createVictoryItems()
652 pVictoryCongratulationsItem_ = new QGraphicsTextItem;
653 pVictoryCongratulationsItem_->setHtml("<font size=\"7\" color = darkorange> <b> Victory!");
654 pVictoryCongratulationsItem_->hide();
655 pVictoryCongratulationsItem_->setPos(355,50);
656 pVictoryCongratulationsItem_->setZValue(1000);
657 addItem(pVictoryCongratulationsItem_);
660 //coordinates are relative to the parent
662 QGraphicsTextItem * pTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
663 pTextItem->setHtml("<font size=\"7\" color = darkorange> Congratulations!");
664 pTextItem->setPos(-50,100);
665 pTextItem->setZValue(1000);
667 QGraphicsTextItem * pMiddleTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
668 pMiddleTextItem->setHtml("<font size=\"7\" color = darkorange> You have saved all the ghosts.");
669 pMiddleTextItem->setPos(-145,140);
670 pMiddleTextItem->setZValue(1000);
672 QGraphicsTextItem * pLowestTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
673 pLowestTextItem->setHtml("<font size=\"7\" color = darkorange> Tap to play again");
674 pLowestTextItem->setPos(-50,220);
675 pLowestTextItem->setZValue(1000);
679 void SeaScene::createAboutBoxItems()
681 pAboutBoxItem_ = new QGraphicsTextItem;
682 addItem(pAboutBoxItem_);
683 pAboutBoxItem_->setPos(25,50);
684 pAboutBoxItem_->setZValue(1000);
685 pAboutBoxItem_->hide();
687 pAboutBoxItem_->setHtml(tr("<font color = darkorange size = \"7\">"
688 "%1 <br> <font size = \"7\"> Version %2"
689 "<p><font size = \"6\"> Copyright 2011 Heli Hyvättinen"
690 "<p><font size = \"6\"> License: General Public License v2"
691 "<p><font size = \"5\"> Web: http://ghostsoverboard.garage.maemo.org/<br>"
692 "Bug Reports: <br> https://bugs.maemo.org/"
693 "enter_bug.cgi?product=Ghosts%20Overboard"
694 ).arg(QApplication::applicationName(),QApplication::applicationVersion()));
698 void SeaScene::setItemPointersNull()
700 pPausetextItem_ = NULL;
701 pRestartLevelItem_ = NULL;
702 pRestartGameItem_ = NULL;
703 pSettingsItem_ = NULL;
706 // pMinimizeItem_ = NULL; //Fremantle spesific
708 pAboutBoxItem_ = NULL;
711 void SeaScene::turnPauseOn()
713 pPauseAction_->setChecked(true);