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 //This ensures that nextlevel will not be called until its safe to delete the Ship object.
74 //Leaving out Qt::QueuedConnection or calling nextlevel directly instead of emitting the signal will CRASH
75 connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()),Qt::QueuedConnection);
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 //empty the list of moving items
107 movingItems_.clear();
109 //empty the list of free slots
112 //fill the list of free slots
114 int numberOfXTiles = width() / 40;
115 int numberOfYTiles = height() /40;
117 // qDebug() << numberOfXTiles << " slots in x direction";
118 // qDebug() << numberOfYTiles << " slots in y rirection";
120 for (int i = 0; i < numberOfXTiles; i++ )
122 for (int j = 0; j < numberOfYTiles; j++)
124 freeTiles_.append(QPointF(i*40,j*40));
131 for (int i = 0; i < rocks; i++)
133 QPointF * pPosition = findRandomFreeSlot();
135 //If there was no room no point to continue
136 if (pPosition == NULL)
139 QPixmap rockPixmap (":/pix/kari.png");
140 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
141 pRock->setData(0,"rock");
142 pRock->setPos(*pPosition);
149 ghostsLeft_ = ghosts;
150 spreadGhosts(ghosts);
154 //spread the octopuses
156 QList <Octopus*> octopusList;
158 for (int i=0; i < octopuses; i++)
160 QPointF * pPosition = findRandomFreeSlot();
162 //If there was no room no point to continue
163 if (pPosition == NULL)
166 QPixmap octopusPixmap (":/pix/tursas.png");
167 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
168 pOctopus->setData(0,"octopus");
169 pOctopus->setPos(*pPosition);
171 pOctopus->startMoving();
172 movingItems_.append(pOctopus);
173 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
174 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
175 octopusList.append(pOctopus);
183 QPointF * pPosition = findRandomFreeSlot();
184 if (pPosition == NULL)
186 // Game cannot begin without a free position for ship, so give an error message and return
188 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
192 QList<QPixmap> shipImages;
193 shipImages.append(QPixmap(":/pix/laiva.png"));
194 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
195 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
196 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
197 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
198 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
199 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
200 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
201 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
202 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
203 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
205 Ship * pShip = new Ship (shipImages);
206 pShip->setData(0,"ship");
207 pShip->setPos(*pPosition);
209 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
210 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
211 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
212 pShip->startMoving();
213 movingItems_.append(pShip);
214 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
215 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
216 foreach (Octopus* pOctopus, octopusList)
218 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
225 void SeaScene::setupMap(Level level)
227 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
230 void SeaScene::spreadGhosts(int ghosts)
234 //the octopuses and the ship may have moved from their original positions,
235 //so the list of free slots must be adjusted to exclude their current positions
237 QList<QPointF> temporarilyReservedSlots;
239 foreach (QGraphicsItem* pItem, movingItems_)
243 // qDebug() << "NULL item in movingItems_";
247 //round x and y down to fit the slot size
257 QPointF position (x,y);
259 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
261 if (freeTiles_.removeOne(position))
262 temporarilyReservedSlots.append(position);
267 if (freeTiles_.removeOne(position))
268 temporarilyReservedSlots.append(position);
272 if (freeTiles_.removeOne(position))
273 temporarilyReservedSlots.append(position);
277 if (freeTiles_.removeOne(position))
278 temporarilyReservedSlots.append(position);
283 //spread ghosts in random free slots
285 for (int i=0; i < ghosts; i++)
287 QPointF * pPosition = findRandomFreeSlot();
289 //If there was no room no point to continue
290 if (pPosition == NULL)
293 QPixmap ghostPixmap(":/pix/aave.png");
294 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
295 pGhost->setData(0,"ghost");
296 pGhost->setPos(*pPosition);
300 //return the slots occupied by moving items to free slots
301 freeTiles_.append(temporarilyReservedSlots);
303 //clear temp for the next round
304 temporarilyReservedSlots.clear();
307 QPointF* SeaScene::findRandomFreeSlot()
309 if (freeTiles_.isEmpty())
312 int index = qrand()%freeTiles_.size();
314 // qDebug() << index << " index";
315 return new QPointF (freeTiles_.takeAt(index));
319 void SeaScene::removeGhost(QGraphicsItem *pGhost)
321 removeItem(pGhost); //remove the item from scene
322 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
325 if (ghostsLeft_ == 0)
327 emit allGhostsPicked();
328 // qDebug() << "All ghosts picked!";
332 void SeaScene::ghostsDropped(int ghosts)
334 ghostsLeft_ += ghosts;
336 spreadGhosts(ghosts);
339 void SeaScene::pause(bool paused)
341 // qDebug() << "pause pressed " << paused;
342 if (paused_ == paused)
349 // qDebug() << "starting to move again";
351 screenLitKeeper_.keepScreenLit(true);
353 pPausetextItem_->hide();
355 autopauseTimer.start(); //Start counting towards autopause
360 qDebug("about to stop movement");
362 screenLitKeeper_.keepScreenLit(false);
363 if (pPausetextItem_ != NULL)
365 qDebug() << "about to show the pause text";
366 pPausetextItem_->show();
367 qDebug() << "showing pause text";
369 else qDebug() << "No pause text available";
371 autopauseTimer.stop(); //No need to count toward autopause when already paused
375 void SeaScene::vibrationActivate(bool on)
377 emit vibrationActivated(on);
380 void SeaScene::handleScreenTapped()
383 //If the game is going just pause it
386 pPauseAction_->setChecked(true);
390 //If the game is paused, chacl if menu item was selected
392 QList<QGraphicsItem *> items = selectedItems();
394 //if nothing selected resume play
398 pPauseAction_->setChecked(false);
403 //If something was selected check if it was one of the menu items and act on it
404 //(Nothing else should be made selectable anyway)
408 QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
409 //... so we can just take the first one
412 if (pItem == pRestartGameItem_)
414 qDebug() << "game restart requested";
418 else if (pItem == pRestartLevelItem_)
420 qDebug() << "Level restart requested";
425 else if (pItem == pSettingsItem_)
427 pVibrateAction_->toggle();
430 settings.setValue("vibration",pVibrateAction_->isChecked());
432 QString text = pSettingsItem_->toHtml();
433 if (pVibrateAction_->isChecked())
434 text.replace(" on"," off"); //don't remove spaces or you get vibratioff...
436 text.replace(" off"," on");
437 pSettingsItem_->setHtml(text);
440 else if (pItem == pAboutItem_)
445 else if (pItem == pQuitItem_)
451 //Selection is just used to get notice of a menu item being clicked, removed after use
455 //The user propably went to paused state just to access menu, so unpause unless vibration set (so the user sees its changed)
457 if (pItem != pSettingsItem_)
459 pPauseAction_->setChecked(false);
465 void SeaScene::createMenuItems()
469 font.setPixelSize(35);
473 pPausetextItem_ = new QGraphicsTextItem;
475 // This is for fremantle
476 // pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
478 //Harmattan needs bigger font
479 pPausetextItem_->setHtml("<font size = \"7\" color = darkorange> Game paused. Tap to continue.");
482 pPausetextItem_->setZValue(1000);
483 pPausetextItem_->setPos(200,50);
484 addItem(pPausetextItem_);
485 pPausetextItem_->hide();
489 // This is for fremantle
490 // QString menufonthtml = "<font size = \"4\" color = darkorange>";
493 //Harmattan needs bigger font
494 QString menufonthtml = "<font size = \"6\" color = darkorange>";
497 pRestartGameItem_ = new QGraphicsTextItem;
498 pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
499 prepareForMenu(pRestartGameItem_);
501 pRestartLevelItem_ = new QGraphicsTextItem;
502 pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
503 prepareForMenu(pRestartLevelItem_);
505 pSettingsItem_ = new QGraphicsTextItem;
506 QString vibraText(tr("Vibration <br> effects "));
508 if (pVibrateAction_->isChecked())
516 vibraText.append(statusText);
517 pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
518 prepareForMenu(pSettingsItem_);
520 pAboutItem_ = new QGraphicsTextItem;
521 pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
522 prepareForMenu(pAboutItem_);
524 pQuitItem_ = new QGraphicsTextItem;
525 pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
526 prepareForMenu(pQuitItem_);
530 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
533 //Menu items have pause text item as their parent and are thus added to scene automatically
534 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
535 //Their coordinates are given relative to the parent.
538 pItem->setParentItem(pPausetextItem_);
539 pItem->setZValue(1000);
540 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
542 pItem->setX(menuItemCount_++*160-150);
546 void SeaScene::about()
548 QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
550 "<p>Copyright 2011 Heli Hyvättinen"
551 "<p>License: General Public License v2"
552 "<p>Bug Reports: https://bugs.maemo.org/ "
553 "enter_bug.cgi?product=Ghosts%20Overboard"
554 ).arg(QApplication::applicationVersion()));
561 void SeaScene::restartLevel()
563 setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
564 vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
565 // qDebug() << pVibrateAction_->isChecked();
566 autopauseTimer.start(); //reset counting towards autopause
571 void SeaScene::nextLevel()
576 if (levelList_.empty())
580 if ( currentLevel_ < levelList_.size() )
588 QDialog* pVictoryDialog = new QDialog();
589 pVictoryDialog->setWindowTitle(tr("You won!"));
592 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
593 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
595 QPixmap victoryIcon (":/pix/aavesaari.png");
596 QLabel* pVictoryLabel = new QLabel();
597 pVictoryLabel->setPixmap(victoryIcon);
599 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
602 QVBoxLayout* pMainLayout = new QVBoxLayout;
604 QHBoxLayout* pTopLayout = new QHBoxLayout;
605 pMainLayout->addLayout(pTopLayout);
607 pTopLayout->addWidget(pVictoryLabel);
608 pTopLayout->addWidget(pTextLabel);
612 QHBoxLayout* pButtonLayout = new QHBoxLayout();
613 pMainLayout->addLayout(pButtonLayout);
615 // pButtonLayout->addWidget(pQuitButton);
616 pButtonLayout->addWidget(pPlayAgainButton);
620 pVictoryDialog->setLayout(pMainLayout);
622 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
624 pVictoryDialog->exec();
626 //Never mind if the user cancels the dialog: restart the game anyway
633 void SeaScene::restartGame()
640 void SeaScene::forcePause()
642 //Pause without setting the pause action state
646 void::SeaScene::softContinue()
648 //Continue if not being paused by the user
649 // Reverts forcePause()
651 pause(pPauseAction_->isChecked());