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 screenLitKeeper_.keepScreenLit(true);
50 QPixmap waves (":/pix/meri.png");
52 setBackgroundBrush(QBrush(waves));
56 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
58 //Setup the level list
61 levelList_.append(level1);
62 Level level2(5,10,2,50);
63 levelList_.append(level2);
64 Level level3(5,15,2,50);
65 levelList_.append(level3);
66 Level level4(5,15,4,50);
67 levelList_.append(level4);
68 Level level5(5,15,5,100);
69 levelList_.append(level5);
73 connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
76 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
77 pVibrateAction_->setCheckable(true);
78 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
80 pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
83 pPauseAction_ = new QAction(tr("Pause"),this);
84 pPauseAction_->setCheckable(true);
85 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
88 autopauseTimer.setSingleShot(true);
89 autopauseTimer.setInterval(15*60*1000);
90 connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(forcePause()));
95 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
103 //empty the list of moving items
105 movingItems_.clear();
107 //empty the list of free slots
110 //fill the list of free slots
112 int numberOfXTiles = width() / 40;
113 int numberOfYTiles = height() /40;
115 // qDebug() << numberOfXTiles << " slots in x direction";
116 // qDebug() << numberOfYTiles << " slots in y rirection";
118 for (int i = 0; i < numberOfXTiles; i++ )
120 for (int j = 0; j < numberOfYTiles; j++)
122 freeTiles_.append(QPointF(i*40,j*40));
129 for (int i = 0; i < rocks; i++)
131 QPointF * pPosition = findRandomFreeSlot();
133 //If there was no room no point to continue
134 if (pPosition == NULL)
137 QPixmap rockPixmap (":/pix/kari.png");
138 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
139 pRock->setData(0,"rock");
140 pRock->setPos(*pPosition);
147 ghostsLeft_ = ghosts;
148 spreadGhosts(ghosts);
152 //spread the octopuses
154 QList <Octopus*> octopusList;
156 for (int i=0; i < octopuses; i++)
158 QPointF * pPosition = findRandomFreeSlot();
160 //If there was no room no point to continue
161 if (pPosition == NULL)
164 QPixmap octopusPixmap (":/pix/tursas.png");
165 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
166 pOctopus->setData(0,"octopus");
167 pOctopus->setPos(*pPosition);
169 pOctopus->startMoving();
170 movingItems_.append(pOctopus);
171 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
172 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
173 octopusList.append(pOctopus);
181 QPointF * pPosition = findRandomFreeSlot();
182 if (pPosition == NULL)
184 // Game cannot begin without a free position for ship, so give an error message and return
186 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
190 QList<QPixmap> shipImages;
191 shipImages.append(QPixmap(":/pix/laiva.png"));
192 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
193 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
194 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
195 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
196 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
197 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
198 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
199 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
200 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
201 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
203 Ship * pShip = new Ship (shipImages);
204 pShip->setData(0,"ship");
205 pShip->setPos(*pPosition);
207 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
208 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
209 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
210 pShip->startMoving();
211 movingItems_.append(pShip);
212 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
213 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
214 foreach (Octopus* pOctopus, octopusList)
216 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
223 void SeaScene::setupMap(Level level)
225 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
228 void SeaScene::spreadGhosts(int ghosts)
232 //the octopuses and the ship may have moved from their original positions,
233 //so the list of free slots must be adjusted to exclude their current positions
235 QList<QPointF> temporarilyReservedSlots;
237 foreach (QGraphicsItem* pItem, movingItems_)
241 // qDebug() << "NULL item in movingItems_";
245 //round x and y down to fit the slot size
255 QPointF position (x,y);
257 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
259 if (freeTiles_.removeOne(position))
260 temporarilyReservedSlots.append(position);
265 if (freeTiles_.removeOne(position))
266 temporarilyReservedSlots.append(position);
270 if (freeTiles_.removeOne(position))
271 temporarilyReservedSlots.append(position);
275 if (freeTiles_.removeOne(position))
276 temporarilyReservedSlots.append(position);
281 //spread ghosts in random free slots
283 for (int i=0; i < ghosts; i++)
285 QPointF * pPosition = findRandomFreeSlot();
287 //If there was no room no point to continue
288 if (pPosition == NULL)
291 QPixmap ghostPixmap(":/pix/aave.png");
292 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
293 pGhost->setData(0,"ghost");
294 pGhost->setPos(*pPosition);
298 //return the slots occupied by moving items to free slots
299 freeTiles_.append(temporarilyReservedSlots);
301 //clear temp for the next round
302 temporarilyReservedSlots.clear();
305 QPointF* SeaScene::findRandomFreeSlot()
307 if (freeTiles_.isEmpty())
310 int index = qrand()%freeTiles_.size();
312 // qDebug() << index << " index";
313 return new QPointF (freeTiles_.takeAt(index));
317 void SeaScene::removeGhost(QGraphicsItem *pGhost)
319 removeItem(pGhost); //remove the item from scene
320 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
323 if (ghostsLeft_ == 0)
325 emit allGhostsPicked();
326 // qDebug() << "All ghosts picked!";
330 void SeaScene::ghostsDropped(int ghosts)
332 ghostsLeft_ += ghosts;
334 spreadGhosts(ghosts);
337 void SeaScene::pause(bool paused)
339 // qDebug() << "pause pressed " << paused;
340 if (paused_ == paused)
347 // qDebug() << "starting to move again";
348 emit fullscreenRequested();
350 screenLitKeeper_.keepScreenLit(true);
352 pPausetextItem_->hide();
354 autopauseTimer.start(); //Start counting towards autopause
359 qDebug("about to stop movement");
361 screenLitKeeper_.keepScreenLit(false);
362 if (pPausetextItem_ != NULL)
364 qDebug() << "about to show the pause text";
365 pPausetextItem_->show();
366 qDebug() << "showing pause text";
368 else qDebug() << "No pause text available";
370 autopauseTimer.stop(); //No need to count toward autopause when already paused
374 void SeaScene::vibrationActivate(bool on)
376 emit vibrationActivated(on);
379 void SeaScene::handleScreenTapped()
382 //If the game is going just pause it
385 pPauseAction_->setChecked(true);
389 //If the game is paused, chacl if menu item was selected
391 QList<QGraphicsItem *> items = selectedItems();
393 //if nothing selected resume play
397 pPauseAction_->setChecked(false);
402 //If something was selected check if it was one of the menu items and act on it
403 //(Nothing else should be made selectable anyway)
407 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
408 //... so we can just take the first one
411 if (pItem == pRestartGameItem_)
413 qDebug() << "game restart requested";
417 else if (pItem == pRestartLevelItem_)
419 qDebug() << "Level restart requested";
424 else if (pItem == pSettingsItem_)
426 //Temporary code for settings, likely to be turned into a QML dialog
429 QMessageBox::StandardButton buttonpressed = QMessageBox::question(NULL,"Settings","Do you wish to have vibration effects enabled?", QMessageBox::Yes | QMessageBox::No);
431 if (buttonpressed == QMessageBox::Yes)
433 pVibrateAction_->setChecked(true);
434 settings.setValue("vibration",true);
436 if (buttonpressed == QMessageBox::No)
438 pVibrateAction_->setChecked(false);
439 settings.setValue("vibration",false);
443 else if (pItem == pAboutItem_)
448 else if(pItem == pMinimizeItem_)
450 emit minimizeRequested();
453 else if (pItem == pQuitItem_)
460 //Selection is just used to get notice of a menu item being clicked, removed after use
464 //The user propably went to paused state just to access menu, so unpause
465 //unless status bar was requested
466 if (pItem != pMinimizeItem_)
468 pPauseAction_->setChecked(false);
474 void SeaScene::createMenuItems()
478 font.setPixelSize(35);
482 pPausetextItem_ = new QGraphicsTextItem;
483 pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
484 pPausetextItem_->setZValue(1000);
485 pPausetextItem_->setPos(165,50);
486 addItem(pPausetextItem_);
487 pPausetextItem_->hide();
491 QString menufonthtml = "<font size = \"4\" color = darkorange>";
493 pRestartGameItem_ = new QGraphicsTextItem;
494 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
495 prepareForMenu(pRestartGameItem_);
497 pRestartLevelItem_ = new QGraphicsTextItem;
498 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
499 prepareForMenu(pRestartLevelItem_);
501 pSettingsItem_ = new QGraphicsTextItem;
502 pSettingsItem_->setHtml(tr("Vibration <br> effects").prepend(menufonthtml));
503 prepareForMenu(pSettingsItem_);
505 pAboutItem_ = new QGraphicsTextItem;
506 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
507 prepareForMenu(pAboutItem_);
509 pMinimizeItem_ = new QGraphicsTextItem;
510 pMinimizeItem_->setHtml(tr("Show <br> status bar").prepend(menufonthtml));
511 prepareForMenu(pMinimizeItem_);
513 pQuitItem_ = new QGraphicsTextItem;
514 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
515 prepareForMenu(pQuitItem_);
519 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
522 //Menu items have pause text item as their parent and are thus added to scene automatically
523 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
524 //Their coordinates are given relative to the parent.
531 pItem->setParentItem(pPausetextItem_);
532 pItem->setZValue(1000);
533 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
535 int row = menuItemCount_/(itemsPerRow);
536 pItem->setY(150+row*120);
537 pItem->setX(((menuItemCount_%(itemsPerRow))*180+5));
543 void SeaScene::about()
545 QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
547 "<p>Copyright 2011 Heli Hyvättinen"
548 "<p>License: General Public License v2"
549 "<p>Bug Reports: https://bugs.maemo.org/ "
550 "enter_bug.cgi?product=Ghosts%20Overboard"
551 ).arg(QApplication::applicationVersion()));
558 void SeaScene::restartLevel()
560 setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
561 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
562 // qDebug() << pVibrateAction_->isChecked();
563 autopauseTimer.start(); //reset counting towards autopause
568 void SeaScene::nextLevel()
573 if (levelList_.empty())
577 if ( currentLevel_ < levelList_.size() )
585 QDialog* pVictoryDialog = new QDialog();
586 pVictoryDialog->setWindowTitle(tr("You won!"));
589 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
590 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
592 QPixmap victoryIcon (":/pix/aavesaari.png");
593 QLabel* pVictoryLabel = new QLabel();
594 pVictoryLabel->setPixmap(victoryIcon);
596 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
599 QVBoxLayout* pMainLayout = new QVBoxLayout;
601 QHBoxLayout* pTopLayout = new QHBoxLayout;
602 pMainLayout->addLayout(pTopLayout);
604 pTopLayout->addWidget(pVictoryLabel);
605 pTopLayout->addWidget(pTextLabel);
609 QHBoxLayout* pButtonLayout = new QHBoxLayout();
610 pMainLayout->addLayout(pButtonLayout);
612 // pButtonLayout->addWidget(pQuitButton);
613 pButtonLayout->addWidget(pPlayAgainButton);
617 pVictoryDialog->setLayout(pMainLayout);
619 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
621 pVictoryDialog->exec();
623 //Never mind if the user cancels the dialog: restart the game anyway
630 void SeaScene::restartGame()
637 void SeaScene::forcePause()
639 //Pause without setting the pause action state
643 void::SeaScene::softContinue()
645 //Continue if not being paused by the user
646 // Reverts forcePause()
648 pause(pPauseAction_->isChecked());