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>
36 const QString ghostImageFilename_ = ":/pix/aave.png";
37 const QString rockImageFilename_ =":/pix/kari.png";
38 const QString octopusImageFilename_= ":/pix/tursas.png";
41 SeaScene::SeaScene(QObject *parent) :
42 QGraphicsScene(parent)
45 screenLitKeeper_.keepScreenLit(true);
49 QPixmap waves (":/pix/meri.png");
51 setBackgroundBrush(QBrush(waves));
55 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
57 //Setup the level list
60 levelList_.append(level1);
61 Level level2(5,10,2,50);
62 levelList_.append(level2);
63 Level level3(5,15,2,50);
64 levelList_.append(level3);
65 Level level4(5,15,4,50);
66 levelList_.append(level4);
67 Level level5(5,15,5,100);
68 levelList_.append(level5);
72 connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
75 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
76 pVibrateAction_->setCheckable(true);
77 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
80 pPauseAction_ = new QAction(tr("Pause"),this);
81 pPauseAction_->setCheckable(true);
82 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
85 autopauseTimer.setInterval(15*60*1000);
86 connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(forcePause()));
91 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
99 //empty the list of moving items
101 movingItems_.clear();
103 //empty the list of free slots
106 //fill the list of free slots
108 int numberOfXTiles = width() / 40;
109 int numberOfYTiles = height() /40;
111 // qDebug() << numberOfXTiles << " slots in x direction";
112 // qDebug() << numberOfYTiles << " slots in y rirection";
114 for (int i = 0; i < numberOfXTiles; i++ )
116 for (int j = 0; j < numberOfYTiles; j++)
118 freeTiles_.append(QPointF(i*40,j*40));
125 for (int i = 0; i < rocks; i++)
127 QPointF * pPosition = findRandomFreeSlot();
129 //If there was no room no point to continue
130 if (pPosition == NULL)
133 QPixmap rockPixmap (":/pix/kari.png");
134 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
135 pRock->setData(0,"rock");
136 pRock->setPos(*pPosition);
143 ghostsLeft_ = ghosts;
144 spreadGhosts(ghosts);
148 //spread the octopuses
150 QList <Octopus*> octopusList;
152 for (int i=0; i < octopuses; i++)
154 QPointF * pPosition = findRandomFreeSlot();
156 //If there was no room no point to continue
157 if (pPosition == NULL)
160 QPixmap octopusPixmap (":/pix/tursas.png");
161 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
162 pOctopus->setData(0,"octopus");
163 pOctopus->setPos(*pPosition);
165 pOctopus->startMoving();
166 movingItems_.append(pOctopus);
167 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
168 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
169 octopusList.append(pOctopus);
177 QPointF * pPosition = findRandomFreeSlot();
178 if (pPosition == NULL)
180 // Game cannot begin without a free position for ship, so give an error message and return
182 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
186 QList<QPixmap> shipImages;
187 shipImages.append(QPixmap(":/pix/laiva.png"));
188 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
189 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
190 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
191 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
192 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
193 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
194 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
195 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
196 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
197 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
199 Ship * pShip = new Ship (shipImages);
200 pShip->setData(0,"ship");
201 pShip->setPos(*pPosition);
203 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
204 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
205 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
206 pShip->startMoving();
207 movingItems_.append(pShip);
208 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
209 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
210 foreach (Octopus* pOctopus, octopusList)
212 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
219 void SeaScene::setupMap(Level level)
221 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
224 void SeaScene::spreadGhosts(int ghosts)
228 //the octopuses and the ship may have moved from their original positions,
229 //so the list of free slots must be adjusted to exclude their current positions
231 QList<QPointF> temporarilyReservedSlots;
233 foreach (QGraphicsItem* pItem, movingItems_)
237 // qDebug() << "NULL item in movingItems_";
241 //round x and y down to fit the slot size
251 QPointF position (x,y);
253 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
255 if (freeTiles_.removeOne(position))
256 temporarilyReservedSlots.append(position);
261 if (freeTiles_.removeOne(position))
262 temporarilyReservedSlots.append(position);
266 if (freeTiles_.removeOne(position))
267 temporarilyReservedSlots.append(position);
271 if (freeTiles_.removeOne(position))
272 temporarilyReservedSlots.append(position);
277 //spread ghosts in random free slots
279 for (int i=0; i < ghosts; i++)
281 QPointF * pPosition = findRandomFreeSlot();
283 //If there was no room no point to continue
284 if (pPosition == NULL)
287 QPixmap ghostPixmap(":/pix/aave.png");
288 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
289 pGhost->setData(0,"ghost");
290 pGhost->setPos(*pPosition);
294 //return the slots occupied by moving items to free slots
295 freeTiles_.append(temporarilyReservedSlots);
297 //clear temp for the next round
298 temporarilyReservedSlots.clear();
301 QPointF* SeaScene::findRandomFreeSlot()
303 if (freeTiles_.isEmpty())
306 int index = qrand()%freeTiles_.size();
308 // qDebug() << index << " index";
309 return new QPointF (freeTiles_.takeAt(index));
313 void SeaScene::removeGhost(QGraphicsItem *pGhost)
315 removeItem(pGhost); //remove the item from scene
316 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
319 if (ghostsLeft_ == 0)
321 emit allGhostsPicked();
322 // qDebug() << "All ghosts picked!";
326 void SeaScene::ghostsDropped(int ghosts)
328 ghostsLeft_ += ghosts;
330 spreadGhosts(ghosts);
333 void SeaScene::pause(bool paused)
335 // qDebug() << "pause pressed " << paused;
336 if (paused_ == paused)
343 // qDebug() << "starting to move again";
345 screenLitKeeper_.keepScreenLit(true);
347 pPausetextItem_->hide();
349 autopauseTimer.start(); //Start counting towards autopause
354 qDebug("about to stop movement");
356 screenLitKeeper_.keepScreenLit(false);
357 if (pPausetextItem_ != NULL)
359 qDebug() << "about to show the pause text";
360 pPausetextItem_->show();
361 qDebug() << "showing pause text";
363 else qDebug() << "No pause text available";
365 autopauseTimer.stop(); //No need to count toward autopause when already paused
369 void SeaScene::vibrationActivate(bool on)
371 emit vibrationActivated(on);
374 void SeaScene::handleScreenTapped()
377 //If the game is going just pause it
380 pPauseAction_->setChecked(true);
384 //If the game is paused, chacl if menu item was selected
386 QList<QGraphicsItem *> items = selectedItems();
388 //if nothing selected resume play
392 pPauseAction_->setChecked(false);
397 //If something was selected check if it was one of the menu items and act on it
398 //(Nothing else should be made selectable anyway)
402 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
403 //... so we can just take the first one
406 if (pItem == pRestartGameItem_)
408 qDebug() << "game restart requested";
412 else if (pItem == pRestartLevelItem_)
414 qDebug() << "Level restart requested";
419 else if (pItem == pSettingsItem_)
421 //Temporary code for settings, likely to be turned into a QML dialog
423 QMessageBox::StandardButton buttonpressed = QMessageBox::question(NULL,"Settings","Do you wish to have vibration effects enabled?", QMessageBox::Yes | QMessageBox::No);
425 if (buttonpressed == QMessageBox::Yes)
426 pVibrateAction_->setChecked(true);
427 if (buttonpressed == QMessageBox::No)
428 pVibrateAction_->setChecked(false);
431 else if (pItem == pAboutItem_)
436 else if (pItem == pQuitItem_)
442 //Selection is just used to get notice of a menu item being clicked, removed after use
446 //The user propably went to paused state just to access menu, so unpause
448 pPauseAction_->setChecked(false);
454 void SeaScene::createMenuItems()
458 font.setPixelSize(35);
462 pPausetextItem_ = new QGraphicsTextItem;
463 pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
464 pPausetextItem_->setZValue(1000);
465 pPausetextItem_->setPos(200,50);
466 addItem(pPausetextItem_);
467 pPausetextItem_->hide();
471 QString menufonthtml = "<font size = \"4\" color = darkorange>";
473 pRestartGameItem_ = new QGraphicsTextItem;
474 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
475 prepareForMenu(pRestartGameItem_);
477 pRestartLevelItem_ = new QGraphicsTextItem;
478 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
479 prepareForMenu(pRestartLevelItem_);
481 pSettingsItem_ = new QGraphicsTextItem;
482 pSettingsItem_->setHtml(tr("Vibration <br> effects").prepend(menufonthtml));
483 prepareForMenu(pSettingsItem_);
485 pAboutItem_ = new QGraphicsTextItem;
486 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
487 prepareForMenu(pAboutItem_);
489 pQuitItem_ = new QGraphicsTextItem;
490 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
491 prepareForMenu(pQuitItem_);
495 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
498 //Menu items have pause text item as their parent and are thus added to scene automatically
499 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
500 //Their coordinates are given relative to the parent.
503 pItem->setParentItem(pPausetextItem_);
504 pItem->setZValue(1000);
505 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
507 pItem->setX(menuItemCount_++*160-150);
511 void SeaScene::about()
513 QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
515 "<p>Copyright 2011 Heli Hyvättinen"
516 "<p>License: General Public License v2"
517 "<p>Bug Reports: https://bugs.maemo.org/ "
518 "enter_bug.cgi?product=Ghosts%20Overboard"
519 ).arg(QApplication::applicationVersion()));
526 void SeaScene::restartLevel()
528 setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
529 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
530 // qDebug() << pVibrateAction_->isChecked();
531 autopauseTimer.start(); //reset counting towards autopause
536 void SeaScene::nextLevel()
541 if (levelList_.empty())
545 if ( currentLevel_ < levelList_.size() )
553 QDialog* pVictoryDialog = new QDialog();
554 pVictoryDialog->setWindowTitle(tr("You won!"));
557 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
558 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
560 QPixmap victoryIcon (":/pix/aavesaari.png");
561 QLabel* pVictoryLabel = new QLabel();
562 pVictoryLabel->setPixmap(victoryIcon);
564 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
567 QVBoxLayout* pMainLayout = new QVBoxLayout;
569 QHBoxLayout* pTopLayout = new QHBoxLayout;
570 pMainLayout->addLayout(pTopLayout);
572 pTopLayout->addWidget(pVictoryLabel);
573 pTopLayout->addWidget(pTextLabel);
577 QHBoxLayout* pButtonLayout = new QHBoxLayout();
578 pMainLayout->addLayout(pButtonLayout);
580 // pButtonLayout->addWidget(pQuitButton);
581 pButtonLayout->addWidget(pPlayAgainButton);
585 pVictoryDialog->setLayout(pMainLayout);
587 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
589 pVictoryDialog->exec();
591 //Never mind if the user cancels the dialog: restart the game anyway
598 void SeaScene::restartGame()
605 void SeaScene::forcePause()
607 //Pause without setting the pause action state
611 void::SeaScene::softContinue()
613 //Continue if not being paused by the user
614 // Reverts forcePause()
616 pause(pPauseAction_->isChecked());