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)));
87 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
95 //empty the list of moving items
99 //empty the list of free slots
102 //fill the list of free slots
104 int numberOfXTiles = width() / 40;
105 int numberOfYTiles = height() /40;
107 // qDebug() << numberOfXTiles << " slots in x direction";
108 // qDebug() << numberOfYTiles << " slots in y rirection";
110 for (int i = 0; i < numberOfXTiles; i++ )
112 for (int j = 0; j < numberOfYTiles; j++)
114 freeTiles_.append(QPointF(i*40,j*40));
121 for (int i = 0; i < rocks; i++)
123 QPointF * pPosition = findRandomFreeSlot();
125 //If there was no room no point to continue
126 if (pPosition == NULL)
129 QPixmap rockPixmap (":/pix/kari.png");
130 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
131 pRock->setData(0,"rock");
132 pRock->setPos(*pPosition);
139 ghostsLeft_ = ghosts;
140 spreadGhosts(ghosts);
144 //spread the octopuses
146 QList <Octopus*> octopusList;
148 for (int i=0; i < octopuses; i++)
150 QPointF * pPosition = findRandomFreeSlot();
152 //If there was no room no point to continue
153 if (pPosition == NULL)
156 QPixmap octopusPixmap (":/pix/tursas.png");
157 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
158 pOctopus->setData(0,"octopus");
159 pOctopus->setPos(*pPosition);
161 pOctopus->startMoving();
162 movingItems_.append(pOctopus);
163 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
164 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
165 octopusList.append(pOctopus);
173 QPointF * pPosition = findRandomFreeSlot();
174 if (pPosition == NULL)
176 // Game cannot begin without a free position for ship, so give an error message and return
178 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
182 QList<QPixmap> shipImages;
183 shipImages.append(QPixmap(":/pix/laiva.png"));
184 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
185 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
186 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
187 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
188 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
189 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
190 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
191 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
192 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
193 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
195 Ship * pShip = new Ship (shipImages);
196 pShip->setData(0,"ship");
197 pShip->setPos(*pPosition);
199 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
200 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
201 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
202 pShip->startMoving();
203 movingItems_.append(pShip);
204 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
205 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
206 foreach (Octopus* pOctopus, octopusList)
208 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
213 void SeaScene::setupMap(Level level)
215 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
218 void SeaScene::spreadGhosts(int ghosts)
222 //the octopuses and the ship may have moved from their original positions,
223 //so the list of free slots must be adjusted to exclude their current positions
225 QList<QPointF> temporarilyReservedSlots;
227 foreach (QGraphicsItem* pItem, movingItems_)
231 // qDebug() << "NULL item in movingItems_";
235 //round x and y down to fit the slot size
245 QPointF position (x,y);
247 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
249 if (freeTiles_.removeOne(position))
250 temporarilyReservedSlots.append(position);
255 if (freeTiles_.removeOne(position))
256 temporarilyReservedSlots.append(position);
260 if (freeTiles_.removeOne(position))
261 temporarilyReservedSlots.append(position);
265 if (freeTiles_.removeOne(position))
266 temporarilyReservedSlots.append(position);
271 //spread ghosts in random free slots
273 for (int i=0; i < ghosts; i++)
275 QPointF * pPosition = findRandomFreeSlot();
277 //If there was no room no point to continue
278 if (pPosition == NULL)
281 QPixmap ghostPixmap(":/pix/aave.png");
282 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
283 pGhost->setData(0,"ghost");
284 pGhost->setPos(*pPosition);
288 //return the slots occupied by moving items to free slots
289 freeTiles_.append(temporarilyReservedSlots);
291 //clear temp for the next round
292 temporarilyReservedSlots.clear();
295 QPointF* SeaScene::findRandomFreeSlot()
297 if (freeTiles_.isEmpty())
300 int index = qrand()%freeTiles_.size();
302 // qDebug() << index << " index";
303 return new QPointF (freeTiles_.takeAt(index));
307 void SeaScene::removeGhost(QGraphicsItem *pGhost)
309 removeItem(pGhost); //remove the item from scene
310 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
313 if (ghostsLeft_ == 0)
315 emit allGhostsPicked();
316 // qDebug() << "All ghosts picked!";
320 void SeaScene::ghostsDropped(int ghosts)
322 ghostsLeft_ += ghosts;
324 spreadGhosts(ghosts);
327 void SeaScene::pause(bool paused)
329 // qDebug() << "pause pressed " << paused;
330 if (paused_ == paused)
337 // qDebug() << "starting to move again";
339 screenLitKeeper_.keepScreenLit(true);
341 pPausetextItem_->hide();
346 qDebug("about to stop movement");
348 screenLitKeeper_.keepScreenLit(false);
349 if (pPausetextItem_ != NULL)
351 qDebug() << "about to show the pause text";
352 pPausetextItem_->show();
353 qDebug() << "showing pause text";
355 else qDebug() << "No pause text available";
359 void SeaScene::vibrationActivate(bool on)
361 emit vibrationActivated(on);
364 void SeaScene::handleScreenTapped()
367 //If the game is going just pause it
370 pPauseAction_->setChecked(true);
374 //If the game is paused, chacl if menu item was selected
376 QList<QGraphicsItem *> items = selectedItems();
378 //if nothing selected resume play
382 pPauseAction_->setChecked(false);
387 //If something was selected check if it was one of the menu items and act on it
388 //(Nothing else should be made selectable anyway)
392 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
393 //... so we can just take the first one
396 if (pItem == pRestartGameItem_)
398 qDebug() << "game restart requested";
402 else if (pItem == pRestartLevelItem_)
404 qDebug() << "Level restart requested";
409 else if (pItem == pSettingsItem_)
411 //Temporary code for settings, likely to be turned into a QML dialog
413 QMessageBox::StandardButton buttonpressed = QMessageBox::question(NULL,"Settings","Do you wish to have vibration effects enabled?", QMessageBox::Yes | QMessageBox::No);
415 if (buttonpressed == QMessageBox::Yes)
416 pVibrateAction_->setChecked(true);
417 if (buttonpressed == QMessageBox::No)
418 pVibrateAction_->setChecked(false);
421 else if (pItem == pAboutItem_)
426 else if (pItem == pQuitItem_)
432 //Selection is just used to get notice of a menu item being clicked, removed after use
436 //The user propably went to paused state just to access menu, so unpause
438 pPauseAction_->setChecked(false);
444 void SeaScene::createMenuItems()
448 font.setPixelSize(35);
452 pPausetextItem_ = new QGraphicsTextItem;
453 pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
454 pPausetextItem_->setZValue(1000);
455 pPausetextItem_->setPos(200,50);
456 addItem(pPausetextItem_);
457 pPausetextItem_->hide();
461 QString menufonthtml = "<font size = \"4\" color = darkorange>";
463 pRestartGameItem_ = new QGraphicsTextItem;
464 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
465 prepareForMenu(pRestartGameItem_);
467 pRestartLevelItem_ = new QGraphicsTextItem;
468 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
469 prepareForMenu(pRestartLevelItem_);
471 pSettingsItem_ = new QGraphicsTextItem;
472 pSettingsItem_->setHtml(tr("Vibration <br> effects").prepend(menufonthtml));
473 prepareForMenu(pSettingsItem_);
475 pAboutItem_ = new QGraphicsTextItem;
476 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
477 prepareForMenu(pAboutItem_);
479 pQuitItem_ = new QGraphicsTextItem;
480 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
481 prepareForMenu(pQuitItem_);
485 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
488 //Menu items have pause text item as their parent and are thus added to scene automatically
489 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
490 //Their coordinates are given relative to the parent.
493 pItem->setParentItem(pPausetextItem_);
494 pItem->setZValue(1000);
495 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
497 pItem->setX(menuItemCount_++*160-150);
501 void SeaScene::about()
503 QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
505 "<p>Copyright 2011 Heli Hyvättinen"
506 "<p>License: General Public License v2"
507 "<p>Bug Reports: https://bugs.maemo.org/ "
508 "enter_bug.cgi?product=Ghosts%20Overboard"
509 ).arg(QApplication::applicationVersion()));
516 void SeaScene::restartLevel()
518 setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
519 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
520 // qDebug() << pVibrateAction_->isChecked();
525 void SeaScene::nextLevel()
530 if (levelList_.empty())
534 if ( currentLevel_ < levelList_.size() )
542 QDialog* pVictoryDialog = new QDialog();
543 pVictoryDialog->setWindowTitle(tr("You won!"));
546 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
547 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
549 QPixmap victoryIcon (":/pix/aavesaari.png");
550 QLabel* pVictoryLabel = new QLabel();
551 pVictoryLabel->setPixmap(victoryIcon);
553 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
556 QVBoxLayout* pMainLayout = new QVBoxLayout;
558 QHBoxLayout* pTopLayout = new QHBoxLayout;
559 pMainLayout->addLayout(pTopLayout);
561 pTopLayout->addWidget(pVictoryLabel);
562 pTopLayout->addWidget(pTextLabel);
566 QHBoxLayout* pButtonLayout = new QHBoxLayout();
567 pMainLayout->addLayout(pButtonLayout);
569 // pButtonLayout->addWidget(pQuitButton);
570 pButtonLayout->addWidget(pPlayAgainButton);
574 pVictoryDialog->setLayout(pMainLayout);
576 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
578 pVictoryDialog->exec();
580 //Never mind if the user cancels the dialog: restart the game anyway
587 void SeaScene::restartGame()
594 void SeaScene::forcePause()
596 //Pause without setting the pause action state
600 void::SeaScene::softContinue()
602 //Continue if not being paused by the user
603 // Reverts forcePause()
605 pause(pPauseAction_->isChecked());