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)));
90 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
98 //empty the list of moving items
100 movingItems_.clear();
102 //empty the list of free slots
105 //fill the list of free slots
107 int numberOfXTiles = width() / 40;
108 int numberOfYTiles = height() /40;
110 // qDebug() << numberOfXTiles << " slots in x direction";
111 // qDebug() << numberOfYTiles << " slots in y rirection";
113 for (int i = 0; i < numberOfXTiles; i++ )
115 for (int j = 0; j < numberOfYTiles; j++)
117 freeTiles_.append(QPointF(i*40,j*40));
124 for (int i = 0; i < rocks; i++)
126 QPointF * pPosition = findRandomFreeSlot();
128 //If there was no room no point to continue
129 if (pPosition == NULL)
132 QPixmap rockPixmap (":/pix/kari.png");
133 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
134 pRock->setData(0,"rock");
135 pRock->setPos(*pPosition);
142 ghostsLeft_ = ghosts;
143 spreadGhosts(ghosts);
147 //spread the octopuses
149 QList <Octopus*> octopusList;
151 for (int i=0; i < octopuses; i++)
153 QPointF * pPosition = findRandomFreeSlot();
155 //If there was no room no point to continue
156 if (pPosition == NULL)
159 QPixmap octopusPixmap (":/pix/tursas.png");
160 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
161 pOctopus->setData(0,"octopus");
162 pOctopus->setPos(*pPosition);
164 pOctopus->startMoving();
165 movingItems_.append(pOctopus);
166 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
167 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
168 octopusList.append(pOctopus);
176 QPointF * pPosition = findRandomFreeSlot();
177 if (pPosition == NULL)
179 // Game cannot begin without a free position for ship, so give an error message and return
181 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
185 QList<QPixmap> shipImages;
186 shipImages.append(QPixmap(":/pix/laiva.png"));
187 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
188 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
189 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
190 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
191 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
192 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
193 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
194 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
195 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
196 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
198 Ship * pShip = new Ship (shipImages);
199 pShip->setData(0,"ship");
200 pShip->setPos(*pPosition);
202 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
203 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
204 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
205 pShip->startMoving();
206 movingItems_.append(pShip);
207 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
208 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
209 foreach (Octopus* pOctopus, octopusList)
211 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
216 void SeaScene::setupMap(Level level)
218 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
221 void SeaScene::spreadGhosts(int ghosts)
225 //the octopuses and the ship may have moved from their original positions,
226 //so the list of free slots must be adjusted to exclude their current positions
228 QList<QPointF> temporarilyReservedSlots;
230 foreach (QGraphicsItem* pItem, movingItems_)
234 // qDebug() << "NULL item in movingItems_";
238 //round x and y down to fit the slot size
248 QPointF position (x,y);
250 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
252 if (freeTiles_.removeOne(position))
253 temporarilyReservedSlots.append(position);
258 if (freeTiles_.removeOne(position))
259 temporarilyReservedSlots.append(position);
263 if (freeTiles_.removeOne(position))
264 temporarilyReservedSlots.append(position);
268 if (freeTiles_.removeOne(position))
269 temporarilyReservedSlots.append(position);
274 //spread ghosts in random free slots
276 for (int i=0; i < ghosts; i++)
278 QPointF * pPosition = findRandomFreeSlot();
280 //If there was no room no point to continue
281 if (pPosition == NULL)
284 QPixmap ghostPixmap(":/pix/aave.png");
285 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
286 pGhost->setData(0,"ghost");
287 pGhost->setPos(*pPosition);
291 //return the slots occupied by moving items to free slots
292 freeTiles_.append(temporarilyReservedSlots);
294 //clear temp for the next round
295 temporarilyReservedSlots.clear();
298 QPointF* SeaScene::findRandomFreeSlot()
300 if (freeTiles_.isEmpty())
303 int index = qrand()%freeTiles_.size();
305 // qDebug() << index << " index";
306 return new QPointF (freeTiles_.takeAt(index));
310 void SeaScene::removeGhost(QGraphicsItem *pGhost)
312 removeItem(pGhost); //remove the item from scene
313 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
316 if (ghostsLeft_ == 0)
318 emit allGhostsPicked();
319 // qDebug() << "All ghosts picked!";
323 void SeaScene::ghostsDropped(int ghosts)
325 ghostsLeft_ += ghosts;
327 spreadGhosts(ghosts);
330 void SeaScene::pause(bool paused)
332 // qDebug() << "pause pressed " << paused;
333 if (paused_ == paused)
340 // qDebug() << "starting to move again";
342 screenLitKeeper_.keepScreenLit(true);
344 pPausetextItem_->hide();
349 qDebug("about to stop movement");
351 screenLitKeeper_.keepScreenLit(false);
352 if (pPausetextItem_ != NULL)
354 qDebug() << "about to show the pause text";
355 pPausetextItem_->show();
356 qDebug() << "showing pause text";
358 else qDebug() << "No pause text available";
362 void SeaScene::vibrationActivate(bool on)
364 emit vibrationActivated(on);
367 void SeaScene::handleScreenTapped()
370 //If the game is going just pause it
373 pPauseAction_->setChecked(true);
377 //If the game is paused, chacl if menu item was selected
379 QList<QGraphicsItem *> items = selectedItems();
381 //if nothing selected resume play
385 pPauseAction_->setChecked(false);
390 //If something was selected check if it was one of the menu items and act on it
391 //(Nothing else should be made selectable anyway)
395 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
396 //... so we can just take the first one
399 if (pItem == pRestartGameItem_)
401 qDebug() << "game restart requested";
405 else if (pItem == pRestartLevelItem_)
407 qDebug() << "Level restart requested";
412 else if (pItem == pSettingsItem_)
414 pVibrateAction_->toggle();
417 settings.setValue("vibration",true);
420 settings.setValue("vibration",false);
424 else if (pItem == pAboutItem_)
429 else if (pItem == pQuitItem_)
435 //Selection is just used to get notice of a menu item being clicked, removed after use
439 //The user propably went to paused state just to access menu, so unpause
441 pPauseAction_->setChecked(false);
447 void SeaScene::createMenuItems()
451 font.setPixelSize(35);
455 pPausetextItem_ = new QGraphicsTextItem;
456 pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
457 pPausetextItem_->setZValue(1000);
458 pPausetextItem_->setPos(200,50);
459 addItem(pPausetextItem_);
460 pPausetextItem_->hide();
464 QString menufonthtml = "<font size = \"4\" color = darkorange>";
466 pRestartGameItem_ = new QGraphicsTextItem;
467 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
468 prepareForMenu(pRestartGameItem_);
470 pRestartLevelItem_ = new QGraphicsTextItem;
471 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
472 prepareForMenu(pRestartLevelItem_);
474 pSettingsItem_ = new QGraphicsTextItem;
475 QString vibraText(tr("Vibration <br> effects"));
477 if (pVibrateAction_->isChecked())
485 vibraText.append(statusText);
486 pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
487 prepareForMenu(pSettingsItem_);
489 pAboutItem_ = new QGraphicsTextItem;
490 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
491 prepareForMenu(pAboutItem_);
493 pQuitItem_ = new QGraphicsTextItem;
494 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
495 prepareForMenu(pQuitItem_);
499 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
502 //Menu items have pause text item as their parent and are thus added to scene automatically
503 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
504 //Their coordinates are given relative to the parent.
507 pItem->setParentItem(pPausetextItem_);
508 pItem->setZValue(1000);
509 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
511 pItem->setX(menuItemCount_++*160-150);
515 void SeaScene::about()
517 QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
519 "<p>Copyright 2011 Heli Hyvättinen"
520 "<p>License: General Public License v2"
521 "<p>Bug Reports: https://bugs.maemo.org/ "
522 "enter_bug.cgi?product=Ghosts%20Overboard"
523 ).arg(QApplication::applicationVersion()));
530 void SeaScene::restartLevel()
532 setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
533 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
534 // qDebug() << pVibrateAction_->isChecked();
539 void SeaScene::nextLevel()
544 if (levelList_.empty())
548 if ( currentLevel_ < levelList_.size() )
556 QDialog* pVictoryDialog = new QDialog();
557 pVictoryDialog->setWindowTitle(tr("You won!"));
560 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
561 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
563 QPixmap victoryIcon (":/pix/aavesaari.png");
564 QLabel* pVictoryLabel = new QLabel();
565 pVictoryLabel->setPixmap(victoryIcon);
567 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
570 QVBoxLayout* pMainLayout = new QVBoxLayout;
572 QHBoxLayout* pTopLayout = new QHBoxLayout;
573 pMainLayout->addLayout(pTopLayout);
575 pTopLayout->addWidget(pVictoryLabel);
576 pTopLayout->addWidget(pTextLabel);
580 QHBoxLayout* pButtonLayout = new QHBoxLayout();
581 pMainLayout->addLayout(pButtonLayout);
583 // pButtonLayout->addWidget(pQuitButton);
584 pButtonLayout->addWidget(pPlayAgainButton);
588 pVictoryDialog->setLayout(pMainLayout);
590 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
592 pVictoryDialog->exec();
594 //Never mind if the user cancels the dialog: restart the game anyway
601 void SeaScene::restartGame()
608 void SeaScene::forcePause()
610 //Pause without setting the pause action state
614 void::SeaScene::softContinue()
616 //Continue if not being paused by the user
617 // Reverts forcePause()
619 pause(pPauseAction_->isChecked());