1 /**************************************************************************
2 Ghosts Overboard - a game for 'Meego 1.2 Harmattan'
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);
77 //This ensures that nextlevel will not be called until its safe to delete the Ship object.
78 //Leaving out Qt::QueuedConnection or calling nextlevel directly instead of emitting the signal will CRASH
79 connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()),Qt::QueuedConnection);
82 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
83 pVibrateAction_->setCheckable(true);
84 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
86 pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
89 pPauseAction_ = new QAction(tr("Pause"),this);
90 pPauseAction_->setCheckable(true);
91 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
94 autopauseTimer.setSingleShot(true);
95 autopauseTimer.setInterval(15*60*1000);
96 connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(turnPauseOn()));
101 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
107 setItemPointersNull();
111 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(); fremantle specific (since no "show statusbar" action in harmattan version)
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 or about box, 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 == pQuitItem_)
499 void SeaScene::createMenuItems()
503 font.setPixelSize(35);
507 pPausetextItem_ = new QGraphicsTextItem;
509 // This is for fremantle
510 // pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
512 //Harmattan needs bigger font
513 pPausetextItem_->setHtml("<font size = \"7\" color = darkorange> Game paused. Tap to continue.");
516 pPausetextItem_->setZValue(1000);
517 pPausetextItem_->setPos(200,50);
518 addItem(pPausetextItem_);
519 pPausetextItem_->hide();
523 // This is for fremantle
524 // QString menufonthtml = "<font size = \"4\" color = darkorange>";
527 //Harmattan needs bigger font
528 QString menufonthtml = "<font size = \"6\" color = darkorange>";
531 pRestartGameItem_ = new QGraphicsTextItem;
532 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
533 prepareForMenu(pRestartGameItem_);
535 pRestartLevelItem_ = new QGraphicsTextItem;
536 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
537 prepareForMenu(pRestartLevelItem_);
539 pSettingsItem_ = new QGraphicsTextItem;
540 QString vibraText(tr("Turn vibration <br> effects "));
542 if (pVibrateAction_->isChecked())
550 vibraText.append(statusText);
551 pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
552 prepareForMenu(pSettingsItem_);
554 pAboutItem_ = new QGraphicsTextItem;
555 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
556 prepareForMenu(pAboutItem_);
558 pQuitItem_ = new QGraphicsTextItem;
559 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
560 prepareForMenu(pQuitItem_);
564 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
567 //Menu items have pause text item as their parent and are thus added to scene automatically
568 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
569 //Their coordinates are given relative to the parent.
572 pItem->setParentItem(pPausetextItem_);
573 pItem->setZValue(1000);
574 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
576 pItem->setX(menuItemCount_++*160-150);
580 void SeaScene::about()
582 pPausetextItem_->hide();
583 pAboutBoxItem_->show();
587 void SeaScene::restartLevel()
589 setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
590 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
591 // qDebug() << pVibrateAction_->isChecked();
592 autopauseTimer.start(); //reset counting towards autopause
597 void SeaScene::nextLevel()
602 if (levelList_.empty())
606 if ( currentLevel_ < levelList_.size() )
614 pPauseAction_->setChecked(true); //Pause the game while showing the victory dialog
616 pPausetextItem_->hide();
618 pVictoryCongratulationsItem_->show();
624 void SeaScene::restartGame()
631 void SeaScene::forcePause()
633 //Pause without setting the pause action state
637 void SeaScene::softContinue()
639 //Continue if not being paused by the user
640 // Reverts forcePause()
642 pause(pPauseAction_->isChecked());
646 void SeaScene::createVictoryItems()
648 pVictoryCongratulationsItem_ = new QGraphicsTextItem;
649 pVictoryCongratulationsItem_->setHtml("<font size=\"7\" color = darkorange> <b> Victory!");
650 pVictoryCongratulationsItem_->hide();
651 pVictoryCongratulationsItem_->setPos(355,50);
652 pVictoryCongratulationsItem_->setZValue(1000);
653 addItem(pVictoryCongratulationsItem_);
656 //coordinates are relative to the parent
658 QGraphicsTextItem * pTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
659 pTextItem->setHtml("<font size=\"7\" color = darkorange> Congratulations!");
660 pTextItem->setPos(-50,100);
661 pTextItem->setZValue(1000);
663 QGraphicsTextItem * pMiddleTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
664 pMiddleTextItem->setHtml("<font size=\"7\" color = darkorange> You have saved all the ghosts.");
665 pMiddleTextItem->setPos(-145,140);
666 pMiddleTextItem->setZValue(1000);
668 QGraphicsTextItem * pLowestTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
669 pLowestTextItem->setHtml("<font size=\"7\" color = darkorange> Tap to play again");
670 pLowestTextItem->setPos(-50,220);
671 pLowestTextItem->setZValue(1000);
675 void SeaScene::createAboutBoxItems()
677 pAboutBoxItem_ = new QGraphicsTextItem;
678 addItem(pAboutBoxItem_);
679 pAboutBoxItem_->setPos(25,50);
680 pAboutBoxItem_->setZValue(1000);
681 pAboutBoxItem_->hide();
683 pAboutBoxItem_->setHtml(tr("<font color = darkorange size = \"7\">"
684 "%1 <br> <font size = \"7\"> Version %2"
685 "<p><font size = \"6\"> Copyright 2011 Heli Hyvättinen"
686 "<p><font size = \"6\"> License: General Public License v2"
687 "<p><font size = \"5\"> Web: http://ghostsoverboard.garage.maemo.org/<br>"
688 "Bug Reports: <br> https://bugs.maemo.org/"
689 "enter_bug.cgi?product=Ghosts%20Overboard"
690 ).arg(QApplication::applicationName(),QApplication::applicationVersion()));
694 void SeaScene::setItemPointersNull()
696 pPausetextItem_ = NULL;
697 pRestartLevelItem_ = NULL;
698 pRestartGameItem_ = NULL;
699 pSettingsItem_ = NULL;
702 // pMinimizeItem_ = NULL; //Fremantle spesific
704 pAboutBoxItem_ = NULL;
707 void SeaScene::turnPauseOn()
709 pPauseAction_->setChecked(true);