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 setItemPointersNull();
50 screenLitKeeper_.keepScreenLit(true);
54 QPixmap waves (":/pix/meri.png");
56 setBackgroundBrush(QBrush(waves));
60 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
62 //Setup the level list
65 levelList_.append(level1);
66 Level level2(5,10,2,50);
67 levelList_.append(level2);
68 Level level3(5,15,2,50);
69 levelList_.append(level3);
70 Level level4(5,15,4,50);
71 levelList_.append(level4);
72 Level level5(5,15,5,100);
73 levelList_.append(level5);
78 connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
81 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
82 pVibrateAction_->setCheckable(true);
83 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
85 pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
88 pPauseAction_ = new QAction(tr("Pause"),this);
89 pPauseAction_->setCheckable(true);
90 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
93 autopauseTimer.setSingleShot(true);
94 autopauseTimer.setInterval(15*60*1000);
95 connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(forcePause()));
100 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
106 setItemPointersNull();
110 createAboutBoxItems();
112 createVictoryItems();
115 //empty the list of moving items
117 movingItems_.clear();
119 //empty the list of free slots
122 //fill the list of free slots
124 int numberOfXTiles = width() / 40;
125 int numberOfYTiles = height() /40;
127 // qDebug() << numberOfXTiles << " slots in x direction";
128 // qDebug() << numberOfYTiles << " slots in y rirection";
130 for (int i = 0; i < numberOfXTiles; i++ )
132 for (int j = 0; j < numberOfYTiles; j++)
134 freeTiles_.append(QPointF(i*40,j*40));
141 for (int i = 0; i < rocks; i++)
143 QPointF * pPosition = findRandomFreeSlot();
145 //If there was no room no point to continue
146 if (pPosition == NULL)
149 QPixmap rockPixmap (":/pix/kari.png");
150 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
151 pRock->setData(0,"rock");
152 pRock->setPos(*pPosition);
159 ghostsLeft_ = ghosts;
160 spreadGhosts(ghosts);
164 //spread the octopuses
166 QList <Octopus*> octopusList;
168 for (int i=0; i < octopuses; i++)
170 QPointF * pPosition = findRandomFreeSlot();
172 //If there was no room no point to continue
173 if (pPosition == NULL)
176 QPixmap octopusPixmap (":/pix/tursas.png");
177 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
178 pOctopus->setData(0,"octopus");
179 pOctopus->setPos(*pPosition);
181 pOctopus->startMoving();
182 movingItems_.append(pOctopus);
183 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
184 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
185 octopusList.append(pOctopus);
193 QPointF * pPosition = findRandomFreeSlot();
194 if (pPosition == NULL)
196 // Game cannot begin without a free position for ship, so give an error message and return
198 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
202 QList<QPixmap> shipImages;
203 shipImages.append(QPixmap(":/pix/laiva.png"));
204 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
205 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
206 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
207 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
208 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
209 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
210 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
211 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
212 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
213 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
215 Ship * pShip = new Ship (shipImages);
216 pShip->setData(0,"ship");
217 pShip->setPos(*pPosition);
219 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
220 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
221 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
222 pShip->startMoving();
223 movingItems_.append(pShip);
224 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
225 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
226 foreach (Octopus* pOctopus, octopusList)
228 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
235 void SeaScene::setupMap(Level level)
237 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
240 void SeaScene::spreadGhosts(int ghosts)
244 //the octopuses and the ship may have moved from their original positions,
245 //so the list of free slots must be adjusted to exclude their current positions
247 QList<QPointF> temporarilyReservedSlots;
249 foreach (QGraphicsItem* pItem, movingItems_)
253 // qDebug() << "NULL item in movingItems_";
257 //round x and y down to fit the slot size
267 QPointF position (x,y);
269 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
271 if (freeTiles_.removeOne(position))
272 temporarilyReservedSlots.append(position);
277 if (freeTiles_.removeOne(position))
278 temporarilyReservedSlots.append(position);
282 if (freeTiles_.removeOne(position))
283 temporarilyReservedSlots.append(position);
287 if (freeTiles_.removeOne(position))
288 temporarilyReservedSlots.append(position);
293 //spread ghosts in random free slots
295 for (int i=0; i < ghosts; i++)
297 QPointF * pPosition = findRandomFreeSlot();
299 //If there was no room no point to continue
300 if (pPosition == NULL)
303 QPixmap ghostPixmap(":/pix/aave.png");
304 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
305 pGhost->setData(0,"ghost");
306 pGhost->setPos(*pPosition);
310 //return the slots occupied by moving items to free slots
311 freeTiles_.append(temporarilyReservedSlots);
313 //clear temp for the next round
314 temporarilyReservedSlots.clear();
317 QPointF* SeaScene::findRandomFreeSlot()
319 if (freeTiles_.isEmpty())
322 int index = qrand()%freeTiles_.size();
324 // qDebug() << index << " index";
325 return new QPointF (freeTiles_.takeAt(index));
329 void SeaScene::removeGhost(QGraphicsItem *pGhost)
331 removeItem(pGhost); //remove the item from scene
332 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
335 if (ghostsLeft_ == 0)
337 emit allGhostsPicked();
338 // qDebug() << "All ghosts picked!";
342 void SeaScene::ghostsDropped(int ghosts)
344 ghostsLeft_ += ghosts;
346 spreadGhosts(ghosts);
349 void SeaScene::pause(bool paused)
351 // qDebug() << "pause pressed " << paused;
352 if (paused_ == paused)
359 // qDebug() << "starting to move again";
360 emit fullscreenRequested();
362 screenLitKeeper_.keepScreenLit(true);
364 pPausetextItem_->hide();
366 autopauseTimer.start(); //Start counting towards autopause
371 qDebug("about to stop movement");
373 screenLitKeeper_.keepScreenLit(false);
374 if (pPausetextItem_ != NULL)
376 qDebug() << "about to show the pause text";
377 pPausetextItem_->show();
378 qDebug() << "showing pause text";
380 else qDebug() << "No pause text available";
382 autopauseTimer.stop(); //No need to count toward autopause when already paused
386 void SeaScene::vibrationActivate(bool on)
388 emit vibrationActivated(on);
391 void SeaScene::handleScreenTapped()
394 //If the game is going just pause it
397 pPauseAction_->setChecked(true);
401 //If the game is paused and about box is shown, close it and show the pause text and menu again
405 if(pAboutBoxItem_->isVisible())
407 pAboutBoxItem_->hide();
408 pPausetextItem_->show();
413 //If the game is paused, check if the victory item is being shown
414 if(pVictoryCongratulationsItem_)
416 if (pVictoryCongratulationsItem_->isVisibleTo(NULL)) //returns visibility to scene
418 pVictoryCongratulationsItem_->hide();
420 pPauseAction_->setChecked(false); // unpause
425 //If the game is paused and no victory, check if menu item was selected
427 QList<QGraphicsItem *> items = selectedItems();
429 //if nothing selected resume play
433 pPauseAction_->setChecked(false);
438 //If something was selected check if it was one of the menu items and act on it
439 //(Nothing else should be made selectable anyway)
443 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
444 //... so we can just take the first one
446 //Selection is just used to get notice of a menu item being clicked, removed after use
451 //Act upon the selected item
454 if (pItem == pRestartGameItem_)
456 qDebug() << "game restart requested";
458 pPauseAction_->setChecked(false); //unpause game
462 else if (pItem == pRestartLevelItem_)
464 qDebug() << "Level restart requested";
466 pPauseAction_->setChecked(false); //unpause game
470 else if (pItem == pSettingsItem_)
472 pVibrateAction_->toggle();
475 settings.setValue("vibration",pVibrateAction_->isChecked());
477 QString text = pSettingsItem_->toHtml();
478 if (pVibrateAction_->isChecked())
479 text.replace(" on"," off"); //don't remove spaces or you get vibratioff...
481 text.replace(" off"," on");
482 pSettingsItem_->setHtml(text);
485 else if (pItem == pAboutItem_)
490 else if(pItem == pMinimizeItem_)
492 emit minimizeRequested();
495 else if (pItem == pQuitItem_)
503 void SeaScene::createMenuItems()
507 font.setPixelSize(35);
511 pPausetextItem_ = new QGraphicsTextItem;
512 pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
513 pPausetextItem_->setZValue(1000);
514 pPausetextItem_->setPos(165,50);
515 addItem(pPausetextItem_);
516 pPausetextItem_->hide();
520 QString menufonthtml = "<font size = \"4\" color = darkorange>";
522 pRestartGameItem_ = new QGraphicsTextItem;
523 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
524 prepareForMenu(pRestartGameItem_);
526 pRestartLevelItem_ = new QGraphicsTextItem;
527 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
528 prepareForMenu(pRestartLevelItem_);
530 pSettingsItem_ = new QGraphicsTextItem;
531 QString vibraText(tr("Vibration <br> effects "));
533 if (pVibrateAction_->isChecked())
541 vibraText.append(statusText);
542 pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
543 prepareForMenu(pSettingsItem_);
545 pAboutItem_ = new QGraphicsTextItem;
546 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
547 prepareForMenu(pAboutItem_);
549 pMinimizeItem_ = new QGraphicsTextItem;
550 pMinimizeItem_->setHtml(tr("Show <br> status bar").prepend(menufonthtml));
551 prepareForMenu(pMinimizeItem_);
553 pQuitItem_ = new QGraphicsTextItem;
554 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
555 prepareForMenu(pQuitItem_);
559 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
562 //Menu items have pause text item as their parent and are thus added to scene automatically
563 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
564 //Their coordinates are given relative to the parent.
571 pItem->setParentItem(pPausetextItem_);
572 pItem->setZValue(1000);
573 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
575 int row = menuItemCount_/(itemsPerRow);
576 pItem->setY(150+row*120);
577 pItem->setX(((menuItemCount_%(itemsPerRow))*180+5));
583 void SeaScene::about()
585 pPausetextItem_->hide();
586 pAboutBoxItem_->show();
590 void SeaScene::restartLevel()
592 setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
593 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
594 // qDebug() << pVibrateAction_->isChecked();
595 autopauseTimer.start(); //reset counting towards autopause
600 void SeaScene::nextLevel()
605 if (levelList_.empty())
609 if ( currentLevel_ < levelList_.size() )
617 pPauseAction_->setChecked(true); //Pause the game while showing the victory dialog
619 pPausetextItem_->hide();
621 pVictoryCongratulationsItem_->show();
627 void SeaScene::restartGame()
634 void SeaScene::forcePause()
636 //Pause without setting the pause action state
640 void SeaScene::softContinue()
642 //Continue if not being paused by the user
643 // Reverts forcePause()
645 pause(pPauseAction_->isChecked());
649 void SeaScene::createVictoryItems()
651 pVictoryCongratulationsItem_ = new QGraphicsTextItem;
652 pVictoryCongratulationsItem_->setHtml("<font size=\"6\" color = darkorange> Victory!");
653 pVictoryCongratulationsItem_->hide();
654 pVictoryCongratulationsItem_->setPos(300,50);
655 pVictoryCongratulationsItem_->setZValue(1000);
656 addItem(pVictoryCongratulationsItem_);
658 // QGraphicsPixmapItem * pImageItem = new QGraphicsPixmapItem(QPixmap(":/pix/aavesaari.png"),pVictoryCongratulationsItem_);
659 // pImageItem->setPos(-100,150);
660 // pImageItem->setZValue(1000);
661 // pImageItem->setScale(2.0);
664 QGraphicsTextItem * pTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
665 pTextItem->setHtml("<center> <font size=\"5\" color = darkorange> Congratulations! <br> You have saved all the ghosts."
666 "<br><br> Tap to play again ");
667 pTextItem->setPos(-50,100);
668 pTextItem->setZValue(1000);
671 void SeaScene::createAboutBoxItems()
673 pAboutBoxItem_ = new QGraphicsTextItem;
674 addItem(pAboutBoxItem_);
675 pAboutBoxItem_->setPos(25,50);
676 pAboutBoxItem_->setZValue(1000);
677 pAboutBoxItem_->hide();
679 pAboutBoxItem_->setHtml(tr("<font color = darkorange size = \"7\">"
680 "%1 <br> <font size = \"5\"> Version %2"
681 "<p><font size = \"4\"> Copyright 2011 Heli Hyvättinen"
682 "<p><font size = \"4\"> License: General Public License v2"
683 "<p><font size = \"3\"> Bug Reports: <br> https://bugs.maemo.org/ "
684 "enter_bug.cgi?product=Ghosts%20Overboard"
685 ).arg(QApplication::applicationName(),QApplication::applicationVersion()));
689 void SeaScene::setItemPointersNull()
691 pPausetextItem_ = NULL;
692 pRestartLevelItem_ = NULL;
693 pRestartGameItem_ = NULL;
694 pSettingsItem_ = NULL;
697 pMinimizeItem_ = NULL;
699 pAboutBoxItem_ = NULL;