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 connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
79 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
80 pVibrateAction_->setCheckable(true);
81 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
83 pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
86 pPauseAction_ = new QAction(tr("Pause"),this);
87 pPauseAction_->setCheckable(true);
88 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
91 autopauseTimer.setSingleShot(true);
92 autopauseTimer.setInterval(15*60*1000);
93 connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(forcePause()));
98 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
104 setItemPointersNull();
108 createAboutBoxItems();
110 //empty the list of moving items
112 movingItems_.clear();
114 //empty the list of free slots
117 //fill the list of free slots
119 int numberOfXTiles = width() / 40;
120 int numberOfYTiles = height() /40;
122 // qDebug() << numberOfXTiles << " slots in x direction";
123 // qDebug() << numberOfYTiles << " slots in y rirection";
125 for (int i = 0; i < numberOfXTiles; i++ )
127 for (int j = 0; j < numberOfYTiles; j++)
129 freeTiles_.append(QPointF(i*40,j*40));
136 for (int i = 0; i < rocks; i++)
138 QPointF * pPosition = findRandomFreeSlot();
140 //If there was no room no point to continue
141 if (pPosition == NULL)
144 QPixmap rockPixmap (":/pix/kari.png");
145 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
146 pRock->setData(0,"rock");
147 pRock->setPos(*pPosition);
154 ghostsLeft_ = ghosts;
155 spreadGhosts(ghosts);
159 //spread the octopuses
161 QList <Octopus*> octopusList;
163 for (int i=0; i < octopuses; i++)
165 QPointF * pPosition = findRandomFreeSlot();
167 //If there was no room no point to continue
168 if (pPosition == NULL)
171 QPixmap octopusPixmap (":/pix/tursas.png");
172 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
173 pOctopus->setData(0,"octopus");
174 pOctopus->setPos(*pPosition);
176 pOctopus->startMoving();
177 movingItems_.append(pOctopus);
178 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
179 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
180 octopusList.append(pOctopus);
188 QPointF * pPosition = findRandomFreeSlot();
189 if (pPosition == NULL)
191 // Game cannot begin without a free position for ship, so give an error message and return
193 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
197 QList<QPixmap> shipImages;
198 shipImages.append(QPixmap(":/pix/laiva.png"));
199 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
200 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
201 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
202 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
203 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
204 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
205 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
206 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
207 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
208 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
210 Ship * pShip = new Ship (shipImages);
211 pShip->setData(0,"ship");
212 pShip->setPos(*pPosition);
214 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
215 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
216 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
217 pShip->startMoving();
218 movingItems_.append(pShip);
219 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
220 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
221 foreach (Octopus* pOctopus, octopusList)
223 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
230 void SeaScene::setupMap(Level level)
232 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
235 void SeaScene::spreadGhosts(int ghosts)
239 //the octopuses and the ship may have moved from their original positions,
240 //so the list of free slots must be adjusted to exclude their current positions
242 QList<QPointF> temporarilyReservedSlots;
244 foreach (QGraphicsItem* pItem, movingItems_)
248 // qDebug() << "NULL item in movingItems_";
252 //round x and y down to fit the slot size
262 QPointF position (x,y);
264 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
266 if (freeTiles_.removeOne(position))
267 temporarilyReservedSlots.append(position);
272 if (freeTiles_.removeOne(position))
273 temporarilyReservedSlots.append(position);
277 if (freeTiles_.removeOne(position))
278 temporarilyReservedSlots.append(position);
282 if (freeTiles_.removeOne(position))
283 temporarilyReservedSlots.append(position);
288 //spread ghosts in random free slots
290 for (int i=0; i < ghosts; i++)
292 QPointF * pPosition = findRandomFreeSlot();
294 //If there was no room no point to continue
295 if (pPosition == NULL)
298 QPixmap ghostPixmap(":/pix/aave.png");
299 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
300 pGhost->setData(0,"ghost");
301 pGhost->setPos(*pPosition);
305 //return the slots occupied by moving items to free slots
306 freeTiles_.append(temporarilyReservedSlots);
308 //clear temp for the next round
309 temporarilyReservedSlots.clear();
312 QPointF* SeaScene::findRandomFreeSlot()
314 if (freeTiles_.isEmpty())
317 int index = qrand()%freeTiles_.size();
319 // qDebug() << index << " index";
320 return new QPointF (freeTiles_.takeAt(index));
324 void SeaScene::removeGhost(QGraphicsItem *pGhost)
326 removeItem(pGhost); //remove the item from scene
327 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
330 if (ghostsLeft_ == 0)
332 emit allGhostsPicked();
333 // qDebug() << "All ghosts picked!";
337 void SeaScene::ghostsDropped(int ghosts)
339 ghostsLeft_ += ghosts;
341 spreadGhosts(ghosts);
344 void SeaScene::pause(bool paused)
346 // qDebug() << "pause pressed " << paused;
347 if (paused_ == paused)
354 // qDebug() << "starting to move again";
355 emit fullscreenRequested();
357 screenLitKeeper_.keepScreenLit(true);
359 pPausetextItem_->hide();
361 autopauseTimer.start(); //Start counting towards autopause
366 qDebug("about to stop movement");
368 screenLitKeeper_.keepScreenLit(false);
369 if (pPausetextItem_ != NULL)
371 qDebug() << "about to show the pause text";
372 pPausetextItem_->show();
373 qDebug() << "showing pause text";
375 else qDebug() << "No pause text available";
377 autopauseTimer.stop(); //No need to count toward autopause when already paused
381 void SeaScene::vibrationActivate(bool on)
383 emit vibrationActivated(on);
386 void SeaScene::handleScreenTapped()
389 //If the game is going just pause it
392 pPauseAction_->setChecked(true);
396 //If the game is paused and about box is shown, close it and show the pause text and menu again
400 if(pAboutBoxItem_->isVisible())
402 pAboutBoxItem_->hide();
403 pPausetextItem_->show();
407 //If the game is paused, check if menu item was selected
409 QList<QGraphicsItem *> items = selectedItems();
411 //if nothing selected resume play
415 pPauseAction_->setChecked(false);
420 //If something was selected check if it was one of the menu items and act on it
421 //(Nothing else should be made selectable anyway)
425 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
426 //... so we can just take the first one
428 //Selection is just used to get notice of a menu item being clicked, removed after use
433 //Act upon the selected item
436 if (pItem == pRestartGameItem_)
438 qDebug() << "game restart requested";
440 pPauseAction_->setChecked(false); //unpause game
444 else if (pItem == pRestartLevelItem_)
446 qDebug() << "Level restart requested";
448 pPauseAction_->setChecked(false); //unpause game
452 else if (pItem == pSettingsItem_)
454 pVibrateAction_->toggle();
457 settings.setValue("vibration",pVibrateAction_->isChecked());
459 QString text = pSettingsItem_->toHtml();
460 if (pVibrateAction_->isChecked())
461 text.replace(" on"," off"); //don't remove spaces or you get vibratioff...
463 text.replace(" off"," on");
464 pSettingsItem_->setHtml(text);
467 else if (pItem == pAboutItem_)
472 else if(pItem == pMinimizeItem_)
474 emit minimizeRequested();
477 else if (pItem == pQuitItem_)
485 void SeaScene::createMenuItems()
489 font.setPixelSize(35);
493 pPausetextItem_ = new QGraphicsTextItem;
494 pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
495 pPausetextItem_->setZValue(1000);
496 pPausetextItem_->setPos(165,50);
497 addItem(pPausetextItem_);
498 pPausetextItem_->hide();
502 QString menufonthtml = "<font size = \"4\" color = darkorange>";
504 pRestartGameItem_ = new QGraphicsTextItem;
505 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
506 prepareForMenu(pRestartGameItem_);
508 pRestartLevelItem_ = new QGraphicsTextItem;
509 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
510 prepareForMenu(pRestartLevelItem_);
512 pSettingsItem_ = new QGraphicsTextItem;
513 QString vibraText(tr("Vibration <br> effects "));
515 if (pVibrateAction_->isChecked())
523 vibraText.append(statusText);
524 pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
525 prepareForMenu(pSettingsItem_);
527 pAboutItem_ = new QGraphicsTextItem;
528 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
529 prepareForMenu(pAboutItem_);
531 pMinimizeItem_ = new QGraphicsTextItem;
532 pMinimizeItem_->setHtml(tr("Show <br> status bar").prepend(menufonthtml));
533 prepareForMenu(pMinimizeItem_);
535 pQuitItem_ = new QGraphicsTextItem;
536 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
537 prepareForMenu(pQuitItem_);
541 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
544 //Menu items have pause text item as their parent and are thus added to scene automatically
545 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
546 //Their coordinates are given relative to the parent.
553 pItem->setParentItem(pPausetextItem_);
554 pItem->setZValue(1000);
555 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
557 int row = menuItemCount_/(itemsPerRow);
558 pItem->setY(150+row*120);
559 pItem->setX(((menuItemCount_%(itemsPerRow))*180+5));
565 void SeaScene::about()
567 pPausetextItem_->hide();
568 pAboutBoxItem_->show();
572 void SeaScene::restartLevel()
574 setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
575 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
576 // qDebug() << pVibrateAction_->isChecked();
577 autopauseTimer.start(); //reset counting towards autopause
582 void SeaScene::nextLevel()
587 if (levelList_.empty())
591 if ( currentLevel_ < levelList_.size() )
599 QDialog* pVictoryDialog = new QDialog();
600 pVictoryDialog->setWindowTitle(tr("You won!"));
603 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
604 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
606 QPixmap victoryIcon (":/pix/aavesaari.png");
607 QLabel* pVictoryLabel = new QLabel();
608 pVictoryLabel->setPixmap(victoryIcon);
610 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
613 QVBoxLayout* pMainLayout = new QVBoxLayout;
615 QHBoxLayout* pTopLayout = new QHBoxLayout;
616 pMainLayout->addLayout(pTopLayout);
618 pTopLayout->addWidget(pVictoryLabel);
619 pTopLayout->addWidget(pTextLabel);
623 QHBoxLayout* pButtonLayout = new QHBoxLayout();
624 pMainLayout->addLayout(pButtonLayout);
626 // pButtonLayout->addWidget(pQuitButton);
627 pButtonLayout->addWidget(pPlayAgainButton);
631 pVictoryDialog->setLayout(pMainLayout);
633 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
635 pVictoryDialog->exec();
637 //Never mind if the user cancels the dialog: restart the game anyway
644 void SeaScene::restartGame()
651 void SeaScene::forcePause()
653 //Pause without setting the pause action state
657 void::SeaScene::softContinue()
659 //Continue if not being paused by the user
660 // Reverts forcePause()
662 pause(pPauseAction_->isChecked());
665 void SeaScene::createAboutBoxItems()
667 pAboutBoxItem_ = new QGraphicsTextItem;
668 addItem(pAboutBoxItem_);
669 pAboutBoxItem_->setPos(25,50);
670 pAboutBoxItem_->setZValue(1000);
671 pAboutBoxItem_->hide();
673 pAboutBoxItem_->setHtml(tr("<font color = darkorange size = \"7\">"
674 "%1 <br> <font size = \"5\"> Version %2"
675 "<p><font size = \"4\"> Copyright 2011 Heli Hyvättinen"
676 "<p><font size = \"4\"> License: General Public License v2"
677 "<p><font size = \"3\"> Bug Reports: <br> https://bugs.maemo.org/ "
678 "enter_bug.cgi?product=Ghosts%20Overboard"
679 ).arg(QApplication::applicationName(),QApplication::applicationVersion()));
683 void SeaScene::setItemPointersNull()
685 pPausetextItem_ = NULL;
686 pRestartLevelItem_ = NULL;
687 pRestartGameItem_ = NULL;
688 pSettingsItem_ = NULL;
691 pMinimizeItem_ = NULL;
693 pAboutBoxItem_ = NULL;