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>
38 const QString ghostImageFilename_ = ":/pix/aave.png";
39 const QString rockImageFilename_ =":/pix/kari.png";
40 const QString octopusImageFilename_= ":/pix/tursas.png";
43 SeaScene::SeaScene(QObject *parent) :
44 QGraphicsScene(parent)
47 screenLitKeeper_.keepScreenLit(true);
51 QPixmap waves (":/pix/meri.png");
53 setBackgroundBrush(QBrush(waves));
57 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
59 //Setup the level list
62 levelList_.append(level1);
63 Level level2(5,10,2,50);
64 levelList_.append(level2);
65 Level level3(5,15,2,50);
66 levelList_.append(level3);
67 Level level4(5,15,4,50);
68 levelList_.append(level4);
69 Level level5(5,15,5,100);
70 levelList_.append(level5);
75 connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
78 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
79 pVibrateAction_->setCheckable(true);
80 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
82 pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
85 pPauseAction_ = new QAction(tr("Pause"),this);
86 pPauseAction_->setCheckable(true);
87 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
90 autopauseTimer.setSingleShot(true);
91 autopauseTimer.setInterval(15*60*1000);
92 connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(forcePause()));
97 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
105 createVictoryItems();
108 //empty the list of moving items
110 movingItems_.clear();
112 //empty the list of free slots
115 //fill the list of free slots
117 int numberOfXTiles = width() / 40;
118 int numberOfYTiles = height() /40;
120 // qDebug() << numberOfXTiles << " slots in x direction";
121 // qDebug() << numberOfYTiles << " slots in y rirection";
123 for (int i = 0; i < numberOfXTiles; i++ )
125 for (int j = 0; j < numberOfYTiles; j++)
127 freeTiles_.append(QPointF(i*40,j*40));
134 for (int i = 0; i < rocks; i++)
136 QPointF * pPosition = findRandomFreeSlot();
138 //If there was no room no point to continue
139 if (pPosition == NULL)
142 QPixmap rockPixmap (":/pix/kari.png");
143 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
144 pRock->setData(0,"rock");
145 pRock->setPos(*pPosition);
152 ghostsLeft_ = ghosts;
153 spreadGhosts(ghosts);
157 //spread the octopuses
159 QList <Octopus*> octopusList;
161 for (int i=0; i < octopuses; i++)
163 QPointF * pPosition = findRandomFreeSlot();
165 //If there was no room no point to continue
166 if (pPosition == NULL)
169 QPixmap octopusPixmap (":/pix/tursas.png");
170 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
171 pOctopus->setData(0,"octopus");
172 pOctopus->setPos(*pPosition);
174 pOctopus->startMoving();
175 movingItems_.append(pOctopus);
176 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
177 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
178 octopusList.append(pOctopus);
186 QPointF * pPosition = findRandomFreeSlot();
187 if (pPosition == NULL)
189 // Game cannot begin without a free position for ship, so give an error message and return
191 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
195 QList<QPixmap> shipImages;
196 shipImages.append(QPixmap(":/pix/laiva.png"));
197 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
198 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
199 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
200 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
201 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
202 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
203 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
204 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
205 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
206 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
208 Ship * pShip = new Ship (shipImages);
209 pShip->setData(0,"ship");
210 pShip->setPos(*pPosition);
212 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
213 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
214 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
215 pShip->startMoving();
216 movingItems_.append(pShip);
217 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
218 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
219 foreach (Octopus* pOctopus, octopusList)
221 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
228 void SeaScene::setupMap(Level level)
230 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
233 void SeaScene::spreadGhosts(int ghosts)
237 //the octopuses and the ship may have moved from their original positions,
238 //so the list of free slots must be adjusted to exclude their current positions
240 QList<QPointF> temporarilyReservedSlots;
242 foreach (QGraphicsItem* pItem, movingItems_)
246 // qDebug() << "NULL item in movingItems_";
250 //round x and y down to fit the slot size
260 QPointF position (x,y);
262 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
264 if (freeTiles_.removeOne(position))
265 temporarilyReservedSlots.append(position);
270 if (freeTiles_.removeOne(position))
271 temporarilyReservedSlots.append(position);
275 if (freeTiles_.removeOne(position))
276 temporarilyReservedSlots.append(position);
280 if (freeTiles_.removeOne(position))
281 temporarilyReservedSlots.append(position);
286 //spread ghosts in random free slots
288 for (int i=0; i < ghosts; i++)
290 QPointF * pPosition = findRandomFreeSlot();
292 //If there was no room no point to continue
293 if (pPosition == NULL)
296 QPixmap ghostPixmap(":/pix/aave.png");
297 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
298 pGhost->setData(0,"ghost");
299 pGhost->setPos(*pPosition);
303 //return the slots occupied by moving items to free slots
304 freeTiles_.append(temporarilyReservedSlots);
306 //clear temp for the next round
307 temporarilyReservedSlots.clear();
310 QPointF* SeaScene::findRandomFreeSlot()
312 if (freeTiles_.isEmpty())
315 int index = qrand()%freeTiles_.size();
317 // qDebug() << index << " index";
318 return new QPointF (freeTiles_.takeAt(index));
322 void SeaScene::removeGhost(QGraphicsItem *pGhost)
324 removeItem(pGhost); //remove the item from scene
325 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
328 if (ghostsLeft_ == 0)
330 emit allGhostsPicked();
331 // qDebug() << "All ghosts picked!";
335 void SeaScene::ghostsDropped(int ghosts)
337 ghostsLeft_ += ghosts;
339 spreadGhosts(ghosts);
342 void SeaScene::pause(bool paused)
344 // qDebug() << "pause pressed " << paused;
345 if (paused_ == paused)
352 // qDebug() << "starting to move again";
353 emit fullscreenRequested();
355 screenLitKeeper_.keepScreenLit(true);
357 pPausetextItem_->hide();
359 autopauseTimer.start(); //Start counting towards autopause
364 qDebug("about to stop movement");
366 screenLitKeeper_.keepScreenLit(false);
367 if (pPausetextItem_ != NULL)
369 qDebug() << "about to show the pause text";
370 pPausetextItem_->show();
371 qDebug() << "showing pause text";
373 else qDebug() << "No pause text available";
375 autopauseTimer.stop(); //No need to count toward autopause when already paused
379 void SeaScene::vibrationActivate(bool on)
381 emit vibrationActivated(on);
384 void SeaScene::handleScreenTapped()
387 //If the game is going just pause it
390 pPauseAction_->setChecked(true);
394 //If the game is paused, check if the victory item is being shown
395 if(pVictoryCongratulationsItem_)
397 if (pVictoryCongratulationsItem_->isVisibleTo(NULL)) //returns visibility to scene
399 pVictoryCongratulationsItem_->hide();
401 pPauseAction_->setChecked(false); // unpause
406 //If the game is paused and no victory, check if menu item was selected
408 QList<QGraphicsItem *> items = selectedItems();
410 //if nothing selected resume play
414 pPauseAction_->setChecked(false);
419 //If something was selected check if it was one of the menu items and act on it
420 //(Nothing else should be made selectable anyway)
424 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
425 //... so we can just take the first one
427 //Selection is just used to get notice of a menu item being clicked, removed after use
432 //Act upon the selected item
435 if (pItem == pRestartGameItem_)
437 qDebug() << "game restart requested";
439 pPauseAction_->setChecked(false); //unpause game
443 else if (pItem == pRestartLevelItem_)
445 qDebug() << "Level restart requested";
447 pPauseAction_->setChecked(false); //unpause game
451 else if (pItem == pSettingsItem_)
453 pVibrateAction_->toggle();
456 settings.setValue("vibration",pVibrateAction_->isChecked());
458 QString text = pSettingsItem_->toHtml();
459 if (pVibrateAction_->isChecked())
460 text.replace(" on"," off"); //don't remove spaces or you get vibratioff...
462 text.replace(" off"," on");
463 pSettingsItem_->setHtml(text);
466 else if (pItem == pAboutItem_)
471 else if(pItem == pMinimizeItem_)
473 emit minimizeRequested();
476 else if (pItem == pQuitItem_)
484 void SeaScene::createMenuItems()
488 font.setPixelSize(35);
492 pPausetextItem_ = new QGraphicsTextItem;
493 pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
494 pPausetextItem_->setZValue(1000);
495 pPausetextItem_->setPos(165,50);
496 addItem(pPausetextItem_);
497 pPausetextItem_->hide();
501 QString menufonthtml = "<font size = \"4\" color = darkorange>";
503 pRestartGameItem_ = new QGraphicsTextItem;
504 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
505 prepareForMenu(pRestartGameItem_);
507 pRestartLevelItem_ = new QGraphicsTextItem;
508 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
509 prepareForMenu(pRestartLevelItem_);
511 pSettingsItem_ = new QGraphicsTextItem;
512 QString vibraText(tr("Vibration <br> effects "));
514 if (pVibrateAction_->isChecked())
522 vibraText.append(statusText);
523 pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
524 prepareForMenu(pSettingsItem_);
526 pAboutItem_ = new QGraphicsTextItem;
527 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
528 prepareForMenu(pAboutItem_);
530 pMinimizeItem_ = new QGraphicsTextItem;
531 pMinimizeItem_->setHtml(tr("Show <br> status bar").prepend(menufonthtml));
532 prepareForMenu(pMinimizeItem_);
534 pQuitItem_ = new QGraphicsTextItem;
535 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
536 prepareForMenu(pQuitItem_);
540 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
543 //Menu items have pause text item as their parent and are thus added to scene automatically
544 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
545 //Their coordinates are given relative to the parent.
552 pItem->setParentItem(pPausetextItem_);
553 pItem->setZValue(1000);
554 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
556 int row = menuItemCount_/(itemsPerRow);
557 pItem->setY(150+row*120);
558 pItem->setX(((menuItemCount_%(itemsPerRow))*180+5));
564 void SeaScene::about()
566 QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
568 "<p>Copyright 2011 Heli Hyvättinen"
569 "<p>License: General Public License v2"
570 "<p>Bug Reports: https://bugs.maemo.org/ "
571 "enter_bug.cgi?product=Ghosts%20Overboard"
572 ).arg(QApplication::applicationVersion()));
579 void SeaScene::restartLevel()
581 setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
582 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
583 // qDebug() << pVibrateAction_->isChecked();
584 autopauseTimer.start(); //reset counting towards autopause
589 void SeaScene::nextLevel()
594 if (levelList_.empty())
598 if ( currentLevel_ < levelList_.size() )
606 pPauseAction_->setChecked(true); //Pause the game while showing the victory dialog
608 pPausetextItem_->hide();
610 pVictoryCongratulationsItem_->show();
616 void SeaScene::restartGame()
623 void SeaScene::forcePause()
625 //Pause without setting the pause action state
629 void SeaScene::softContinue()
631 //Continue if not being paused by the user
632 // Reverts forcePause()
634 pause(pPauseAction_->isChecked());
637 void SeaScene::createVictoryItems()
639 pVictoryCongratulationsItem_ = new QGraphicsTextItem;
640 pVictoryCongratulationsItem_->setHtml("<font size=\"6\" color = darkorange> Victory!");
641 pVictoryCongratulationsItem_->hide();
642 pVictoryCongratulationsItem_->setPos(300,50);
643 pVictoryCongratulationsItem_->setZValue(1000);
644 addItem(pVictoryCongratulationsItem_);
646 // QGraphicsPixmapItem * pImageItem = new QGraphicsPixmapItem(QPixmap(":/pix/aavesaari.png"),pVictoryCongratulationsItem_);
647 // pImageItem->setPos(-100,150);
648 // pImageItem->setZValue(1000);
649 // pImageItem->setScale(2.0);
652 QGraphicsTextItem * pTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
653 pTextItem->setHtml("<center> <font size=\"5\" color = darkorange> Congratulations! <br> You have saved all the ghosts."
654 "<br><br> Tap to play again ");
655 pTextItem->setPos(-50,100);
656 pTextItem->setZValue(1000);