1 /**************************************************************************
2 Ghosts Overboard - a game for Maemo 5
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>
37 const QString ghostImageFilename_ = ":/pix/aave.png";
38 const QString rockImageFilename_ =":/pix/kari.png";
39 const QString octopusImageFilename_= ":/pix/tursas.png";
42 SeaScene::SeaScene(QObject *parent) :
43 QGraphicsScene(parent)
46 setItemPointersNull();
49 screenLitKeeper_.keepScreenLit(true);
53 QPixmap waves (":/pix/meri.png");
55 setBackgroundBrush(QBrush(waves));
59 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
61 //Setup the level list
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);
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);
81 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
82 pVibrateAction_->setCheckable(true);
83 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
85 pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
88 pPauseAction_ = new QAction(tr("Pause"),this);
89 pPauseAction_->setCheckable(true);
90 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
93 autopauseTimer.setSingleShot(true);
94 autopauseTimer.setInterval(15*60*1000);
95 connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(forcePause()));
100 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
106 setItemPointersNull();
110 createAboutBoxItems();
112 //empty the list of moving items
114 movingItems_.clear();
116 //empty the list of free slots
119 //fill the list of free slots
121 int numberOfXTiles = width() / 40;
122 int numberOfYTiles = height() /40;
124 // qDebug() << numberOfXTiles << " slots in x direction";
125 // qDebug() << numberOfYTiles << " slots in y rirection";
127 for (int i = 0; i < numberOfXTiles; i++ )
129 for (int j = 0; j < numberOfYTiles; j++)
131 freeTiles_.append(QPointF(i*40,j*40));
138 for (int i = 0; i < rocks; i++)
140 QPointF * pPosition = findRandomFreeSlot();
142 //If there was no room no point to continue
143 if (pPosition == NULL)
146 QPixmap rockPixmap (":/pix/kari.png");
147 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
148 pRock->setData(0,"rock");
149 pRock->setPos(*pPosition);
156 ghostsLeft_ = ghosts;
157 spreadGhosts(ghosts);
161 //spread the octopuses
163 QList <Octopus*> octopusList;
165 for (int i=0; i < octopuses; i++)
167 QPointF * pPosition = findRandomFreeSlot();
169 //If there was no room no point to continue
170 if (pPosition == NULL)
173 QPixmap octopusPixmap (":/pix/tursas.png");
174 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
175 pOctopus->setData(0,"octopus");
176 pOctopus->setPos(*pPosition);
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);
190 QPointF * pPosition = findRandomFreeSlot();
191 if (pPosition == NULL)
193 // Game cannot begin without a free position for ship, so give an error message and return
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.");
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"));
212 Ship * pShip = new Ship (shipImages);
213 pShip->setData(0,"ship");
214 pShip->setPos(*pPosition);
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)
225 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
232 void SeaScene::setupMap(Level level)
234 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
237 void SeaScene::spreadGhosts(int ghosts)
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
244 QList<QPointF> temporarilyReservedSlots;
246 foreach (QGraphicsItem* pItem, movingItems_)
250 // qDebug() << "NULL item in movingItems_";
254 //round x and y down to fit the slot size
264 QPointF position (x,y);
266 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
268 if (freeTiles_.removeOne(position))
269 temporarilyReservedSlots.append(position);
274 if (freeTiles_.removeOne(position))
275 temporarilyReservedSlots.append(position);
279 if (freeTiles_.removeOne(position))
280 temporarilyReservedSlots.append(position);
284 if (freeTiles_.removeOne(position))
285 temporarilyReservedSlots.append(position);
290 //spread ghosts in random free slots
292 for (int i=0; i < ghosts; i++)
294 QPointF * pPosition = findRandomFreeSlot();
296 //If there was no room no point to continue
297 if (pPosition == NULL)
300 QPixmap ghostPixmap(":/pix/aave.png");
301 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
302 pGhost->setData(0,"ghost");
303 pGhost->setPos(*pPosition);
307 //return the slots occupied by moving items to free slots
308 freeTiles_.append(temporarilyReservedSlots);
310 //clear temp for the next round
311 temporarilyReservedSlots.clear();
314 QPointF* SeaScene::findRandomFreeSlot()
316 if (freeTiles_.isEmpty())
319 int index = qrand()%freeTiles_.size();
321 // qDebug() << index << " index";
322 return new QPointF (freeTiles_.takeAt(index));
326 void SeaScene::removeGhost(QGraphicsItem *pGhost)
328 removeItem(pGhost); //remove the item from scene
329 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
332 if (ghostsLeft_ == 0)
334 emit allGhostsPicked();
335 // qDebug() << "All ghosts picked!";
339 void SeaScene::ghostsDropped(int ghosts)
341 ghostsLeft_ += ghosts;
343 spreadGhosts(ghosts);
346 void SeaScene::pause(bool paused)
348 // qDebug() << "pause pressed " << paused;
349 if (paused_ == paused)
356 // qDebug() << "starting to move again";
358 screenLitKeeper_.keepScreenLit(true);
360 pPausetextItem_->hide();
362 autopauseTimer.start(); //Start counting towards autopause
367 qDebug("about to stop movement");
369 screenLitKeeper_.keepScreenLit(false);
370 if (pPausetextItem_ != NULL)
372 qDebug() << "about to show the pause text";
373 pPausetextItem_->show();
374 qDebug() << "showing pause text";
376 else qDebug() << "No pause text available";
378 autopauseTimer.stop(); //No need to count toward autopause when already paused
382 void SeaScene::vibrationActivate(bool on)
384 emit vibrationActivated(on);
387 void SeaScene::handleScreenTapped()
390 //If the game is going just pause it
393 pPauseAction_->setChecked(true);
397 //If the game is paused and about box is shown, close it and show the pause text and menu again
401 if(pAboutBoxItem_->isVisible())
403 pAboutBoxItem_->hide();
404 pPausetextItem_->show();
408 //If the game is paused, check if menu item was selected
410 QList<QGraphicsItem *> items = selectedItems();
412 //if nothing selected resume play
416 pPauseAction_->setChecked(false);
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)
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
429 //Selection is just used to get notice of a menu item being clicked, removed after use
434 //Act upon the selected item
437 if (pItem == pRestartGameItem_)
439 qDebug() << "game restart requested";
441 pPauseAction_->setChecked(false); //unpause game
445 else if (pItem == pRestartLevelItem_)
447 qDebug() << "Level restart requested";
449 pPauseAction_->setChecked(false); //unpause game
453 else if (pItem == pSettingsItem_)
455 pVibrateAction_->toggle();
458 settings.setValue("vibration",pVibrateAction_->isChecked());
460 QString text = pSettingsItem_->toHtml();
461 if (pVibrateAction_->isChecked())
462 text.replace(" on"," off"); //don't remove spaces or you get vibratioff...
464 text.replace(" off"," on");
465 pSettingsItem_->setHtml(text);
468 else if (pItem == pAboutItem_)
473 else if (pItem == pQuitItem_)
482 void SeaScene::createMenuItems()
486 font.setPixelSize(35);
490 pPausetextItem_ = new QGraphicsTextItem;
492 // This is for fremantle
493 // pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
495 //Harmattan needs bigger font
496 pPausetextItem_->setHtml("<font size = \"7\" color = darkorange> Game paused. Tap to continue.");
499 pPausetextItem_->setZValue(1000);
500 pPausetextItem_->setPos(200,50);
501 addItem(pPausetextItem_);
502 pPausetextItem_->hide();
506 // This is for fremantle
507 // QString menufonthtml = "<font size = \"4\" color = darkorange>";
510 //Harmattan needs bigger font
511 QString menufonthtml = "<font size = \"6\" color = darkorange>";
514 pRestartGameItem_ = new QGraphicsTextItem;
515 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
516 prepareForMenu(pRestartGameItem_);
518 pRestartLevelItem_ = new QGraphicsTextItem;
519 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
520 prepareForMenu(pRestartLevelItem_);
522 pSettingsItem_ = new QGraphicsTextItem;
523 QString vibraText(tr("Vibration <br> effects "));
525 if (pVibrateAction_->isChecked())
533 vibraText.append(statusText);
534 pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
535 prepareForMenu(pSettingsItem_);
537 pAboutItem_ = new QGraphicsTextItem;
538 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
539 prepareForMenu(pAboutItem_);
541 pQuitItem_ = new QGraphicsTextItem;
542 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
543 prepareForMenu(pQuitItem_);
547 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
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.
555 pItem->setParentItem(pPausetextItem_);
556 pItem->setZValue(1000);
557 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
559 pItem->setX(menuItemCount_++*160-150);
563 void SeaScene::about()
565 pPausetextItem_->hide();
566 pAboutBoxItem_->show();
570 void SeaScene::restartLevel()
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
580 void SeaScene::nextLevel()
585 if (levelList_.empty())
589 if ( currentLevel_ < levelList_.size() )
597 QDialog* pVictoryDialog = new QDialog();
598 pVictoryDialog->setWindowTitle(tr("You won!"));
601 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
602 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
604 QPixmap victoryIcon (":/pix/aavesaari.png");
605 QLabel* pVictoryLabel = new QLabel();
606 pVictoryLabel->setPixmap(victoryIcon);
608 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
611 QVBoxLayout* pMainLayout = new QVBoxLayout;
613 QHBoxLayout* pTopLayout = new QHBoxLayout;
614 pMainLayout->addLayout(pTopLayout);
616 pTopLayout->addWidget(pVictoryLabel);
617 pTopLayout->addWidget(pTextLabel);
621 QHBoxLayout* pButtonLayout = new QHBoxLayout();
622 pMainLayout->addLayout(pButtonLayout);
624 // pButtonLayout->addWidget(pQuitButton);
625 pButtonLayout->addWidget(pPlayAgainButton);
629 pVictoryDialog->setLayout(pMainLayout);
631 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
633 pVictoryDialog->exec();
635 //Never mind if the user cancels the dialog: restart the game anyway
642 void SeaScene::restartGame()
649 void SeaScene::forcePause()
651 //Pause without setting the pause action state
655 void::SeaScene::softContinue()
657 //Continue if not being paused by the user
658 // Reverts forcePause()
660 pause(pPauseAction_->isChecked());
663 void SeaScene::createAboutBoxItems()
665 pAboutBoxItem_ = new QGraphicsTextItem;
666 addItem(pAboutBoxItem_);
667 pAboutBoxItem_->setPos(25,50);
668 pAboutBoxItem_->setZValue(1000);
669 pAboutBoxItem_->hide();
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ä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()));
681 void SeaScene::setItemPointersNull()
683 pPausetextItem_ = NULL;
684 pRestartLevelItem_ = NULL;
685 pRestartGameItem_ = NULL;
686 pSettingsItem_ = NULL;
689 // pMinimizeItem_ = NULL; //Fremantle spesific
691 pAboutBoxItem_ = NULL;